// JavaScript Document
/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
 
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

 
function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false
	 }

	 return true					
}




function validateEnquiryForm(thisForm)
{
		var errorString = 'Error:<br/>';
		var errorFlag = 0;
		
		var position = document.getElementById('00N90000000Lyug');
		with(thisForm){
			if (salutation.value == "")
			{
				errorString = errorString +  'Please select salutation.<br />';
				errorFlag = 1;
			}			
			
			if (first_name.value==null||first_name.value==""){
				errorString = errorString + 'First name field is compulsory.<br />';
				errorFlag = 1;
			}
			if (last_name.value==null||last_name.value==""){
				errorString = errorString + 'Last name field is compulsory.<br />';
				errorFlag = 1;
			}
			
			if (email.value==null||email.value==""){
				errorString = errorString + 'Email field is compulsory.<br />';
				errorFlag = 1;
			}else {
				if (echeck(email.value)==false){
					errorString = errorString + 'Email is invalid.<br />';
					errorFlag = 1;
				}
			}
			
			if (phone.value==null||phone.value==""){
				errorString = errorString + 'Phone field is compulsory.<br />';
				errorFlag = 1;
			}
			
			if (company.value==null||company.value==""){
				errorString = errorString + 'Company field is compulsory.<br />';
				errorFlag = 1;
			}			
			
		}
		
		if (position.value==null||position.value==""){
				errorString = errorString + 'Position field is compulsory.<br />';
				errorFlag = 1;
		}		
		
		if (errorFlag == 1) {			
			document.getElementById('error').innerHTML=errorString;
			return false;
		}
		
		if(!validateCaptcha()){
			errorString = errorString + 'The security code you entered did not match. Please try again.<br />';
			errorFlag = 1;
			document.getElementById('error').innerHTML=errorString;
			return false;
		}
		
		
		return true;
		
}



function showRecaptcha(element) {
  Recaptcha.create('6LezAAkAAAAAAPOCWVjESllkLe4PQF3yXXwiKOnN', element, {
	theme: "white", 
	callback: Recaptcha.focus_response_field});
}


function validateCaptcha()
{
	
	challengeField = Recaptcha.get_challenge();	
	responseField =  Recaptcha.get_response();
	
	//console.log(challengeField);
	//console.log(responseField);
	//return false;
	
	var html = $.ajax({
		type: "POST",
		url: "http://www.stuartwright.com.sg/courseEnquiry/ajax.recaptcha.php",
		data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
		async: false
		}).responseText;


	//console.log( html );
	if(html == "success") {
		//Add the Action to the Form
		//$("form").attr("action", "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8");
		//Indicate a Successful Captcha
		//$("#captchaStatus").html("Success!");
		// Uncomment the following line in your application
		return true;
	} else {
		//$("#captchaStatus").html("The security code you entered did not match. Please try again.");
		Recaptcha.reload();
		return false;
	}
}	

	
