// JavaScript Document

//Generates the captcha function   

window.onload = populateCaptcha;

function populateCaptcha(){
	
	var a = Math.ceil(Math.random() * 9)+ '';
	var b = Math.ceil(Math.random() * 9)+ '';       
	var c = Math.ceil(Math.random() * 9)+ '';  
	var d = Math.ceil(Math.random() * 9)+ '';  
	var e = Math.ceil(Math.random() * 9)+ '';  
	  
	var code = a + b + c + d + e;
	var hiddenField = document.getElementById("txtCaptcha");
	var captcha = document.getElementById("txtCaptchaDiv");
	hiddenField.value = code;
	captcha.innerHTML = code;	
	
	var a = Math.ceil(Math.random() * 9)+ '';
	var b = Math.ceil(Math.random() * 9)+ '';       
	var c = Math.ceil(Math.random() * 9)+ '';  
	var d = Math.ceil(Math.random() * 9)+ '';  
	var e = Math.ceil(Math.random() * 9)+ '';  
	  
	var code = a + b + c + d + e;
	var hiddenField2 = document.getElementById("txtCaptcha2");
	var captcha2 = document.getElementById("txtCaptchaDiv2");
	hiddenField2.value = code;
	captcha2.innerHTML = code;	
}



function checkform(theform){
	
	var why = "";
	 
	if(theform.contact_greeting.value == "" || theform.contact_greeting.value == "* Address me as"){
		why += "- Name is required.\n";
	}
	
	if(theform.contact_email.value == "" || theform.contact_email.value == "* Email"){
		why += "- Email is required.\n";
	}
	 
	if(theform.txtInput.value == ""){
		why += "- Security code should not be empty.\n";
	}
	if(theform.txtInput.value != ""){
		if(ValidCaptcha(theform.txtInput.value) == false){
			why += "- Security code did not match.\n";
		}
	}
	if(why != ""){
		alert(why);
		return false;
	}else{
		theform.submit();
	}
}

function checkformcasereview(theform){
	

	
	var why = "";
	 
	if(theform.cd_first_name.value == ""){
		why += "- First Name is required.\n";
	}
	
	if(theform.cd_last_name.value == ""){
		why += "- Last Name is required.\n";
	}
	
	if(theform.cd_email.value == ""){
		why += "- Email is required.\n";
	}
	
	 
	if(theform.txtInput.value == ""){
		why += "- Security code should not be empty.\n";
	}
	if(theform.txtInput.value != ""){
		if(ValidCaptcha(theform.txtInput.value) == false){
			why += "- Security code did not match.\n";
		}
	}
	
	
	if(why != ""){
		alert(why);
		return false;
	}else{
		theform.submit();
	}
}

function checkformcasereview2(theform){
	
	var why = "";
	 
	if(theform.pi_first_name.value == ""){
		why += "- First Name is required.\n";
	}
	
	if(theform.pi_last_name.value == ""){
		why += "- Last Name is required.\n";
	}
	
	if(theform.pi_email.value == ""){
		why += "- Email is required.\n";
	}
	 
	if(theform.txtInput2.value == ""){
		why += "- Security code should not be empty.\n";
	}
	if(theform.txtInput2.value != ""){
		if(ValidCaptcha2(theform.txtInput2.value) == false){
			why += "- Security code did not match.\n";
		}
	}
	if(why != ""){
		alert(why);
		return false;
	}else{
		theform.submit();
	}
}
	
// Validate the Entered input aganist the generated security code function   
function ValidCaptcha(){
	var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
	var str2 = removeSpaces(document.getElementById('txtInput').value);
	if (str1 == str2){
		return true;	
	}else{
		return false;
	}
}

function ValidCaptcha2(){
	var str1 = removeSpaces(document.getElementById('txtCaptcha2').value);
	var str2 = removeSpaces(document.getElementById('txtInput2').value);
	if (str1 == str2){
		return true;	
	}else{
		return false;
	}
}

// Remove the spaces from the entered and generated code
function removeSpaces(string){
	return string.split(' ').join('');
}
	


