var doregister=false;
var AJAX = new ajaxClass();
var pass = false;
// SET CALLBACK HANDLER
registerCallback = function(xmldata) {
	if (xmldata) {
		var response = '';
		if(xmldata.getElementsByTagName('status').item(0).firstChild){
			var response = xmldata.getElementsByTagName('status').item(0).firstChild.data;
		}
		if (response != '') {
			document.getElementById('availability_response').innerHTML = response;
		}
	} else {
		alert('Cannot verify username with the server. Please try again.');
	}				
}
registerCallbackSubmit = function(xmldata) {
	if (xmldata) {
		var response = '';
		if(xmldata.getElementsByTagName('status').item(0).firstChild){
			var response = xmldata.getElementsByTagName('status').item(0).firstChild.data;
		}
		if (response != '') {
			document.getElementById('email_response').innerHTML = response;
			document.register.submitButton.disabled=false;
			document.register.submitButton.value='submit';
		}
	} else {
		alert('Cannot verify email address with the server. Please try again.');
	}
}
// SET CALLBACK HANDLER
emailCallback = function(emailxmldata) {
	if (emailxmldata) {
		var emailresponse = '';
		if(emailxmldata.getElementsByTagName('emailstatus').item(0).firstChild){
			var emailresponse = emailxmldata.getElementsByTagName('emailstatus').item(0).firstChild.data;
		}
		if (emailresponse != '') {
			document.getElementById('email_response').innerHTML = emailresponse;
		}
	} else {
		alert('Cannot verify email address with the server. Please try again.');
	}				
}
emailCallbackSubmit = function(emailxmldata) {
	if (emailxmldata) {
		var emailresponse = '';
		if(emailxmldata.getElementsByTagName('emailstatus').item(0).firstChild){
			var emailresponse = emailxmldata.getElementsByTagName('emailstatus').item(0).firstChild.data;
		}
		if (emailresponse != '') {
			document.getElementById('email_response').innerHTML = emailresponse;
			document.register.submitButton.disabled=false;
			document.register.submitButton.value='submit';
		}
	} else {
		alert('Cannot verify username with the server. Please try again.');
	}
}
// CHECK USERNAME AVAILABILITY
function checkEmail(process) {
	var e = document.getElementById('gigoit_email').value;
	if(e != '' && process == 0){
		if (!doregister) {
			AJAX.fetchXML ('includes/validate.email.php', 'email='+e, emailCallback);
		}
	} 
	else if(e != '' && process == 1){
		if (!doregister) {
			AJAX.fetchXML ('includes/validate.email.php', 'email='+e, emailCallbackSubmit);
		}
	}
	else {
		document.getElementById('email_response').innerHTML = '';
	}
}
// CHECK USERNAME AVAILABILITY
function checkUsername(process) {
	var u = document.getElementById('gigoit_username').value;
	if(u != '' && process == 0){
		if (!doregister) {
			AJAX.fetchXML ('includes/validate.username.php', 'username='+u, registerCallback);
		}
	} 
	else if(u != '' && process == 1){
		if (!doregister) {
			AJAX.fetchXML ('includes/validate.username.php', 'username='+u, registerCallbackSubmit);
		}
	}
	else {
		document.getElementById('availability_response').innerHTML = '';
	}
}

// CHECK ZIP AVAILABILITY
function checkZip(process) {
	var theZip = document.getElementById('gigoit_zip').value;
	if(theZip != '' && process == 0){
		if (!doregister) {
			AJAX.fetchXML ('includes/validate.zip.php', 'theZip='+theZip, checkZipCallBack);
		}
	} 
	else if(theZip != '' && process == 1){
		if (!doregister) {
			AJAX.fetchXML ('includes/validate.zip.php', 'theZip='+theZip, checkZipCallBackSubmit);
		}
	}
}

checkZipCallBack = function(xmldata) {
	if (xmldata) {
		var response = '';
		if(xmldata.getElementsByTagName('zipstatus').item(0).firstChild){
			var response = xmldata.getElementsByTagName('zipstatus').item(0).firstChild.data;
		}
		if (response != '') {
			document.getElementById('zip_status').innerHTML = response;
			if(response.charAt(22) == "!" && response.charAt(23) == "!" && response.charAt(24) == "!"){
				document.getElementById('zip').value = '';
			}
		}
	} else {
		alert('Cannot verify username with the server. Please try again.');
	}				
}

checkZipCallBackSubmit = function(xmldata) {
	if (xmldata) {
		var response = '';
		if(xmldata.getElementsByTagName('zipstatus').item(0).firstChild){
			var response = xmldata.getElementsByTagName('zipstatus').item(0).firstChild.data;
		}
		if (response != '') {
			document.getElementById('zip_status').innerHTML = response;
			document.register.submitButton.disabled=false;
			document.register.submitButton.value='submit';
			if(response.charAt(22) == "!" && response.charAt(23) == "!" && response.charAt(24) == "!"){
				return pass = false;
			} else {
				return pass = true;
			}
		}
	} else {
		alert('Cannot verify zip with the server. Please try again.');
	}				
}



var AJAX= new ajaxClass();

function openIndividual(){
	document.getElementById('account_info').style.visibility='visible';	
	document.getElementById('corporate_details').style.visibility='hidden';
	document.getElementById('account_info').style.height='330px';	
	document.getElementById('corporate_details').style.height='0px';
	document.getElementById('process_registration').style.height='25px';
	document.getElementById('process_registration').style.visibility='visible';
	document.getElementById('ind_button').innerHTML='<img src="../images/newuser_ind,0.gif" name="individual" width="100" height="33" border="0" id="individual" />';
	document.getElementById('corp_button').innerHTML='<a href="javascript:void(0);" onclick="this.blur();openCorporate();" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'corporate\',\'\',\'../images/newuser_corp,0.gif\',1)"><img src="../images/newuser_corp,1.gif" name="corporate" width="100" height="33" border="0" id="corporate" /></a>';
	document.register.gigoit_account.value = 'individual';
	document.getElementById('TaxExemptCheck').style.visibility='hidden';
	document.register.gigoit_username.focus();
}
function openCorporate(){
	document.getElementById('account_info').style.visibility='visible';	
	document.getElementById('corporate_details').style.visibility='visible';
	document.getElementById('account_info').style.height='330px';	
	document.getElementById('corporate_details').style.height='300px';
	document.getElementById('process_registration').style.height='25px';
	document.getElementById('process_registration').style.visibility='visible';
	document.getElementById('ind_button').innerHTML='<a href="javascript:void(0);" onclick="this.blur();openIndividual();" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'individual\',\'\',\'../images/newuser_ind,0.gif\',1)"><img src="../images/newuser_ind,1.gif" name="individual" width="100" height="33" border="0" id="individual" /></a>';
	document.getElementById('corp_button').innerHTML='<img src="../images/newuser_corp,0.gif" name="corporate" width="100" height="33" border="0" id="corporate" />';
	document.register.gigoit_account.value = 'organization';
	taxExemptCheck(document.register.businesstype.value);
	document.register.gigoit_username.focus();
}
function showOther(value){
	if(value == 'Other'){
		document.getElementById('referred_other').style.display = '';
		document.getElementById('gigoit_referred_other').focus();
	} else {
		document.getElementById('referred_other').style.display = 'none';
	}
}
function taxExemptCheck(value){
	if(value == "nonprofit"){
		document.getElementById('TaxExemptCheck').style.visibility='visible';
	} else {
		document.getElementById('TaxExemptCheck').style.visibility='hidden';
	}
}
function formValidation(){
	
	// define form
	var f = document.register;
	
	// get feedback from username check to verify unique name
	var usernameFeedback = document.getElementById('availability_response').innerHTML;
	var zipFeedback = document.getElementById('zip_status').innerHTML;
	var emailFeedback = document.getElementById('email_response').innerHTML;
	
	// test for completion of main account info
	if(f.gigoit_username.value != "") {
		checkUsername(1);
	}
	if(f.gigoit_zip.value != ""){
		checkZip(1);	
	}
	if(f.gigoit_email.value != ""){
		checkEmail(1);	
	}
	if(f.gigoit_account.value == "individual" || f.gigoit_account.value == "organization"){
		if(f.gigoit_username.value == ""){
			alert("Please enter your Username");
			f.gigoit_username.focus();
			return false;
		}
		else if(usernameFeedback.charAt(22) == "!" && usernameFeedback.charAt(23) == "!" && usernameFeedback.charAt(24) == "!"){
			alert("The username you have selected is invalid or is already taken");
			f.gigoit_username.focus();
			return false;
		}
		else if(f.gigoit_password.value == ""){
			alert("Please enter your Password");
			f.gigoit_password.focus();
			return false;
		}
		else if(f.gigoit_password2.value == ""){
			alert("Please confirm your Password");
			f.gigoit_password2.focus();
			return false;
		}
		else if(f.gigoit_password.value != f.gigoit_password2.value){
			alert("Your passwords do not match");
			f.gigoit_password2.focus();
			return false;
		}
		else if(f.gigoit_email.value == ""){
			alert("Please enter your Email Address");
			f.gigoit_email.focus();
			return false;
		}
		else if(f.gigoit_email.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1){
			alert('Please enter a valid Email Address.');
			f.gigoit_email.focus();
			return false;
		}
		else if(emailFeedback.charAt(22) == "!" && emailFeedback.charAt(23) == "!" && emailFeedback.charAt(24) == "!"){
			alert("The email address you have entered has already been used");
			f.gigoit_email.focus();
			return false;
		}
		else if(f.gigoit_zip.value == ""){
			alert("Please enter your Zip Code");
			f.gigoit_zip.focus();
			return false;
		}
		else if(zipFeedback.charAt(22) == "!" && zipFeedback.charAt(23) == "!" && zipFeedback.charAt(24) == "!"){
			alert("Invalid Zip Code");
			f.gigoit_zip.value = '';
			f.gigoit_zip.focus();
			return false;
		}
		else if(f.gigoit_radius.value == ""){
			alert("Please enter the Distance you are\nwilling to travel to pick up an item.");
			f.gigoit_radius.focus();
			return false;
		}
		else if(f.gigoit_securityquestion.value == ""){
			alert("Please choose a security question.");
			f.gigoit_securityquestion.focus();
			return false;
		}
		else if(f.gigoit_securityanswer.value == ""){
			alert("Please answer a security question.");
			f.gigoit_securityanswer.focus();
			return false;
		}
		else if(f.gigoit_securityanswer.value != f.gigoit_securityanswer2.value){
			alert("Your security answers do not match.");
			f.gigoit_securityanswer2.focus();
			return false;
		}
	}
	// test for completion for organization account forms if user selected organization
	if(f.gigoit_account.value == "organization"){
		if(f.companyname.value == ""){
			alert("Please enter your Company name");
			f.companyname.focus();
			return false;
		}
		else if(f.businesstype.value == ""){
			alert("Please select your Business Type");
			f.businesstype.focus();
			return false;
		}
		else if(f.businesstype.value == "nonprofit" && f.TaxExempt[0].checked == false && f.TaxExempt[1].checked == false){
			alert("Is your Non Profit Organizaton Tax Exempt?");
			f.TaxExempt[0].focus();
			return false;
		}
		else if(f.address1.value == ""){
			alert("Please enter your Address");
			f.address1.focus();
			return false;
		}
		else if(f.city.value == ""){
			alert("Please enter your City");
			f.city.focus();
			return false;
		}
		else if(f.state.value == ""){
			alert("Please select your State");
			f.state.focus();
			return false;
		}
		else if(f.mailing_zip.value == ""){
			alert("Please enter your Zip Code");
			f.mailing_zip.focus();
			return false;
		}
	}
	if(f.agree.checked == false){
		alert("Please check that you agree to our Terms of Use & Privacy Policy");
		f.agree.focus();
		return false;
	}
}
function validateStepOne(){
	if(document.register.agree.checked == false){
		alert('Please state that you agree to be 13 years of age or older.');
		document.register.agree.focus();
		return false;
	}
}
function validateStepTwo(){
	if(document.register.gigoit_account[0].checked == false && document.register.gigoit_account[1].checked == false){
		alert('Please select an account type.');
		document.register.gigoit_account[0].focus();
		return false;
	}
}