//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";
}

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('text', document.forms['trialform'].BillingFirstName, "First Name", "BillingFirstName");
		validateField('text', document.forms['trialform'].BillingLastName, "Last Name", "BillingLastName");
		validateField('phone', document.forms['trialform'].BillingPhoneNumber, "Phone Number", "BillingPhoneNumber");
		validateField('zip', document.forms['trialform'].BillingPostalCode, "Zip Code", "BillingPostalCode");
		validateField('selectlist', document.getElementById('BillingCountry'), "Country", "BillingCountry");		
		validateField('selectlist', document.getElementById('Customers.Custom_Field_Industry'), "Industry","Customers.Custom_Field_Industry");	
		validateField('email', document.forms['trialform'].Email, "Email", "Email");
		validateField('password', document.forms['trialform'].password, "Password", "password");
		
		//website is not required at this time
		//validateField(1, document.forms['trialform'].WebsiteAddress, "Website Address", "WebsiteAddress");
	}

	else {
		validateField('password', document.forms['trialform2'].password, "Password", "password");
		validateField('email', document.forms['trialform2'].Email, "Email", "Email");
	}
	
	if(errMsg.length>0){
		alert(errMsg);
		return false;
	}else{
		//remove the formating characters from the phone before sending it off
		var stripValue = document.forms['trialform'].BillingPhoneNumber.value;
		stripValue = stripValue.replace(/[\s\-\(\)\.\[\]\+]/g,'');
		document.forms['trialform'].BillingPhoneNumber.value = stripValue;		
		
		var stripValue2 = document.forms['trialform'].BillingFirstName.value;
		stripValue2 = stripValue2.replace(/\-\-/,'-');
		document.forms['trialform'].BillingFirstName.value = stripValue2;	
		
		var stripValue3 = document.forms['trialform'].BillingLastName.value;
		stripValue3 = stripValue3.replace(/\-\-/,'-');
		document.forms['trialform'].BillingLastName.value = stripValue3;
		
		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;
}
