/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
@end @*/


function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;
isHot=false;


// whitespace characters
var whitespace = " \t\n\r";
var defaultEmptyOK = false

function isEmpty(s)
{   	
	var s1;
	
	//s1 = Trim(s);
   
 	return ((s == null) || (s.length == 0))
}

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function mAlert(form, errMessage) 
{
	var cnt_toshow = document.getElementById(form.name + "_error");
	if(cnt_toshow) {
		cnt_toshow.className = "error";
		cnt_toshow.innerHTML = errMessage;
	}
}

function mAlertHide(form) 
{
	var cnt_tohide = document.getElementById(form.name + "_error");
	if(cnt_tohide && (cnt_tohide.className == "error" || cnt_tohide.innerHTML != "")) {

		cnt_tohide.className = "inv error";
		cnt_tohide.innerHTML = "";
	}
}

function trim(str) {
	
	return typeof(str) == 'string' ? str.replace(/^\s+|\s+$/g,"") : '';
}


function check_forgotten_password(form)
{  
	var email, sUrl, httpRequest, LoginOK
	validated = true;
	email = form.Email_ID.value;

	var triggerCnt = form.antispamTrigger;

	if (isEmpty(email)) {
		form.Email_ID.focus();
		mAlert(form.Email_ID, "Please enter email");
		if(form.Email_ID_invalid) 
			form.Email_ID_invalid.className = "visible";
		return false;
	}
	else if (isEmail(email) == false) {                           	
		form.Email_ID.focus();
		mAlert(form.Email_ID, "Invalid e-mail address, please re-enter");
		if(form.Email_ID_invalid) 
			form.Email_ID_invalid.className = "visible";
		return false;
	}
	else {
		if(form.Email_ID_invalid) 
			form.Email_ID_invalid.className = "inv";

		if(!triggerCnt || triggerCnt.value == "false")
			mAlertHide(form.Email_ID);
	}


	if(form.Verification && triggerCnt && triggerCnt.value == "true") {
		
		if(trim(form.Verification.value.toUpperCase()) != form.Verification_Char.value.toUpperCase()) {
			mAlertHide(form.Email_ID);

			validated = false;
			form.Verification_img.className = "visible";
			document.getElementById("Verification_error").className = "error";
			document.getElementById("Verification_error").innerHTML = form.Verification.value == "" ? "Invalid" : "Characters do not match";
			form.Verification.focus();

			reloadVerificationCode(document.getElementById("verificationDiv"), form.Verification_Char, form.Verification);
		}
		else {
			form.Verification_img.className = "inv";
			document.getElementById("Verification_error").className = "error inv";
			document.getElementById("Verification_error").innerHTML = "";
		}
		if(!validated) return false;
	}


	sUrl = 'co_present_remind.asp?Email_ID=' + escape(email)
	httpRequest = new XMLHttpRequest();
	httpRequest.open('GET', sUrl, false);
	httpRequest.send(null); 

	LoginOK = httpRequest.responseText;
	if ("YES" == LoginOK) {
		mAlertHide(form);
		if(form.Email_ID_invalid) 
			form.Email_ID_invalid.className = "inv";
		return true;				
	} else {
		form.Email_ID.focus();
		mAlert(form.Email_ID, "Your e-mail address was not recognized");
		if(form.Email_ID_invalid) 
			form.Email_ID_invalid.className = "visible";

		if(form.Verification && triggerCnt && triggerCnt.value == "true")
			reloadVerificationCode(document.getElementById("verificationDiv"), 
				form.Verification_Char,
				form.Verification
				);

		if(triggerCnt) {
			document.getElementById(triggerCnt.id).value = "true";
			document.getElementById("antispamRow_0").style.display = "";
			document.getElementById("antispamRow_1").style.display = "";
		}

		

		return false;		 		  
	}

	mAlertHide(form.Email_ID);
	if(form.Email_ID_invalid) 
		form.Email_ID_invalid.className = "inv";
	return true;
}


function check_password_change(form) {

	var charDigits = '0123456789';
	var charLower = 'abcdefghijklmnopqrstuvwxyz';
	var charUpper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var email, sUrl, httpRequest, LoginOK;
	var validated = true;
	if(form.Email_ID)
		email = form.Email_ID.value;

	var triggerCnt = form.antispamTrigger;
	var password = trim(form.old_password.value);

	if (isEmpty(password)) {
		form.old_password.focus();
		mAlert(form.old_password, "Invalid");
		if(form.old_password_invalid) 
			form.old_password_invalid.className = "visible";
		return false;

	}
	else if(email && trim(email) != "")  {
	        //verify if email/password pair is valid
		sUrl = 'co_check_agent_login.asp?Email_ID=' + escape(email)  + '&Help_PW=' + escape(password);
		
		httpRequest = new XMLHttpRequest(); 
		httpRequest.open('GET', sUrl, false); 
		httpRequest.send(null); 

		LoginOK = httpRequest.responseText;
		var loginValid = false;
		
		if (-1 != LoginOK.indexOf("YES")) {
			loginValid = true;
		} else if ("TIME_EXPIRED" == LoginOK) {
			loginValid = true;
		} else if ("MONEY_EXPIRED" == LoginOK) {	
			loginValid = true;
		} else if ("OVER_LIMIT" == LoginOK) {						
			loginValid = true;
		} else if ("NOT_AGENT" == LoginOK) {						
			loginValid = true;
		} else if ("DBERROR" == LoginOK) {
			loginValid = false;
		} else {
			loginValid = false;
		}

		if(!loginValid) {
			form.old_password.focus();
			mAlert(form.old_password, "Invalid password");
			if(form.old_password_invalid) 
				form.old_password_invalid.className = "visible";

			if(triggerCnt) {
				document.getElementById(triggerCnt.id).value = "true";
				document.getElementById("antispamRow_0").style.display = "";
				document.getElementById("antispamRow_1").style.display = "";
			}
			return false;
		}
		else {
			if(form.old_password_invalid) 
				form.old_password_invalid.className = "inv";
		}
	}


	mAlertHide(form.old_password);
	if (isEmpty(form.new_password.value)) {
		form.new_password.focus();
		mAlert(form.new_password, "Invalid");
		if(form.new_password_invalid) 
			form.new_password_invalid.className = "visible";
		return false;
	}
	else {
		var newPassword = trim(form.new_password.value);
		var hasDigit = false;
		var hasLowercase = false;
		var hasUppercase = false;
		//check complexity requirements
		for (i = 0; i < newPassword.length; i++)
		{   
			var char = newPassword.charAt(i);
			hasDigit |= charDigits.indexOf(char) != -1;
			hasLowercase |= charLower.indexOf(char) != -1;
			hasUppercase |= charUpper.indexOf(char) != -1;
		}

		if(!hasDigit || !hasLowercase || !hasUppercase || newPassword.length < 8 || newPassword.length > 25) {
			form.new_password.focus();
			mAlert(form.new_password, "Invalid");
			if(form.new_password_invalid) 
				form.new_password_invalid.className = "visible";
			return false;
		}
		else {
			if(form.new_password_invalid) 
				form.new_password_invalid.className = "inv";

			mAlertHide(form.new_password);
		}
	}

	if (isEmpty(form.new_password2.value)) {
		form.new_password2.focus();
		mAlert(form.new_password2, "Invalid");
		if(form.new_password2_invalid) 
			form.new_password2_invalid.className = "visible";
		return false;
	}
	else if (form.new_password.value != form.new_password2.value) {
		form.new_password.focus();
		mAlert(form.new_password2, "Passwords do not match");
		if(form.new_password_invalid) 
			form.new_password_invalid.className = "visible";
		if(form.new_password2_invalid) 
			form.new_password2_invalid.className = "visible";
		return false;
	}
	else {
		if(form.new_password2_invalid) 
			form.new_password2_invalid.className = "inv";

		mAlertHide(form.new_password2);
	}


	if(form.Verification && triggerCnt && triggerCnt.value == "true") {
		
		if(trim(form.Verification.value.toUpperCase()) != form.Verification_Char.value.toUpperCase()) {

			validated = false;
			form.Verification_img.className = "visible";
			document.getElementById("Verification_error").className = "error";
			document.getElementById("Verification_error").innerHTML = form.Verification.value == "" ? "Invalid" : "Characters do not match";
			form.Verification.focus();

			reloadVerificationCode(document.getElementById("verificationDiv"), form.Verification_Char, form.Verification);
		}
		else {
			form.Verification_img.className = "inv";
			document.getElementById("Verification_error").className = "error inv";
			document.getElementById("Verification_error").innerHTML = "";
		}
		if(!validated) return false;
	}

	return true;
}


function onPasswordReset(form) {
	if(!check_forgotten_password(form))
		return false;

	document.getElementById("row0").style.display="none";
	document.getElementById("row1").style.display="none";
	document.getElementById("antispamRow_0").style.display="none";
	document.getElementById("antispamRow_1").style.display="none";

	form.Email_ID.disabled = "disabled";
	form.Verification.disabled = "disabled";

	document.getElementById("subColRight").style.display="none";
	document.getElementById(form.name + "_submit").onclick = function() { return false; };	
	document.getElementById(form.name + "_submit").style.cursor = "default";
	document.getElementById(form.name + "_success").className = "visible";
	document.getElementById(form.name + "_success").style.display = "block";


	return false;
}


function onPasswordChange(form) {
	if(!check_password_change(form))
		return false;

	return true;
}


function check_input_data_new(form)
{  
	var emailCNT, email, password, sUrl, httpRequest, LoginOK;
	var errMessage = "";
	var remember = true;

	if(form.Email_ID)
		emailCNT = form.Email_ID;
	else if (form.email)
		emailCNT = form.email;

	if(emailCNT)
		email = trim(emailCNT.value);

	if(form.Password)
		password = trim(form.Password.value);

	if(form.rememberMe)
		remember = form.rememberMe.checked;
	else if(form.RememberMe)
		remember = form.RememberMe.checked;
	else if(form.IDStore)
		remember = form.IDStore.checked;

	isValid = true;

	if(!validateValue('not_empty', form.contact_name, 'contact_name_img', 'contact_name_error', 'Name missing')) {
		if(form.Verification)
			reloadVerificationCode("verificationDiv", form.Verification_Char, form.Verification, false);
		return 0;
	}

	if(!validateValue('not_empty', form.first_name, 'first_name_img', 'first_name_error', null)) {
		if(form.Verification)
			reloadVerificationCode("verificationDiv", form.Verification_Char, form.Verification, false);
		return 0;
	}

	if(!validateValue('not_empty', form.last_name, 'last_name_img', 'last_name_error', null)) {
		if(form.Verification)
			reloadVerificationCode("verificationDiv", form.Verification_Char, form.Verification, false);
		return 0;
	}

	if(!validateValue('not_empty', form.job_title, 'job_title_img', 'job_title_error', 'Missing')) {
		if(form.Verification)
			reloadVerificationCode("verificationDiv", form.Verification_Char, form.Verification, false);
		return 0;
	}

	if(form.name == "whiteForm" && !validateValue('phone', form.phone, null, 'phone_error', 'Invalid phone number')) {
		if(form.Verification)
			reloadVerificationCode("verificationDiv", form.Verification_Char, form.Verification, false);
		return 0;
	}
	
	if(!validateValue('email', emailCNT, 'Email_ID_img', $$$(form.name + '_Email_ID_invalid') ? form.name + '_Email_ID_invalid' : 'Email_ID_invalid', 'Invalid email&nbsp;')) {
		if(form.Verification)
			reloadVerificationCode("verificationDiv", form.Verification_Char, form.Verification, false);
		return 0;
	}

	if(!validateValue('not_empty', form.company, null, 'company_error', 'Invalid')) {
		if(form.Verification)
			reloadVerificationCode("verificationDiv", form.Verification_Char, form.Verification, false);
		return 0;
	}

	if(!validateValue('not_empty', form.company_site, null, 'company_site_error', 'Invalid')) {
		if(form.Verification)
			reloadVerificationCode("verificationDiv", form.Verification_Char, form.Verification, false);
		return 0;
	}

	if(!validateValue('not_empty', form.question, null, 'question_error', 'Please enter your question')) {
		if(form.Verification)
			reloadVerificationCode("verificationDiv", form.Verification_Char, form.Verification, false);
		return 0;
	}

	if(form.Verification && trim(form.Verification.value.toUpperCase()) != form.Verification_Char.value.toUpperCase()) {
		//alert(form.Verification_Char.value);
		isValid = false;
		reportValidationError(form.Verification, null, 'Verification_error', form.Verification.value == "" ? "Invalid" : "Characters do not match");
		reloadVerificationCode("verificationDiv", form.Verification_Char, form.Verification, false);
		return false;
	}
		
	else {
		clearValidationError(null, null, 'Verification_error');
	}

	if (!validateValue('not_empty', form.Password, 'password_img', 'password_invalid', 'Password missing&nbsp;')) {
		return 0;
	}

	if(!form.Password)
		return isValid;       

       
	if (isCookiesDisabled()) { 
		mAlert(form, "Please enable cookies on this browser.");   
		return 0;
	}	         

	sUrl = 'co_check_agent_login.asp?Email_ID=' + escape(email)  + '&Help_PW=' + escape(password) + '&IDStore=' + (remember ? 'ON':'OFF');
	
	httpRequest = new XMLHttpRequest(); 
	httpRequest.open('GET', sUrl, false); 
	httpRequest.send(null); 

	LoginOK = httpRequest.responseText;
	//alert(LoginOK);
	
	if (-1 != LoginOK.indexOf("YES")) {
		clearValidationError(null, null, 'Email_ID_invalid');
    		return 1;				
	} else if ("TIME_EXPIRED" == LoginOK) {
		window.location = 'co_expired_time.asp';
		return 0;		 		  			
	} else if ("MONEY_EXPIRED" == LoginOK) {	
		//alert("Please buy LiveLOOK credit.")	  
		window.location = 'co_expired_time.asp';
		return 0;		
	} else if ("OVER_LIMIT" == LoginOK) {						
		clearValidationError(null, null, 'Email_ID_invalid');
		return 4;					
	} else if ("NOT_AGENT" == LoginOK) {						
		window.location = 'not_agent.asp';
		return 0;
	} else if ("DBERROR" == LoginOK) {
		reportValidationError(null, null, 'Email_ID_invalid', 'Error connecting to the database.');
		return 0;
	} else {
		reportValidationError(null, null, 'Email_ID_invalid', 'Invalid login or password');
		form.Password.value = "";
		return 0;
	}

	mAlertHide(form);
	return true;
}

function onContactAttempt(form) {
	var result = check_input_data_new(form);
	return (result != 0);
}

function onDownloadAttempt(form) {
	var result = check_input_data_new(form);
	return (result != 0);
}


function onLoginAttempt(form) {
	var PaymentPlan = "";
	var result = check_input_data_new(form); 
	if (1 ==  result){
		var url = location.protocol  + "//" + location.host + location.pathname;
		window.location.replace(url + "?login=true");
	  	return false;
	} 
                   
                    
	else if (4 == result) {
		window.location.replace('co_over_limit.htm');					
		return false;						
	}

	else 
		return false;
		                                                                
}

function displayImage(image_name, image_file_name) {
    
		// Firefox
		document.getElementById(image_name).src = image_file_name;	
		
}

function displayText(label_name, label_text) {

    //debug_msg(label_text);
    
		// Firefox
		document.getElementById(label_name).textContent = label_text;	
		
}




function isCookiesDisabled()
{
	return 0;
}



function isEmail(s)
{   
    
     if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);

    // is s whitespace?
    if (isWhitespace(s)) 
		return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


function reloadVerificationCode(imgCnt, valCnt, inputCnt) {

	imgCnt = $$$(imgCnt);
	sUrl = "getVerificationCode.asp";
	
	httpRequest = new XMLHttpRequest(); 
	httpRequest.open('GET', sUrl, false); 
	httpRequest.send(null); 

	codeString = httpRequest.responseText;
	var imgPos = codeString.indexOf("img:");
	var valPos = codeString.indexOf("val:");

	if(imgPos == -1 || valPos == -1)
		return false;

	var imgVal = codeString.substring(imgPos + 4, valPos);
	var valVal = codeString.substring(valPos + 4);

	imgCnt.style.visibility = "hidden";
	imgCnt.innerHTML = imgVal;
	imgCnt.style.visibility = "visible";
	valCnt.value = valVal;

	if(inputCnt)
		inputCnt.value = "";

	//alert(valCnt.value+", "+imgCnt.innerHTML);

	return true;
}


//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
	object.zoom = "1";
}


function $$$(element) {
	//alert(element);
	if(typeof(element) == 'string') {
		return document.getElementById(element);
	}
		
	return element;
}

function reportValidationError(inputElement, imageElement, spanElement, errorMessage) {
	
	inputElement = $$$(inputElement);
	imageElement = $$$(imageElement);
	spanElement = $$$(spanElement);
	
	if (imageElement && imageElement.style.visibility != 'hidden')
		imageElement.className = "visible";
	else if (inputElement)
		inputElement.style.borderColor = "red";
	
	if (spanElement && spanElement.style.visibility != 'hidden') {
		spanElement.className = "error";
		spanElement.innerHTML = (errorMessage != null && errorMessage != '') ? errorMessage : "Invalid";
	}
	
	if(inputElement)
		inputElement.focus();
}

function clearValidationError(inputElement, imageElement, spanElement, errorMessage) {
	inputElement = $$$(inputElement);
	imageElement = $$$(imageElement);
	spanElement = $$$(spanElement);

	if (imageElement)
		imageElement.className = "inv";
	else if (inputElement)
		inputElement.style.borderColor = "";
		
	if (spanElement && spanElement.style.visibility != 'hidden') {
		spanElement.className = "error inv";
		spanElement.innerHTML = "";
	} 
}



function validateValue(validationType, inputElement, imageElement, spanElement, errorMessage) {

	if(!inputElement || inputElement == undefined) {
		return true;}

	inputElement = $$$(inputElement);
	imageElement = $$$(imageElement);
	spanElement = $$$(spanElement);

	if(!inputElement || inputElement == undefined) {
		return true;}

	var validationResult = true;
	var elementValue = trim(inputElement.value);
	
	if(validationType == "not_empty") {
		
		if(isEmpty(elementValue) || isWhitespace(elementValue)) {
			validationResult = false;
			reportValidationError(inputElement, imageElement, spanElement, errorMessage);
		}
		else {
			clearValidationError(inputElement, imageElement, spanElement, errorMessage);
		}
		
		return validationResult;
		
	}
	
	else if (validationType == 'email') {
		//alert($$$(spanElement));
		validationResult = validateValue('not_empty', inputElement, imageElement, spanElement, errorMessage);
		if(!validationResult) 
			return false;
		else if (!isEmail(elementValue)) {
			validationResult = false;
			reportValidationError(inputElement, imageElement, spanElement, errorMessage == null || errorMessage == '' ? 'Invalid email' : errorMessage);
		}
		else {
			clearValidationError(inputElement, imageElement, spanElement, errorMessage);
		}
		
		return validationResult;
	}
	
	else if (validationType == 'phone') {
		validationResult = validateValue('not_empty', inputElement, imageElement, spanElement, errorMessage);
		if(!validationResult) 
			return false;
		else if (elementValue.length < 10) {
			validationResult = false;
			reportValidationError(inputElement, imageElement, spanElement, errorMessage);
		}
		else {
			clearValidationError(inputElement, imageElement, spanElement, errorMessage);
		}
		return validationResult;
	}
	
	
	return validationResult;
}



function onScheduleTour(form) {
	var allowSubmit = false;
	
	if(!validateValue('not_empty', form.customerName, null, 'customerName_error', null))
		return false;

	if(!validateValue('phone', form.customerPhone, null, 'customerPhone_error', null))
		return false;
	
	if(!validateValue('email', form.customerEmail, null, 'customerEmail_error', null))
		return false;

	if(!validateValue('not_empty', form.customerPreferredTime, null, 'customerPreferredTime_error', null))
		return false;

	if(!validateValue('not_empty', form.customerTimeZone, null, 'customerTimeZone_error', null))
		return false;
	
	return true;
}

function onSubmitApplication(form) {
		var allowSubmit = false;
		if(!validateValue('not_empty', form.fname, null, 'fname_error', null))
			return false;
		if(!validateValue('not_empty', form.lname, null, 'lname_error', null))
			return false;
		if(!validateValue('not_empty', form.cname, null, 'cname_error', null))
			return false;
		if(!validateValue('not_empty', form.address, null, 'address_error', null))
			return false;
		if(!validateValue('not_empty', form.city, null, 'city_error', null))
			return false;
			
		var country = form.country.value;
		if(country == 'United States') {
			if(!validateValue('not_empty', form.state, null, 'state_error', null))
				return false;
			if(!validateValue('not_empty', form.zip, null, 'zip_error', null))
				return false;
		}
		else
		{
			clearValidationError(form.state, null, 'state_error', null);
			clearValidationError(form.zip, null, 'zip_error', null);
			
		}

		if(!validateValue('not_empty', form.country, null, 'country_error', null))
			return false;
		if(!validateValue('phone', form.phone, null, 'phone_error', null))
			return false;
		if(!validateValue('email', form.email, null, 'email_error', null))
			return false;
		if(!validateValue('not_empty', form.industry, null, 'industry_error', null))
			return false;
			
			
		return true;
}