var _global = this;
var browser = navigator.appName;

//function to close step pop-up window
function closeThisStep(){
	if(this.window.opener && !this.window.opener.closed){
		this.window.close();
		window.opener.focus();
	}
	else{
		location='torders.cfm';
	}
}

//trim whitespace from start and end of any string.
function trim(strText){
	strText=strText.replace(/^\s+/, '');
	strText=strText.replace(/\s+$/, '');
	return strText;
}

//unsynchonous http request for extras prefs submission *******************************
var contactRo;
_global.contactSubmitButton=false;
function handleContactsResponse(){
	if(contactRo.readyState == 4){
		var response = trim(contactRo.responseText);
		_global.contactSubmitButton.disabled=false;
		_global.contactSubmitButton.value='Submit Contact Info';
		if(contactRo.status == 200 && contactRo.responseText.indexOf('title06.gif') == -1){
			alert(response);
		}
		else{
			alert("We're sorry - an error ocurred.");
		}
	}
}

//validate contact form
function validateForm(theForm, button){
	_global.contactSubmitButton=button;
	fName=theForm.fName.value;
	lName=theForm.lName.value;
	phone=theForm.phone.value;
	fax=theForm.fax.value;
	email=theForm.email.value;
	street=theForm.street.value;
	city=theForm.city.value;
	state=theForm.state.value;
	country=theForm.country.value;
	zip=theForm.zip.value;
	teamName=theForm.teamName.value;
	qtys=theForm.qtys.value;

	firstREString="[^a-zA-Z_,'\\/\.!\&\(\) \-]";
	firstRE=new RegExp(firstREString);
	lastREString="[^a-zA-Z_,'\\/\.!\&\(\) \-]";
	lastRE=new RegExp(lastREString);
	street1REString="[^a-zA-Z0-9_,'\\/\.!\&\(\) \-]";
	street1RE=new RegExp(street1REString);
	cityREString="[^a-zA-Z0-9_,'\\/\.!\(\) \-]";
	cityRE=new RegExp(cityREString);
	stateREString="[^a-zA-Z0-9_,'\\/\.!\(\) \-]";
	stateRE=new RegExp(cityREString);
	countryREString="[^a-zA-Z0-9_,'\\/\.!\(\) \-]";
	countryRE=new RegExp(cityREString);
	zipREString="[^a-zA-Z0-9_ ]";
	zipRE=new RegExp(zipREString);
	emailREString="[^a-zA-Z0-9_,'\\/\.!\&\(\)\@\-]";
	emailREStringB = /.*\@.*\..*/;
	emailRE=new RegExp(emailREString);
	emailREB=new RegExp(emailREStringB);
	phoneREString="[^0-9_\.\(\) \-]";
	phoneRE=new RegExp(phoneREString);
	faxREString="[^0-9_\.\(\) \-]";
	faxRE=new RegExp(faxREString);
	teamNameREString="[^a-zA-Z0-9_,'\\/\.!\&\(\) \-]";
	teamNameRE=new RegExp(teamNameREString);
	qtysREString= /[#%;]/;
	qtysRE=new RegExp(qtysREString);

	//test billing entries
	if(fName == "" || firstRE.test(fName) || fName.length<2){
		window.alert("We require at least a valid 1st name so we know who to contact regarding this order.");
		return false;
	}
	if(lastRE.test(lName)){
		window.alert("There's a problem with your last name. Please make sure there are no special characters.");
		return false;
	}
	if( (email == "" || emailRE.test(email) || email.length<5 || !emailREB.test(email)) && (phone == "" || phoneRE.test(phone) || phone.length<10) && (fax == "" || faxRE.test(fax) || fax.length<10) ){
		window.alert("We require a valid email address, phone number, or fax number in order to contact you regarding this order.");
		return false;
	}
	if(street1RE.test(street)){
		window.alert("There's a problem with your street address. Please make sure there are no special characters.");
		return false;
	}
	if(street1RE.test(street)){
		window.alert("There's a problem with your street address. Please make sure there are no special characters.");
		return false;
	}
	if(cityRE.test(city)){
		window.alert("There's a problem with your city. Please make sure there are no special characters.");
		return false;
	}
	if(stateRE.test(state)){
		window.alert("There's a problem with your state. Please make sure there are no special characters.");
		return false;
	}
	if(country.length<2 || countryRE.test(country)){
		window.alert("There's a problem with your country. Please make sure you've properly filled in this field and that there are no symbols or special characters contained in it.");
		return false;
	}
	if(teamNameRE.test(teamName)){
		window.alert("There's a problem with your team name. Please make sure there are no special characters.");
		return false;
	}
	if(qtysRE.test(qtys)){
		window.alert("There's a problem with your team quantities description. Please make sure there are no unusual special characters (quotation marks, etc).");
		return false;
	}

	//start http request for contact form
	button.value='Sending...';
	button.disabled=true;
	if(browser == "Microsoft Internet Explorer") contactRo = new ActiveXObject("Microsoft.XMLHTTP");
	else contactRo = new XMLHttpRequest();
	contactRo.open('post', 'torders-s5-req.cfm');
	queryString='fName='+fName+'&lName='+lName+'&phone='+phone+'&fax='+fax+'&email='+email+'&street='+street+'&city='+city+'&state='+state+'&country='+country+'&zip='+zip+'&qtys='+qtys+'&teamName='+teamName+'&pageFunction='+theForm.pageFunction.value;
	contactRo.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    contactRo.send(queryString);
	contactRo.onreadystatechange = handleContactsResponse;
}