/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
	 OUT: s - the return string without leading blanks
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim
	 OUT: s - the return string without leading blanks
*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim
   OUT: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

			var illegalCharsEmail = /[\~\`\!\#\$\^\&\*\+\=\|\{\}\%\?\'\/\(\)\<\>\,\;\:\\\"\[\]]/;

			function validate(field) {
				if (Trim(field.value) != "") {
                    			return true;
				} else {
                			return false;
				}
			}


			//Fix for Bug 19076
			//This is to support email which have more than 3 characters after dot as suffix
			//e.g name@company.INFO, myname@yahoo.co.in , myname@auc.inc.in
			var emailFilter=/^.+@.+\..{2,20}$/;

			function validateEmail(field) {
				if (Trim(field.value) != "") {
					var s = field.value;
			    if (!(emailFilter.test(s))) {
                			return false;
			    }else if (s.match(illegalCharsEmail)) {
                			return false;
			    }else{
                			return true;
			    }
				} else {
                			return false;
				}
			}


			
function validateRegisterForm() {
	var fName = document.registrationForm.firstName;
	var lName = document.registrationForm.lastName;
	var email = document.registrationForm.email;
	
	var cCity = document.registrationForm.contactCity;
	var cState = document.registrationForm.contactState;
	var cAdd= document.registrationForm.contactAddress;
	var cZip = document.registrationForm.contactZip;
	var cPhone = document.registrationForm.contactPhone;

	var cName = document.registrationForm.companyName;
	var coAddress = document.registrationForm.companyAddress;
	var coCity = document.registrationForm.companyCity;
	var coState = document.registrationForm.companyState;
	var coPhone = document.registrationForm.companyPhone;
	var coZip = document.registrationForm.companyZip;
	
	var uName = document.registrationForm.userName;
	var password = document.registrationForm.password;
	var password2 = document.registrationForm.password2;
	
	var button = document.registrationForm.Register;
	var submitButton = document.getElementById('cancelButton');
	var accountNumber = document.registrationForm.erpOrgId;
	var accountSuccess = document.registrationForm.accountSucess.value;
	var orgSuccesfullSubmission = "";
	if(document.registrationForm.orgSuccesfullSubmission !=null && document.registrationForm.orgSuccesfullSubmission !="undefined"){
		orgSuccesfullSubmission = document.registrationForm.orgSuccesfullSubmission.value;
	}
	

	var existing = false;
	if(document.getElementById('existing_customer').checked){
		existing = true;
	}

	if(existing){
		//Here the checks are done for all the required fields except for company details
		if (validate(accountNumber)) {
		if(accountSuccess == "true" && orgSuccesfullSubmission == "true" && validate(fName) && validate(lName) && validate(email) && validate(cAdd) && validate(cCity) && validate(cState) && validate(cZip) && validate(cPhone) && validate(uName) && validate(password) && validate(password2) && password.value == password2.value){
			button.disabled = false;
		}else{
			button.disabled = true;
		}
		
		submitButton.disabled = false;
		} else {
		button.disabled = true;
		submitButton.disabled = true;
		}
	}else{
	//validateEmail(email)
		//Here the checks are done for all the required fields except for account number
		if (validate(fName) && validate(lName) && validate(email) && validate(cAdd) && validate(cCity) && validate(cState) && validate(cZip) && validate(cPhone) &&  validate(cName) && validate(coAddress) && validate(coCity) && validate(coState) && validate(coZip) && validate(coPhone) && validate(uName) && validate(password) && validate(password2) && password.value == password2.value) {
			button.disabled = false;
		} else {
			button.disabled = true;
		}
	
	
	}


}

function addNewAddressClassChange()
{
	vForm = document.addressBookForm;
	if(vForm.addrFirstName.value == "")
       vForm.addrFirstName.className = "form-fields-required w200px"
    else
	    vForm.addrFirstName.className = "form-fields w200px"
    
	if(vForm.addrLastName.value == "")
       vForm.addrLastName.className = "form-fields-required w200px"
    else
    	vForm.addrLastName.className = "form-fields w200px"

	if(vForm.address1.value == "")
       vForm.address1.className = "form-fields-required w200px"
    else
    	vForm.address1.className = "form-fields w200px"
   
   	if(vForm.city.value == "")
       vForm.city.className = "form-fields-required w200px"
    else
    	vForm.city.className = "form-fields w200px"
    	
   	if(vForm.zip.value == "")
       vForm.zip.className = "form-fields-required w200px"
    else
     vForm.zip.className = "form-fields w200px"


	if(vForm.state.value == "")
       vForm.state.className = "form-fields-required w200px"
    else
    	vForm.state.className = "form-fields w200px"


	if(vForm.addrPhoneNumber.value == "")
       vForm.addrPhoneNumber.className = "form-fields-required w200px"
    else
    	vForm.addrPhoneNumber.className = "form-fields w200px"


	if(vForm.addrEmail.value == "")
       vForm.addrEmail.className = "form-fields-required w200px"
    else
    	vForm.addrEmail.className = "form-fields w200px"

}

// New Java Script Function for Change of class if the required field is blank

 function classchange()
{
vForm = document.registrationForm;
  if(vForm.firstName.value == "")
            {
               vForm.firstName.className = "form-fields-required w200px"
            }
            else
            {
            vForm.firstName.className = "form-fields w200px"
            }

			if(vForm.lastName.value == "")
            {
               vForm.lastName.className = "form-fields-required w200px"
            }
            else
            {
            vForm.lastName.className = "form-fields w200px"
            }

		if(vForm.email.value == "")
            {
               vForm.email.className = "form-fields-required w200px"
            }
            else
            {
            vForm.email.className = "form-fields w200px"
            }
           
		   if(vForm.contactAddress.value == "")
            {
               vForm.contactAddress.className = "form-fields-required w200px"
            }
            else
            {
            vForm.contactAddress.className = "form-fields w200px"
            }
           
		   if(vForm.contactCity.value == "")
            {
               vForm.contactCity.className = "form-fields-required w200px"
            }
            else
            {
            vForm.contactCity.className = "form-fields w200px"
            }

			if(vForm.contactState.value == "")
            {
               vForm.contactState.className = "form-fields-required w200px"
            }
            else
            {
            vForm.contactState.className = "form-fields w200px"
            }

			if(vForm.contactZip.value == "")
            {
               vForm.contactZip.className = "form-fields-required w200px"
            }
            else
            {
            vForm.contactZip.className = "form-fields w200px"
            }

			if(vForm.contactPhone.value == "")
            {
               vForm.contactPhone.className = "form-fields-required w200px"
            }
            else
            {
            vForm.contactPhone.className = "form-fields w200px"
            }

			if(vForm.erpOrgId.value == "")
            {
               vForm.erpOrgId.className = "form-fields-required w200px"
            }
            else
            {
            vForm.erpOrgId.className = "form-fields w200px"
            }

			
			if(vForm.companyName.value == "")
            {
               vForm.companyName.className = "form-fields-required w200px"
            }
            else
            {
            vForm.companyName.className = "form-fields w200px"
            }

			if(vForm.companyAddress.value == "")
            {
               vForm.companyAddress.className = "form-fields-required w200px"
            }
            else
            {
            vForm.companyAddress.className = "form-fields w200px"
            }

			if(vForm.companyCity.value == "")
            {
               vForm.companyCity.className = "form-fields-required w200px"
            }
            else
            {
            vForm.companyCity.className = "form-fields w200px"
            }

			if(vForm.companyState.value == "")
            {
               vForm.companyState.className = "form-fields-required w200px"
            }
            else
            {
            vForm.companyState.className = "form-fields w200px"
            }

			if(vForm.companyZip.value == "")
            {
               vForm.companyZip.className = "form-fields-required w200px"
            }
            else
            {
            vForm.companyZip.className = "form-fields w200px"
            }

			if(vForm.companyPhone.value == "")
            {
               vForm.companyPhone.className = "form-fields-required w200px"
            }
            else
            {
            vForm.companyPhone.className = "form-fields w200px"
            }

			if(vForm.userName.value == "")
            {
               vForm.userName.className = "form-fields-required w200px"
            }
            else
            {
            vForm.userName.className = "form-fields w200px"
            }

			if(vForm.password.value == "")
            {
               vForm.password.className = "form-fields-required w200px"
            }
            else
            {
            vForm.password.className = "form-fields w200px"
            }

			if(vForm.password2.value == "")
            {
               vForm.password2.className = "form-fields-required w200px"
            }
            else
            {
            vForm.password2.className = "form-fields w200px"
            }

}



function validateProfileForm() {
	var fName = document.updateProfileForm.firstName;
	var lName = document.updateProfileForm.lastName;
	var email = document.updateProfileForm.email;
	
	var password = document.updateProfileForm.password;
	var password2 = document.updateProfileForm.password2;
	var userName = document.updateProfileForm.userName;
	
	var button = document.updateProfileForm.Register;
	if(userName.value !='' && fName.value !='' && lName.value !='' && email.value !='' && validateEmail(email) && password.value !='' && password2.value !='' && password.value == password2.value) {
		button.disabled = false;
	} else {
		button.disabled = true;
	}
}

	function fnUpdate(){
		document.updateProfileForm.updateProfile.value="true";
		document.updateProfileForm.submit();
	}

function toggleField(theform, selectElem) {
    for (var i = 0; i < theform.elements.length; i++) {
    var elem = theform.elements[i];
      if (elem.type == "checkbox" && elem.id == "orderid[]") {
        if (selectElem.checked == true) {
           elem.checked = true;
        } else {
           elem.checked = false;
        }
      }
    }
}

function checkUnCheckAll(allObj,checkList){
	isChecked = allObj.checked;
	if(checkList.length){
		for(i=0;i<checkList.length;i++)
		{
			if (checkList[i].value != null && checkList[i].value != '' )
				checkList[i].checked = isChecked;
		}
	}else
	checkList.checked = isChecked;
}

function fnCheckUncheckAll(obj, checkAll)
{
         oForm=obj.form;
    obj=oForm.elements[obj.name];
    if(obj.length)
    {
		bChecked=true;
		nChecked=0;
		for(i=0;i<obj.length;i++)
			if(obj[i].checked)
				nChecked++;
		if(nChecked<obj.length)
			bChecked=false;
 		checkAll.checked=bChecked;
	}
}

function colorchange()
{
	vForm = document.updateProfileForm;
	if(vForm.userName.value == "")
	            {
	               vForm.userName.className = "form-fields-required w200px"
	            }
	            else
	            {
	            vForm.userName.className = "form-fields w200px"
            }
            
	 if(vForm.firstName.value == "")
            {
               vForm.firstName.className = "form-fields-required w200px"
            }
            else
            {
            vForm.firstName.className = "form-fields w200px"
            }

			if(vForm.lastName.value == "")
            {
               vForm.lastName.className = "form-fields-required w200px"
            }
            else
            {
            vForm.lastName.className = "form-fields w200px"
            }
			if(vForm.email.value == "")
            {
               vForm.email.className = "form-fields-required w200px"
            }
            else
            {
            vForm.email.className = "form-fields w200px"
            }

			if(vForm.contactPhone.value == "")
            {
               vForm.contactPhone.className = "form-fields-required w200px"
            }
            else
            {
            vForm.contactPhone.className = "form-fields w200px"
            }

			if(vForm.password.value == "")
            {
               vForm.password.className = "form-fields-required w200px"
            }
            else
            {
            vForm.password.className = "form-fields w200px"
            }

			if(vForm.password2.value == "")
            {
               vForm.password2.className = "form-fields-required w200px"
            }
            else
            {
            vForm.password2.className = "form-fields w200px"
            }

}

function auto_complete_off(elmt) { 
	if (!document.getElementById) return false; 
	var f = document.getElementById(elmt);
	if (f != null)
		f.setAttribute("autocomplete", "off");
	}
