
function isDate(str)
{
  try
  {
    arr = str.split('-');
    day = parseInt(removeLeadingZeros(arr[0]));
    month = parseInt(removeLeadingZeros(arr[1])) - 1;
    year = parseInt(removeLeadingZeros(arr[2]));
    var d = new Date(year, month, day);
    return d.getMonth() == month && d.getFullYear() == year && d.getDate() == day;
  }
  catch(err)
  {
    return false;
  }
}

function checkStep1() {
    if(!CompareToday(document.BookingStep1.ArrDate.value)){
        alert("Arrival date must later than Today !\n");
        return false;
    }
}

function checkStep4() {
     if (trim(document.BookingStep4.CustomerName.value)==""){
        alert("Enter Guest Name, Please ! \n");
        return false;
     }else{ if (!checkEmail(document.BookingStep4.Email.value)){
                alert("Invalid Email! \n");
                return false;
            }else{if(document.BookingStep4.Email.value!=document.BookingStep4.ConfirmEmail.value){
                    alert("Check youe email, please \n");
                    return false;
                }else{if(!document.BookingStep4.accept.checked){
                        alert("Please read and accept the Terms & Conditions ! \n");
                        return false;
                    }
                }
            }
     }
}

function checkEmail(e)
{
 if(e.length!=0)
  {
    if (e.charAt(0)=="." ||
         e.charAt(0)=="@"||
         e.indexOf('@', 0) == -1 ||
         e.indexOf('.', 0) == -1 ||
         e.lastIndexOf("@")==e.length-1 ||
         e.lastIndexOf(".")==e.length-1)
     {return false;
      }
   }
 else
  {return false;
   }
 return true;
}

function CompareToday(s){
var start= new Date();
var today = new Date();
start.setFullYear(parseInt(s.charAt(6)+s.charAt(7)+s.charAt(8)+s.charAt(9)),parseInt(s.charAt(3))*10+parseInt(s.charAt(4))-1,parseInt(s.charAt(0))*10+parseInt(s.charAt(1)))
//start.setFullYear(s.charAt(6)+s.charAt(7)+s.charAt(8)+s.charAt(9),parseInt(s.charAt(3)+s.charAt(4))-1,s.charAt(0)+s.charAt(1))
today.setFullYear(today.getYear(),today.getMonth(),today.getDate())
if (today.getTime() > start.getTime()){return false;
     }else{return true;}
}




function onlyNum()
{
 if(!(event.keyCode==46)&&!(event.keyCode==8)&&!(event.keyCode==37)&&!(event.keyCode==39)&&!(event.keyCode==190))
  if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
    event.returnValue=false;
}

function nDateAdd(s,n){
    dd = new Date();
    dd.setFullYear(parseInt(s.charAt(6)+s.charAt(7)+s.charAt(8)+s.charAt(9)),parseInt(s.charAt(3))*10+parseInt(s.charAt(4))-1,parseInt(s.charAt(0))*10+parseInt(s.charAt(1)))
    dd.setTime(dd.getTime() + (n * 24 * 3600 * 1000));
    yy = dd.getYear();
    mm = dd.getMonth() + 1;
    dd = dd.getDate();
    if (yy < 2000) { yy += 1900; }
    if (mm < 10) { mm = "0" + mm; }
    if (dd < 10) { dd = "0" + dd; }
    return dd + "-" + mm + "-" + yy ;
}







function CalNights(s,e){
var start= new Date();
var finish = new Date();
start.setFullYear(s.charAt(6)+s.charAt(7)+s.charAt(8)+s.charAt(9),s.charAt(3)+s.charAt(4),s.charAt(0)+s.charAt(1))
finish.setFullYear(e.charAt(6)+e.charAt(7)+e.charAt(8)+e.charAt(9),e.charAt(3)+e.charAt(4),e.charAt(0)+e.charAt(1))
if (finish.getTime() <= start.getTime()){alert("Departure date must later than arrival date !\n");return 0;
     }else{return (finish-start)/86400000;}
}

function nToday(){
    var today = new Date();
    yy = today.getYear();
    mm = today.getMonth() + 1;
    dd = today.getDate();
    if (yy < 2000) { yy += 1900; }
    if (mm < 10) { mm = "0" + mm; }
    if (dd < 10) { dd = "0" + dd; }
    return dd + "-" + mm + "-" + yy ;
}





function CompareDate(s,e){
var start= new Date();
var finish = new Date();
start.setFullYear(parseInt(s.charAt(6)+s.charAt(7)+s.charAt(8)+s.charAt(9)),parseInt(s.charAt(3))*10+parseInt(s.charAt(4))-1,parseInt(s.charAt(0))*10+parseInt(s.charAt(1)))
finish.setFullYear(parseInt(e.charAt(6)+e.charAt(7)+e.charAt(8)+e.charAt(9)),parseInt(e.charAt(3))*10+parseInt(e.charAt(4))-1,parseInt(e.charAt(0))*10+parseInt(e.charAt(1)))

//start.setFullYear(s.charAt(6)+s.charAt(7)+s.charAt(8)+s.charAt(9),s.charAt(3)+s.charAt(4),s.charAt(0)+s.charAt(1))
//finish.setFullYear(e.charAt(6)+e.charAt(7)+e.charAt(8)+e.charAt(9),e.charAt(3)+e.charAt(4),e.charAt(0)+e.charAt(1))

if (finish.getTime() <= start.getTime()){return false;
     }else{return true;}
}



function _getSerList(o){
	var allvalue="";
    if(typeof(o)=="undefined"){return "";}
	if (typeof(o.length)=="undefined"){
		if(o.value!=''){return o.value+ ",";}else{return "";}
	}
	for(var i=0;i<o.length;i++){
		if(o[i].value!=''){
			allvalue +=o[i].value+";"+sercat[i][0]+";"+sercat[i][1]+";"+sercat[i][2]+";"+sercat[i][3]+"|";
		}
	}
	return allvalue;
}



function checkStep5() {
    if(!document.BookingStep5.accept.checked){
        alert("Please read and accept the Terms & Conditions ! \n");
        return false;
   }
}

function GuestPaymentCheck() {
     if (trim(document.GuestPayment.CardHolder.value)==""){
        alert("Enter the name on card, Please ! \n");
        return false;
        }else{if(!CheckCardNumber(document.GuestPayment)){
            return false;
            }else{if(!document.GuestPayment.accept.checked){
                alert("Please read and accept the Terms & Conditions ! \n");
                return false;
            }
        }
     }
}

function UpdateCheck() {
     if (trim(document.BookingUpdate.CustomerName.value)==""){
        alert("Enter Cuest Name, Please ! \n");
        return false;
     }else{ if (!checkEmail(document.BookingUpdate.Email.value)){
                alert("Invalid Email! \n");
                return false;
            }else{return true}
     }
}

function EnquiryCheck() {
     if (trim(document.contactform.txtname.value)==""){
        alert("Enter Your Name, Please ! \n");
        return false;
     }else{ if (!checkEmail(document.contactform.txtemail.value)){
                alert("Invalid Email! \n");
                return false;
            }else{if(document.contactform.txtemail.value!=document.contactform.ConfirmEmail.value){
                    alert("Check youe email, please \n");
                    return false;
                    }else{if (document.contactform.Enquiry.value==""){
                     alert("Enter your enquiry, Please ! \n");
                     return false;
                    }else{ return true}
                }
            }
     }
}

function RoomRequestCheck() {
     if (trim(document.EnquiryForm.CustomerName.value)==""){
        alert("Enter Your Name, Please ! \n");
        return false;
     }else{ if (!checkEmail(document.EnquiryForm.Email.value)){
                alert("Invalid Email! \n");
                return false;
            }else{if(document.EnquiryForm.Email.value!=document.EnquiryForm.ConfirmEmail.value){
                    alert("Check youe email, please \n");
                    return false;
                    }else{if (document.EnquiryForm.Enquiry.value==""){
                     alert("Enter your enquiry, Please ! \n");
                     return false;
                    }else{ return true}
                }
            }
     }
}

function EnquiryReplyCheck() {
     if (trim(document.EnquiryReply.ReplyDetail.value)==""){
        alert("Enter Detail, Please ! \n");
        return false;
     }else{if(confirm('Are you sure want to Reply Enquiry ?\n\n It will automatic send email to customer.')){return true;
          }else{return false;}
     }
}

function CheckPWD() {
     if (document.ChangePWDNow.PWD.value.length<6){
        alert("Password must be longer then 6 characters! \n");
        return false;
     }else{ if (document.ChangePWDNow.PWD.value!=document.ChangePWDNow.RePWD.value){
                alert("Check Your Password, please! \n");
                return false;
            }else{ return true}
     }
}

function CompanyCheck() {
     if (trim(document.EditCompany.CompanyName.value)==""){
        alert("Enter Company Name, Please ! \n");
        return false;
     }else{ if (!checkEmail(document.EditCompany.CEMail.value)){
                alert("Invalid Email! \n");
                return false;
            }else{return true}
     }
}

function removeLeadingZeros(str)
{
  while (str.charAt(0) == '0')
    str = str.substr(1);
  return str;
}


function ltrim(s){
 return s.replace( /^\s*/, "");
}

function rtrim(s){
 return s.replace( /\s*$/, "");
}

function trim(s){
 return rtrim(ltrim(s));
}

function IsIP(val)
{
reg=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
var r = val.match(reg);
if(r==null) return false;
if(r[1]<256 && r[2] <256 && r[3]<256 && r[4]<256){ return true;
    }else{return false}
}

function IPCheck() {
    if (!IsIP(document.EditIPRange.IP_S_Text.value)){
       alert("Invalid Start IP Address! \n");
       return false;
    }else{if (!IsIP(document.EditIPRange.IP_E_Text.value)){
             alert("Invalid End IP Address! \n");
             return false;
            }
    }
}

function textCounter(field,counter,maxlimit) {
	var charcnt = field.value.length;
	// trim the extra text
	if (charcnt > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
	else {
    var charleft = maxlimit - charcnt ;
   	document.getElementById(counter).innerHTML="Number of characters left: "+charleft
	}
}

function checkCCard() {
    if (trim(document.SendCard.CardHolder.value)==""){
        alert("Enter the name on card, Please ! \n");
            return false;
        }else{if(!CheckCardNumber(document.SendCard)){
            return false;
            }else{if(!document.SendCard.declare.checked){
                alert("Please declare your credit card details ! \n");
                return false;
            }
        }
   }
}

