//JavaScript Document

function Scroll_To_Top() {
	var index = window.location.href.indexOf('#');
	if (index != -1) {
		window.location = window.location.href.substr(0, index) + '#top';
	}
	else {
		window.location = window.location.href + '#top';
	}
}

function Show_Forgot_Password_Link() {
	var element = document.getElementById('forgot_password_link');
	element.style['display'] = '';
}

function Open_Forgot_Password_Window() {
	window.open('/login_sendpass.asp?email=' + document.forms['trialform'].Email.value);
}

function CheckEmail_Success(arg_response) {
	 // alert('CheckEmail_Success:'+arg_response);
	if (arg_response.indexOf("<verify_login_xml_success>true</verify_login_xml_success>") != -1) {
		sendAjax('GET','/ajax_receiver.asp?system=verify_login&Email='+document.forms['trialform'].Email.value+'&password='+document.forms['trialform'].password.value,null,CheckEmailPass_Success,CheckEmailPass_Failure,false)
	} else {
		// alert('CheckEmail_Success_ELSE:'+arg_response);
		document.forms['trialform'].submit();
	}
}
function CheckEmail_Failure(arg_status,arg_response) {
	// alert('CheckEmail_Failure:'+arg_status+':'+arg_response);
	alert('Failed to submit. Please try to sumit again. We apologize for the inconvenience.')
}
function CheckEmailPass_Success(arg_response) {
	// alert('CheckEmailPass_Success:'+arg_response);
	if (arg_response.indexOf("<verify_login_xml_success>true</verify_login_xml_success>") != -1) {

		document.forms['trialform'].submit();		
	} else {
		document.forms["trialform"].password.focus();
		document.getElementById("overlaymaster").style.display='block';
		document.getElementById("overlaymasterbackground").style.display='block';
		document.getElementById("forgottenPasswordPrompt").style.display='block';
	}
}
function CheckEmailPass_Failure(arg_status,arg_response) {
	alert('failure:'+arg_status+':'+arg_response);
}
function switch_to_create_new_account() {
		document.getElementById('login_existing_customer').style.display="none";
		document.getElementById('create_new_account').style.display="";
}
function switch_to_login_existing_customer() {
		document.getElementById('login_existing_customer').style.display="";
		document.getElementById('create_new_account').style.display="none";
}

/*************** FORM VALIDATION ***************/


var errMsg = "";

function validateField(invalidCharType, matchName, errorName, errorTarget){

	var invalidChar;
	var errMsgText;
	
	switch (invalidCharType) {
		case 1: 
			invalidChar=/\W/;
			errMsgText="Please remove the invalid characters \/\|\!\@\#\$\%\^\&\*\-\(\)\,\+\=\{\}\?\:\"'from the "+errorName+" field.\n";
			break;
		case 2:
			invalidChar=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9-])+.([a-zA-Z0-9]{2,4})+$/;
			errMsgText="Please enter a valid e-mail address.\n";
			break;
		case 3:
			invalidChar=/\|/;
			errMsgText="Please choose another password without a '|'.\n";
			break;
		case 4:
			invalidChar="";
			errMsgText="";
			break;
		default:
			invalidChar=/\/|\||\!|\@|\#|\$|\%|\^|\&|\*|\-|\(|\)|\,|\+|\=|\{|\}|\?|\:|\"/;
			errMsgText="Please remove the invalid characters \/\|\!\@\#\$\%\^\&\*\-\(\)\,\+\=\{\}\?\:\"'from the "
						+errorName+" field.\n";
			break;
	}

	if (matchName.value == "" && invalidCharType < 3)	{
			errMsg += "Please enter your "+errorName+".\n";
			document.getElementById(errorTarget).className = ('empty');
		
	}else if (matchName.value.length < 6 && invalidCharType == 3){
			errMsg += "Please enter a "+errorName+" of at least 6 characters.\n";
			document.getElementById(errorTarget).className = ('empty');
	}else if (matchName.selectedIndex == 0 && invalidCharType == 4){
			errMsg += "Please select your "+errorName+".\n";
			document.getElementById(errorTarget).className = ('empty');
	}else {
		var matchArray=matchName.value.match(invalidChar);
		
		if (invalidCharType == 2 && !matchArray){
			errMsg += errMsgText;
			document.getElementById(errorTarget).className = ('invalid');
		}
		else if (invalidCharType != 2 && invalidCharType != 4 && matchArray) {
			errMsg += errMsgText;
			document.getElementById(errorTarget).className = ('invalid');
		}else{
			document.getElementById(errorTarget).className = ('passed');
		}
	}
	return false;
}

function validateForm()
{
	errMsg = "";
	
	if (document.getElementById('login_existing_customer').style.display=='none') {
		//must be a new customer
	} 
	else {
		if (document.forms['trialform2'].existing_password.value.length < 1) {
			alert("Please type your password above."); return false;
		}
		// also clear out the firstname field, to signify NOT to update the customer information...
		document.forms['trialform2'].BillingFirstName.value = "";
		document.forms['trialform2'].password.value = document.forms['trialform2'].existing_password.value;
	}
	
	document.forms['trialform'].Emailagain.value = document.forms['trialform'].Email.value;
	document.forms['trialform'].passwordagain.value = document.forms['trialform'].password.value;
	
	document.forms['trialform2'].Emailagain.value = document.forms['trialform2'].Email.value;
	document.forms['trialform2'].passwordagain.value = document.forms['trialform2'].password.value;
	
	if (document.getElementById('login_existing_customer').style.display=='none') {
		
		validateField(1, document.forms['trialform'].BillingFirstName, "First Name", "BillingFirstName");
		validateField(1, document.forms['trialform'].BillingLastName, "Last Name", "BillingLastName");
		validateField(1, document.forms['trialform'].BillingPhoneNumber, "Phone Number", "BillingPhoneNumber");
		validateField(1, document.forms['trialform'].BillingPostalCode, "Zip Code", "BillingPostalCode");
		validateField(4, document.getElementById('BillingCountry'), "Country", "BillingCountry");		
		validateField(4, document.getElementById('Customers.Custom_Field_Industry'), "Industry", "Customers.Custom_Field_Industry");	
		validateField(2, document.forms['trialform'].Email, "Email", "Email");
		validateField(3, document.forms['trialform'].password, "Password", "password");
		
		//website is not required at this time
		//validateField(1, document.forms['trialform'].WebsiteAddress, "Website Address", "WebsiteAddress");
	}

	else {
		validateField(3, document.forms['trialform2'].password, "Password", "password");
		validateField(2, document.forms['trialform2'].Email, "Email", "Email");
	}
	
	if(errMsg.length>0){
		alert(errMsg);
		return false;
	}else{
	
		if (document.getElementById('login_existing_customer').style.display=='none') {
			// alert ("Sending new customer info"); return false; 
			sendAjax('GET','/ajax_receiver.asp?system=verify_login&Email='+document.forms['trialform'].Email.value,null,CheckEmail_Success,CheckEmail_Failure,false)
		} else {
			// alert ("Sending returning customer info"); return false; 
			sendAjax('GET','/ajax_receiver.asp?system=verify_login&Email='+document.forms['trialform2'].Email.value+'&password='+document.forms['trialform2'].password.value,null,CheckEmailPass_Success,CheckEmailPass_Failure,false)
		}
	
	}
	return false; // so in that case, force return false;
}