/**
 *  File     : user.js
 *  Author(s)    : Naveen Kumar
 *  Creation Date: Sep 25, 2009
 *  Functionality: This file contains user releated functions.
 *  Dependencies : N/A
 *  Copyright    : P&G
 *  Modifications: <Author> <Date> <Description of Modification>
 */
/**
 *	Function : login()
 *	Purpose  : This is only for checking the validation
  *	author	 : Naveen
 */
 /*Check for Empty String*/
function isEmptyString(chkStr) {

	if (chkStr == "") {
		return true;
	}
	else {
		var numWhiteSpaces = 0;
		for (var i=0; i < chkStr.length; i++) {
			if (chkStr.substring(i, i+1) == " ") {
				numWhiteSpaces++;
			}
		}
		if (numWhiteSpaces == chkStr.length) {
			return true;
		}
		else {
			return false;
		}
	}
}
/*Function will return true/false based 
on whether the passed non-blank 
value is a number or not*/
function isNumeric(strString)
{
	 //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
  var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

}
 
 
function loginValidation() {
	var message = "";
	var areaCodeObj = document.getElementById("areaCodeExp");
	var telePrefixObj = document.getElementById("telePrefixExp");
	var teleSuffixObj = document.getElementById("teleSuffixExp");
	var passwordObj = document.getElementById("passwordExp");
	var focus = "";
	var phone = areaCodeObj.value + telePrefixObj.value + teleSuffixObj.value;
	/*if (isEmptyString(phone)) {
		message = message + " Please verify that your phone number is correct and attempt log in again. \n";
		focus = areaCodeObj;
	} else {
		if (phone.length != 10) {
			message = message + " Please verify that your phone number is correct and attempt log in again. \n";
			focus = areaCodeObj;
		} else {
			if (isNumeric(phone) === false) {
				message = message + " Please verify that your phone number is correct and attempt log in again. \n";
				focus = areaCodeObj;
			}
		}
	}
		if (message != "") {
		alert(message);
		focus.focus();
		return false;
		}else{*/
	if (isEmptyString(areaCodeObj.value)) {
		message = message + "Veuillez inscrire l\'indicatif r\u00E9gional \n";
		focus = areaCodeObj;
	} else {
		if (areaCodeObj.value.length != 3) {
			message = message + "Veuillez inscrire un indicatif r\u00E9gional valide \n";
			focus = areaCodeObj;
		} else {
			if (isNumeric(areaCodeObj.value) == false) {
				message = message + "Veuilles isncrire une valeur num\u00E9rique comme indicatif r\u00E9gional \n";
				focus = areaCodeObj;
			}
		}
	}
	if (isEmptyString(telePrefixObj.value)) {
		message = message + "Veuillez inscrire le pr\u00E9fixe t\u00E9l\u00E9phonique \n";
	} else {
		if (telePrefixObj.value.length != 3) {
			message = message + "Veuillez inscrire un pr\u00E9fixe t\u00E9l\u00E9phonique valide \n";
			if (focus == "") {
				focus = telePrefixObj;
			}
		} else {
			if (isNumeric(telePrefixObj.value) == false) {
				message = message + "Veuilles isncrire une valeur num\u00E9rique comme pr\u00E9fixe t\u00E9l\u00E9phonique \n";
				if (focus == "") {
					focus = telePrefixObj;
				}
			}
		}
	}
	if (isEmptyString(teleSuffixObj.value)) {
		message = message + "Veuillez inscrire le suffixe t\u00E9l\u00E9phonique \n";
		if (focus == "") {
			focus = teleSuffixObj;
		}
	} else {
		if (teleSuffixObj.value.length != 4) {
			message = message + "Veuillez inscrire un suffixe t\u00E9l\u00E9phonique valide \n";
			if (focus == "") {
				focus = teleSuffixObj;
			}
		} else {
			if (isNumeric(teleSuffixObj.value) == false) {
				message = message + "Veuilles isncrire une valeur num\u00E9rique comme suffixe t\u00E9l\u00E9phonique \n";
				if (focus == "") {
					focus = teleSuffixObj;
				}
			}
		}
	}
if (isEmptyString(passwordObj.value)) {
		message = message + "Veuillez inscrire un mot de passe  \n";
	}
	if (focus == "") {
		focus = passwordObj;
	}
	if (message != "") {
		alert(message);
		focus.focus();
		return false;
	} else {
		
		document.loginForm.action = "logincafr.do?method=submit";
		document.loginForm.submit();
	}
	
}
function moveTab(obj, id) {
	var areaCodeObj = document.getElementById("areaCode");
	var telePrefixObj = document.getElementById("telePrefix");
	var teleSuffixObj = document.getElementById("teleSuffix");
	var passwordObj = document.getElementById("password");
	if (id == "areaCode") {
		if (obj.value.length == 3) {
			telePrefixObj.focus();
		}
	}
	if (id == "telePrefix") {
		if (obj.value.length == 3) {
			teleSuffixObj.focus();
		}
	}
}
function submitForm(formName) {
	document.forms[0].action = formName;
	document.forms[0].submit();
}

function moveTabExp(obj, id) {
	var areaCodeObj = document.getElementById("areaCodeExp");
	var telePrefixObj = document.getElementById("telePrefixExp");
	var teleSuffixObj = document.getElementById("teleSuffixExp");
	var passwordObj = document.getElementById("password");
	if (id == "areaCode") {
		if (obj.value.length == 3) {
			telePrefixObj.focus();
		}
	}
	if (id == "telePrefix") {
		if (obj.value.length == 3) {
			teleSuffixObj.focus();
		}
	}
}
function validateAddress(flag)
{	//for registration page
	if(flag == 'reg')
	{
		var address1 = document.getElementById("mailingAddress");
		var address2 = document.getElementById("mailingAddressAptOrSuit");
		var address3 = document.getElementById("shoppingAddress");
		var address4 = document.getElementById("shoppingAptOrSuit");
	}
	else
	{//for profilr page
		var address1 = document.getElementById("mailingAddress1");
		var address2 = document.getElementById("mailingAddress2");
		var address3 = document.getElementById("shippingAddress1");
		var address4 = document.getElementById("shippingAddress2");

	}
	if(validatePOBoxInAddress(address1) && validatePOBoxInAddress(address2) && validatePOBoxInAddress(address3) && validatePOBoxInAddress(address4))
	{
		return true;
	}
	else
	{
		return false;
	}
}
function validatePOBoxInAddress(add)
{
	var addr = add.value;
	var address = addr.toUpperCase();
	var pos = address.indexOf("PO BOX");
	var pos1 = address.indexOf("P.O. BOX");
	var pos2 = address.indexOf("P.O. BOX.");
	var pos3 = address.indexOf("POST BOX");
	var pos4 = address.indexOf("P BOX");
	var pos5 = address.indexOf("P.BOX");
	if ((pos==-1) && (pos1==-1) && (pos2==-1) && (pos3==-1) && (pos4==-1) && (pos5==-1))
	{
	//alert(pos);
	return true;
	}
	else
	{
	alert('D\u00E9sol\u00E9, nous n\'acceptons pas les num\u00E9ros de cas postale.');
	add.focus();	
	return false;
	}

}


var windowVar;
var popupWindow;
function newPopUpWindow(file, windows,toolBarObj,locationObj,directoriesObj,statusObj,menuBarObj,scrollbarsObj,resizableObj,widthObj,heightObj,topObj,leftObj)
{	
	var optionVar="toolbar="+toolBarObj+",location="+locationObj+",directories="+directoriesObj+",status="+statusObj+",menuBar="+menuBarObj+",scrollbars="+scrollbarsObj+",resizable="+resizableObj+",width="+widthObj+",height="+heightObj+",top="+topObj+",left="+leftObj+"";	
	//windowVar = window.open(file,windows, optionVar);
	popupWindow = window.open(file,windows, optionVar);;
	//alert("windowVar :"+windowVar);
	//window.onfocus=checkOpenWindow;
	//windowVar.focus();
}

function checkOpenWindow()
{

	try{
		if(popupWindow!=undefined && !popupWindow.closed)
		{	
			var abssize = document.body.offsetWidth-36; 
			if (window.event.clientY < 0 && window.event.clientX>=abssize)
			{
				if(popupWindow.name!=undefined && popupWindow.name!="")
				{
					alert("Veuillez terminer de remplir les champs de la fen\u00EAtre fille avant de tenter de remplir les champs de la fen\u00EAtre m\u00E8re.");
				}
				else
				{
		    		alert("Veuillez terminer de remplir les champs de la fen\u00EAtre fille avant de tenter de remplir les champs de la fen\u00EAtre m\u00E8re.");
		    	}
		    	popupWindow.focus();
		    }else{
			popupWindow.focus();
			}
			return 0;
		}
	}catch(exception){}
}
