var SiteUrl = 'http://www.boaterslife.com/';

function checkForm (){
	if (document.forms["Request"].protect.value == 1){
		document.forms["Request"].submit.disabled = true;
		return false;	// already in process ....
	}
	document.forms["Request"].protect.value = 1;
	if ( document.forms["Request"].VIPTour.checked ){
		if ( checkPDF() ){
			if (checkBT()){
				return true;
			}
		}
	} else if (document.forms["Request"].DealerContact.checked){
		if ( checkPDF() ){
			if (checkDC()){
				return true;
			}
		}
	} else if (document.forms["Request"].MailBrochure.checked){
		if ( checkPDF() ){
			if (checkMB()){
				return true;
			}
		}
	} else if (document.forms["Request"].DownloadPDF.checked){
		if (checkPDF())	{
			return true;
		}
	} else {
		if (checkPDF())	{
			return true;
		}
	}
	document.forms["Request"].protect.value = '';
	return false;
}

function confirm (){
	alert ("Your Request has been Submitted,\nplease wait for Confirmation.");
}

function checkPDF(){
	nTest = '^[a-zA-Z .]+$';
	var regex = new RegExp(nTest);

//	if (document.forms["Request"].First_Name.value == ''){
	if (! regex.test (document.forms["Request"].First_Name.value)){
		document.forms["Request"].First_Name.focus();
		alert ("Please type First name! ");
		document.forms["Request"].protect.value = '';
		document.forms["Request"].submit.disabled = false;
		return false;
	}
//	if (document.forms["Request"].Last_Name.value == ''){
	if (! regex.test (document.forms["Request"].Last_Name.value)){
		document.forms["Request"].Last_Name.focus();
		alert ("Please type Last name!");
		document.forms["Request"].protect.value = '';
		document.forms["Request"].submit.disabled = false;
		return false;
	}
	if (document.forms["Request"].email_from.value == ''){
		document.forms["Request"].email_from.focus();
		alert ("Please type Your valid email!");
		document.forms["Request"].protect.value = '';
		document.forms["Request"].submit.disabled = false;
		return false;
	} else if ( ! checkEmail( document.forms["Request"].email_from.value ) ){
		document.forms["Request"].email_from.focus();
		document.forms["Request"].protect.value = '';
		document.forms["Request"].submit.disabled = false;
		alert ("Please type valid email!");
		return false;
	}
	if (document.forms["Request"].ZipCode.value == ''){
		document.forms["Request"].ZipCode.focus();
		document.forms["Request"].protect.value = '';
		document.forms["Request"].submit.disabled = false;
		alert ("Please enter Your ZIP codde!");
		return false;
	}
	if (! checkZipCode(document.forms["Request"].ZipCode.value)){
		document.forms["Request"].ZipCode.focus();
		alert ("Please enter a correct zip code!");
		document.forms["Request"].protect.value = '';
		document.forms["Request"].submit.disabled = false;
		return false;
	}
	document.forms["Request"].name_from.value = document.forms["Request"].First_Name.value +  ' ' + document.forms["Request"].Last_Name.value;
	return true;
}

function checkEmail(email){
	emailTest = '^[_\\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z_-]+\\.)+[a-zA-Z]{2,4}$';
	var regex = new RegExp(emailTest);
	if ( regex.test(email) ) return true;
	else return false;
}

function checkZipCode(Zip){
	if (document.forms["Request"].Country.value == 'United States'){ // for USA
		zipTest = '^[0-9]{5}$|^[0-9]{5}-[0-9]{4}$|^[0-9]{7}$';
	} else if (document.forms["Request"].Country.value == 'Canada'){ // for CANADA
		zipTest = '^[a-zA-Z][0-9][a-zA-Z] [0-9][a-zA-Z][0-9]$';
	} else {
		return true;
	}

	var regex = new RegExp(zipTest);
	if ( ! regex.test(Zip) ) { return false };
	if ( Zip.length < 5 ) { return false};
	return true;
}

function checkPhones(){
	pTest = '^[-+0-9\(\) ]{7,15}$';
	var regex = new RegExp(pTest);
	if(	 regex.test (document.forms["Request"].WorkPhone.value) ||
		regex.test (document.forms["Request"].HomePhone.value)){
		return true;
	}
	return false;
}

function checkAddress(){
	if(	document.forms["Request"].Address.value != '' ||
		document.forms["Request"].Address2.value != '' ){
		if (document.forms["Request"].City.value != '' ) {
			return true;
		}
		document.forms["Request"].City.focus();
		return false;
	}
	document.forms["Request"].Address.focus();
	return false;
}

function checkState(){
	state = document.getElementById('State');
	if (state.style.display == 'none'){
		state = document.getElementById('State2');
	}

	if (state.value != ''){
		return true;
	}
	return false;
}


function checkMB(){
	if ( checkAddress () && checkState () ){
		return true;
	}
	alert ("Sending a Brochure requires an Address, City and State/Province.\nPlease complete the form or uncheck the Brochure request.");
	document.forms["Request"].protect.value = '';
	document.forms["Request"].submit.disabled = false;
	return false;
}

function checkDC(){
	if ( checkAddress () && checkState () && checkPhones() ){
			return true;
	}
	alert ("Having a Dealer Contact you requires your Phone Number (work or home), Address, City and State/Province.\nPlease complete the form or uncheck the Dealer Contact request.");
	document.forms["Request"].protect.value = '';
	document.forms["Request"].submit.disabled = false;
	return false;
}

function checkBT(){
	if ( checkAddress () && checkState () && checkPhones() ){
		return true;
	}
	alert ("Arranging A Private VIP Tour requires your Phone Number (work or home), Address, City and State/Province.\nPlease complete the form or uncheck the Private VIP Tour request.");
	return false;
}

function lookup(text, url) {
		if ( document.forms["Request"].Country.value == "United States"){
			var country = 'US';
			var msg = "Wrong Zip code.\nPlease type it again.\nFormat XXXXX"
		} else if ( document.forms["Request"].Country.value == "Canada"){
			var country = 'CA';
			var msg = "Wrong Postal code.\nPlease type it again.\nFormat XXX XXX"
		} else {
			return;
		}
		if (text.length < 5 && text.length > 0) {
			alert (msg);
			return;
		}
		if(!this.http){
            this.http = get_http();
            this.working = false;
        }
	if (!this.working && this.http) {
            var http = this.http;
            url = url + "?zip="+encodeURIComponent(text) + "&country=" + country;
            this.http.open("GET", url, true);
            this.http.onreadystatechange = function() {
                if (http.readyState == 4) {
                    fill(http.responseText);
                    this.working = false;
                  }else{
                  }
            }
            this.working = true;
            this.http.send(null);
        }
        if(!this.http){
              alert('Error to create XMLHTTP!');
        }
    }

function get_http(){
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new 
                ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
    @else
        xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

function fill (data){

	if ( document.forms["Request"].Country.value == "United States"){
		var msg = "Wrong Zip code.\nPlease type it again.\nFormat XXXXX"
	} else if ( document.forms["Request"].Country.value == "Canada"){
		var msg = "Wrong Postal code.\nPlease type it again.\nFormat XXX XXX"
	} else {
		return;
	}
    if(data.length == 0) {
		alert ( msg );
		return;
	}
    var arr = data.split('\n');
//alert (data);
    for(var i in arr){
		if (arr[i].length != 0 ){
	        val = arr[i].split('\t');
			document.forms["Request"].City.value = val[1];
			state = document.getElementById('State');
			state.value = val[2];
		}
    }
}

function loadStates (){

	var state = document.getElementById('State');
	state.options.length = 0;
	state.style.display = '';
	var state1 = document.getElementById('State2');
	state1.style.display = 'none';

	var zm = document.getElementById('zip_must');
	if (document.forms["Request"].Country.value == "United States"){
		ccode = '232';
	} else if (document.forms["Request"].Country.value == "Canada"){
		ccode = '43';
	} else {
//		state.options[state.options.length]= new Option(' Othere ', 'othere', false, false);
		state1.style.display = '';
		state.style.display = 'none';
		return;
	}
	if(!this.http){
		this.http = get_http();
        this.working = false;
	}

	if (!this.working && this.http) {
		var http = this.http;
        url = "getregion.php?parent="+encodeURIComponent(ccode);
        this.http.open("GET", url, true);
        this.http.onreadystatechange = function() {
		    if (http.readyState == 4) {
				fillSelect('State',http.responseText);
			    this.working = false;
			}else{
			}
		}
		this.working = true;
		this.http.send(null);
	}
    if(!this.http){
		alert('Error to create XMLHTTP!')
	}
}

function fillSelect (id,text){

	var state = document.getElementById('State');
	state.options.length = 0;
	if (text.length == 0){
		return;
	}
	var opts = text.split('\n');
    for(var i in opts){
		if (opts[i].length > 0){
			opt = opts[i].split('\t');
			state.options[state.options.length]= new Option(opt[1], opt[0], false, false);
		}
	}
}