var ajaxv = false;

function ajaxvalidate(t) {
$.ajax( 
{
async: false,
cache: false,
type: 'POST',
dataType: 'text',
url: 'images/captcha/validate.php',
data: {v: t},
success: function(data) {
if(data.length == 1) {
v = parseInt(data);
ajaxv = v == 1 ? true : false;
return ajaxv;
}
else
return false;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
return false;
}
}

);
}

String.prototype.trim = function() { return this.replace( /(^\s*)|(\s*$)/g, '' ) ; }

// Proper e-mail check script
function emailCheck( emailFormField, showerror ) {

	var txt=emailFormField.value;  
	var error = "";
	
	var emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
	var phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/
                  
	if (!(emailRe.test(txt))) { 
		   error = "Please enter a valid email address.\n";
	}
	
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (txt.match(illegalChars)) {
	   error += "The email address contains illegal characters.\n";
	}
	
	if( error.length > 0 ) {
		if(showerror) {
		    emailFormField.focus();
			alert(error);	
		}
		return false;
	}
	return true;  
}



function onButtonClick() { 
	var message = "";
	   
	// validation
	if (document.form.validate.value.length != 6) { 
	message = message + "Please enter proper security code\n";
	}
	else {
		ajaxvalidate(document.form.validate.value);
		if(!ajaxv) message+='Security code not accepted, please try again\n';
	}
	
	if (document.form.email.value.length == "") { 
	message = message + "Email is required\n";
	}

   if(message!=""){
    alert("The following form field(s) were incomplete or incorrect:\n\n" + message + "\n\nPlease complete or correct the form and submit again.");
  }    
  else {
    alert("Thank you, your request is being submitted.");
    document.form.action = "mail/send.php?send=form";
    document.form.method = "post";
    document.form.submit();
  } 
}

