/*************************************************************************************************
**************************************************************************************************
**************************************************************************************************
*************************************        AJAX POST        ************************************
**************************************************************************************************
**************************************************************************************************
**************************************************************************************************/
//
// JavaScript Document  http://jgwebmanagement.com/wp-content/themes/jgweb/mail.php
if(window.location.host=='localhost')
{
	var root_url='http://'+window.location.host+'/customswag/';
}
else
{
	var root_url='http://'+window.location.host+'/';
}
var http_request = false;
   function makePOSTRequest(url, parameters,from) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
	  switch(from)
	  {
		  case 'send_mail_ajax':
		  	http_request.onreadystatechange = postSendMailAjax;
			break;
	  }
      
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function postSendMailAjax() {
	  // alert(http_request.readyState+'  '+http_request.status+'  '+http_request.responseText)
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
			var result = http_request.responseText;
			document.getElementById('process_msg').innerHTML=result;
           	document.getElementById('name').value='';
			document.getElementById('email').value='';
			/*document.getElementById('telephone').value='';
			document.getElementById('time').value='';
			document.getElementById('service').value='';
			document.getElementById('enquiry').value='';*/
			setTimeout("document.getElementById('process_msg').innerHTML=''",10000);
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
/*FEATURE YOUR BUSINESS MAIL SENDING CODE*/
function send_mail_ajax()
{
	//document.getElementById('ajax_loader_id').style.display='block';
	
	var name=document.getElementById('name').value;
	var email=document.getElementById('email').value;
	/*var telephone=document.getElementById('telephone').value;
	var time=document.getElementById('time').value;
	var service=document.getElementById('service').value;
	var enquiry=document.getElementById('enquiry').value;*/
	
	var err='';
	if(name=='')
	{
		err +='Please enter your fullname\n';
	}
	if(email=='')
	{
		err +='Please enter your email id\n';
	}
	else if(validateEmailv2(email)==false)
	{
		err +='Please enter your valid email id\n';
	}
	/*if(telephone == '')
	{
		err +='Please enter your telephone no\n';
	}

	if(enquiry=='')
	{
		err +='Please enter your enquiry\n';
	}*/
	
	if(err=='')
   {
	   document.getElementById('process_msg').innerHTML='Please wait.';
			 var poststr = "name=" + encodeURI(name)+"&email=" + encodeURI(email)+"&from=send_mail_ajax";
			// alert(root_url+'mail.php?'+poststr);
			 makePOSTRequest(root_url+'mail.php', poststr,'send_mail_ajax');
   }
   else
   {
		alert(err);
   }
}


/*VALIDATE EMAIL*/
function validateEmailv2(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(email.length <= 0)
	{
	  return true;
	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}


