var SITE_HOST_JS='www.ivr.com';
var gCaptchaElement = null;
var gCaptchaForm = null;
var gCaptchaImgContainer = null;

var CH_SEP=0;var CH_DGT=1;var CH_PLS=2;var CH_WHT=3;var CH_EXT=4;
var STATE_START = 1;var STATE_EXT = 2;var STATE_GROUP = 3;var STATE_OPEN=4;

function chType(ch) {
	switch (ch){
	case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':return CH_DGT;
	case '(': case ')': case '.': case '-': return CH_SEP;
	case '+':return CH_PLS;
	case ' ':return CH_WHT;	
	case 'x':case 'X': return CH_EXT;
	default: return -1;
	}
}

function validatePhoneNumber(number)
{
	if (!number || !number.value) {
		return false;
	}
	var num = number.value;	var digitCount = 0;	var groupDigits = 0;var state = STATE_START;		
	for(i=0;i<num.length;i++){								
		switch (state){		
		case STATE_START: {
			switch (chType(num.charAt(i))) {
			case CH_WHT: break;
			case CH_SEP: state=STATE_GROUP; break;
			case CH_DGT: digitCount++; state=STATE_OPEN; break;
			case CH_PLS: state = STATE_OPEN; break;
			default: return false;			
			}		
			break;
		}
		case STATE_OPEN: {
			switch (chType(num.charAt(i))) {
			case CH_WHT: break;
			case CH_SEP: state=STATE_GROUP;break;
			case CH_DGT: digitCount++;break;
			case CH_EXT: if (digitCount < 7) return false; else{state=STATE_EXT;break;}
			default: return false;
			}
			break;
		}
		case STATE_GROUP: {
			switch(chType(num.charAt(i))){
			case CH_WHT: break;
			case CH_DGT: digitCount++; groupDigits++;break;			
			case CH_SEP: if(groupDigits < 2 || groupDigits > 10) return false; else {state=STATE_OPEN;groupDigits=0;break;}
			case CH_EXT: if(groupDigits < 2 || groupDigits > 10) return false; else {state=STATE_EXT;groupDigits=0;break;}	
			default: return false;
			}
			break;
		}
		case STATE_EXT: {		
			switch (chType(num.charAt(i))) {
			case CH_WHT: break;
			case CH_DGT: if (groupDigits > 5) return false; else{groupDigits++;break;}
			default: return false;
			}
			break;
		}
		}
	}
	if (digitCount > 6 && digitCount < 20)return true;
	return false;
}
/**
 * The form can validate the following form input vales:
 * 
 *  fname,lname,company,country,phone,email,con_email,website,subject,ivr_captcha_response
 * 
 * @param thisform The form to validate.
 * @param captchaImgContainer the id of the container, usually a div, that holds the captcha image. Must contain a .src attribute.
 * @param isShort Validate a short/quick form that only requires Name, email and phone.
 * @return Always returns false, this script will call submit on the form if validation is successful.
 */
function validateContactForm(thisform, captchaImgContainer, isShort) {

	with (thisform) {
		
		if (emptyvalidation(fname, "Please enter your First Name.") == false) {

			fname.focus();
			return false;
		}		
		if (emptyvalidation(phone, "Please enter your Phone Number.") == false) {
			phone.focus();
			return false;
		}
		
		if (!validatePhoneNumber(phone)) {
			alert("Sorry, you have entered an invalid Phone Number.");
			phone.focus();
			return false;
		}
		
		if (emptyvalidation(email, "Please enter your Email Address.") == false) {
			email.focus();
			return false;
		}
		if (emailvalidation(email) == false) {
			alert("Sorry, you have entered an invalid Email Address.");
			email.select();
			email.focus();
			return false;
		}				
		
		if (!isShort)
		{
			if (emptyvalidation(lname, "Please enter your Last Name.") == false) {

				lname.focus();
				return false;
			}
			
			if (emptyvalidation(con_email, "Please confirm your Email Address.") == false) {
				con_email.focus();
				return false;
			}
			if (emailvalidation(con_email) == false) {
				alert("Sorry, you have entered an invalid Email Address.");
				con_email.select();
				con_email.focus();
				return false;
			}
			
			if (thisform.email.value != thisform.con_email.value) {
				alert("Sorry, your e-mail entries do not match.");
				con_email.select();
				con_email.focus();
				return false;
			}
			
			if (emptyvalidation(company, "Please enter your Company.") == false) {
				company.focus();
				return false;
			}
			/*
			  if (emptyvalidation(address,"Please fill Address.")==false) {		 
			  	address.focus(); 
			  	return false; 
			  }
			 */
			if (emptyvalidation(country, "Please enter your Country.") == false) {
				country.focus();
				return false;
			}
					
			if (thisform.website.value != "") {
				var theurl = thisform.website.value;
				var tomatch = /^(http:\/\/)?www\.[a-z]+\.[A-Za-z]{2}/;
	
				if (tomatch.test(theurl) == false) {
					alert("Sorry, you have entered an invalid URL Address.");
					website.select();
					website.focus();
					return false;
				}
			}
	
			if (typeof subject === 'undefined')
			{
				//online support request
			}
			else
			{
				//online contact request
				if (emptyvalidation(subject, "Please enter a Subject.") == false) {
	
					subject.focus();
					return false;
				}
			}
		}
		
		//Check Captcha
		if (ivr_captcha_response.value == ivr_captcha_response.defaultValue) {
			ivr_captcha_response.value = "";
		}
		if (emptyvalidation(ivr_captcha_response,"Please Enter the Verification Code.") == false) {
			ivr_captcha_response.focus();
			return false;
		}
				
		checkCaptchaResponseAjax(window.location.protocol + '//' + SITE_HOST_JS + '/process_mail.php',
				thisform,captchaImgContainer,ivr_captcha_response);
		
		return false;
	}	
	return false;
}

/**
 * Replaces the buttonContainer with a new button image, dynamically created.
 * @param buttonContainer The id of the containing element, usually a div, must have a .src attribute.
 * @return Always returns false.
 */
function captcha_refresh(buttonContainer) {
		
	//Appending Math.rand() changes the query parameters to button.php which forces the client to re-load button.php.	
	document.getElementById(buttonContainer).src = window.location.protocol + '//' + SITE_HOST_JS + '/button.php?'+Math.random();
	return false;
}

var xmlHttp=null;
function checkCaptchaResponseAjax(argurl, captchaForm,captchaImgContainer,captchaResponse) {

	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		//Fall through and let the server side handle the validation.		
		return;
	}
	var url = argurl + '?ivr_captcha_response=' + captchaResponse.value;
	
	gCaptchaForm = captchaForm;
	gCaptchaElement = captchaResponse;
	gCaptchaImgContainer = captchaImgContainer;
	xmlHttp.onreadystatechange = check_captcha;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function check_captcha() {
	
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		
		if (xmlHttp.responseText == "yes") {
			if (gCaptchaForm != null)
				gCaptchaForm.submit();
			return true;

		} else {
			alert("Sorry, your Verification Code is incorrect. Please re-enter the Verification Code.");
			captcha_refresh(gCaptchaImgContainer);

			if (gCaptchaElement != null) {
				gCaptchaElement.value = '';
				gCaptchaElement.focus();
				gCaptchaElement.select();
			}
			return false;
		}
	}
}

function GetXmlHttpObject() {
	var objXMLHttp = null;
	if (window.XMLHttpRequest) {
		objXMLHttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}
