var first = 0;

function add_option(listname,showtxt,passvalue) 
{
    listname.length++;
    listname.options[listname.length-1].value = passvalue;
    listname.options[listname.length-1].text = showtxt;
}

function setDefaultYear(){
    aForm = document.getElementById("search");
    bForm = document.getElementById("activities");
    if(aForm){
        var now = new Date();
        now.setDate(now.getDate()+3);
        aForm.aday.selectedIndex = now.getDate();
        aForm.amonth.selectedIndex = now.getMonth()+1;
        aForm.ayear.selectedIndex = 1;
    }
    if (bForm){
        var now = new Date();
        //now.setDate(now.getDate()+2);
        bForm.aday.selectedIndex = now.getDate();
        bForm.amonth.selectedIndex = now.getMonth()+1;
        bForm.ayear.selectedIndex = 1;
    }
}

function isEnter(e){
    var keynum;
    if(window.event) // IE
        keynum = e.keyCode;
    else if(e.which) // Netscape/Firefox/Opera
        keynum = e.which;
    if(keynum == 13)
        return true;
    return false;
}

function act_getStay() {
    aForm = document.getElementById("activities");
    if(aForm){
        if((aForm.aday.value != "Select") && (aForm.amonth.value != "Select") && (aForm.ayear.value != "Select")) {
            var m, d, y, tmp, t, ma, da,mplusone;
            d = new Number(aForm.aday.value);
            m = new Number(aForm.amonth.value);
            y = new Number(aForm.ayear.value);
            if(d < 10) {
                aForm.act_date.value = "0" + d + "/";
            }
            else {
                aForm.act_date.value = d + "/";
            }
            if(m < 9) {
                aForm.act_date.value += "0" + (m+1) + "/" + y;
            }
            else {
                aForm.act_date.value += (m+1) + "/" + y;
            }
            ok = isNaN(y);
            if (ok) {
                alert("Date must be in format dd/mm/yyyy");
                aForm.act_date.value = "";
            }
            else {
                newDate = new Date(y, m , d);
                newDate.setDate((newDate.getDate()+num));
                var mt = newDate.getMonth()+1;
                var dt = newDate.getDate();
                var yr = newDate.getFullYear();
                if(mt < 10) mt = "0" + mt;
                if(dt < 10) dt = "0" + dt;
			
                aForm.aday.selectedIndex=dt-1;
                aForm.amonth.selectedIndex=mt-1;
                for(i=0;i<aForm.dyear.length;i++){
                    if(aForm.ayear.options[i].value==yr){
                        aForm.ayear.selectedIndex=i;
                        break;
                    }
                }
            }
        }
    }
}

function getStay() {
    aForm = document.getElementById("search");
    if(aForm){
        if ((aForm.dur.value == "nights") || (aForm.dur.value == "Select")) {
            aForm.ddate.value == "";
        } else{
            if((aForm.aday.value != "Select") && (aForm.amonth.value != "Select") && (aForm.ayear.value != "Select")) {
                var num = new Number(aForm.dur.value);
                var m, d, y, tmp, t, ma, da,mplusone;
                d = new Number(aForm.aday.value);
                m = new Number(aForm.amonth.value);
                y = new Number(aForm.ayear.value);
                if(d < 10) {
                    aForm.adate.value = "0" + d + "/";
                }
                else {
                    aForm.adate.value = d + "/";
                }
                if(m < 9) {
                    aForm.adate.value += "0" + (m+1) + "/" + y;
                }
                else {
                    aForm.adate.value += (m+1) + "/" + y;
                }
                ok = isNaN(y);
                if (ok) {
                    alert("Date must be in format dd/mm/yyyy");
                    aForm.adate.value = "";
                } else {
                    if (num !== "nights") {
                        newDate = new Date(y, m , d);
                        newDate.setDate((newDate.getDate()+num));
                        var mt = newDate.getMonth()+1;
                        var dt = newDate.getDate();
                        var yr = newDate.getFullYear();
                        if(mt < 10) mt = "0" + mt;
                        if(dt < 10) dt = "0" + dt;
                        aForm.ddate.value = dt + "/" + mt + "/" + yr;
                        if(aForm.dday && aForm.dmonth && aForm.dyear){
                            aForm.dday.selectedIndex=dt-1;
                            aForm.dmonth.selectedIndex=mt-1;
                            for(i=0;i<aForm.dyear.length;i++){
                                if(aForm.dyear.options[i].value==yr){
                                    aForm.dyear.selectedIndex=i;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

function messages(mess) {
    if (mess == "x") message.innerText = ".:Studios92.com Ltd Secure Online Booking Form:.";
    else message.innerText = mess;
}


function messages(mess) {
    if (mess == "x") message.innerText = ".:Studios92.com Ltd Secure Online Booking Form:.";
    else message.innerText = mess;
}
function checkField(evt) {
    if(evt.value == "") {
        message.innerText = "!! You cannot leave this field blank !!";
        evt.style.backgroundColor = "mistyrose";
    } else {
        message.innerText = ".:Studios92.com Ltd Secure Online Booking Form:.";
        evt.style.backgroundColor = "aliceblue";
    }
}
function validate() {
    aForm = document.getElementById("search");
    if (aForm.acctype && aForm.acctype.value == "Select") {
        alert("You did not select an accommodation type!");
    } else if (aForm.location && aForm.location.value == "Location") {
        alert("You did not select a location");
    } else if (aForm.ddate.value == "") {
        alert("Your departure date has unselected values, please check it carefully");
    } else if (!validateDate()){
        alert("Please select at least 3 days ahead for your arrival date!")
    } else if (!checkDateExist()) {
        alert("Arrival date does not exist !");
    } else if (aForm.dur.value == "nights") {
        alert("You did not select your length of stay!");
    } else if (!emptyRoom()) {
        alert("You tryed to book a room with no one in!");
    } else {
        var xMax = screen.width, yMax = screen.height;
        var xOffset = (xMax - 270)/2, yOffset = (yMax - 70)/2;
        //window.open('popups/finding.htm','finding','width=270,height=70,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');

        aForm.submit();
    //finding.focus();
    }
}

function emptyRoom(){
var str = document.getElementById('rms').options[document.getElementById('rms').selectedIndex].value;
    switch(str)
    {
        case "1":
            aForm = document.getElementById("search");
            if((document.getElementById('adt1').options[document.getElementById('adt1').selectedIndex].value != "0") || (document.getElementById('tng1').options[document.getElementById('tng1').selectedIndex].value != "0") || (document.getElementById('chd1').options[document.getElementById('chd1').selectedIndex].value != "0")|| (document.getElementById('inf1').options[document.getElementById('inf1').selectedIndex].value != "0")) {
                 return true;
            }
            else
                 return false;
            break;
        case "2":
            aForm = document.getElementById("search");
            if((document.getElementById('adt1').options[document.getElementById('adt1').selectedIndex].value != "0") || (document.getElementById('tng1').options[document.getElementById('tng1').selectedIndex].value != "0") || (document.getElementById('chd1').options[document.getElementById('chd1').selectedIndex].value != "0")|| (document.getElementById('inf1').options[document.getElementById('inf1').selectedIndex].value != "0")) {
                if((document.getElementById('adt2').options[document.getElementById('adt2').selectedIndex].value != "0") || (document.getElementById('tng2').options[document.getElementById('tng2').selectedIndex].value != "0") || (document.getElementById('chd2').options[document.getElementById('chd2').selectedIndex].value != "0")|| (document.getElementById('inf2').options[document.getElementById('inf2').selectedIndex].value != "0")) {
                      return true;
                }
                else
                      return false;
            }
            else
                 return false;
            break;
        default:
            aForm = document.getElementById("search");
            if((document.getElementById('adt1').options[document.getElementById('adt1').selectedIndex].value != "0") || (document.getElementById('tng1').options[document.getElementById('tng1').selectedIndex].value != "0") || (document.getElementById('chd1').options[document.getElementById('chd1').selectedIndex].value != "0")|| (document.getElementById('inf1').options[document.getElementById('inf1').selectedIndex].value != "0")) {
                if((document.getElementById('adt2').options[document.getElementById('adt2').selectedIndex].value != "0") || (document.getElementById('tng2').options[document.getElementById('tng2').selectedIndex].value != "0") || (document.getElementById('chd2').options[document.getElementById('chd2').selectedIndex].value != "0")|| (document.getElementById('inf2').options[document.getElementById('inf2').selectedIndex].value != "0")) {
                    if((document.getElementById('adt2').options[document.getElementById('adt2').selectedIndex].value != "0") || (document.getElementById('tng2').options[document.getElementById('tng2').selectedIndex].value != "0") || (document.getElementById('chd2').options[document.getElementById('chd2').selectedIndex].value != "0")|| (document.getElementById('inf2').options[document.getElementById('inf2').selectedIndex].value != "0")) {
                          return true;
                    }
                    else
                          return false;
                }
                else
                      return false;
            }
            else
                 return false;
            break;
    }
}
function condition() {
    aForm = document.getElementById("search");
    if ((aForm.dur.value < 3) && (aForm.amonth.value == 11) && ((aForm.aday.value == 29) || (aForm.aday.value == 30) || (aForm.aday.value == 31))){
        alert("You have to stay at least 3 nights if you are planning to arrive on 29th, 30th or 31st of December.");
    }else {
        validate();
    }
}

function fillstay() {
    aForm = document.getElementById("search");
    if(aForm){
        if(aForm.dur){
            var it = aForm.dur.selectedIndex;
            aForm.ddate.value = "";
            for(i=aForm.dur.options.length; i >= 0; i--) {
                aForm.dur.options[i] = null;
            }
        }
        for(i=1; i< 151; i++) {
            add_option(aForm.dur,i,i);
        }
        if(aForm.acctype){
            if (aForm.acctype.value == "Studios") {
                //CHANGE 7 IF YOU WANT TO CHANGE THE NUMBER MINIMUM OF NIGTHS TO BOOK A FLAT
                getLoc("fs");
                //CHANGE 7 IF YOU WANT TO CHANGE THE NUMBER MINIMUM OF NIGTHS TO BOOK A FLAT
                if(it > 1) {
                    aForm.dur.selectedIndex = it
                } else {
                    aForm.dur.selectedIndex = 1;
                }
            } else {
                if (aForm.acctype.value == "All") getLoc("all");
                if (aForm.acctype.value == "Hotel / Bed and Breakfast / Guest House") getLoc("hb");
                if (aForm.acctype.value == "Luxury Hotels") getLoc("lh");
                if (aForm.acctype.value == "Youth Hostel") getLoc("h");
                if (aForm.acctype.value == "Apartments") getLoc("a");
                if (aForm.acctype.value == "Campsites / Caravans / Resorts") getLoc("c");
                if (aForm.acctype.value == "YHA / Youth hostels Association") getLoc("yh");
                aForm.dur.selectedIndex = it;
            }
        }
    }
}

function compare(date_1, date_2){
    diff = date_1.getTime()-date_2.getTime();
    return (diff==0?diff:diff/Math.abs(diff));
}

function validateDate(){
    aForm = document.getElementById("search");
    if((aForm.aday.value != "Select") && (aForm.amonth.value != "Select") && (aForm.ayear.value != "Select")) {
        var day = aForm.aday.value;
        var month = aForm.amonth.value;
        var year = aForm.ayear.value;
        var arrivalDate = new Date(year,month,day);
        arrivalDate.setDate(arrivalDate.getDate()-1);
        if (compare(arrivalDate,new Date()) < 0)
            return false;
        else
            return true;
    }else
        return false;
}

function checkDateExist() {
    aForm = document.getElementById("search");

    //specific check if february is selected (number 1)
    if (aForm.amonth.value == 1) {
        if ( ((aForm.ayear.value % 4) == 0) && (aForm.aday.value > 29) ) {  //if a year where february has 29 days
            return false;
        } else if (aForm.aday.value > 28) {   //normal year
            return false;
        } else {	//other days of the month
            return true;
        }
    }

    //check if month between January and July
    if (aForm.amonth.value <= 6) {
        if ((aForm.aday.value == 31) && ((aForm.amonth.value % 2) != 0)) {  //months which have even have 30 days
            return false;
        } else {
            return true;
        }
    } else if (aForm.amonth.value > 6) {
        if ((aForm.aday.value == 31) && ((aForm.amonth.value % 2) == 0) ) {	//months which have odd have 30 days
            if (aForm.amonth.value != 7) { //specific case of august
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }
}

function openWin(url,name){
    window.open(url,name,"");
}

function popUp(url,name,width,height){
    if(width!=true)
        width=680;
    if(height!=true)
        height=650;
    window.open(url,name,'resizable=yes,scrollbars=yes,width='+width+',height='+height);
}
function display(){
    var str = document.getElementById('rms').options[document.getElementById('rms').selectedIndex].value;
    switch(str)
    {
        case "1":
            document.getElementById('r1').style.display="block";
            document.getElementById('r2').style.display="none";
            document.getElementById('r3').style.display="none";
            break;
        case "2":
            document.getElementById('r1').style.display="block";
            document.getElementById('r2').style.display="block";
            document.getElementById('r3').style.display="none";
            break;
        default:
            document.getElementById('r1').style.display="block";
            document.getElementById('r2').style.display="block";
            document.getElementById('r3').style.display="block";
            break;
    }
}

function sel(ths, at,dur) {
    var xMax = screen.width, yMax = screen.height;
    var xOffset = (xMax - 270)/2, yOffset = (yMax - 70)/2;
    if (((at == "92Z2") || (at == "BU3") || (at == "GA141") || (at == "HA7") || (at == "MA29")|| (at == "MH2") || (at == "MI107") || (at == "NO98") || (at == "SH55") || (at == "SH57") || (at == "PR104")) && (dur < 7)) {
        alert("This option has a minimum stay of 7 nights. \nPlease click back and change your number of nights!");//memo giuliano
    } else {
        document.getElementById('booking').aut.value=at;
        document.getElementById('booking').name.value=ths;
        //window.open("viewDetails.asp?aut=" + at + "&name=" + ths + "&pax=<%=pax%>&a=<%=a%>&d=<%=d%>&stay=<%=stay%>&search=yes&refered=<%=fr%>", "_self");
        document.getElementById('booking').submit();
        window.open('popups/waiting.asp?field=2','waiting','width=270,height=70,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');
    }
}

function getday() {
    aForm = document.getElementById("search");
    if(aForm){
        var m, d, y;
        d = new Number(aForm.aday.value);
        m = new Number(aForm.amonth.value);
        y = new Number(aForm.ayear.value);
        if(d < 10) {
            aForm.adate.value = "0" + d + "/";
        }
        else {
            aForm.adate.value = d + "/";
        }
        if(m < 9) {
            aForm.adate.value += "0" + (m+1) + "/" + y;
        }
        else {
            aForm.adate.value += (m+1) + "/" + y;
        }
        ok = isNaN(y);
        if (ok) {
            alert("Date must be in format dd/mm/yyyy");
            aForm.adate.value = "";
        } else {
                   
            newDate = new Date(y, m , d);
            var mt = newDate.getMonth()+1;
            var dt = newDate.getDate();
            var yr = newDate.getFullYear();
            if(mt < 10) mt = "0" + mt;
            if(dt < 10) dt = "0" + dt;
            aForm.fr.value = dt + "/" + mt + "/" + yr;
            if(aForm.day && aForm.month && aForm.year){
                aForm.day.selectedIndex=dt-1;
                aForm.month.selectedIndex=mt-1;
                for(i=0;i<aForm.year.length;i++){
                    if(aForm.year.options[i].value==yr){
                        aForm.year.selectedIndex=i;
                        break;
                               
                    }
                }
            }
        }
    }
}

function getPers(elem1) {
    aForm = document.getElementById("search");
    if(aForm){
        var adt1,adt2,adt3,tng1,tng2,tng3,chd1,chd2,chd3,inf1,inf2,inf3,rm;
        adt1 = new Number(aForm.adt1.value);
        adt2 = new Number(aForm.adt2.value);
        adt3 = new Number(aForm.adt3.value);
        tng1 = new Number(aForm.tng1.value);
        tng2 = new Number(aForm.tng2.value);
        tng3 = new Number(aForm.tng3.value);
        chd1 = new Number(aForm.chd1.value);
        chd2 = new Number(aForm.chd2.value);
        chd3 = new Number(aForm.chd3.value);
        inf1 = new Number(aForm.inf1.value);
        inf2 = new Number(aForm.inf2.value);
        inf3 = new Number(aForm.inf3.value);
        rm= document.getElementById('rms').options[document.getElementById('rms').selectedIndex].value;
        switch(rm)
        {
            case "1":
                aForm.adt.value = adt1;
                aForm.tng.value = tng1;
                aForm.chd.value = chd1;
                aForm.inf.value = inf1;
                break;
            case "2":
                aForm.adt.disabled;
                aForm.tng.disabled;
                aForm.chd.disabled;
                aForm.inf.disabled;
                break;
            case "3":
                aForm.adt.disabled;
                aForm.tng.disabled;
                aForm.chd.disabled;
                aForm.inf.disabled;
                break;
            default:
                aForm.adt.value = adt1;
                aForm.tng.value = tng1;
                aForm.chd.value = chd1;
                aForm.inf.value = inf1;
        }
    }
}
function reset(){
    aForm = document.getElementById("search");
    aForm.rms.value=1;
}
window.onload = function(){
    reset();
    setDefaultYear();
    fillstay();
    getStay();
    act_getStay();
    getday();
    getPers();
};


