﻿ document.write('<div id="ResultDiv" style="position:absolute; z-index:-5000;margin-left:auto;margin-right:auto;width:320px; height:80px;top:-20;"></div>');
 document.onclick = function()
 {$('#ResultDiv').empty();
 $('#ResultDiv').css({zIndex:'-5000'});}
 var siteURL = "http://www.myindiafinance.com/insurance/ApplyForInsurance.asmx"; 
   var WebsiteURL ="http://www.myindiafinance.com";
 //TO MAKE ENTER AS DEFAULT BUTTON
 function doClick_HlthIns(e)
  {
  
       try{
          var key;
          if(window.event)
               key = window.event.keyCode;     //IE
          else
               key = e.which;     //firefox
         if (key == 13)
         { 
            SaveApplyNowDetails();
                 return false;
         }
       }
       catch(err){}
  } 
  
   //Javascript function validate the Date
      function IsValidDate(Day,Mn,Yr)
      {
          try{
          var DateVal = Mn + "/" + Day + "/" + Yr;
          var dt = new Date(DateVal);

          if(dt.getDate()!=Day)
              return(false);
          else if(dt.getMonth()!=Mn-1) //this is for the purpose JavaScript starts the month from 0
              return(false);
          else if(dt.getFullYear()!=Yr)
              return(false);
          return(true);
          }
          catch(err){return(false);}
      }  

 var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); 
 //to verify date of birth and it should be more than or equal to 18 
  function VerifyDOB() 
  {
     try{
      var vlDOB = document.getElementById('tbDOB');
      if(vlDOB.value!="")
      {
          
          var sDOB=vlDOB.value;
          
          arrDOB=sDOB.split("/");
          sDOB=arrDOB[1]+"/"+arrDOB[0]+"/"+arrDOB[2];
          var DOBms=Date.parse(sDOB); 
          var DateFormat = new Date();
          //var dCD=new Date(montharray[DateFormat.getMonth]+ DateFormat.getDay(), DateFormat.getFullYear());
          var dCD=new Date();
         
          var CDms=Date.parse(dCD);
          var dAGE=new Date();
          var vDays;
          var AGEms=CDms-DOBms;
          
          DAYms=1000*60*60*24;
          
          dAGE.setTime(AGEms);
          vDays =  Math.ceil(dAGE.getTime()/DAYms);
          iYear=dAGE.getFullYear()-1970;
    
          var AgeInDays=0;
          var blnValidAge=true; 
          if(iYear == 0 || iYear > 100)
          {
              /*if (eval(vDays)>= 91)
                  blnValidAge=true;
              else*/
                  blnValidAge=false;
          }
          else if(iYear < 18)
          {
              blnValidAge=false;
          }
          else if(!IsValidDate(arrDOB[0], arrDOB[1], arrDOB[2]))
          {
              blnValidAge=false;
          }
          if(blnValidAge==false)
          {
            return false;
            
          }
          else
          {
              return true;
             
          }
          //return true;
        }
     }
    catch(err){}
  }          

//To save applynow details
function SaveApplyNowDetails()
{
    var status = true;
    $("#lblResult").css({visibility:"hidden",display:"none"});
     var Name = $("#txtName").val();
     var Email = $("#txtEmail").val();
     var Mobile = $("#txtMobile").val();
     var City = $("#ddlCity option[@selected]").text();
     var DOB = $("#tbDOB").val();
     var PolicyType = $("#ddPolicyDetail option[@selected]").text(); 
      $("#vldName").css({visibility:"hidden",display:"none"}); 
      $("#vldEmail").css({visibility:"hidden",display:"none"}); 
      $("#vldMobile").css({visibility:"hidden",display:"none"}); 
      $("#vldCity").css({visibility:"hidden",display:"none"}); 
      $("#vldDOB").css({visibility:"hidden",display:"none"}); 
      $("#vldPolicy").css({visibility:"hidden",display:"none"}); 
      $("#vldTerms").css({visibility:"hidden",display:"none"});     
        
     if($('#chkTerms').is(':checked') == false)
     {
         $("#vldTerms").css({visibility:"visible",display:"block"}); 
         $("#chkTerms").focus();
         status = false;
     }
     if($("#ddPolicyDetail option[@selected]").val().length <= 0)
     {
         $("#vldPolicy").css({visibility:"visible",display:"block"}); 
         $("#ddPolicyDetail").focus();
         status = false;
     }
     if(DOB.length > 0)
     {
         if(ValidateDOB(DOB) == false)
         {
             $("#vldDOB").css({visibility:"visible",display:"block"}); 
             $("#vldDOBRange").css({visibility:"hidden",display:"none"}); 
             $("#vldDOBReq").css({visibility:"hidden",display:"none"});
             $("#vldDOBFormat").css({visibility:"visible",display:"block"});
             $("#tbDOB").focus();
             status = false;
         }
     }
     if(DOB.length > 0)
     {
         if( VerifyDOB() == false)
         {
             $("#vldDOB").css({visibility:"visible",display:"block"}); 
             $("#vldDOBRange").css({visibility:"visible",display:"block"}); 
             $("#vldDOBReq").css({visibility:"hidden",display:"none"});
             $("#vldDOBFormat").css({visibility:"hidden",display:"none"});
             $("#tbDOB").focus();
             status = false;
         }
     } 
     if(DOB.length <= 0)
     {
         $("#vldDOB").css({visibility:"visible",display:"block"});
         $("#vldDOBReq").css({visibility:"visible",display:"block"}); 
         $("#vldDOBRange").css({visibility:"hidden",display:"none"});
         $("#vldDOBFormat").css({visibility:"hidden",display:"none"}); 
         $("#tbDOB").focus();
         status = false;
     } 
     if($("#ddlCity").val().length <= 0)
     {
         $("#vldCity").css({visibility:"visible",display:"block"}); 
         $("#ddlCity").focus();
         status = false;
     }
      if(Mobile.length <= 0)
     {
        $("#vldMobile").css({visibility:"visible",display:"block"});
        $("#txtMobile").focus();
        status = false;
     }
     if(Mobile.length > 0)
     {
        if(ValidateMobile(Mobile) == false)
        {
            $("#vldMobile").css({visibility:"visible",display:"block"}); 
            $("#txtMobile").focus();
             status = false;
        }
     }
     if(Email.length <= 0)
     {
        $("#vldEmail").css({visibility:"visible",display:"block"}); 
        $("#txtEmail").focus();
         status = false;
     }
     if(Email.length > 0)
     {
        if(validateEmail(Email) == false)
        { 
            $('#vldEmail').css({visibility:"visible",display:"block"});         
            $("#vldEmail").focus();
             status = false;
        }
      }     
     if(Name.length <= 0)
     {
        $("#vldName").css({visibility:"visible",display:"block"});  
        $("#txtName").focus();
        status = false;
     }
     if(!status)
      {
             return false;
      }
     else{
             var itemID = {"Name" : Name,"Email" : Email,"Mobile":Mobile,"City":City,"DOB":DOB,"PolicyType":PolicyType};  
             var jsonStr = JSON.stringify(itemID); 
             // Call our Web Service, passing in our JSON params. The error function is called  
             // whenever an exception is thrown by the web service (among other times)  
                  $.ajax({           
                   type: "POST",  
                   url: siteURL+"/SaveDetails",  
                   data: jsonStr,  
                   contentType: "application/json; charset=utf-8",  
                   dataType: "json",                     
                   success: function(msg){
                   var JObject = eval('(' + msg + ')');         
                   var strJObject = JObject.split(',');
                        if(strJObject[0].toLowerCase() == "ok")
                        {
                           //Redirect to thanks Page;
                              _pageURL=WebsiteURL+"/thankyou.aspx?Type=HealthInsurance&User="+Name;                 
                            document.location.href=_pageURL;
                           return true;
                        }
                        if(strJObject[0].toLowerCase() == "notunique")
                        {
                          var ResultString ="Dear <b>"+Name+"</b>,<br/>You have already applied for "+PolicyType+".";  
                          ShowMessageBox(ResultString)              ;
                           return true;
                        }      
                   },
                   error: function(xhr, msg){
                   return false; 
                 }  
             });           
             return false; 
      } 
}


// To show modal pop-up window
function ShowPinPopUp(Pin)
{
   var divString= "<div style='width: 500px; background-color: #FFFFFF; vertical-align: middle;border: solid 1px green;'>"+
                       "<table width='100%' cellpadding='0' cellspacing='0' class='bodytext'>"+
                       "<tr><td align='right' colspan='2'><a onclick='HideDialogBox();' href='#' style='font-family: Verdana; font-size: 11px; color: #c2c2c3;cursor:pointer;'>Close [X]</a></td></tr>"+
                       "<tr><td colspan='2'>&nbsp;</td></tr>"+
                       "<tr><td colspan='2'>&nbsp;</td></tr>"+
                       "<tr><td valign='top' style='padding-left: 10px;'><img id='Img7' src='http://www.myindiafinance.com/insurance/images/AutoLanding/Alert.gif' alt='' runat='server' /></td>"+
                       "<td align='justify' style='padding-left: 20px; padding-right: 10px;'>   You did not enter the mobile verification code or the mobile verification code entered"+
                       "is invalid. The mobile verification code is important in order to make sure that nobody else is using your mobile number.<br /><br />The mobile verification is for your safety and is mandatory. If you do not verify your mobile number then your application process may take longer or may not be accepted.<br />"+
                       "<br />This process is for your security. Please click <a  class='aLinks1' Style='text-decoration: underline' onclick=SendMobileVerificationCode('lblPopUpMsg','txtMobile'); href='javascript:void(0);'>[Get verification code]</a>,you will receive a 4 digit code on your phone.</td></tr>"+
                       "<tr><td colspan='2'>&nbsp;</td></tr>"+
                       "<tr><td colspan='2'>&nbsp;</td></tr>"+
                       "<tr><td></td><td align='left' style='padding-left: 20px;'>Enter that code here &nbsp;&nbsp;&nbsp;<input id='txtVerCode'  maxlength='4' class='text_box' value='"+Pin+"' /></td></tr>"+
                       "<tr><td>&nbsp;</td><td style='padding-left: 177px;height:15px;'><span id='lblPopUpMsg' style='color:Red; visibility:hidden;display:none' /></td></tr>"+
                       "<tr><td></td><td align='Left' style='padding-left: 156px;'><a  onclick='VerifyMobileNumber();' href='javascript:void(0);' ><img src='http://www.myindiafinance.com/insurance/images/AutoLanding/submit.png' alt='Apply Now' style='border:0px;' /></a>"+
                       "</td></tr></table>"+
                        "</div>";    
    $("#dialog").empty();                  
    $("#dialog").append(divString);
    $("#dialog").css({height:"330px"});
    jQuery('#dialog').center(true);  
    $("#dialog").jqmShow();
   
                           
}


//To send verification code to user
function SendMobileVerificationCode(lblResult,txtMobile)
{
    var lblResultID = "#"+lblResult;
    var txtMobileID = "#"+txtMobile;
    $(lblResultID).css({visibility:"hidden",display:"none"});
     var Mobile = $(txtMobileID).val();
     var itemID = {"Mobile":Mobile};  
     var jsonStr = JSON.stringify(itemID); 
     // Call our Web Service, passing in our JSON params. The error function is called  
     // whenever an exception is thrown by the web service (among other times)  
          $.ajax({           
           type: "POST",  
           url: siteURL+"/SendVerificationCode",  
           data: jsonStr,  
           contentType: "application/json; charset=utf-8",  
           dataType: "json",                     
           success: function(msg){
               var JObject = eval('(' + msg + ')');
               $(lblResultID).css({visibility:"visible",display:"block"});
               $(lblResultID).text(JObject);           
                return true;           
           },
           error: function(xhr, msg){
            return false; 
         }  
     });           
     return false;  
}


//To verify mobile number
function VerifyMobileNumber()
{
     var Mobile = $("#txtMobile").val();
     var VerificationCode = $("#txtVerCode").val();
     $("#lblPopUpMsg").css({visibility:"hidden",display:"none"});
     if(VerificationCode.length<=0)
     {
        $("#lblPopUpMsg").css({visibility:"visible",display:"block"});
        $("#lblPopUpMsg").text("Please enter verification code"); 
        return;
     }
     var itemID = {"VerificationCode":VerificationCode,"Mobile":Mobile};  
     var jsonStr = JSON.stringify(itemID); 
     // Call our Web Service, passing in our JSON params. The error function is called  
     // whenever an exception is thrown by the web service (among other times)  
          $.ajax({           
           type: "POST",  
           url: siteURL+"/VerifyMobileNumber",  
           data: jsonStr,  
           contentType: "application/json; charset=utf-8",  
           dataType: "json",                     
           success: function(msg){
               var JObject = eval('(' + msg + ')');
               if(JObject.toLowerCase() == "ok")
                {
                   //Redirect to thanks Page;
                  //window.location="thankyou.aspx";
                  //document.location.href = "thankyou.aspx";
                     _pageURL=WebsiteURL+"/thankyou.aspx?Type=HealthInsurance&User="+$("#txtName").val();                 
                            document.location.href=_pageURL;
//                  $("#FormDiv").css({visibility:"hidden",display:"none"});
//                  $("#ThankDiv").css({visibility:"visible",display:"block"});
//                  $("#lblName").text($("#txtName").val());
//                  writeThankScript();
//                  $("#dialog").jqmHide();
                  return true;
                }
               if(JObject.toLowerCase() == "notok")
                {
                   $("#lblPopUpMsg").css({visibility:"visible",display:"block"});
                   $("#lblPopUpMsg").text("Please enter valid verification code");
                   
                   return true;
                }                      
           },
           error: function(xhr, msg){
            return false; 
         }  
     });           
     return false;  
}

//To set SrcID and CID value to cookie
function SetCookie()
{
       var SrcID="";
       var CID="";

       var qryString = window.location.search.substring(1);
       var qStrParam = qryString.split("&");
       for (i=0;i<qStrParam.length;i++)
       {
          var qStr1 = qStrParam[i].split("=");
          if($.trim((qStr1[0]).toLowerCase())== "srcid" || $.trim((qStr1[0]).toLowerCase())== "affrefer" )
          {
            SrcID = $.trim(qStr1[1]);
          }
          if($.trim((qStr1[0]).toLowerCase())== "cid")
          {
            CID = $.trim(qStr1[1]);
          }
          
       }
       var itemID = {"SRCID":SrcID,"CID":CID,"CookiePrefix":"MIInsurance_HealthIns"};  
       var jsonStr = JSON.stringify(itemID); ""
       $.ajax({           
           type: "POST",  
           url: siteURL+"/SetCookie",  
           data: jsonStr,  
           contentType: "application/json; charset=utf-8",  
           dataType: "json",                     
           success: function(msg){              
                   return true;                  
           },
           error: function(xhr, msg){
            return false; 
         }  
     });           
     return false;
}

    //To hide Validation message
    function HideValidation(DivID)
    {
        var _DivID = "#"+DivID;  
        $(_DivID).css({visibility:"hidden",display:"none"});
       
    }


   //To validate Email   
    function validateEmail(emailToValidate)
    {
        var emailRegex = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
         if (emailRegex.test(emailToValidate)){
 	        return true;
         }
         return false;
    }
    
    //To validate mobile number
    function ValidateMobile(mobileToValidate)
    {
         var mobileRegex =  /([9]{1}[0-9]{9})$/;
         if (mobileRegex.test(mobileToValidate)){
 	        return true;
         }
         return false;
    }
    
    //To validate DOB
    function ValidateDOB(DOB)
    {
        var dobRegex =  /(0[1-9]|[12][0-9]|3[01]|[1-9])[- /.](0[1-9]|1[012]|[1-9])[- /.](19|20)\d\d/;
        if (dobRegex.test(DOB)){
 	        return true;
         }
         return false;
    }
    
    
//To send verification code to user from main form
function SendMobileVerificationCodeOnPage()
{
    var status = true;
    var Mobile = $("#txtMobile").val();   
    $("#vldMobile").css({visibility:"hidden",display:"none"});
     if(Mobile.length <= 0)
     {
        $("#vldMobile").css({visibility:"visible",display:"block"});
        $("#txtMobile").focus();
        status = false;
     }
     if(Mobile.length > 0)
     {
        if(ValidateMobile(Mobile) == false)
        {
            $("#vldMobile").css({visibility:"visible",display:"block"}); 
            $("#txtMobile").focus();
             status = false;
        }
     } 
    if(!status)
    {
        return false;
    }
    else{ 
             var itemID = {"Mobile":Mobile};  
             var jsonStr = JSON.stringify(itemID); 
             // Call our Web Service, passing in our JSON params. The error function is called  
             // whenever an exception is thrown by the web service (among other times)  
                  $.ajax({           
                   type: "POST",  
                   url: siteURL+"/SendVerificationCode",  
                   data: jsonStr,  
                   contentType: "application/json; charset=utf-8",  
                   dataType: "json",                     
                   success: function(msg){
                       var JObject = eval('(' + msg + ')');
                        ShowMessageBox(JObject);                         
                        return true;           
                   },
                   error: function(xhr, msg){
                    return false; 
                 }  
             });           
             return false;  
     }
}
//Javascript for thanks page
function writeThankScript()
{
   
}


//To show message box
function ShowMessageBox(Message)
{
     var ResultString ="<div style='margin: auto;width:405px; height:128px;background-image:url(http://www.myindiafinance.com/insurance/images/shape.gif); font-weight:bold; font-size:12px;' >"
                        + "<table><tr><td style='padding-left:20px;padding-top:20px;'>"+Message+"</td></tr></table>"
                        + "</div>"; 
    $('#ResultDiv').empty();
    $('#ResultDiv').append(ResultString);                     
    $('#ResultDiv').css({zIndex:'5000'}); 
    jQuery('#ResultDiv').center(true); 
}

// Allow number only    
function AllowCharacters()
{
   k = (document.all)?event.keyCode : arguments.callee.caller.arguments[0].which;
   if(k == 32)
   {
     return true;
   }
   else
   {
     if (k < 97 || k > 112)
     {
        return false;
     }
   }        
}

// Allow numbers and slash for date format only    
function AllowDateFormat()
{
//    k = (document.all)?event.keyCode : arguments.callee.caller.arguments[0].which;
//    
//    if(k == 8 || k == 0 || k == 13 || k == 9 || (k >= 96 && k<= 105))
//    {
//      return true;
//    }
//    else
//    {
//      if (k<47||k>57)
//        return false;
//    }       
}



