﻿
/// <summary>
 /// Mail Sending function
/// <created by:Raji>
/// <created on: 01-03-2010>
/// </summary>

var xmlHttp= null;

function getXMLHTTPRequest()
{
    var xRequest=null;
    if (window.XMLHttpRequest)
    {
        xRequest = new XMLHttpRequest();
    }
    else if (typeof ActiveXObject != "undefined")
    {
        xRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xRequest;
} 

function $get(obj) { return document.getElementById(obj); }
//function AjaxCall(URL,values,OnSuccessFunctionName)
//{    
//    xmlHttp = getXMLHTTPRequest();
//    if(xmlHttp != null)
//    {
//        xmlHttp.onreadystatechange = OnSuccessFunctionName;
//        xmlHttp.open("POST",URL,true);
//        xmlHttp.setRequestHeader("Content-type", "application/json");
//        xmlHttp.send(values); 
//    }
//}

  function SendFeedBack()
  {
    
    var qvalid;
    qvalid = ValidateQPage();
    if(qvalid==true)
    { 
        var qname=document.getElementById("qname").value;
        var qmail=document.getElementById("qmail").value;  
          var qphone=document.getElementById("qphone").value; 
            var qcompany=document.getElementById("qcompany").value;  
         var qmessage=document.getElementById("qmessage").value; 
         xmlHttp=getXMLHTTPRequest();
    
         if(xmlHttp!=null)
            {  
                xmlHttp.onreadystatechange = ONS_SendFeedBack;    
                var URL="MainService.asmx/SendFeedBack?tm="+new Date().getTime();
                var values= '{"name":"'+qname+'","email":"'+qmail+'","phone":"'+qphone+'","company":"'+qcompany+'","commnts":"'+qmessage+'"}';
                xmlHttp.open("POST",URL,true);
                xmlHttp.setRequestHeader ("Content-type", "application/json");
                xmlHttp.send(values);  
                return false;   
            }
    }          
    else{
             return false;
        }   
   return false;
  }
  
  function ONS_SendFeedBack()
    {
    if(xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
            {
                var data = eval('('+xmlHttp.responseText+')');
                if(data.d != null)
                {
                    alert(data.d);
                    document.getElementById("qname").value="";
                    document.getElementById("qmail").value="";                
                    document.getElementById("qmessage").value="";  
                    document.getElementById("qphone").value="";  
                    document.getElementById("qcompany").value="";                   
                    return false;  
                }
            }
     } 
}
  
 function SendMessage()
  {
    
    var qvalid;
    qvalid = ValidateQPage();
    if(qvalid==true)
    { 
        var qname=document.getElementById("qname").value;
        var qmail=document.getElementById("qmail").value;  
          var qphone=document.getElementById("qphone").value; 
            var qcompany=document.getElementById("qcompany").value;  
         var qmessage=document.getElementById("qmessage").value; 
         xmlHttp=getXMLHTTPRequest();
    
         if(xmlHttp!=null)
            {  
                xmlHttp.onreadystatechange = ONS_SendFeedBack;    
                var URL="MainService.asmx/SendFeedBack?tm="+new Date().getTime();
                var values= '{"name":"'+qname+'","email":"'+qmail+'","phone":"'+qphone+'","company":"'+qcompany+'","commnts":"'+qmessage+'"}';
                xmlHttp.open("POST",URL,true);
                xmlHttp.setRequestHeader ("Content-type", "application/json");
                xmlHttp.send(values);  
                return false;   
            }
    }          
    else{
             return false;
        }   
   return false;
  }

function ValidateQPage()
{
        if (document.getElementById("qname").value == "" )
                {
                    alert("Please fill in the ' Name'." );
		            document.getElementById("qname").focus();
                    return false;		        
                } 
        if (document.getElementById("qmail").value.length== "" )
                 {
		             alert("Please Enter your Email ID")
		             document.getElementById("qmail").focus();
		             return false;	
	              }
	   if (echeck(document.getElementById("qmail").value)==false)
	              {
		                document.getElementById("qmail").value=""
		                document.getElementById("qmail").focus();
		               return false;		 
	              }	
	               if (document.getElementById("qphone").value.length== "") 
                 {
                     alert("Please enter Phone number.");
                    document.getElementById("qphone").focus();
                      valid = false;
		             return valid;
                  } 
      else if (chkNumeric(document.getElementById("qphone").value) == false) 
                  {
                      alert("Please check - non numeric value!");
                      document.getElementById("qphone").focus();
                      document.getElementById("qphone").value=""
                      valid = false;
		             return valid;
                  } 
	   if (document.getElementById("qmessage").value == "" )
                {
                    alert("Please fill in the ' Message'." );
		            document.getElementById("qmessage").focus();
                    return false;		        
                } 
	               return true;		
}

 function chkNumeric(strString)  //  check for valid numeric strings	
 {
               var strValidChars = "0123456789.-";
               var strChar;
               var blnResult = true;

               if (strString.length == 0) return false;

               //  test strString consists of valid characters listed above
               for (i = 0; i < strString.length && blnResult == true; i++)
                  {
                  strChar = strString.charAt(i);
                  if (strValidChars.indexOf(strChar) == -1)
                     {
                     blnResult = false;
                     }
                  }
               return blnResult;
 }     
      
 function echeck(str) 
 {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}


