// JavaScript Document


// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//
// Orignal Code by Travis Beckham -  http://www.squidfingers.com
// Changed / Updated to live validation by Cody Lindley - http://www.codylindley.com
//
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// --- version date: 2/9/2006 ---------------------------------------------------------


function isEmpty(str){
	return (str == null) || (str.length == 0);
}

function isEmailValid(str){
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}

function isAlphaNumeric(str){
	var re = /[^a-zA-Z0-9]/g
	if (re.test(str)) return false;
	return true;
}

//********* START OF VICTOR FEINMAN'S ADDITIONS *******
//I have added the following functions bellow

function validateProducts(){
	//if the Products and Services' radio button is checked
	if (document.Contact.Category[0].checked)
	{
		//if the industry in the drop down menu is not selected
		if (document.Contact.Industry.value=="none")
		{
			//display the error message
			document.getElementById("industryError").className = "required";
			//false trips the error message at the top of the page and halts submission
			return false;
		}
	}
	//If any other category is selected return true... no validation needed. 
	document.getElementById("industryError").className = "industryError"
	//true will continue validation without any errors up to this point
	return true; 
}

//When a radio button with the name Category is selected this function checks to see if the industry drop 
//down menu is displayed or not. The drop down menu should only be displayed when the Products and Services
//radio button is checked. 
function industryDisplayCheck(){
	if (document.Contact.Category[0].checked)
		document.getElementById("industryMenu").className = "show"
	else
		{
		document.getElementById("industryMenu").className = "hide"
		}
}

//When an industry is selected this method will either validate the selection if there was already a previous error
//if there was not a previous error, no validations will take place. This prevents a premature error message.
function industryErrorClear(){
	//if there was an error already. 
	if (window.location.hash == '#errorwarning')
	{
		//validate form. 
		configureValidation1();
	}
}

//******** END OF VICTOR FEINMAN'S CODE ***********

function validateForm(f,preCheck,theformfunction){
	var valid = true;
	valid = validateProducts();
	var i,e,t,v,g,b,spantxt,spanid,spanelement,hiddenspan,revalidate,errorwarning;
	errorwarning = document.getElementById('errorwarning');

//	for(j=true; j==true; j=document.getElementById(f.elements[i].id+"L"

	for(i=0; i < f.elements.length; i++){
		e = f.elements[i];
		if (theformfunction == 'configureValidation1') revalidate = function(){configureValidation1()};
		if (e.type == 'text'){e.onkeyup = revalidate};
		if(e.optional) continue;
		t = e.type;
		v = e.value;
		g = e.id + "L";
		if(document.getElementById(g)) b = document.getElementById(g);
		spanid = e.id + "m";
		spanelement = document.createElement('span');
		spanelement.id = spanid;
		spanelement.className = "errortxt"
		if (!document.getElementById(spanid)) e.parentNode.appendChild(spanelement);
		hiddenspan = document.getElementById(spanid);
		if(t == 'text'){
			if(f.elements[i].id == "") continue;
			if(isEmpty(v)){
				valid = false;
				b.className = "errorLabel";
				hiddenspan.style.display = 'block';
				hiddenspan.innerHTML = 'Required Information';
				continue;
			}

			else{
				hiddenspan.style.display = 'none';
				hiddenspan.innerHTML = '';
				b.className = "fixedLabel"
			}

			if(e.isEmailValid){
				if(!isEmailValid(v)){
					valid = false;
					b.className = "errorLabel";
					hiddenspan.style.display = 'block';
					hiddenspan.innerHTML = 'The format of the email address you entered was not recognized.  Please retype your email address in a standard format, e.g., aaaa@yahoo.com';
					continue;
				}else{
					hiddenspan.style.display = 'none';
					hiddenspan.innerHTML = '';
					b.className = "fixedLabel"
				}
			}
			//if((f.elements[i+1].id)=="") i++;
		}
	}
	;
	if(preCheck == false){valid = false};
	if(preCheck == false || valid == false){
			errorwarning.style.display = 'block';
			(window.location.hash == '#errorwarning') ? null : window.location.hash = 'errorwarning';
		}else{
			errorwarning.style.display = 'none'
		};
	
	return valid;
}



function configureValidation1(){
    f = null;
	f = document.forms[0]; //the form must be set here
	f.Name.value = f.FirstName.value + " " + f.LastName.value;
	f.FirstName.isAlphaNumeric = true;
	f.LastName.isAlphaNumeric = true;
	f.EmailAddress.isEmailValid = true;
	precheck = true;
	f.PhoneNumber.optional = true;
	return validateForm(f,precheck,'configureValidation1');
}

/*	
	function checkEmail(form) {

	var requestType = form.requestType.value;
	var emailAddress = form.EmailAddress.value;
	var address = form.StreetAddress1.value;
	var city = form.City.value;
	var name = form.Name.value;
	var company = form.CompanyName.value;
	var tcountry = form.Temp_Country.value;
	var phoneNumber = form.PhoneNumber.value;



		//alert(requestType);

		if (name == "")
		{
		alert ('You must enter your Name.')
		return false;
		}

		if (company == "")
		{
		alert ('You must enter your Company Name.')
		return false;
		}

		if (address == "")
		{
		alert ('You must enter an Address.')
		return false;
		}

		if (city == "")
		{
		alert ('You must enter a City.')
		return false;
		}

		if (tcountry == "5chsCntry")
		{
		alert ('You must select a Country for your address.')
		return false;
		}

		if (emailAddress == "")
		{
		alert ('You must enter a valid E-Mail Address.')
		return false;
		}

		if (phoneNumber == "")
		{
		alert ('You must enter a Phone Number.')
		return false;
		}

		return true;

	}
*/
	//Select Region Automatically After Choosing a Country
	//
	//NOTE: To change the region for a country, change the number
	//to reflect the number within the regionArray

	function findRegion(form,selected){

		var regionArray = new Array()
		regionArray[0]="ER"
		regionArray[1]="APR"
		regionArray[2]="NAR"
		regionArray[3]="LAR"
		regionArray[4]="ER"
		regionArray[5]="Other"

		var s=Contact.Temp_Country.options[Contact.Temp_Country.selectedIndex].value;
		var i=parseInt(s.charAt(0));

		if (s != null){
			Contact.Region.value = regionArray[i];
			var number = Contact.Temp_Country.options[Contact.Temp_Country.selectedIndex].value;
			var removechar = number.charAt(0);
			dec = number.indexOf(removechar);
			tmp = number.substring(0,dec);
			tmp += number.substring(dec+1,number.length);
			Contact.Country.value = tmp;
		}

		if ( navigator.appName == 'Netscape') {        //Netscape workaround

			if (parseInt(navigator.appVersion) < 4) {

				window.history.go(0);
			}
			else {

				if (navigator.platform == 'Win32' || navigator.platform == 'Win16') {

					window.history.go(0);
				}
		 	}

		}
	}

	function confirmReset() {
		if(confirm("Are you sure you want to reset this form, all data will be lost?"))
			window.location.reload();
		else
			return false;
	}
	