//// JavaScript Document
//function phpads_deliverActiveX(content) {
//    document.write(content);
//}

//sfHover = function() {
//    var sfEls = document.getElementById("nav").getElementsByTagName("LI");
//    for (var i = 0; i < sfEls.length; i++) {
//        sfEls[i].onmouseover = function() {
//            this.className += " sfhover";
//        }
//        sfEls[i].onmouseout = function() {
//            this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
//        }
//    }
//}
//if (window.attachEvent) window.attachEvent("onload", sfHover);


//Validate Date Function

function y2k(number) {
    return (number < 1000) ? number + 1900 : number;
}

function isDate(day, month, year) {
    // checks if date passed is valid
    // will accept dates in following format:
    // isDate(dd,mm,ccyy), or
    // isDate(dd,mm) - which defaults to the current year, or
    // isDate(dd) - which defaults to the current month and year.
    // Note, if passed the month must be between 1 and 12, and the
    // year in ccyy format.

    var today = new Date();
    year = ((!year) ? y2k(today.getYear()) : year);
    month = ((!month) ? today.getMonth() : month - 1);
    if (!day) return false
    var test = new Date(year, month, day);
    if ((y2k(test.getYear()) == year) && (month == test.getMonth()) && (day == test.getDate()))
        return true;
    else
        return false
}

//End validation of date


//Validate Landing Page Forms
var d = new Date();

function changedate() {
    var thisDate = new String;
    var dateElem;
    thisDate = document.tick1.bdate.value;
    dateElem = thisDate.split("/");
    setDateDropdown(dateElem[0], dateElem[1], dateElem[2])
}

function setDateDropdown(day, month, year) {
    document.tick1.day.options[(day - 1)].selected = true;
    document.tick1.month.options[(month - 1)].selected = true;
    document.tick1.year.options[(year - 2005)].selected = true;
}

function setDate(dayDate, monthDate, yearDate, FormName) {
    var form = eval("document." + FormName);
    form.bdate.value = dayDate + "-" + literalMonth(monthDate, form) + "-" + yearDate;
}

function literalMonth(month, form) {
    var shortMonth = new String();
    var numericMonth = new Number();

    numericMonth = month;

    if (numericMonth == 1) { shortMonth = "Jan" }
    if (numericMonth == 2) { shortMonth = "Feb" }
    if (numericMonth == 3) { shortMonth = "Mar" }
    if (numericMonth == 4) { shortMonth = "Apr" }
    if (numericMonth == 5) { shortMonth = "May" }
    if (numericMonth == 6) { shortMonth = "Jun" }
    if (numericMonth == 7) { shortMonth = "Jul" }
    if (numericMonth == 8) { shortMonth = "Aug" }
    if (numericMonth == 9) { shortMonth = "Sep" }
    if (numericMonth == 10) { shortMonth = "Oct" }
    if (numericMonth == 11) { shortMonth = "Nov" }
    if (numericMonth == 12) { shortMonth = "Dec" }

    return shortMonth;
}

function checkForm(thisForm) {

    if (thisForm.ticketbookA_0.value == 0 && thisForm.ticketbookC_0.value == 0) {
        alert("You did not enter the number of tickets you require");
        return false;
    }
    if (thisForm.cont.value == 'N') {
        alert("The date you are trying to book is not available for this attraction");
        return false;
    }

    if (isDate(thisForm.day.value, thisForm.month.value, thisForm.year.value) == false) {
        alert("The date entered (" + thisForm.day.value + "/" + thisForm.month.value + "/" + thisForm.year.value + ") is invalid. Please select a new date and press the 'Search' button.");

        return false;
    }

    var chosenDate = thisForm.day.value + " " + literalMonth(thisForm.month.value) + " " + thisForm.year.value;
    var date = new Date(chosenDate);
    var now = new Date();
    var diff = date.getTime() - now.getTime();
    var days = Math.floor(diff / (1000 * 60 * 60 * 24));
    if (days < 0) {
        alert("Please choose a date in the future");
        return false;
    }

    {


    }
    return true;
}

// End Landing Page Validation


function anchorclick() {
}


//Begin Search Form validation
function changeSearchdate() {
    var thisDate = new String;
    var dateElem;
    thisDate = document.bookingStage1.checkindate.value;
    dateElem = thisDate.split("/");
    setDateDropdown(dateElem[0], dateElem[1], dateElem[2])
}

function setSearchDateDropdown(day, month, year) {
    document.bookingStage1.day.options[(day - 1)].selected = true;
    document.bookingStage1.month.options[(month - 1)].selected = true;
    document.bookingStage1.year.options[(year - 2005)].selected = true;
}

function setSearchDate(dayDate, monthDate, yearDate) {
    document.bookingStage1.checkindate.value = dayDate + "-" + literalMonth(monthDate) + "-" + yearDate;
}


function checkSearchForm(thisForm) {

    if (isDate(thisForm.day.value, thisForm.month.value, thisForm.year.value) == false) {
        alert("The date entered (" + thisForm.day.value + "/" + thisForm.month.value + "/" + thisForm.year.value + ") is invalid. Please select a new date and press the 'Search' button.");

        return false;
    }

    var chosenDate = thisForm.day.value + " " + literalMonth(thisForm.month.value) + " " + thisForm.year.value;
    var date = new Date(chosenDate);
    var now = new Date();
    var diff = date.getTime() - now.getTime();
    var days = Math.floor(diff / (1000 * 60 * 60 * 24));
    if (days < 0) {
        alert("Please choose a date in the future");
        return false;
    }

    if (days < 3) {
        alert("The date you have selected is within three days, please phone us on 0870 705 5000");
        return false;
    }

    {
        if (thisForm.destination_id.value == "des_0") {
            alert("Please select your destination.");
            return false;
        }



    }
    return true;
}
// End Search Validation

// Used to pop up the window for the video clips
function genericpopwin(windowURL, windowWidth, windowHeight, scrollBars, reSizable) {
    if (scrollBars) {
        //
    } else {
        scrollBars = 'no'
    }

    if (reSizable) {
        //
    } else {
        reSizable = 'no'
    }
    var windowName = "generic" + Math.round(Math.random(1) * 1000);
    var windowOptions = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=" + scrollBars + ",resizable=" + reSizable + ",width=" + windowWidth + ",height=" + windowHeight;
    var mywin = window.open(windowURL, windowName, windowOptions);
}


function toggleLayer(whichLayer) {
    if (document.getElementById) {
        // this is the way the standards work
        var style2 = document.getElementById(whichLayer).style;
        style2.display = style2.display ? "" : "block";
    }
    else if (document.all) {
        // this is the way old msie versions work
        var style2 = document.all[whichLayer].style;
        style2.display = style2.display ? "" : "block";
    }
    else if (document.layers) {
        // this is the way nn4 works
        var style2 = document.layers[whichLayer].style;
        style2.display = style2.display ? "" : "block";
    }
}

//function showPriceCalendar(contract,dropdtls,form)
//{
//	xmlHttp=GetXmlHttpObject()
//	if (xmlHttp==null)
//	{
//		//alert ("Browser does not support HTTP Request")
//	return
//	} 
//	var url="include/allocation-popup-ajax.asp"
//	url=url+"?contract="+contract+"&dropdtls="+dropdtls+"&form="+form
//	url=url+"&sid="+Math.random()
//	xmlHttp.onreadystatechange=stateChangedCalendar
//	xmlHttp.open("GET",url,true)
//	xmlHttp.send(null)
//	
//	function stateChangedCalendar() 
//	{ 	
//		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
//		{ 
//			document.getElementById("Allocation"+form).innerHTML=xmlHttp.responseText
//			toggleLayer("CalendarForm"+form);
//		} 
//	} 
//}

function showPriceCalendar(contract, product, dropdtls, form, qty, ticketType) {
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        //alert ("Browser does not support HTTP Request")
        return
    }
    var url = "ajax/allocation.aspx";
    url = url + "?contract=" + contract + "&dropdtls=" + dropdtls + "&form=" + form;
    if (product != '') {
        url += "&product=" + product;
    }
    url += "&qty=" + qty;
    url += "&ttype=" + ticketType;
    url = url + "&sid=" + Math.random();
    xmlHttp.onreadystatechange = stateChangedCalendar;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

    function stateChangedCalendar() {
        if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
            document.getElementById("Allocation" + form).innerHTML = xmlHttp.responseText
            toggleLayer("CalendarForm" + form);
        }
    }
}




function showPoll(poll, value) {
    xmlHttp = GetXmlHttpObject()
    if (xmlHttp == null) {
        //alert ("Browser does not support HTTP Request")
        return
    }
    var url = "include/poll-ajax.asp"
    url = url + "?poll=" + poll + "&value=" + value
    url = url + "&sid=" + Math.random()
    xmlHttp.onreadystatechange = stateChangedPoll
    xmlHttp.open("GET", url, true)
    xmlHttp.send(null)

    function stateChangedPoll() {
        if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
            document.getElementById("customer_poll").innerHTML = xmlHttp.responseText
        }
    }
}

function stateChangedDC() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        var responseArray = xmlHttp.responseText.split("XX");
        document.getElementById("Allocation" + responseArray[0]).innerHTML = responseArray[1];
    }
}

function showPrice(day, month, year, contract, formnumber, ttype, attrVal) {

    var bookingButtonOrig = document.getElementById("bookingButton" + formnumber);
    bookingButtonOrig.style.visibility = 'hidden';
    
    if (isDate(day, month, year) == false) {
        alert("Please enter a valid date (date entered does not exist)");
    }
    xmlHttp = GetXmlHttpObject()
    if (xmlHttp == null) {
        //alert ("Browser does not support HTTP Request")
        return
    }
    
    var url = "ajax/calendar-price.aspx";
    url = url + "?day=" + day + "&month=" + month + "&year=" + year + "&contract=" + contract + "&formnumber=" + formnumber;
    url += "&ttype=" + ttype;
    url += "&contrID=" + attrVal;
    url = url + "&sid=" + Math.random();
    xmlHttp.onreadystatechange = stateChanged
    xmlHttp.open("GET", url, true)
    xmlHttp.send(null)

}

function stateChanged() {

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        var responseArray = xmlHttp.responseText.split("X");

        var id = (responseArray.length) - 1;

        if (document.getElementById("adultAjax" + responseArray[id]) != null)
            document.getElementById("adultAjax" + responseArray[id]).innerHTML = responseArray[0];

        if (document.getElementById("childAjax" + responseArray[id]) != null)
            document.getElementById("childAjax" + responseArray[id]).innerHTML = responseArray[1];

        if (document.getElementById("ageAjax" + responseArray[id]) != null)
            document.getElementById("ageAjax" + responseArray[id]).innerHTML = responseArray[2];

        if (document.frmBooking.ChildAge.value != null)
            document.frmBooking.ChildAge.value = responseArray[3];

        var str = responseArray[0];
        var bookingButton = document.getElementById("bookingButton" + responseArray[id]);       

        if (str == "&pound;NA") {            
            bookingButton.style.visibility = 'hidden';            
        }
        else {
            bookingButton.style.visibility = 'visible';
        }
    }
}

function GetXmlHttpObject(handler) {
    var objXmlHttp = null

    if (navigator.userAgent.indexOf("Opera") >= 0) {
        //alert("Opera not supported...")
        return;
    }
    if (navigator.userAgent.indexOf("MSIE") >= 0) {
        if (navigator.appVersion.indexOf("MSIE 7.0") >= 1) {
            try {
                objXmlHttp = new XMLHttpRequest()
                //objXmlHttp.onreadystatechange=handler
                return objXmlHttp
            }
            catch (e) {
                //alert("Error. Scripting for XML might be disabled")
                return
            }
        }
        else {
            var strName = "Msxml2.XMLHTTP"
            if (navigator.appVersion.indexOf("MSIE 5.5") >= 0) {
                strName = "Microsoft.XMLHTTP"
            }
            try {
                objXmlHttp = new ActiveXObject(strName)
                //objXmlHttp.onreadystatechange=handler
                return objXmlHttp
            }
            catch (e) {
                //alert("Error. Scripting for ActiveX might be disabled")
                return
            }
        }
    }

    if (navigator.userAgent.indexOf("Mozilla") >= 0) {
        objXmlHttp = new XMLHttpRequest()
        objXmlHttp.onload = handler
        objXmlHttp.onerror = handler
        return objXmlHttp
    }
}


function SetCalendarValue(thisForm, FormDay, FormMonth, FormYear, contract) {

    document.getElementById("tick" + thisForm).day.value = FormDay;
    document.getElementById("tick" + thisForm).month.value = FormMonth;
    document.getElementById("tick" + thisForm).year.value = FormYear;
    showPrice(FormDay, FormMonth, FormYear, contract, thisForm, document.frmBooking.TicketType.value, document.frmBooking.SiteAttribute.value)
}

function CreateBookmarkLink(url) {
    title = "AttractionTix - Pay, Print, Present";

    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url, "");
    }
    else if (window.external) { // IE Favorite
        window.external.AddFavorite(url, title);
    }
    else if (window.opera && window.print) { // Opera Hotlist
        return true;
    }
}



function show_email_terms() {
    newwindow = window.open(
		'http://www.portaventuraholidays.co.uk/email_submit.php?email_address=' + escape(document.getElementById('email_address').value).replace(/\+/g, '%2B'),
		'name',
		'height=500,width=500,top=200,left=300,resizable, scrollbars=1'
	);
    if (window.focus) { newwindow.focus(); }
}

function resetForms() {
    for (i = 0; i < document.forms.length; i++) {
        document.forms[i].reset();
    }
}
