﻿function isInteger(s){
 var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
 var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
 // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
 for (var i = 1; i <= n; i++) {
  this[i] = 31
  if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
  if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
  var dtCh= ".";
  var minYear=1900;
  var maxYear=2100;
 var daysInMonth = DaysArray(12)
 var pos1=dtStr.indexOf(dtCh)
 var pos2=dtStr.indexOf(dtCh,pos1+1)
 var strDay=dtStr.substring(0,pos1)
 var strMonth=dtStr.substring(pos1+1,pos2)
 var strYear=dtStr.substring(pos2+1)
 strYr=strYear
 if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
 if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
 for (var i = 1; i <= 3; i++) {
  if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
 }
 month=parseInt(strMonth)
 day=parseInt(strDay)
 year=parseInt(strYr)
 if (pos1==-1 || pos2==-1){ 
  return false
 }
 if (strMonth.length<1 || month<1 || month>12){ 
  return false
 }
 if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
   return false
 }
 if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
   return false
 }
 if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
  return false
 }
return true
}

 

//function to remove spaces from front and end of string
function trimAll(sString){
 
    while (sString.substring(0,1) == ' '){
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' '){
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

//function to check special character in string
function checkSpecialChars(parValue){
     var receivedValue = parValue;
  var iChars = "\\\';/\""; 
     //var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~`";     
     for (var i = 0; i < receivedValue.length; i++) {
        if (iChars.indexOf(receivedValue.charAt(i)) != -1) {
            return -1;               
       }
    }
    return 1;    
}
//function to check numeric field in string
function IsNumeric_info(strString) {
        //  check for valid numeric strings 
      
           var strValidChars = "0123456789.-";
           var strChar;
           var blnResult = true;

         //  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 for email check.
function echeck(str) {

  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  var lastdot=str.lastIndexOf(dot)
  
    temp = (str.substring(lastdot+1,lstr));
  //alert(temp);
  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){
     return false
  }

  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      return false
  }

   if (str.indexOf(at,(lat+1))!=-1){
      return false
   }

   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return false
   }

   if (str.indexOf(dot,(lat+2))==-1){
      return false
   }
  
   if (str.indexOf(" ")!=-1){
      return false
   }
   
   if(isNaN(temp))
   {
        return true;
     }
     else
     {
        return false;
     }

    return true;     
 }
//function to subscribe a newsletter
function subscribe_letter(){
         if (trimAll(document.getElementById('first_name').value) !="" && checkSpecialChars(document.getElementById('first_name').value)==-1){
    document.getElementById('fname').innerHTML = '<font color="red" size="1">Invalid first name.</font>';
   return false;
  }
  if (trimAll(document.getElementById('last_name').value) !="" && checkSpecialChars(document.getElementById('last_name').value)==-1){
   document.getElementById('lname').innerHTML = '<font color="red" size="1">Invalid Last Name.</font>';  
      return false;
  }
  
      if (trimAll(document.getElementById('telephone').value) !="" && checkSpecialChars(document.getElementById('telephone').value)==-1){
    document.getElementById('tel').innerHTML = '<font color="red" size="1">Invalid telephone number.</font>'; 
        return false;
  }
 if (trimAll(document.getElementById('email').value) ==""){
        document.getElementById('email_id').innerHTML = '<font color="red" size="1">This Field is required.</font>';
        return false;
      }

   if (trimAll(document.getElementById('email').value) !="" && echeck(document.getElementById('email').value)==false){
    document.getElementById('email_id').innerHTML = '<font color="red" size="1">Invalid Email-Id.</font>';
   return false;
  }
  
  if (trimAll(document.getElementById('random').value) == ""){
    document.getElementById('random_id').innerHTML = '<font color="red" size="1">This Field is required.</font>';
   return false;
  }

  document.add_subscription.method = 'post';
  document.add_subscription.submit();

}

//function to submit and validate the inquire details
function inquire_validation(){
 var d = new Date();
      var curr_date = d.getDate();
      var curr_month = d.getMonth();
      curr_month++;
      var curr_year = d.getFullYear();
      var today = curr_month + "/" + curr_date + "/" + curr_year;
 
    if (trimAll(document.getElementById('first_name').value) ==""){
        document.getElementById('fname').innerHTML = '<font color="red" size="1">This field is required</font>';
        return false;
      }
      if (trimAll(document.getElementById('first_name').value) !="" && checkSpecialChars(document.getElementById('first_name').value)==-1){
    document.getElementById('fname').innerHTML = '<font color="red" size="1">Invalid Last name.</font>';
   return false;
  }
  if (trimAll(document.getElementById('last_name').value) ==""){
        document.getElementById('lname').innerHTML = '<font color="red" size="1">This field is required</font>';
        return false;

      }
  if (trimAll(document.getElementById('last_name').value) !="" && checkSpecialChars(document.getElementById('last_name').value)==-1){
   document.getElementById('lname').innerHTML = '<font color="red" size="1">Invalid Last Name.</font>';  
      return false;
  }
   if (trimAll(document.getElementById('country').value) !="" && checkSpecialChars(document.getElementById('country').value)==-1){
   document.getElementById('count').innerHTML = '<font color="red" size="1">Invalid country.</font>';  
      return false;
  }
      if (trimAll(document.getElementById('telephone').value) !="" && checkSpecialChars(document.getElementById('telephone').value)==-1){
    document.getElementById('tel').innerHTML = '<font color="red" size="1">Invalid telephone number.</font>'; 
        return false;
  }
  if (trimAll(document.getElementById('email').value) ==""){
        document.getElementById('email_id').innerHTML = '<font color="red" size="1">This field is required</font>';
        return false;
      }
   if (trimAll(document.getElementById('email').value) !="" && echeck(document.getElementById('email').value)==false){
    document.getElementById('email_id').innerHTML = '<font color="red" size="1">Invalid Email-Id.</font>';
   return false;
  }
 //start and end date
      var startdate =document.getElementById('startDate').value;
       var enddate =document.getElementById('endDate').value; 
      if((startdate!="" && enddate== "")||(startdate=="" && enddate!= "")){
		 document.getElementById('sdate').innerHTML = '<font color="red" size="1">Select second  Date.</font>';
		  return false;
	  }
        var mytool_array_start = startdate.split(".");
          mytool_array_start =  mytool_array_start[1]+'/'+mytool_array_start[0]+'/'+mytool_array_start[2];  
    
      
         var mytool_array_end = enddate.split(".");
      mytool_array_end = mytool_array_end[1]+'/'+mytool_array_end[0]+'/'+mytool_array_end[2];
     
     
  
      // change date into an integer string 
      start_date= Date.parse(mytool_array_start); 
      end_date= Date.parse(mytool_array_end); 
      today_date = Date.parse(today); 
     
      datediff = ((end_date-start_date)/(60*60*24*1000));

      datediff1 = ((start_date-today_date)/(60*60*24*1000));
      datediff2 = ((end_date-today_date)/(60*60*24*1000));  
      /*  if (datediff1 < 0)  {
document.getElementById('sdate').innerHTML = '<font color="red" size="1">Invalid Date.</font>';
            
            return false;  
        } 
        if (datediff2 < 0)  {
document.getElementById('edate').innerHTML = '<font color="red" size="1">Invalid Date.</font>';

            return false;  
        } */
        if (datediff < 0)  {
document.getElementById('edate').innerHTML = '<font color="red" size="1">Invalid Date.</font>';

              return false;  
        } 
if (trimAll(document.getElementById('comment').value) !="" && checkSpecialChars(document.getElementById('comment').value)==-1){
    document.getElementById('msg').innerHTML = '<font color="red" size="1">Invalid comment.</font>'; 
        return false;
  }
  
  if (trimAll(document.getElementById('random').value) == ""){
    document.getElementById('random_id').innerHTML = '<font color="red" size="1">This Field is required..</font>';
    return false;
  }
  
  document.getElementById('EnqSubmit').disabled=true;
  document.inquire_submit.method = 'post';
  document.inquire_submit.submit();

}

//function to submit and validate the favorite details
function favorite_validation(){

if (trimAll(document.getElementById('fname').value) !="" && checkSpecialChars(document.getElementById('fname').value)==-1){
   document.getElementById('f_name').className="warning_message";
    document.getElementById('f_name').innerHTML = '<font color="red" size="1">Invalid first name.</font>'; 
        return false;
  }
  if (trimAll(document.getElementById('lname').value) !="" && checkSpecialChars(document.getElementById('lname').value)==-1){
   document.getElementById('l_name').className="warning_message";
    document.getElementById('l_name').innerHTML = '<font color="red" size="1">Invalid last name.</font>'; 
        return false;
  }
  if (trimAll(document.getElementById('country').value) !="" && checkSpecialChars(document.getElementById('country').value)==-1){
  document.getElementById('count').className="warning_message";
    document.getElementById('count').innerHTML = '<font color="red" size="1">Invalid country.</font>'; 
        return false;
  }
      if (trimAll(document.getElementById('phone_no').value) !="" && checkSpecialChars(document.getElementById('phone_no').value)==-1){
      document.getElementById('phone_no_err').className="warning_message";
    document.getElementById('phone_no_err').innerHTML = '<font color="red" size="1">Invalid phone number.</font>'; 
        return false;
  }

  if (trimAll(document.getElementById('email').value) ==""){
  document.getElementById('email_err_msg').className="warning_message";
        document.getElementById('email_err_msg').innerHTML = '<font color="red" size="1">This field is required</font>';
        return false;
  }
   if (trimAll(document.getElementById('email').value) !="" && echeck(document.getElementById('email').value)==false){
   document.getElementById('email_err_msg').className="warning_message";
    document.getElementById('email_err_msg').innerHTML = '<font color="red" size="1">Invalid email-id.</font>';
   return false;
  }
  
var startdate1=trimAll(document.getElementById('startDate').value);
var enddate1=trimAll(document.getElementById('endDate').value);
if((startdate1!="" && enddate1== "")||(startdate1=="" && enddate1!= "")){
document.getElementById('sdate').className="warning_message";
		 document.getElementById('sdate').innerHTML = '<font color="red" size="1">Select second  Date.</font>';
		  return false;
	  }

   if(startdate1!=""){
    if (isDate(startdate1)==false){
    document.getElementById('sdate').className="warning_message";
  document.getElementById('sdate').innerHTML = '<font color="red" size="1">Invalid date.</font>';
  return false;
  }
 }

   if(enddate1!=""){
    if (isDate(enddate1)==false){
    document.getElementById('edate').className="warning_message";
  document.getElementById('edate').innerHTML = '<font color="red" size="1">Invalid date.</font>';
  return false;
  }
 }
if (trimAll(document.getElementById('comment').value) !="" && checkSpecialChars(document.getElementById('comment').value)==-1){
document.getElementById('comm').className="warning_message";
    document.getElementById('comm').innerHTML = '<font color="red" size="1">Invalid comments.</font>'; 
        return false;
  }
  
  if (trimAll(document.getElementById('random').value) == ""){
    document.getElementById('random_id').className="warning_message";
    document.getElementById('random_id').innerHTML = '<font color="red" size="1">This Field is required..</font>';
    return false;
  }

/*

 //start and end date
      var startdate =document.getElementById('startDate').value; 
      var mytool_array_start = startdate.split("/");
          mytool_array_start =  mytool_array_start[0]+'-'+mytool_array_start[1]+'-'+mytool_array_start[2];  
    
       var enddate =document.getElementById('endDate').value;

    var mytool_array_end = enddate.split("/");
      mytool_array_end = mytool_array_end[0]+'-'+mytool_array_end[1]+'-'+mytool_array_end[2];
     

alert(mytool_array_start+"---"+mytool_array_end+"<<<>>>>>"+today);
  
      // change date into an integer string 
      start_date= Date.parse(mytool_array_start); 
      end_date= Date.parse(mytool_array_end); 
      today_date = Date.parse(today);
     
 alert(start_date+"---"+end_date+"<<<>>>>>"+today_date);

      datediff = ((end_date-start_date)/(60*60*24*1000));

      datediff1 = ((start_date-today_date)/(60*60*24*1000));
      datediff2 = ((end_date-today_date)/(60*60*24*1000));  
        
       alert(datediff+"---"+datediff1+"<<<>>>>>"+datediff2);
         
      if (datediff1 < 0)  {
      document.getElementById('sdate').className="warning_message";
document.getElementById('sdate').innerHTML = '<font color="red" size="1">Departure Date should not be less than today date.</font>';
            

            return false;  
        } 
        if (datediff2 < 0)  {
        document.getElementById('edate').className="warning_message";
document.getElementById('edate').innerHTML = '<font color="red" size="1">Return Date should not be less than today date.</font>';

            return false;  
        } 
        if (datediff < 0)  {
        document.getElementById('edate').className="warning_message";
document.getElementById('edate').innerHTML = '<font color="red" size="1">Return should not be less than Departure date.</font>';

              return false;  
        } 
*/
  document.favorite_frm.method = 'post';
  //document.favorite_frm.submit();
  return true;
}

//function to contact to livethedream
function contact_validation(){

      if (trimAll(document.getElementById('subject').value) ==""){

        document.getElementById('sub').innerHTML = '<font color="red" size="1">This field is required.</font>';
        return false;
      }
      if (trimAll(document.getElementById('subject').value) !="" && checkSpecialChars(document.getElementById('subject').value)==-1){
    document.getElementById('sub').innerHTML = '<font color="red" size="1">Invalid subject.</font>';
   return false;
  }
    if (trimAll(document.getElementById('first_name').value) ==""){
        document.getElementById('fname').innerHTML = '<font color="red" size="1">This field is required..</font>';
        return false;
      }
      if (trimAll(document.getElementById('first_name').value) !="" && checkSpecialChars(document.getElementById('first_name').value)==-1){
    document.getElementById('fname').innerHTML = '<font color="red" size="1">Invalid first name.</font>';
   return false;
  }
  if (trimAll(document.getElementById('last_name').value) !="" && checkSpecialChars(document.getElementById('last_name').value)==-1){
   document.getElementById('lname').innerHTML = '<font color="red" size="1">Invalid Last Name.</font>';  
      return false;
  }
      if (trimAll(document.getElementById('email').value) ==""){
        document.getElementById('email_id').innerHTML = '<font color="red" size="1">This field is required..</font>';
        return false;
      }  
   if (trimAll(document.getElementById('email').value) !="" && echeck(document.getElementById('email').value)==false){
    document.getElementById('email_id').innerHTML = '<font color="red" size="1">Invalid Email-Id.</font>';
   return false;
  }
  if (trimAll(document.getElementById('comments').value) !="" && checkSpecialChars(document.getElementById('comments').value)==-1){
    document.getElementById('comm').innerHTML = '<font color="red" size="1">Invalid comment.</font>';
   return false;
  }
  
  if (trimAll(document.getElementById('random').value) == ""){
    document.getElementById('random_id').innerHTML = '<font color="red" size="1">This Field is required..</font>';
    return false;
  }
  
document.contact_form.method = 'post';
  document.contact_form.submit();
  

}
//function to send vill information to friend
function friend_validation(){
     if (trimAll(document.getElementById('friend_email').value) ==""){
        document.getElementById('friend').innerHTML = '<font color="red" size="1">This field is required..</font>';
        return false;
      }  
   if (trimAll(document.getElementById('friend_email').value) !="" && echeck(document.getElementById('friend_email').value)==false){
    document.getElementById('friend').innerHTML = '<font color="red" size="1">Invalid Email-Id.</font>';
   return false;
  }
  if (trimAll(document.getElementById('your_email').value) ==""){
        document.getElementById('your').innerHTML = '<font color="red" size="1">This field is required..</font>';
        return false;
      }  
   if (trimAll(document.getElementById('your_email').value) !="" && echeck(document.getElementById('your_email').value)==false){
    document.getElementById('your').innerHTML = '<font color="red" size="1">Invalid Email-Id.</font>';
   return false;
  }
    if (trimAll(document.getElementById('name').value) ==""){
        document.getElementById('ur_name').innerHTML = '<font color="red" size="1">This field is required..</font>';
        return false;
      }
      if (trimAll(document.getElementById('name').value) !="" && checkSpecialChars(document.getElementById('name').value)==-1){
    document.getElementById('ur_name').innerHTML = '<font color="red" size="1">Invalid name.</font>';
   return false;
  }
 
      
  if (trimAll(document.getElementById('message').value) !="" && checkSpecialChars(document.getElementById('message').value)==-1){
    document.getElementById('msg').innerHTML = '<font color="red" size="1">Invalid message.</font>';
   return false;
  }
  
  if (trimAll(document.getElementById('random').value) == ""){
    document.getElementById('random_id').innerHTML = '<font color="red" size="1">This Field is required..</font>';
   return false;
  }/**/
  
  document.friend_form.method = 'post';
  document.friend_form.submit();

}
//function to submit and validate the inquire details
function review_validation(){
if (trimAll(document.getElementById('startDate').value)==""){
        alert("Please Enter the date");
        return false;
      }
   var startdate1=trimAll(document.getElementById('startDate').value);
   if(startdate1!=""){
    if (isDate(startdate1)==false){
  document.getElementById('date').innerHTML = '<font color="red" size="1">Invalid date.</font>';
  return false
  }
 }

 var d = new Date();
      var curr_date = d.getDate();
      var curr_month = d.getMonth();
      curr_month++;
      var curr_year = d.getFullYear();
      var today = curr_month + "/" + curr_date + "/" + curr_year;
   
      var enddate =document.getElementById('startDate').value;
   var mytool_array_end = enddate.split(".");
      mytool_array_start = mytool_array_end[1]+'/'+mytool_array_end[0]+'/'+mytool_array_end[2];

   // change date into an integer string 
      start_date= Date.parse(mytool_array_start); 
      today_date = Date.parse(today); 

 if (start_date>today_date){
  document.getElementById('date').innerHTML = '<font color="red" size="1">Invalid date.</font>';
  return false;
 }else{
  document.getElementById('date').innerHTML = '';
 }


if (trimAll(document.getElementById('name').value) ==""){
        document.getElementById('review_name').innerHTML = '<font color="red" size="1">This field is required.</font>';
        return false;
      }
      if (trimAll(document.getElementById('name').value) !="" && checkSpecialChars(document.getElementById('name').value)==-1){
    document.getElementById('review_name').innerHTML = '<font color="red" size="1">Invalid name.</font>';
   return false;
  }
   
      if (trimAll(document.getElementById('from').value) !="" && checkSpecialChars(document.getElementById('from').value)==-1){
    document.getElementById('review_from').innerHTML = '<font color="red" size="1">Invalid characters in from column.</font>';
   return false;
  }
   if (trimAll(document.getElementById('star_rating').value) ==""){
        document.getElementById('rating').innerHTML = '<font color="red" size="1">This field is required.</font>';
        return false;
      }
  
   if (trimAll(document.getElementById('star_rating').value) !=""){
    if(!IsNumeric_info(document.getElementById('star_rating').value)) {
        document.getElementById('rating').innerHTML = '<font color="red" size="1">Please enter the numeric value.</font>';
        return false;
       } 
    if(trimAll(document.getElementById('star_rating').value)<0 || trimAll(document.getElementById('star_rating').value)>5){
  document.getElementById('rating').innerHTML = '<font color="red" size="1">Invalid range of rating.</font>';
        return false;
       }   
      }
  if (trimAll(document.getElementById('message').value) !="" && checkSpecialChars(document.getElementById('message').value)==-1){
   document.getElementById('mess').innerHTML = '<font color="red" size="1">Invalid message.</font>';  
      return false;
  }
  
  if (trimAll(document.getElementById('random').value) == ""){
    document.getElementById('random_id').innerHTML = '<font color="red" size="1">This Field is required..</font>';
    return false;
  }/**/
  
  document.review_form.method = 'post';
  document.review_form.submit();
}
