var first = 0;

function add_optioning(listname,showtxt,passvalue) 
{
    listname.length++;
    listname.options[listname.length-1].value = passvalue;
    listname.options[listname.length-1].text = showtxt;
}

function setDefaultYearing(){
    aForming = document.getElementById("searching");
    bForm = document.getElementById("activitiesing");
    if(aForming){
        var now = new Date();
        now.setDate(now.getDate()+1);
        aForming.aday.selectedIndex = now.getDate();
        aForming.amonth.selectedIndex = now.getMonth()+1;
        aForming.ayear.selectedIndex = 1;
    }
    if (bForm){
        var now = new Date();
        //now.setDateing(now.getDate()+2);
        bForm.aday.selectedIndex = now.getDate();
        bForm.amonth.selectedIndex = now.getMonth()+1;
        bForm.ayear.selectedIndex = 1;
    }
}

function isEntering(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_getStaying() {
    aForming = document.getElementById("activitiesing");
    if(aForming){
        if((aForming.aday.value != "Selecting") && (aForming.amonthing.value != "Selecting") && (aForming.ayearing.value != "Selecting")) {
            var m, d, y, tmp, t, ma, da,mplusone;
            d = new Number(aForming.aday.value);
            m = new Number(aForming.amonth.value);
            y = new Number(aForming.ayear.value);
            if(d < 10) {
                aForming.act_date.value = "0" + d + "/";
            }
            else {
                aForming.act_date.value = d + "/";
            }
            if(m < 9) {
                aForming.act_date.value += "0" + (m+1) + "/" + y;
            }
            else {
                aForming.act_date.value += (m+1) + "/" + y;
            }
            ok = isNaN(y);
            if (ok) {
                //alert("Date must be in format dd/mm/yyyy");
                aForming.act_date.value = "";
            }
            else {
                var 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;
			
                aForming.aday.selectedIndex=dt-1;
                aForming.amonth.selectedIndex=mt-1;
                for(i=0;i<aForming.dyear.length;i++){
                    if(aForming.ayear.options[i].value==yr){
                        aForming.ayear.selectedIndex=i;
                        break;
                    }
                }
            }
        }
    }
}

function getStaying() {
    aForming = document.getElementById("searching");
    if(aForming){
        if ((aForming.dur.value == "nightsing") || (aForming.dur.value == "Selecting")) {
            aForming.ddate.value == "";
        } else{
            if((aForming.aday.value != "Selecting") && (aForming.amonthing.value != "Selecting") && (aForming.ayear.value != "Selecting")) {
                var num = new Number(aForming.dur.value);
                var m, d, y, tmp, t, ma, da,mplusone;
                d = new Number(aForming.aday.value);
                m = new Number(aForming.amonth.value);
                y = new Number(aForming.ayear.value);
                if(d < 10) {
                    aForming.adate.value = "0" + d + "/";
                }
                else {
                    aForming.adate.value = d + "/";
                }
                if(m < 9) {
                    aForming.adate.value += "0" + (m+1) + "/" + y;
                }
                else {
                    aForming.adate.value += (m+1) + "/" + y;
                }
                ok = isNaN(y);
                if (ok) {
                    //alert("Date must be in format dd/mm/yyyy");
                    aForming.adate.value = "";
                } else {
                    if (num !== "nights") {
                        var 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;
                        aForming.ddate.value = dt + "/" + mt + "/" + yr;
                        if(aForming.dday && aForming.dmonth && aForming.dyear){
                            aForming.dday.selectedIndex=dt-7;
                            aForming.dmonth.selectedIndex=mt-1;
                            for(i=0;i<aForming.dyear.length;i++){
                                if(aForming.dyear.options[i].value==yr){
                                    aForming.dyear.selectedIndex=i;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
flatoption();
}

function messagesing(mess) {
    if (mess == "x") message.innerText = ".:Studios92.com Ltd Secure Online Booking Form:.";
    else message.innerText = mess;
}


function messagesing(mess) {
    if (mess == "x") message.innerText = ".:Studios92.com Ltd Secure Online Booking Form:.";
    else message.innerText = mess;
}
function checkFielding(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 validateing() {
    aForming = document.getElementById("searching");
   /* if (aForming.acctype && aForming.acctype.value == "Selecting") {
        alert("You did not select an accommodation type!");
    } else if (aForming.location && aForming.location.value == "Locationing") {
        alert("You did not select a location");
    } else if (aForming.ddate.value == "") {
        alert("Your departure date has unselected values, please check it carefully");
    } else if (!validateDateing()){
        alert("Please select at least 1 day ahead for your arrival date!")
    } else */if (!checkDateExisting()) {
        alert("Arrival date does not exist !");
    }else if (!validatedate()) {
        alert("The arrival date should be after the current date.\nPlease check your selection.!");
    } else if (aForming.dur.value == "nights") {
        alert("You did not select your length of stay!");
    } else if (!emptyRooming()) {
        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+'');

        aForming.submit();
    //finding.focus();
    }
}

function emptyRooming(){
var str = document.getElementById('rmsing').options[document.getElementById('rmsing').selectedIndex].value;
    switch(str)
    {
        case "1":
            aForming = document.getElementById("searching");
            if((document.getElementById('adt1ing').options[document.getElementById('adt1ing').selectedIndex].value != "0") || (document.getElementById('tng1ing').options[document.getElementById('tng1ing').selectedIndex].value != "0") || (document.getElementById('chd1ing').options[document.getElementById('chd1ing').selectedIndex].value != "0")|| (document.getElementById('inf1ing').options[document.getElementById('inf1ing').selectedIndex].value != "0")) {
                 return true;
            }
            else
                 return false;
            break;
        case "2":
            aForming = document.getElementById("searching");
            if((document.getElementById('adt1ing').options[document.getElementById('adt1ing').selectedIndex].value != "0") || (document.getElementById('tng1ing').options[document.getElementById('tng1ing').selectedIndex].value != "0") || (document.getElementById('chd1ing').options[document.getElementById('chd1ing').selectedIndex].value != "0")|| (document.getElementById('inf1ing').options[document.getElementById('inf1ing').selectedIndex].value != "0")) {
                if((document.getElementById('adt2ing').options[document.getElementById('adt2ing').selectedIndex].value != "0") || (document.getElementById('tng2ing').options[document.getElementById('tng2ing').selectedIndex].value != "0") || (document.getElementById('chd2ing').options[document.getElementById('chd2ing').selectedIndex].value != "0")|| (document.getElementById('inf2ing').options[document.getElementById('inf2ing').selectedIndex].value != "0")) {
                      return true;
                }
                else
                      return false;
            }
            else
                 return false;
            break;
        default:
            aForming = document.getElementById("searching");
            if((document.getElementById('adt1ing').options[document.getElementById('adt1ing').selectedIndex].value != "0") || (document.getElementById('tng1ing').options[document.getElementById('tng1ing').selectedIndex].value != "0") || (document.getElementById('chd1ing').options[document.getElementById('chd1ing').selectedIndex].value != "0")|| (document.getElementById('inf1ing').options[document.getElementById('inf1ing').selectedIndex].value != "0")) {
                if((document.getElementById('adt2ing').options[document.getElementById('adt2ing').selectedIndex].value != "0") || (document.getElementById('tng2ing').options[document.getElementById('tng2ing').selectedIndex].value != "0") || (document.getElementById('chd2ing').options[document.getElementById('chd2ing').selectedIndex].value != "0")|| (document.getElementById('inf2ing').options[document.getElementById('inf2ing').selectedIndex].value != "0")) {
                    if((document.getElementById('adt2ing').options[document.getElementById('adt2ing').selectedIndex].value != "0") || (document.getElementById('tng2ing').options[document.getElementById('tng2ing').selectedIndex].value != "0") || (document.getElementById('chd2ing').options[document.getElementById('chd2ing').selectedIndex].value != "0")|| (document.getElementById('inf2ing').options[document.getElementById('inf2ing').selectedIndex].value != "0")) {
                          return true;
                    }
                    else
                          return false;
                }
                else
                      return false;
            }
            else
                 return false;
            break;
    }
}
function conditioning() {
    aForming = document.getElementById("searching");
    if ((aForming.dur.value < 7) && (aForming.amonth.value == 11) && ((aForming.aday.value == 29) || (aForming.aday.value == 30) || (aForming.aday.value == 31))){
        alert("You have to stay at least 7 nights if you are planning to arrive on 29th, 30th or 31st of December.");
    }else {
        validateing();
    }
}

function fillstaying() {
    aForming = document.getElementById("searching");
    if(aForming){
        if(aForming.dur){
            var it = aForming.dur.selectedIndex;
            aForming.ddate.value = "";
            for(i=aForming.dur.options.length; i >= 0; i--) {
                aForming.dur.options[i] = null;
            }
        }
        for(i=7; i< 151; i++) {
            add_option(aForming.dur,i,i);
        }
        if(aForming.acctype){
            if (aForming.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 > 7) {
                    aForming.dur.selectedIndex = it
                } else {
                    aForming.dur.selectedIndex = 7;
                }
            } else {
                if (aForming.acctype.value == "All") getLoc("all");
                if (aForming.acctype.value == "Hotel / Bed and Breakfast / Guest House") getLoc("hb");
                if (aForming.acctype.value == "Luxury Hotels") getLoc("lh");
                if (aForming.acctype.value == "Youth Hostel") getLoc("h");
                if (aForming.acctype.value == "Apartments") getLoc("a");
                if (aForming.acctype.value == "Campsites / Caravans / Resorts") getLoc("c");
                if (aForming.acctype.value == "YHA / Youth hostels Association") getLoc("yh");
                aForming.dur.selectedIndex = it;
            }
        }
    }
}

function compareing(date_1, date_2){
    diff = date_1.getTime()-date_2.getTime();
    return (diff==0?diff:diff/Math.abs(diff));
}

function validateDateing(){
    aForming = document.getElementById("searching");
    if((aForming.aday.value != "Selecting") && (aForming.amonth.value != "Selecting") && (aForming.ayear.value != "Selecting")) {
        var day = aForming.aday.value;
        var month = aForming.amonth.value;
        var year = aForming.ayear.value;
        var arrivalDate = new Date(year,month,day);
        if (compare(arrivalDate,new Date()) < 0)
            return false;
        else
            return true;
    }else
        return false;
}

function checkDateExisting() {
    aForming = document.getElementById("searching");

    //specific check if february is selected (number 1)
    if (aForming.amonth.value == 1) {
        if ( ((aForming.ayear.value % 4) == 0) && (aForming.aday.value > 29) ) {  //if a year where february has 29 days
            return false;
        } else if (aForming.aday.value > 28) {   //normal year
            return false;
        } else {	//other days of the month
            return true;
        }
    }

    //check if month between January and July
    if (aForming.amonth.value <= 6) {
        if ((aForming.aday.value == 31) && ((aForming.amonth.value % 2) != 0)) {  //months which have even have 30 days
            return false;
        } else {
            return true;
        }
    } else if (aForming.amonth.value > 6) {
        if ((aForming.aday.value == 31) && ((aForming.amonth.value % 2) == 0) ) {	//months which have odd have 30 days
            if (aForming.amonth.value != 7) { //specific case of august
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }
}

function openWining(url,name){
    window.open(url,name,"");
}

function popUping(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 displaying(){
    var str = document.getElementById('rmsing').options[document.getElementById('rmsing').selectedIndex].value;
    switch(str)
    {
        case "1":
            document.getElementById('r1ing').style.display="block";
            document.getElementById('r2ing').style.display="none";
            document.getElementById('r3ing').style.display="none";
            document.getElementById("display_position").style.marginTop="-997px"
            break;
        case "2":
            document.getElementById('r1ing').style.display="block";
            document.getElementById('r2ing').style.display="block";
            document.getElementById('r3ing').style.display="none";
            document.getElementById("display_position").style.marginTop="-1099px"
            break;
        default:
            document.getElementById('r1ing').style.display="block";
            document.getElementById('r2ing').style.display="block";
            document.getElementById('r3ing').style.display="block";
            document.getElementById("display_position").style.marginTop="-1202px"
            break;
    }
}

function seling(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('bookinging').aut.value=at;
        document.getElementById('bookinging').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('bookinging').submit();
        window.open('popups/waiting.asp?field=2','waiting','width=270,height=70,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');
    }
}

function getdaying() {


    aForming = document.getElementById("searching");
    if(aForming){
        var m, d, y;
        d = new Number(aForming.aday.value);
        m = new Number(aForming.amonth.value);
        y = new Number(aForming.ayear.value);
        if(d < 10) {
            aForming.adate.value = "0" + d + "/";
        }
        else {
            aForming.adate.value = d + "/";
        }
        if(m < 9) {
            aForming.adate.value += "0" + (m+1) + "/" + y;
        }
        else {
            aForming.adate.value += (m+1) + "/" + y;
        }
        ok = isNaN(y);
        if (ok) {
           // alert("Date must be in format dd/mm/yyyy");
            aForming.adate.value = "";
        }
         else {
                   
            var 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;
            aForming.fr.value = dt + "/" + mt + "/" + yr;
            if(aForming.day && aForming.month && aForming.year){
                aForming.day.selectedIndex=dt-1;
                aForming.month.selectedIndex=mt-1;
                for(i=0;i<aForming.year.length;i++){
                    if(aForming.year.options[i].value==yr){
                        aForming.year.selectedIndex=i;
                        break;
                               
                    }
                }
            }
        }
    }
}


function validatedate(){
	var currentYear,currentMonth,currentDay;
	today = new Date();
	currentYear = today.getFullYear();
	currentMonth = today.getMonth(); 
	currentDay = today.getDate();
	currentDate = new Date (currentYear, currentMonth, currentDay);
	ArrivalDate = new Date(document.formbooking.ayear.value, document.formbooking.amonth.value, document.formbooking.aday.value);
	if (ArrivalDate.getTime() < currentDate.getTime())
	return false;
	else return true;
}

function getPersing(elem1) {
    aForming = document.getElementById("searching");
    if(aForming){
        var adt1ing,adt2ing,adt3ing,tng1ing,tng2ing,tng3ing,chd1ing,chd2ing,chd3ing,inf1ing,inf2ing,inf3ing,rming;
        adt1 = new Number(aForming.adt1.value);
        adt2 = new Number(aForming.adt2.value);
        adt3 = new Number(aForming.adt3.value);
        tng1 = new Number(aForming.tng1.value);
        tng2 = new Number(aForming.tng2.value);
        tng3 = new Number(aForming.tng3.value);
        chd1 = new Number(aForming.chd1.value);
        chd2 = new Number(aForming.chd2.value);
        chd3 = new Number(aForming.chd3.value);
        inf1 = new Number(aForming.inf1.value);
        inf2 = new Number(aForming.inf2.value);
        inf3 = new Number(aForming.inf3.value);
        rm= document.getElementById('rmsing').options[document.getElementById('rmsing').selectedIndex].value;
        switch(rming)
        {
            case "1":
                aForming.adt.value = adt1;
                aForming.tng.value = tng1;
                aForming.chd.value = chd1;
                aForming.inf.value = inf1;
                break;
            case "2":
                aForming.adt.disabled;
                aForming.tng.disabled;
                aForming.chd.disabled;
                aForming.inf.disabled;
                break;
            case "3":
                aForming.adt.disabled;
                aForming.tng.disabled;
                aForming.chd.disabled;
                aForming.inf.disabled;
                break;
            default:
                aForming.adt.value = adt1;
                aForming.tng.value = tng1;
                aForming.chd.value = chd1;
                aForming.inf.value = inf1;
        }
    }
}



function reseting(){
    aForming = document.getElementById("searching");
    bForming = document.getElementById("rmsing");
    aForming.rms.value=1;
    bForming.selectedIndex = 1;

    
}
function onloading(){
    reseting();
    setDefaultYearing();
    fillstaying();
    getStaying();
    act_getStaying();
    getdaying();
    getPersing();
}

function flatoption() {
	var during=document.getElementById("during").value;
		if(during > 6 &&  during< 14){;
			document.getElementById("textwritten").value="365 CPS Flat Shares - Short term 7-13 days";
			document.getElementById("hotelGeoID").value="25187";
	
	}
	if(during> 13 &&  during< 28){
			document.getElementById("textwritten").value="365 CPS Flat Shares - Mid term 14-27 days";
			document.getElementById("hotelGeoID").value="47582";
	
	}
	if(during > 27){
			document.getElementById("textwritten").value="365 CPS Flat Shares - Long term 28+ days";
			document.getElementById("hotelGeoID").value="47581";
	
	}


}


