﻿function RequiredFormField(source,arguments){
    var field = Trim(arguments.Value);
    var txt = $get(source.controltovalidate);

    if (field.length > 0){
        txt.style.backgroundColor = 'white';
        arguments.IsValid = true;
    }
    else{
        txt.style.backgroundColor = '#ffcccc';
        arguments.IsValid = false;
    }
}

function RequiredFormFieldDate(source,arguments){
    var field = Trim(arguments.Value);
    var txt = $get(source.controltovalidate);
    
    if (field.length > 0 && ValidateDate(field, 'DMY')){
        txt.style.backgroundColor = 'white';
        arguments.IsValid = true;
    }
    else{
        txt.style.backgroundColor = '#ffcccc';
        arguments.IsValid = false;
    }
}

function RequiredFormFieldDateCompare(source,arguments){
    var field = Trim(arguments.Value);
    var txt = $get(source.controltovalidate);
    
    var txt2 = $get(source.controltovalidate.replace('CheckOut','CheckIn'));
    var field2 = txt2.value;
    
    if (StringToDateUTF(field) > StringToDateUTF(field2)){
        txt.style.backgroundColor = 'white';
        arguments.IsValid = true;
    } else {
        txt.style.backgroundColor = '#ffcccc';
        arguments.IsValid = false;
    }
}

function ValidateEmail(source,arguments) {
    var email = Trim(arguments.Value);
    var txt = $get(source.controltovalidate);
    
    if (ValidateEmailRE(email)) {
        txt.style.backgroundColor = 'white';
        arguments.IsValid = true;
    } else {
        txt.style.backgroundColor = '#ffcccc';
        arguments.IsValid = false;
    }
}

function ValidatePassword(source,arguments) {
    var pass = Trim(arguments.Value);
    var txt = $get(source.controltovalidate);
    
    if (pass.length > 0 && pass.length < 21) {
        txt.style.backgroundColor = 'white';
        arguments.IsValid = true;
    } else {
        txt.style.backgroundColor = '#ffcccc';
        arguments.IsValid = false;
    }
}

function CompareEmail(source,arguments) {
    var id = source.controltovalidate;
    var id2 = id.replace('Confirm','');
    var txt = $get(id);
    var txt2 = $get(id2);
    
    if (txt.value == txt2.value){
        if (ValidateEmailRE(txt.value)) {
            txt.style.backgroundColor = 'white';
            arguments.IsValid = true;
        }
        else{
            txt.style.backgroundColor = '#ffcccc';
            arguments.IsValid = true;
        }
    }
    else{
        txt.style.backgroundColor = '#ffcccc';
        arguments.IsValid = false;
    }
}

function HotelMinFields(source,arguments){
    var id = source.controltovalidate;
    var id2 = id.replace('txtCity','ddlStates');
    
    var txtCity = $get(id);
    var ddlStates = $get(id2);
    
    if (Trim(txtCity.value).length == 0 && Trim(ddlStates.value).length == 0){
        txtCity.style.backgroundColor = '#ffcccc';
        ddlStates.style.backgroundColor = '#ffcccc';
        arguments.IsValid = false;
    }
    else
    {
        txtCity.style.backgroundColor = 'white';
        ddlStates.style.backgroundColor = 'white';
        arguments.IsValid = true;
    }
    
}

function ValidatePhoneType(source,arguments) {
    var id = source.controltovalidate;
    var id2 = id.replace('ddl','txt');
    var txt = $get(id);
    var txt2 = $get(id2);
    
    if (Trim(txt2.value).length == 0){
        txt.style.backgroundColor = 'white';
        arguments.IsValid = true;
    }
    else{
        if (Trim(txt.value).length > 0){
            txt.style.backgroundColor = 'white';
            arguments.IsValid = true;
        }
        else{
            txt.style.backgroundColor = '#ffcccc';
            arguments.IsValid = false;
        }
    }
}

function ValidateCreditCard() {
    var table = $get('tableCards');
    var card = GetSelectedRadio();
    if (card == '') {
        table.style.backgroundColor = '#ffcccc';
    }
    else {
        table.style.backgroundColor = 'white';
    }
}

function ValidateEmailRE(email)
{
    var reex = "^[\\w-]+(?:\\.[\\w-]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7}$";
    var re = new RegExp(reex);
    return email.match(re);
}

function Trim(str){return str.replace(/^\s+|\s+$/g,"");}

function ShowVoucher(i,p,h){
    window.open(h + "Voucher.aspx?i=" + i + "&p=" + p);
    return false;
}

function DateToString(date)
{
    var d = date.getDate();
    var m = date.getMonth() + 1;
    var y = date.getFullYear();
    
    var dstr = '';
    var mstr = '';
    
    if (d < 10) {
        dstr = '0' + d;
    }
    else {
        dstr = d;
    }
        
    if (m < 10){
        mstr = '0' + m;
    }
    else {
        mstr = m;
    }
        
    return dstr + "/" + mstr + "/" + y;
}
        
function StringToDate(stringDate)
{
    var date = stringDate.split("/");
    return new Date(date[2], (date[1] - 1), date[0]);
}

function StringToDateUTF(stringDate)
{
    var date = stringDate.split("/");
    return date[2] + (date[1]) + date[0];
}

function ValidateDate(dateStr, format)
{
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
   } else { // The year must be third
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
   }
   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}

function OpenWindow(url,title, width, height)
{
    window.open(url, title, "status=0,menubar=0,resizable=0,width=" + width + ",height=" + height);
    return false;
}