var submit = "x"
var carsubmit = "x"
var flightsubmit = "x"

//Trim function - reusable code

function LTrim(str) {

    var whitespace = new String(" \n\r\t")
    var s = new String(str)

    if (whitespace.indexOf(s.charAt(0)) != -1) {
        var j = 0, i = s.length
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
            j++
        }
        s = s.substring(j, i)
    }
    return (s)
}

function RTrim(str) {

    var whitespace = new String(" \t\n\r")
    var s = new String(str)

    if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
        var i = s.length - 1
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
            i--
        }
        s = s.substring(0, i + 1)
    }
    return (s)
}

function Trim(str) {
    return RTrim(LTrim(str))
}

function setnoerror(roomarray, valarray, property) {

    var room = Trim(roomarray[property])

    //set hidden validator to ok so that unused rooms don't cause validation to fail
    if (room.length < 12) {
        var elem = document.getElementById(valarray[property])
        if (elem) {
            elem.value = "N/A"
        }
    }
}

function copyhidden(roomno, property, paxcount) {

    if (roomno == "1") {
        copyhiddenRoom(1, hiddenRoom1, property, paxcount, hiddenRoom1Val)
        setnoerror(hiddenRoom2, hiddenRoom2Val, property)
        setnoerror(hiddenRoom3, hiddenRoom3Val, property)
        setnoerror(hiddenRoom4, hiddenRoom4Val, property)
    }

    if (roomno == "2") {
        copyhiddenRoom(2, hiddenRoom2, property, paxcount, hiddenRoom2Val)
    }

    if (roomno == "3") {
        copyhiddenRoom(3, hiddenRoom3, property, paxcount, hiddenRoom3Val)
    }

    if (roomno == "4") {
        copyhiddenRoom(4, hiddenRoom4, property, paxcount, hiddenRoom4Val)
    }

}


var valid

function copyhiddenRoom(roomno, roomarray, property, paxcount, valarray) {
    //copies name values to hidden aspx box and checks each html textbox has been completed

    //ASPX control
    var elem = document.getElementById(roomarray[property])
    var roomnames = ""
    var elem2
    valid = "yes"

    for (pax = 1; pax <= paxcount; pax++) {

        //pax seperator
        if (pax > 1) {
            roomnames += "|"
        }

        //HTML control
        //TITLE
        elem2 = document.getElementById('Item' + property + 'Room' + roomno + 'Title' + pax)

        //title will always be valid
        if (elem2) {
            roomnames += elem2.value
        }
        //FIRST NAME  
        elem2 = document.getElementById('Item' + property + 'Room' + roomno + 'FirstName' + pax)
        if (elem2) {
            roomnames += "*" + elem2.value
            //flag error if not completed
            if (Trim(elem2.value) == "") {
                valid = ""
            }
        }
        //SURNAME  
        elem2 = document.getElementById('Item' + property + 'Room' + roomno + 'Surname' + pax)
        if (elem2) {
            roomnames += "*" + elem2.value
            //flag error if not completed
            if (Trim(elem2.value) == "") {
                valid = ""
            }
        }
    }

    //alert(roomnames)  
    if (elem) {
        elem.value = roomnames
    }
    var elem = document.getElementById(valarray[property])
    if (elem) {
        elem.value = valid
    }

    //set visibility of validation controls
    elem2 = document.getElementById("item" + property + "div" + roomno)
    if (elem2) {

        if (valid == "yes") {
            elem2.style.visibility = "hidden"
            elem2.style.display = "none"
        }
        else {
            elem2.style.visibility = "visible"
            elem2.style.display = "block"
        }
    }

    //if all OK, the hidden ASPX room box should read eg Mr*Ashley*Wilson|Miss*Carly*Frost
    //and the valid hidden box will say yes

}

function copynames(from, to, roomcount) {

    for (i = 1; i <= roomcount; i++) {

        for (p = 1; p <= 7; p++) {
            var elem = document.getElementById(from + 'Room' + i + 'Title' + p)

            if (elem) {
                var elem2 = document.getElementById(to + 'Room' + i + 'Title' + p)
                elem2.selectedIndex = elem.selectedIndex
                elem = document.getElementById(from + 'Room' + i + 'FirstName' + p)
                elem2 = document.getElementById(to + 'Room' + i + 'FirstName' + p)
                elem2.value = elem.value
                elem = document.getElementById(from + 'Room' + i + 'Surname' + p)
                elem2 = document.getElementById(to + 'Room' + i + 'Surname' + p)
                elem2.value = elem.value
            }
            else {
                break
            }
        }
    }
}

function showreq(item) {

    var elem = document.getElementById(item)

    if (elem) {

        if (elem.style.display != 'block') {
            elem.style.display = 'block';
            elem.style.visibility = 'visible';
        }
        else {
            elem.style.display = 'none';
            elem.style.visibility = 'hidden';
        }
    }
}

var theproperty

//sets no of latest property that the user has tried to add to basket
function getClicked(propertyno) {
    theproperty = propertyno
}

//checks that (HB) properties haven't run out of availability for any of the rooms selected
//when a property is added to basket
function validateB() {
    var hiddenelem = document.getElementById(hiddenArray[theproperty])

    if (hiddenelem) {

        var allvals = hiddenelem.value
        var splitvals = allvals.split("]")
        var splitval
        var splitval2
        var avail
        var availused

        for (i = 0; i < splitvals.length - 1; i++) {
            splitval = splitvals[i].split("|")
            avail = splitval[3]
            availused = 1

            for (s = 0; s < splitvals.length - 1; s++) {
                //availused=1

                if (i != s) {
                    splitval2 = splitvals[s].split("|")

                    if (splitval2[0] == splitval[0]) {
                        availused += 1

                        if (availused > avail) {
                            alert("Sorry, there is insufficient availability to meet your room requests for this property. Please amend.")
                            return false
                            break
                        }
                    }
                }
            }
        }

        //checks that (HB) properties have the same board type for all rooms
        //when a property is added to basket
        var thisboardtype

        for (i = 0; i < splitvals.length - 1; i++) {
            splitval = splitvals[i].split("|")
            thisboardtype = splitval[4]

            if (thisboardtype.length == 2) {

                for (s = 0; s < splitvals.length - 1; s++) {

                    if (i != s) {
                        splitval2 = splitvals[s].split("|")

                        if (splitval2[4] != splitval[4]) {
                            alert(splitval2[4])
                            alert(splitval[4])
                            alert("This hotel requires an overall board type to be submitted for all rooms within an individual request. Please amend so that all rooms either include or exclude breakfast. If you really want to have different board types, please request availability and add each room individually, so that they appear as different items in your basket.")
                            return false
                            break
                        }
                    }
                }
            }
        }
    }
    return true
}

//checks that (HB) properties haven't run out of availability for any of the rooms selected
//after a property has been added to basket
function validate() {
    var hiddenelem = document.getElementById(hidden)

    if (hiddenelem) {
        var hiddenelemASPX = document.getElementById(hiddenV)
        var allvals = hiddenelem.value
        var splitvals = allvals.split("]")
        var splitval
        var splitval2
        var avail
        var availused

        for (i = 0; i < splitvals.length - 1; i++) {
            splitval = splitvals[i].split("|")
            avail = splitval[4]
            availused = 1

            //don't do this for ATI/FW - each request has guaranteed availability
            var firstchar = splitval[0].charAt(0)

            if (firstchar == "0" || firstchar == "1" || firstchar == "2" || firstchar == "3" || firstchar == "4" || firstchar == "5" || firstchar == "6" || firstchar == "7" || firstchar == "8" || firstchar == "9") {

                for (s = 0; s < splitvals.length - 1; s++) {

                    if (i != s) {
                        splitval2 = splitvals[s].split("|")

                        if (splitval2[0] == splitval[0] && splitval2[2] == splitval[2] && splitval2[5] == splitval[5]) {
                            availused += 1

                            if (availused > avail) {
                                alert("You have added rooms beyond known availability for " + splitval[3] + " at " + splitval[1] + ", check-in " + splitval[5].substring(0, 10) + ". Please amend and re-check availability.")

                                //this causes validation to fail on checkout page
                                if (hiddenelemASPX) {
                                    hiddenelemASPX.value = ""
                                }

                                return false
                                break
                            }
                        }
                    }
                }
            }
        }
    }
    return true
}

function setsubmitted() {
    var hidden = document.getElementById(aspsubmit)

    if (hidden) {
        hidden.value = valid

        if (valid == "yes") {
            busyBox.Show()
            var theform = document.forms[0]
        }
    }
}

var dontdisable = "0"

function disableform() {
    var theform = document.forms[0]

    if (dontdisable == "0") {

        if (document.all || document.getElementById) {
            var submitbutton = document.getElementById(submit)

            if (submitbutton) {
                submitbutton.disabled = true
            }

            submitbutton = document.getElementById(carsubmit)

            if (submitbutton) {
                submitbutton.disabled = true
            }

            submitbutton = document.getElementById(flightsubmit)

            if (submitbutton) {
                submitbutton.disabled = true
            }

        }
    }

    dontdisable = "0"
}

var browserName = navigator.appName.substring(0, 8);
var browserVer = parseFloat(navigator.appVersion);

var Browser = 0;

if (browserName == 'Microsof' && browserVer < 4) { Browser = 0; }
if (browserName == 'Microsof' && browserVer >= 4) { Browser = 1; }
if (browserName == 'Netscape' && browserVer < 4) { Browser = 0; }
if (browserName == 'Netscape' && browserVer >= 4) { Browser = 2; }

function terms() {

    //close any currently open windows
    if (self.awin && Browser == 1) {
        self.awin.close()
    }

    var url = "http://www.goamerica.co.uk/Furniture/terms.htm"
    var awin
    var popup = window.open(url, 'Terms', 'left=0,top=0,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,width=625,height=625')
    self.awin = popup

}

function showfaq(id) {
    var elem = document.getElementById('faq' + id);

    if (elem) {
        if (elem.style.display != 'block') {
            elem.style.display = 'block';
            elem.style.visibility = 'visible';
        }
        else {
            elem.style.display = 'none';
            elem.style.visibility = 'hidden';
        }
    }

}

function showdest(id) {
    var elem = document.getElementById('dest' + id);

    if (elem) {
        if (elem.style.display != 'block') {
            elem.style.display = 'block';
            elem.style.visibility = 'visible';
        }
        else {
            elem.style.display = 'none';
            elem.style.visibility = 'hidden';
        }
    }

}

function readmore() {
    var elem = document.getElementById("moredest")

    if (elem.style.visibility == "hidden") {
        elem.style.visibility = "visible"
        elem.style.display = "block"
        elem = document.getElementById("readmore")
        elem.innerHTML = "READ LESS"
    }
    else {
        elem.style.visibility = "hidden"
        elem.style.display = "none"
        elem = document.getElementById("readmore")
        elem.innerHTML = "READ MORE"
    }
    dontdisable = "1"
}

function selectSearchOption(option) {

    var elem = eval("document.forms[0].radioFAC")

    if (elem) {

        if (elem.length > 1) {
            elem[option].checked = true
            showFlightsAccomCar()
        }
    }
}

function showFlightsAccomCar() {

    var elem = eval("document.forms[0].radioFAC")
    var elemA = document.getElementById("ASearch")
    var elemC = document.getElementById("CSearch")
    var elemFl = document.getElementById("FSearch")

    if (elem) {

        for (o = 0; o < elem.length; o++) {

            if (elem[o].checked) {

                if (o == 0) {
                    elemFl.style.visibility = "visible"
                    elemFl.style.display = "block"
                    elemA.style.visibility = "hidden"
                    elemA.style.display = "none"
                    elemC.style.visibility = "hidden"
                    elemC.style.display = "none"
                }
                if (o == 1) {
                    elemA.style.visibility = "visible"
                    elemA.style.display = "block"
                    elemFl.style.visibility = "hidden"
                    elemFl.style.display = "none"
                    elemC.style.visibility = "hidden"
                    elemC.style.display = "none"
                }
                if (o == 2) {
                    elemC.style.visibility = "visible"
                    elemC.style.display = "block"
                    elemA.style.visibility = "hidden"
                    elemA.style.display = "none"
                    elemFl.style.visibility = "hidden"
                    elemFl.style.display = "none"
                }

                break
            }
        }
    }
    else {
        var elemF = document.getElementById(txtFormType)

        if (elemF.value == "None") {
            elem = document.getElementById("PanelText")
            elem.style.visibility = "hidden"
            elem.style.display = "none"
            elemA.style.visibility = "hidden"
            elemA.style.display = "none"
            elemC.style.visibility = "hidden"
            elemC.style.display = "none"
            elemFl.style.visibility = "hidden"
            elemFl.style.display = "none"
        }

    }

    var elem = document.getElementById("breaksmiddle2");

    if (elem) {
        menupos()
    }

}

function buildseats() {
    var elempax = document.getElementById(txtPax)
    var pax = elempax.value
    var elemseats = document.getElementById(txtSeats)
    var elem
    var elemid
    var seats_selected = 0

    for (i = 0; i < document.forms[0].elements.length; i++) {
        elem = document.forms[0].elements[i]
        elemid = elem.id

        if (elemid.indexOf("chkSeat") > -1) {
            if (elem.checked == true) {
                seats_selected += 1

                if (seats_selected > 1) {
                    elemseats.value += "*"
                }

                elemseats.value += elem.value
            }
        }

    }

    if (seats_selected != pax) {
        elemseats.value = ""
    }

}




