//Regular Expressions for different kinds of data
var nameRegxp = new RegExp("^([a-zA-Z]+)$");

var houseRegxp = new RegExp("^([0-9A-Za-z]+)$");
                                 
var pcodeRegxp = new RegExp("^([A-Za-z]{1,2})([0-9]{2,3})([A-Za-z]{2})$");

var telnoRegxp = new RegExp("^([0-9]{11})$");
                                                                  
var emailRegxp = new RegExp("^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$");

var urlRegxp = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$");

var dobRegxp = new RegExp("^([0-9]){2}(\/|-){1}([0-9]){2}(\/|-)([0-9]){4}$");

var noRegxp = new RegExp("^([0-9]+)$");

function formValidation(chk)
{
    
    if(trimString(chk.FirstName.value)=="") { 
        alert("Please enter First Name");
        chk.FirstName.focus();
        return false;
    }
    
    if(nameRegxp.test(chk.FirstName.value) != true) {
        alert("Please enter valid First Name");
        chk.FirstName.focus();
        return false;
    }
    
    if(nameRegxp.test(chk.LastName.value) != true && trimString(chk.LastName.value)!="") {
        alert("Please enter valid Last Name");
        chk.LastName.focus();
        return false;
    }
    
    if(trimString(chk.BirthDate.value)=="") { 
        alert("Please enter Date of Birth");
        chk.BirthDate.focus();
        return false;
    }
    
    if(dobRegxp.test(chk.BirthDate.value)!=true || chk.BirthDate.value=="00/00/0000"){
        alert("Please enter valid Date of Birth");
        chk.BirthDate.focus();
        return false;
    }
    
    var dt=new Date();
    cur=dt.getDate()+"/"+eval(dt.getMonth()+1)+"/"+dt.getFullYear();
    var st=dateCompare(chk,chk.BirthDate.value,cur);
    
    if(!st){
          alert("Date of Birth should be less that or equal to Current Date");
          chk.BirthDate.focus();
          return false;
    }
    
    // if(trimString(chk.SFirstName.value)=="") { 
    //         alert("Please enter Spouse First Name");
    //         chk.SFirstName.focus();
    //         return false;
    //     }
    //     if(nameRegxp.test(chk.SFirstName.value) != true){
    //         alert("Please enter valid Spouse First Name");
    //         chk.SFirstName.focus();
    //         return false;
    //     }
    //     if(nameRegxp.test(chk.SLastName.value) != true && trimString(chk.SLastName.value)!="") {
    //         alert("Please enter valid Spouse Last Name");
    //         chk.SLastName.focus();
    //         return false;
    //     }
    //      if(trimString(chk.SDOB.value)=="") { 
    //         alert("Please enter Spouse Date of Birth");
    //         chk.SDOB.focus();
    //         return false;
    //     }
    //     if(dobRegxp.test(chk.SDOB.value)!=true || chk.SDOB.value=="00/00/0000"){
    //         alert("Please enter valid Spouse Date of Birth");
    //         chk.SDOB.focus();
    //         return false;
    //     }
    //     
    //     var st=dateCompare(chk,chk.SDOB.value,cur);
    //     
    //     if(!st){
    //           alert("Spouse Date of Birth should be less that or equal to Current Date");
    //           chk.SDOB.focus();
    //           return false;
    //     }
    
    if(trimString(chk.Email.value)=="") { 
        alert("Please enter Email Address");
        chk.Email.focus();
        return false;
    }
     if(!isValidEmail(chk.Email.value)){
                chk.Email.select();
                return false;
    }
    if(trimString(chk.Password.value)==""){ 
        alert("Please enter Password");
        chk.Password.focus();
        return false;
    } 
    if(houseRegxp.test(chk.Password.value)!=true){ 
        alert("Please enter valid Password");
        chk.Password.focus();
        return false;
    }                               
   
    if(trimString(chk.Email2.value)!="" && !isValidEmail(chk.Email2.value)){
                chk.Email2.select();
                return false;
    }
    if(trimString(chk.Email3.value)!="" && !isValidEmail(chk.Email3.value)){
                chk.Email3.select();
                return false;
    }  
   if(trimString(chk.Phone.value)=="") {
       alert("Please enter Home Phone");
       chk.Phone.focus();
       return false;
    }
   /*  if(trimString(chk.Mobile1.value)=="") {
       alert("Please enter Mobile Number 1");
       chk.Mobile1.focus();
       return false;
    }
    if(trimString(chk.Mobile2.value)=="") {
       alert("Please enter Mobile Number 2");
       chk.Mobile2.focus();
       return false;
    }
    if(trimString(chk.Address.value)=="") {
       alert("Please enter Address");
       chk.Address.focus();
       return false;
    }
    if(trimString(chk.City.value)=="") {
       alert("Please enter City");
       chk.City.focus();
       return false;
    }  */
    
    if(trimString(chk.Country.value)=="") {
       alert("Please select Country");
       chk.Country.focus();
       return false;
    }
}

function childValidation(chk)
{
    len=document.getElementById('theValue').value;
    var dt=new Date();
    cur=dt.getDate()+"/"+eval(dt.getMonth()+1)+"/"+dt.getFullYear();
    
    for(var t=1;t<=len;t++)
    {
       if(chk['cfname'+t]!=undefined)
       {
          if(trimString(chk['cfname'+t].value)==""){
             alert("Please enter Child First Name");
             chk['cfname'+t].focus();
             return false;
          }
          if(nameRegxp.test(chk['cfname'+t].value) != true) {
             alert("Please enter valid Child First Name");
             chk['cfname'+t].focus();
             return false;
          }
          if(nameRegxp.test(chk['clname'+t].value) != true && trimString(chk['clname'+t].value)!="") {
             alert("Please enter valid Child Last Name");
             chk['clname'+t].focus();
             return false;
          }
          if(dobRegxp.test(chk['cdob'+t].value)!=true || chk['cdob'+t].value=="00/00/0000"){
            alert("Please enter valid Child Date of Birth");
            chk['cdob'+t].focus();
            return false;
          }
          var st=dateCompare(chk,chk['cdob'+t].value,cur);
    
          if(!st){
              alert("Child Date of Birth should be less that or equal to Current Date");
              chk['cdob'+t].focus();
              return false;
          } 
                      
       }
    }
    
}


function calculate(val,chk,inp,sts){
     
      chk=eval(chk);
      
      if(sts==undefined){
          dt1=chk.stdt.value;
          dt2=chk.enddt.value;
      }else if(sts!=''){
          dt1=chk['stdt'+sts].value;
          dt2=chk['enddt'+sts].value; 
      }
      
      var one_day=1000*60*60*24;  
      var x=dt1.split('/');       
      var date1=new Date(x[2],x[1]-1,x[0]);
      var y=dt2.split('/');
      var date2=new Date(y[2],y[1]-1,y[0]);
      diff=(date2.getTime()-date1.getTime())/(one_day);
      
      if(diff!='NaN' && diff>0){
      
           if(sts==undefined)
           chk.nights.value=  Math.round(diff); 
           else chk['nights'+sts].value=diff; 
      }
      else  if(diff!='NaN' && diff<0){
        
          alert("Departure Date should be greater than Arrival Date");   
          if(sts==undefined)
          chk.enddt.focus();
          else chk['enddt'+sts].focus();
          return false;
      }
      
      if(inp=='1'){
            
            var day=eval(x[0])+eval(1);
            var NextDate= new Date(x[2], x[1]-1, day);
            
            if((NextDate.getMonth()+1)<10) mon = '0'+(NextDate.getMonth()+1);
            else mon=(NextDate.getMonth()+1);
            if(NextDate.getDate()<10) ds='0'+NextDate.getDate();
            else ds = NextDate.getDate();
            
            var Ndate=ds+"/"+(mon)+"/"+NextDate.getYear();
            
            if(sts==undefined){
                $(function()
                {
                   $('.date-pick2').datePicker({startDate:""+Ndate+""})
                });
            }
            else if(sts!=''){
                 $(function()
                 {   
                    $('.date-pick'+sts).datePicker({startDate:""+Ndate+""})
                 });
            }
      }                 
}

function options1(val,k){

   if(val=="")
   {
      chk=document.longbookingform;
      chk['property'+k].value='';
      chk['stdtL'+k].value='';
      chk['enddtL'+k].value='';
      chk['nightsL'+k].value='';
   }
   
}

function bookingValidation(chk,sts)
{
    chk=eval(chk);
    
    /************************************************ For LongTerm Booking ****************************************/
    
    flg=0;
    if(sts!=undefined && sts=='long'){
       
        for(k=1;k<=3;k++)
        {
        
             lt='L'+k;
             if(trimString(chk['property'+k].value)!="" || trimString(chk['stdt'+lt].value)!="" || trimString(chk['enddt'+lt].value)!=""){
               flg=1;     
               break;
            }
        }
    }
      
    if(flg==0 && sts!=undefined && sts=='long'){
          
          alert("Please select any Preference");
          chk['property1'].focus();
          return false;
          
    }
     
    if(sts!=undefined && sts=='long'){
       
        for(k=1;k<=3;k++)
        {
             lt='L'+k;
            
            if(trimString(chk['property'+k].value)!="" || trimString(chk['stdt'+lt].value)!="" || trimString(chk['enddt'+lt].value)!=""){
                if(trimString(chk['property'+k].value)==""){
                  alert("Please select Property");
                  chk['property'+k].focus();
                  return false;
                }
                
                if(trimString(chk['stdt'+lt].value)==""){
                  alert("Please select Arrival Date");
                  chk['stdt'+lt].focus();
                  return false;
                }
                 
                if(trimString(chk['enddt'+lt].value)==""){
                  alert("Please select Departure Date");
                  chk['enddt'+lt].focus();
                  return false;
                }
            }
            
        }
        
        
        for(k=1;k<=3;k++)
        {
              lt='L'+k;
              if(trimString(chk['property'+k].value)!="" || trimString(chk['stdt'+lt].value)!="" || trimString(chk['enddt'+lt].value)!=""){
                      
                      dt1=chk['stdt'+lt].value;
                      dt2=chk['enddt'+lt].value;
                      var one_day=1000*60*60*24;  
                      var x=dt1.split('/');       
                      var date1=new Date(x[2],x[1]-1,x[0]);
                      var y=dt2.split('/');
                      var date2=new Date(y[2],y[1]-1,y[0]);
                      diff=(date2.getTime()-date1.getTime())/(one_day);
                   
                   if(diff=='NaN' || diff<0)   {
                      alert("Departure Date should be greater than Arrival Date");   
                      chk['enddt'+lt].focus();
                      return false;
                   }
                    chk['nights'+lt].value=diff;
                   if(trimString(chk['nights'+lt].value)==""){
                      alert("Please enter # of Nights");
                      chk['nights'+lt].focus();
                      return false;
                    }
                    
              }
        }
    } 
    
    /********************************** End of  LongTerm Booking **********************************/
    
    if(sts==undefined){
    
        if(trimString(chk.property.value)==""){
          alert("Please select Property");
          chk.property.focus();
          return false;
        }
        
        if(trimString(chk.stdt.value)==""){
          alert("Please select Arrival Date");
          chk.stdt.focus();
          return false;
        }
         
        if(trimString(chk.enddt.value)==""){
          alert("Please select Departure Date");
          chk.enddt.focus();
          return false;
        }
        
          dt1=chk.stdt.value;
          dt2=chk.enddt.value;
          var one_day=1000*60*60*24;  
          var x=dt1.split('/');       
          var date1=new Date(x[2],x[1]-1,x[0]);
          var y=dt2.split('/');
          var date2=new Date(y[2],y[1]-1,y[0]);
          diff=(date2.getTime()-date1.getTime())/(one_day);
          
          
       
       if(diff=='NaN' || diff<0)   {
          alert("Departure Date should be greater than Arrival Date");   
          chk.enddt.focus();
          return false;
       }
        chk.nights.value= Math.round(diff);
       if(trimString(chk.nights.value)==""){
          alert("Please enter # of Nights");
          chk.nights.focus();
          return false;
        }
    }
    
    var flg=0;
    for(var t=0;t<chk.type.length;t++){
       if(chk.type[t].checked==true) break;
    }
    if(chk.type[t].value!="Primary"){
    
       if(trimString(chk.fname.value)==""){
          alert("Please enter First Name");
          chk.fname.focus();
          return false;
       }
       if(nameRegxp.test(chk.fname.value) != true) {
          alert("Please enter valid First Name");
          chk.fname.focus();
          return false;
       }
       if(trimString(chk.lname.value)==""){
          alert("Please enter Last Name");
          chk.lname.focus();
          return false;
       }
       if(nameRegxp.test(chk.lname.value) != true) {
          alert("Please enter valid First Name");
          chk.lname.focus();
          return false;
       }
       if(trimString(chk.phone.value)==""){
          alert("Please enter Phone");
          chk.phone.focus();
          return false;
       }
       
       if(trimString(chk.email.value)==""){
          alert("Please enter Email");
          chk.email.focus();
          return false;
       }
       if(!isValidEmail(chk.email.value)){
                chk.email.select();
                return false;
       }                    
       if(chk.terms.checked==false){
          alert("Please accept Terms & Conditions");
          chk.terms.focus();
          return false;
       }
    }
}

function loginValidation(chk)
{ 
    if(trimString(chk.Email.value)==""){ 
        alert("Please enter User Name");
        chk.Email.focus();
        return false;
    }
    if(!isValidEmail(chk.Email.value)){
                chk.Email.select();
                return false;
    }
    if(trimString(chk.Password.value)==""){ 
        alert("Please enter Password");
        chk.Password.focus();
        return false;
    }
}

function adminValidation(chk)
{ 
    if(trimString(chk.username.value)==""){ 
        alert("Please enter User Name");
        chk.username.focus();
        return false;
    }
    if(trimString(chk.password.value)==""){ 
        alert("Please enter Password");
        chk.password.focus();
        return false;
    }
}

function cancellation(chk,url)
{
   if(url!="")
   {
       if(url.indexOf('ann')!="-1")
          var t=confirm('Are You sure you want to delete the Announcement?');
       else if(url.indexOf('msg')!="-1")
          var t=confirm('Are You sure you want to delete the Message?');
       else if(url.indexOf('exh')!="-1")
          var t=confirm('Are You sure you want to delete the Daypoints for Sale?');
       
       if(t==true)
       window.location=chk+'?'+url; 
   }
   else
   {
   var val=chk.yes.value;
    if(trimString(chk.yes.value)=="" || val.toUpperCase()!="YES")
    {
        alert("Please enter YES to cancel the booking");
        chk.yes.focus();
        return false;
    }
   }
}

function new_window(dt,did,sts){

    url='booking_popup.php?dt='+dt+'&did='+did+'&sts='+sts;
    myRef = window.open(url,'mywin','left=20,top=20,width=500,height=500,toolbar=1,resizable=0'); 
    
}

function buyExchange(chk,eid){

    txt=chk;
    chk=eval(chk);
    if(trimString(chk.year.value)=="") { 
       alert("Please select Year");
       chk.year.focus();
       return false;
    }
    if(trimString(chk.daypoints.value)=="") {
       alert("Please enter No of Daypoints");
       chk.daypoints.focus();
       return false;
    }
    if(noRegxp.test(chk.daypoints.value)!= true || parseInt(chk.daypoints.value)==0) {
        alert("Please enter valid No of Daypoints");
        chk.daypoints.focus();
        return false;
    }
    service2('8',txt,'buy',eid);
    
    if(chk.btotal.value!="Calculated Value"){
    
        var val=chk.yes.value;
        if(trimString(chk.yes.value)=="" || val.toUpperCase()!="YES")
        {
            alert("Please enter YES to confirm the Sale");
            chk.yes.focus();
            return false;
        } else {
           
          // document.buyform.action='member-exchange.php';
           document.getElementById("myForm").submit();

         }
    }
}

function saleExchange(chk){

    if(trimString(chk.year.value)=="") { 
       alert("Please select Year");
       chk.year.focus();
       return false;
    }
    if(trimString(chk.daypoints.value)=="") {
       alert("Please enter No of Daypoints");
       chk.daypoints.focus();
       return false;
    }
    if(noRegxp.test(chk.daypoints.value)!= true || parseInt(chk.daypoints.value)==0) {
        alert("Please enter valid No of Daypoints");
        chk.daypoints.focus();
        return false;
    }
    if(trimString(chk.price.value)=="") {
       alert("Please enter Price/Daypoint");
       chk.price.focus();
       return false;
    }
    
    val=chk.price.value;
    if((isNaN(val) && val.indexOf('.')==-1) || val.indexOf('-')!=-1 || parseInt(val)=="0") {
        alert("Please enter valid Price");
        chk.price.focus();
        return false;
    }
    
    tot=eval(chk.daypoints.value * val);
    chk.total.value=parseFloat(tot);
    
    if(trimString(chk.terms.value)=="") {
       alert("Please select Terms");
       chk.terms.focus();
       return false;
    }
    
    var val=chk.yes.value;
    if(trimString(chk.yes.value)=="" || val.toUpperCase()!="YES")
    {
        alert("Please enter YES to confirm the Sale");
        chk.yes.focus();
        return false;
    }
}

function saleCalc(chk,sts){
   
    chk=eval(chk);
    var tot=0;
    var points=chk.daypoints.value;
    var price=chk.price.value;
    
    
    if(trimString(chk.daypoints.value)=="") {
       alert("Please enter No of Daypoints");
       chk.daypoints.focus();
       return false;
    }
    if(noRegxp.test(chk.daypoints.value)!= true || parseInt(chk.daypoints.value)==0) {
        alert("Please enter valid No of Daypoints");
        chk.daypoints.focus();
        return false;
    }
    if(trimString(chk.price.value)=="") {
       alert("Please enter Price/Daypoint");
       chk.price.focus();
       return false;
    }
    
    val=chk.price.value;
    if((isNaN(val) && val.indexOf('.')==-1) || val.indexOf('-')!=-1 || parseInt(val)=="0") {
        alert("Please enter valid Price");
        chk.price.focus();
        return false;
    }
    
    if(price!="")
    {
     var tot=eval(parseInt(points) * parseFloat(price));
    }
    chk.total.value=parseFloat(tot);
}

function trimString(str)         // On 18/11/2008 by Aruna Sri
{
   return str.replace(/^\s+|\s+$/g, '');
}

//******************************************************************************************//
//********************* functions  for email-id validation ****************************//
//******************************************************************************************//
function isValidEmail(emailStr) {
    var checkTLD=1;
    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray=emailStr.match(emailPat);
    if (matchArray==null) {
        alert("Email address seems incorrect (check @ and .'s)");
        return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];
    // Start by checking that only basic ASCII characters are in the strings (0-127).
    for (i=0; i<user.length; i++) {
        if (user.charCodeAt(i)>127) {
            alert("Ths username contains invalid characters.");
            return false;
        }
    }
    for (i=0; i<domain.length; i++) {
        if (domain.charCodeAt(i)>127) {
            alert("Ths domain name contains invalid characters.");
            return false;
        }
    }
    if (user.match(userPat)==null) {
        alert("The username doesn't seem to be valid in Email Address.");
        return false;
    }
    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
        for (var i=1;i<=4;i++) {
            if (IPArray[i]>255) {
                alert("Destination IP address is invalid!");
                return false;
               }
        }
        return true;
    }
    var atomPat=new RegExp("^" + atom + "$");
    var domArr=domain.split(".");
    var len=domArr.length;
    for (i=0;i<len;i++) {
        if (domArr[i].search(atomPat)==-1) {
            alert("The domain name does not seem to be valid.");
            return false;
       }    
    }
    if (checkTLD && domArr[domArr.length-1].length!=2 && 
        domArr[domArr.length-1].search(knownDomsPat)==-1) {
        alert("The address must end in a well-known domain or two letter " + "country.");
        return false;
    }

// Make sure there's a host name preceding the domain.

    if (len<2) {
        alert("This address is missing a hostname!");
        return false;
    }    
    return true;
}

//to Remove JavaScript Element

function removeElement(divNum) {

  
  var divNum1='cfname'+divNum;
  var divNum2='clname'+divNum;
  var divNum3='cdob'+divNum;
  
  var d = document.getElementById('form-row-child');
  var olddiv = document.getElementById(divNum1);
  d.removeChild(olddiv);
  var olddiv = document.getElementById(divNum2);
  d.removeChild(olddiv);
  var olddiv = document.getElementById(divNum3);
  d.removeChild(olddiv);
  
  var num1 = eval(document.getElementById('inc').value)-1;
  document.getElementById('inc').value=num1; 
  
  
  
  if(num1==0){
     document.getElementById('submit-id').innerHTML=''; 
     var olddiv = document.getElementById('csub');
     d.removeChild(olddiv);
  }
}

function helpValidation(chk,val){
  
  if(val=="question"){
     if(trimString(chk.question1.value)==""){
       alert("Plese enter Question");
       chk.question1.focus();
       return false;
     }
  }else if(val=="feedback"){
    if(trimString(chk.feedback1.value)==""){
       alert("Plese enter Feedback");
       chk.feedback1.focus();
       return false;
     }
  }
}

function calc(chk){

     /*var one_day=1000*60*60*24;
     var dt1=chk.stdt.value;
     var x=dt1.split('/');
     var date1=new Date(x[2],x[1]-1,x[0]);
     dest=new Date(date1.getTime()+(chk.nights.value*24*60*60*1000));
     var date2=dest.getDate()+"/"+eval(dest.getMonth()+1)+"/"+dest.getFullYear();
     chk.enddt.value=date2;*/
     
    if(trimString(chk.stdt.value)==""){
      alert("Please select Arrival Date");
      chk.stdt.focus();
      return false;
    }
     
    if(trimString(chk.enddt.value)==""){
      alert("Please select Departure Date");
      chk.enddt.focus();
      return false;
    }
      dt1=chk.stdt.value;
      dt2=chk.enddt.value;
      var one_day=1000*60*60*24;  
      var x=dt1.split('/');       
      var date1=new Date(x[2],x[1]-1,x[0]);
      var y=dt2.split('/');
      var date2=new Date(y[2],y[1]-1,y[0]);
      diff=(date2.getTime()-date1.getTime())/(one_day);
      
      chk.nights.value= Math.round(diff);
}

function dateCompare(chk,dt1,dt2){

  var one_day=1000*60*60*24;  
  var x=dt1.split('/');       
  var date1=new Date(x[2],x[1]-1,x[0]);
  var y=dt2.split('/');
  var date2=new Date(y[2],y[1]-1,y[0]);
  diff=(date2.getTime()-date1.getTime())/(one_day);
  if(diff>=0)  return 1; else return 0;  
}