// JavaScript Document
// @ Author, CK 14.01.09
// @ Description: form handling routines

var strENMessage ="Please fill out all required fields. ";
var strENMailMessage ="Please verify your email address.";
var strENSendingMessage ="Sending messsage now. Please wait for confirmation.";
var strENSuccessMessage ="Your message has been sent!";
var strENNoSuccessMessage ="There was a problem sending your message. Please try again later.";


var strFormScript ='';


function processForm(objForm, strLang)
{
	var strErrors = verifyForm(objForm, strLang);
	if (!strErrors) 
	{
		sendForm(objForm, strLang);
	}
	else
	{
		notifyForm(strErrors);
	};

return false;
}

function verifyForm(objForm, strLang)
{
	//strip spaces
	var reStrip = /^\s+|\s+$/g;
	// verify email address syntax
	var reIsEmail = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i;
	var strErrors = '';

	if ( !objForm.c_name_f.value.replace(reStrip,"").length ||
			!objForm.c_name_l.value.replace(reStrip,"").length ||
			!objForm.c_email.value.replace(reStrip,"").length ||
			!objForm.c_message.value.replace(reStrip,"").length)
	{
		strErrors = strENMessage;
	}
			
	if ( !reIsEmail.test(objForm.c_email.value))
	{
			strErrors += strENMailMessage;
	}

return strErrors;	
}

function sendForm(objForm, strLang)
{
	
	var strPostData = "c_name_f=" + escape( objForm.c_name_f.value);
	strPostData += "&c_name_l=" + escape( objForm.c_name_l.value);
	strPostData += "&c_email=" + escape( objForm.c_email.value);	
	strPostData += "&c_tel=" + escape( objForm.c_tel.value);	
	strPostData += "&c_message=" + escape( objForm.c_message.value);	
	
	//alert(strPostData)
	execFormAjaxCall(strPostData, strLang);	
	return true;
}

function notifyForm(strUserMsg)
{
	alert(strUserMsg);
}

function execFormAjaxCall(strPostData, strLang) {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
	
	// initiating send msg
	//notifyForm(strENSendingMessage);
	
    xmlHttp.onreadystatechange=function() {
      if(xmlHttp.readyState==4) {
			var re = /success/ig;
			//alert(xmlHttp.responseText);
			if (re.exec(xmlHttp.responseText)) {
				notifyForm(strENSuccessMessage);
				if (document.getElementById('contact'))
					document.getElementById('contact').reset();
			}
			else {
				notifyForm(strENNoSuccessMessage);				
			}
      }
    }
	
    xmlHttp.open("POST",strFormScript,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset:UTF-8");
    xmlHttp.setRequestHeader("Content-length", strPostData.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(strPostData);	
 }


