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 captchaImgId the id of the captcha image. Must contain a .src attribute.
 * @param captchaValidateUrl the url to use for validating the captcha with ajax.
 * @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, captchaImgId, captchaValidateUrl, 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 != "") {
                              /**
                               * COMMENTED OUT ON 8/17/2011 BY APTUITIV AS THIS REG EXPRESSION IS TOO RESTRICTIVE
				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;
		}
		
		// AJAX Validation
		$.post(captchaValidateUrl, {value: $('#ivr_captcha_response').val(), id: $('input[name=ap-form-captcha[id]]').val()}, function(data){
			if(data.status == 'ok'){
				thisform.submit();
				return true;
			}else{
				alert("Sorry, your Verification Code is incorrect. Please re-enter the Verification Code.");

				if (ivr_captcha_response != null) {
					ivr_captcha_response.value = '';
					ivr_captcha_response.focus();
					ivr_captcha_response.select();
				}
				return false;
			}
		}, "json");
		
		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(captchaImageId, url) {
	var myImageId = captchaImageId; 
	$.getJSON(url, function(data){
		try{
			$('input[name=ap-form-captcha[id]]', $('#'+myImageId).closest('form')).val(data.captchaId);
			$('#'+myImageId).attr('src', data.src);
		}catch(e){
			alert('There was a problem refreshing the captcha image.');
		}
	});
	return false;
}
