var maxChars = 100;
var has_focus = false;
var object_to_focus = null;
var error = "";
$(document).ready(function() {
	$("textarea[name='comment']").keyup(function() {
		CheckLen(this)
	});
});

function ExtractNumbers(txt) {
	var numb = txt.match(/\d/g);
	numb = numb.join("");
	return numb;
}

function FormValidator() {
	
	document.getElementById("sendform").style.display = "none";
	document.getElementById("submit_status").style.display = "";
	
	var object_to_focus = null;
	has_focus = false;
	error = ""	
	
	var fname = $("#fname");
	var lname = $("#lname");
	var email = $("#email");	
	var comment = $("#comment");
	var phone = $("#phone");
	var ualocal = $("#ualocal");	
	
	if (jQuery.trim(fname.val()) == "") {
		error = AppendError(error, "Please enter a value for the \"First Name\"; field.");
		CheckFocus(fname);
	}

	if (jQuery.trim(lname.val()) == "") {
		error = AppendError(error, "Please enter a value for the \"Last Name\"; field.");
		CheckFocus(lname)
	}
		
	if  (!validEmail(jQuery.trim(email.val()))) {
		error = AppendError(error, "Invalid Email Address");
		CheckFocus(email)
	}	
	
	if (jQuery.trim(phone.val()) == "") {
		error = AppendError(error, "Please enter a value for the \"Phone\"; field.");
		CheckFocus(phone)
	}	
	
	if (jQuery.trim(comment .val()) == "") {
		error = AppendError(error, "Please enter a value for the \"Comment\"; field.");
		CheckFocus(comment)
	}	
	
	//if (jQuery.trim(ualocal.val()) == "" || ualocal.val() == null) {
	//	error = AppendError(error, "Please enter a value for the \"UA Local\" field.");
	//	CheckFocus(country)
	//}
		
	if(error !=  "")
	{
		try {
			jError(error, "Validation Errors", function(r) {
				AfterMessage();
				ResetButton();
			});
		}
		catch(e) {
			alert(replaceAll(error, "<br />", "\n"));
			AfterMessage();
			ResetButton();
		}
	}
	else {
		ValidateCaptcha();
	}
	return false;		
}

function replaceAll(OldString,FindString,ReplaceString) {
  	var SearchIndex = 0;
  	var NewString = ""; 
  	while (OldString.indexOf(FindString,SearchIndex) != -1)    {
    	NewString += OldString.substring(SearchIndex,OldString.indexOf(FindString,SearchIndex));
    	NewString += ReplaceString;
    	SearchIndex = (OldString.indexOf(FindString,SearchIndex) + FindString.length);         
 	}
  	NewString += OldString.substring(SearchIndex,OldString.length);
  	return NewString;
}

function AfterMessage() {
	object_to_focus.focus();
}

function AppendError(error, Text) {
	if(error != "")
		error += "<br />"

	return error += Text;
}

function CheckFocus(obj_set) {
	if(has_focus == false)
	{
		obj_set.focus();
		object_to_focus = obj_set;
		has_focus = true;
	}
}

function validEmail(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 SendForm() {
	document.getElementById("CheckScript").value = "1";
	document.getElementById("frmContact").submit();
}

function CheckLen(val) {
	if (val.value.length > maxChars) {
		return false;
	}
	else if(val.value.length == 0)
	{
		$("#myCounter").html(maxChars);
	}
	else {
		$("#myCounter").html(maxChars - val.value.length);
		return true;
	}
}

function ResetCaptcha() {
	//document.getElementById("captchaf").contentDocument.location.reload(true);
	
	try {
		document.getElementById("captchaf").contentDocument.location.reload(true);
	}
	catch(e) {
		try {
		document.frames["captchaf"].location.href="captcha.asp"; 
		}
		catch(e) {			
			//alert("Captcha reload error");
			//return;
		}
	}
	
	document.getElementById("wsp_code").value = "";
	document.getElementById("wsp_code").focus();
	ResetButton();
}

function ResetButton() {
	document.getElementById("sendform").style.display = "";
	document.getElementById("submit_status").style.display = "none";
}


function ValidateCaptcha() {
	var key = window.frames["captchaf"].testAlert();
	var code = document.getElementById("wsp_code").value;
	var PostStr = "WSP_ImgKey=" + URL.encode(jQuery.trim(key)) + "&WSP_ImgCode=" + URL.encode(jQuery.trim(code));
	try {
		doAJAXCall('includes/ajax_captcha.asp', 'GET', PostStr, ValidateComplete);
	}
	catch(e) {
		alert(e);
		ResetCaptcha();
	}
}

function ValidateComplete(oXML) {
	var result = oXML.responseText;
	if (result == -2) {
		jAlert("Invalid Captcha Code", "Captcha Error", function(r) {
			ResetCaptcha();
		});

	}
	else if(result == -1) {
		jAlert("Invalid Captcha Key/Code", "Captcha Error", function(r) {
			ResetCaptcha();
		});
	}
	else if(result.toUpperCase() == "OK")
	{
		ResetCaptcha();
		SendForm();
	}
	else {
		jAlert(result, "Captcha Error", function(r) {
			ResetCaptcha();
		});
	}
}
