// JavaScript Document

var fieldcheckpassed = false;
var duplicatecheckpassed = false;

function CheckForm(frm) 
{  	
	var ErrorMessage = 'All fields marked with * are required. Please try again.';
	var ShowError = false;

	
	var nametrimmed = frm.name.value.replace(/^\s+|\s+$/g, '');
	var emailtrimmed = frm.email.value.replace(/^\s+|\s+$/g, '');
	var phonetrimmed = frm.phone.value.replace(/^\s+|\s+$/g, '');
	
	var validphone = false;
	var validemail = false;
	var validcontact = false;

	/* Check Name */
	if(nametrimmed.length < 1)
	{
		ShowError = true;
	}
	
	/* Check Phone */
	if (phonetrimmed.length < 3) 
	{  	
		validphone = false;    
	}
	else
	{
		validphone = ValidatePhone(phonetrimmed);
	}
	
	/* Check Email */
	if(emailtrimmed.length < 1)
	{
		validemail = false;
	}
	else
	{  	
		var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
		validemail = emailFilter.test(emailtrimmed);
	}
	
	
	/* Check for valid phone OR valid email */
	validcontact = (validphone && validemail);
	
	if(!validcontact)
	{
		ShowError = true;	
	}
	

	/* Check Property Selection */
	/*var property_choice = false;
	
	
	for (var counter = 0; counter < frm.property.length; counter++)
	{
	// If a checkbox has been selected it will return true
	// (If not it will return false)
		if (frm.property[counter].checked)
		{
			property_choice = true;
		}
	}
	
	if(!property_choice)
	{
		ShowError = true;
	}*/
	
	
	if(ShowError)
	{
		fieldcheckpassed = false;
		
		document.getElementById('confirmation').style.color = "#990000";
		document.getElementById('confirmation').style.fontWeight = "bold";
		document.getElementById('confirmation').style.borderColor = "#990000";
		document.getElementById('confirmation').style.borderStyle = "solid";
		document.getElementById('confirmation').style.borderWidth = "2px";
		document.getElementById('confirmation').style.backgroundColor = "#FFCCCC";
		document.getElementById('confirmation').style.padding = "5px";
		document.getElementById('confirmation').style.marginBottom = "10px";
		document.getElementById('confirmation').innerHTML = ErrorMessage;
		
		return false;
	}
	else
	{
		fieldcheckpassed = true;
		
		document.getElementById('confirmation').style.border = "none";
		document.getElementById('confirmation').style.backgroundColor = "transparent";
		document.getElementById('confirmation').style.padding = "0";
		document.getElementById('confirmation').style.marginBottom = "0";
		document.getElementById('confirmation').innerHTML = '';
		CheckDuplicates(emailtrimmed);
	}

}

function CheckDuplicates(email)
{
	var ErrorMessage = 'There is already a submission with that email address.  Please try again.';
	var formvals = "email=" + email;
	

	//alert(formvals);
	$.ajax({
	   		  asyn:false,
			  type: "POST",
			  url: "/duplicatecheckcall.cfm",
			  data: formvals,
			  success: function(response){
				  
				  //alert(response);
				
					if(response.indexOf("true") != -1)
					{
						document.getElementById('confirmation').style.color = "#990000";
						document.getElementById('confirmation').style.fontWeight = "bold";
						document.getElementById('confirmation').style.borderColor = "#990000";
						document.getElementById('confirmation').style.borderStyle = "solid";
						document.getElementById('confirmation').style.borderWidth = "2px";
						document.getElementById('confirmation').style.backgroundColor = "#FFCCCC";
						document.getElementById('confirmation').style.padding = "5px";
						document.getElementById('confirmation').style.marginBottom = "10px";
						document.getElementById('confirmation').innerHTML = ErrorMessage;
						
						return false;
					}
					else
					{
						duplicatecheckpassed = true;
		
						document.getElementById('confirmation').style.border = "none";
						document.getElementById('confirmation').style.backgroundColor = "transparent";
						document.getElementById('confirmation').style.padding = "0";
						document.getElementById('confirmation').style.marginBottom = "0";
						document.getElementById('confirmation').innerHTML = '';
						FormSubmit(document.form1);
					}
				},
				complete: function(){
				 // Handle the complete event
			   },
			   error:function(xhr,err){
				   //alert(xhr.responseText);

			   }

	});

}


function FormSubmit(frm)
{	

var Message = '<span style="font-weight:bold;">Your request has been successfully submitted.</span><br /><br />Thank you for your interest in Belize real estate opportunities. An Investment Advisor will be contacting you shortly.';
var nametrimmed = frm.name.value.replace(/^\s+|\s+$/g, '');
var emailtrimmed = frm.email.value.replace(/^\s+|\s+$/g, '');
var phonetrimmed = frm.phone.value.replace(/^\s+|\s+$/g, '');



var formvals = "name=" + nametrimmed + "&email=" + emailtrimmed + "&phone=" +phonetrimmed + "&property=" + get_check_value(frm.property) + "&price=" + frm.price.options[frm.price.selectedIndex].value;
//alert(formvals);
	


	$.ajax({
	   		  asyn:false,
			  type: "POST",
			  url: "/insertcall.cfm",
			  data: formvals,
			  success: function(response){
		
				document.getElementById('confirmation').style.color = "#009900";
				document.getElementById('confirmation').style.fontWeight = "normal";
				document.getElementById('confirmation').style.borderColor = "#00DD00";
				document.getElementById('confirmation').style.borderStyle = "solid";
				document.getElementById('confirmation').style.borderWidth = "2px";
				document.getElementById('confirmation').style.backgroundColor = "#EEFFEE";
				document.getElementById('confirmation').style.padding = "5px";
				document.getElementById('confirmation').style.marginBottom = "10px";
				document.getElementById('confirmation').innerHTML = Message;
				ClearForm(frm);
				
				},
				complete: function(){
				 // Handle the complete event
			   },
			   error:function(xhr,err){
				  alert(xhr.responseText);

			   }

	});


}


function get_check_value(checklist)
{
var c_value = "";

	if(checklist.length > 0)
	{
		for (var i=0; i < checklist.length; i++)
		{
			if (checklist[i].checked)
			{
				c_value = c_value + checklist[i].value + ",";
			}
		}
		
		/* Remove last comma from string */
		c_value = c_value.slice(0,-1);
	}
	
	return c_value;

}


/***************** Field Content Validation *****************/
function IsEmpty(txtbox, texttoreplace)
{
	var boxtrimmed =  txtbox.value.replace(/^\s+|\s+$/g, '');
	
	if (boxtrimmed.length < 1)
	{
		txtbox.value = texttoreplace;
	}
}



/***************** Phone Number Validation *****************/

function ValidatePhone(phonenumber)
{
	//strip out acceptable non-numeric characters
	var phonestripped = phonenumber.replace(/[\(\)\.\-\+\ ]/g, '');
	
	if (isNaN(parseInt(phonestripped)))
	{
   		return false;
	}
	else if(phonestripped.length < 10) 
	{
		return false;
	}
	else
	{
		return true;
	}
 }
 
function isValidPhone(phonenumber){
	if (phonenumber != "")
	{
		var goodChars = "+-. 1234567890()";
		
		for (i = 0; i < phonenumber.length; i++)
		{   
		    var c = phonenumber.charAt(i);
		    if (goodChars.indexOf(c) < 0) return false;
		}
		
		return true;
	}
	else
	{
		return false;
	}
}
 


function ClearForm(frm)
{	
	frm.reset();
	
	for (var counter = 0; counter < frm.property.length; counter++)
	{
		frm.property[counter].checked = false;		
	}
	
	frm.price.selectedIndex = 0;
}
