﻿/********************************/
//<Created by:Raji>
//<Created On:23/12/2010>
//<Description:Send Mail Function>
/********************************/
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); 
    }
}

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 ValidateConsultation() {

  
   if(CheckBlank("nameText","Enter your Name")==false
      ||CheckBlank("phoneText","Enter phone number")==false 
      ||CheckBlank("emailText","Enter your Emailid")==false 
      ||fnValidEmail("emailText","Enter your Emailid")==false     
      )
               return false;
      else
      {     
            
            xmlHttp=getXMLHTTPRequest();
    
             if(xmlHttp!=null) {
           
                    var fname=$get('nameText').value;                  
                    var ph=$get('phoneText').value;
                    var mail=$get('emailText').value;
                    xmlHttp.onreadystatechange = ONS_SendConsultation;
                    var URL = "MailWebService.asmx/SendConsultation?tm=" + new Date().getTime();
                    var values= '{"fname":"'+fname+'","ph":"'+ph+'","mail":"'+mail+'"}';
                    
                    xmlHttp.open("POST",URL,true);
                    xmlHttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                     xmlHttp.send(values);                         
                    return false;   
                }          
            
      }
    
}
function ONS_SendConsultation()
{
    if(xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
            {
                var data = eval('('+xmlHttp.responseText+')');                
                if(data.d != null)
                { 
                    alert(data.d);
                    $get('nameText').value="";
                    $get('phoneText').value="";
                    $get('emailText').value="";
                }
            }
      } 
}

//===========================================================================================

function validateContactmail() {


    if (CheckBlank("firstnametext", "Enter your First Name") == false
      || CheckBlank("lastnametext", "Enter your last Name") == false
      || CheckBlank("phonetext", "Enter phone number") == false
      ||CheckBlank("emailtext", "Enter your Emailid") == false
      || fnValidEmail("emailtext", "Enter your Emailid") == false
      )
        return false;
    else {

        xmlHttp = getXMLHTTPRequest();

        if (xmlHttp != null) {

            var fname = $get('firstnametext').value;
            var lname = $get('lastnametext').value;
            
            var ph = $get('phonetext').value;
            var mail = $get('emailtext').value;
            var situation = $get('situationtext').value; 
            
            xmlHttp.onreadystatechange = ONS_SendContactMail;
            var URL = "MailWebService.asmx/SendContactMail?tm=" + new Date().getTime();
            var values = '{"fname":"' + fname + '","lname":"' + lname + '","ph":"' + ph + '","mail":"' + mail + '","situation":"' + situation + '"}';

            xmlHttp.open("POST", URL, true);
            xmlHttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            xmlHttp.send(values);
            return false;
        }

    }

}
function ONS_SendContactMail() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            var data = eval('(' + xmlHttp.responseText + ')');
            if (data.d != null) {
                alert(data.d);
                $get('firstnametext').value = "";
                $get('lastnametext').value = "";
                $get('phonetext').value = "";
                $get('emailtext').value = "";
                $get('situationtext').value = ""; 
            }
        }
    }
}

//=================================================================================================================

/* ---- script to remove white space at index 0 and check blank value on control and customized message ------  */
    function CheckBlank(control,massege)
    {
      var controlname=$get(control);
      var strControlValue = controlname.value;
      var Strlen = strControlValue.length;
      for (var i = 0; i < Strlen; i++)
      {
        if (strControlValue.indexOf(" ") == 0)
        {
            strControlValue = strControlValue.substring(1,strControlValue.length);
            controlname.value = strControlValue;
        }
      }
      if(controlname.value=="")
      {
        alert(massege);
        controlname.focus();
        return false;
     }
   }
/*--------------------- script for valid email (Regular expression) -----------------  */ 
  
function isEmail (ctrl)
 {
   var s=$get(ctrl).value;
   var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;  
   if(String(s).search (isEmail_re) != -1)
    return true
  else
    {
        alert("Invalid  Email ID.");
        return false;
    }
  // return String(s).search (isEmail_re) != -1; 
}

 /*--------------------- script to check valid email -----------------  */  
 function fnValidEmail(objStr)
        {
            strEmail=$get(objStr).value;
	        trim(strEmail);
	        intReturn=fnCheckEmaiID(trim(strEmail));
	        if (intReturn==1)
	        {
	            alert(" Invalid  Email ID ");
	            $get(objStr).focus();
		        return false;
		    }
	        //return true;
       }

function fnCheckEmaiID(strString)
{
	var intLength=strString.length;
	var intReturn=1;
	var index;
	if (strString.charAt(0)=="@" || strString.charAt(0)==".")
	{
		return intReturn;
	}
	for (index=0;index<intLength; index++)
	{
			if (strString.charAt(index)=="@")
			{	
					intReturn=0;
					index=index+1;
					break;
			}
	}
	var counter=0;
	var intCount=0;
	var intDotCount=0;
	var intLastDot=0;
	if (intReturn==0)
	{
		for (;index<intLength ;index++ )
		{
			counter++;
			if (strString.charAt(index)==".")
			{	
					intReturn=0;
					index=index+1;
					for (;index<intLength ;index++ )
					{
						intCount=intCount+1;
						//if (strString.charAt(index)=="@" || strString.charAt(index)==".")
						if (strString.charAt(index)=="@")
							{intReturn=1;break;}

						if (strString.charAt(index)==".")
							{	
								intDotCount=intDotCount + 1;

								if (intLastDot==index-1)
								{intReturn=1;break;}
								if (intCount==1)
								{intReturn=1;break;}
								//<<if dots are more than 2
								//alert(intDotCount);
								if (intDotCount >= 2)
								{intReturn=1;break;}
								
								intLastDot=index;
							}
					}
				
					if (intCount==0)
					{intReturn=1;}
					//<<dot should not be last char
					if (strString.charAt(intLength - 1)==".")
					{intReturn=1;}

					break;
			}		
			else
				intReturn=1;
		}
		if (counter==0)
		{
			intReturn=1;
		}
	}
	if (counter==1)
	{
		intReturn=1;
	}
	return intReturn;	
}

function trim(str){return str.replace(/^\s*|\s*$/g,"");}
   
 /*--------------------- script to input numeric value  -----------------  */
  function isNumberKey(evt)
    { 
        var charCode = (evt.which) ? evt.which : event.keyCode
        
       // if ((charCode >47 && charCode< 58) || (charCode > 36 && charCode < 41) || (charCode>95 && charCode<106) || charCode == 46 || charCode == 8 || charCode == 9)
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        return true; 
          
//        if ((charCode >47 && charCode< 58) || (charCode > 36 && charCode < 41) || (charCode>95 && charCode<106) || charCode == 46 || charCode == 8 || charCode == 9)
//        {
//            return true;
//        }
//        else
//        {
//            return false;
//        }
           
    }
    
    
    
     /*------------------ script to Validate phono 10 digits ------------------------  */ 
    /* function validatePhone(CtlPh1,Message)
     {
            var objPhone1P= $get(CtlPh1);

	        if(objPhone1P.value!="")
            {
                if(!isNaN(objPhone1P))
                {
		                objPhone1P.focus();
		                alert(Message);
		                return false;
                }                
            }
        }*/

    function validatePhone(CtlPh1, Message)
     {

         var objPhone1P = $get(CtlPh1);
         if (objPhone1P.value != "") {
             		var goodChars = "+-  1234567890()"
             		for (i = 0; i < objPhone1P.value.length; i++) {
             		    var c = objPhone1P.value.charAt(i);
             	            if (goodChars.indexOf(c) < 0) {
             	                objPhone1P.focus();
             	                alert(Message);
             	                return false;
             	            }
             	}
            }
        
    }

//     function isValidPhone(phonenumber){
//	if (phonenumber != "") {
//		var goodChars = "+- 1234567890()"
//		for (i = 0; i < phonenumber.length; i++){   
//		    var c = phonenumber.charAt(i);
//		    if (goodChars.indexOf(c) < 0) return false;
//		}
//		return true;
//	} else {
//		return false;
//	}
//}
