// BRANDO: Added to make chkvalue available to booking form
 


function echeck(str) 
{
	var email = str;
	if(email == "")
		return false;

	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	//var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.'_]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.'_]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	if (!reg1.test(email) && reg2.test(email))	// if syntax is valid	
		return true;

	return false;
}

function formatDate1(toFormatDate,toFormatMonth,toFormatYear)
  { 
    
    if ((toFormatDate=="1")||(toFormatDate=="2")||(toFormatDate=="3")||(toFormatDate=="4")||(toFormatDate=="5")||(toFormatDate=="6")||(toFormatDate=="7")||(toFormatDate=="8")||(toFormatDate=="9"))
    toFormatDate="0"+toFormatDate;
    
    if((toFormatDate=="01")||(toFormatDate=="21")||(toFormatDate=="31"))
    toFormatDate = toFormatDate+"st "  
    
    else if((toFormatDate=="02")||(toFormatDate=="22"))
    toFormatDate = toFormatDate+"nd "
     
     else if((toFormatDate=="03")||(toFormatDate=="23"))
     toFormatDate = toFormatDate+"rd "
    
    else
    toFormatDate = toFormatDate+"th "
    
    if (toFormatMonth=="01") toFormatMonth="January";
    
    else if (toFormatMonth=="02") toFormatMonth="February";
    
    else if (toFormatMonth=="03") toFormatMonth="March";

    else if (toFormatMonth=="04") toFormatMonth="April";

    else if (toFormatMonth=="05") toFormatMonth="May";

    else if (toFormatMonth=="06") toFormatMonth="June";

    else if (toFormatMonth=="07") toFormatMonth="July";

    else if (toFormatMonth=="08") toFormatMonth="August";

    else if (toFormatMonth=="09") toFormatMonth="September";

    else if (toFormatMonth=="10") toFormatMonth="October";

    else if (toFormatMonth=="11") toFormatMonth="November";

    else if (toFormatMonth=="12") toFormatMonth="December";
    
    
    
    formatTotal=toFormatDate+toFormatMonth+" "+toFormatYear; // Urvashi CR3017 Removed '-'
    
    return(formatTotal);
   
  }



function ChkNumber(no_adults1)
{
  var str1 = "1234567890";
  if (no_adults1.length>0)
 { 
   for (var k1=0; k1 < no_adults1.length; k1++)
    { 
      if (str1.indexOf(no_adults1.charAt(k1))==-1)
        { 
          return false;
        }
     }  
 }

}

 
function convertDate(input) {
    input = new String(input);
    var theDay = input.substring(0,2);
    var theMonth = input.substring(3,5);
    var theYear = input.substring(6,10);
    var output = new Date(theYear,theMonth-1,theDay);
    return output;
}

function todaysDate() {
        var mydate=new Date();
        var theyear=mydate.getFullYear();
        var themonth=mydate.getMonth();
        var thetoday=mydate.getDate();
        var output = new Date(theyear,themonth,thetoday);
        return output;
    }
 
  function chkvalue()
  { 
    
    
      // step 1 : validations 
    
    // Validation for hotel 
    
    // if (document.avail.hotels.value=="")
    //  { 
    //    alert("Please choose at least one Hotel");
    //    document.avail.arr_day.focus(); return(false);
    //  }
     
     
    // Validation for room type 
    
    //var roomType=document.avail.grt_type.options[document.avail.grt_type.selectedIndex].value;
    //if((roomType=="") || (roomType=="Select"))
    //  { 
    //    alert("Please choose a Room Type");
    //    document.avail.grt_type.focus();return(false);
    //  }
     
     
      // Validation for No. of Rooms
      
      


    if (ChkNumber(document.avail.no_rooms.value)==false)
         { 
            alert("Please enter a valid value for No. of rooms");
            document.avail.no_rooms.value="";
            document.avail.no_rooms.focus();return false
        }
    
    if (document.avail.no_rooms.value=="0")
        { 
            alert("Number of rooms cannot be zero");
             document.avail.no_rooms.focus();return(false);
        }
     
     if (document.avail.no_rooms.value==0)
        { 
            alert("Please enter valid Number of Rooms");
             document.avail.no_rooms.focus();return(false);
        }      
        
    

//step 2: values retrieved from date text boxes, number of nights calculated.      

     
   
    
    
    var arrDate = convertDate(document.avail.arr_day.value);
    var depDate = convertDate(document.avail.dep_day.value); 

    var daysDiff = (depDate.valueOf() - arrDate.valueOf());


    daysDiff = Math.floor(Math.abs((((daysDiff  / 1000) / 60) / 60) / 24));
    //alert (arrDate + " | " + depDate + " | " + daysDiff);
    //alert(daysDiff);
    document.avail.no_nights.value = daysDiff;
    
    if (arrDate >= depDate) {
          alert("The arrival date chosen is later than, or the same as, your departure date.");
          return false;
    } else if (arrDate < todaysDate()){
          alert("The arrival date chosen is in the past."); 
          return false;
    } 
    
    if ((document.avail.sender_name.length == 0)||(document.avail.sender_name.value =="name")) {
          alert("Please fill in your name.");
          return false; 
    }
      
    if ((document.avail.sender_email.length == 0) || (document.avail.sender_email.value == "email")) {
          alert("Please fill in your email address");
          return false;
    }
    if (echeck(document.avail.sender_email.value)==false){
          alert("That email address is invalid");
          return false;
      
      
    
    
    
    } else {
          
          /*arrDate = new String(document.avail.arr_day.value);
          arrDate = arrDate.split("/");
          document.avail.arr_day.value = formatDate1(arrDate[0], arrDate[1], arrDate[2]);
          depDate = new String(document.avail.dep_day.value);
          depDate = depDate.split("/");
          document.avail.dep_day.value = formatDate1(depDate[0], depDate[1], depDate[2]);
          alert(document.avail.arr_day.value);*/
          
          return true;
          
    }
 
      

  
}