var timeout;
var logged_in = false;
var veriCode = "";
var tmrMenu = null;

var isFF = (navigator.userAgent.indexOf("Firefox") != -1);
var isMac = (navigator.appVersion.indexOf("Mac") != -1);
var isNN4 = document.layers?1:0;
var isIE4 = document.getElementById?0:1;
var isOPR = (navigator.userAgent.indexOf("Opera")!=-1)?1:0;
var isOPR7 = 0;
var isNS6 = (document.getElementById && !document.all && !isOPR)?1:0;
var isNS7 = (navigator.userAgent.indexOf("Netscape/7")!=-1)?1:0;

function doLinks()
{
    if (!document.getElementById) return false;

    if (document.getElementsByTagName)
    {
        var links = document.getElementsByTagName("a");
        for (var i = 0; i < links.length; i++)
        {
            if (links[i].className.match("link"))
            {
                links[i].onclick = function()
                {
                    GetPage("GET", this.getAttribute("href"));
                    return false;
                };
            }
        }
    }
}

function GetXMLHTTPObject(){
    var XMLHTTP=false;    
    try {
        XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            XMLHTTP = false;
        }
    }    
    if (!XMLHTTP && typeof XMLHttpRequest!='undefined') {
        try {
            XMLHTTP = new XMLHttpRequest();
        } catch (e) {
            XMLHTTP=false;
        }
    }
    if (!XMLHTTP && window.createRequest) {
        try {
            XMLHTTP = window.createRequest();
        } catch (e) {
            XMLHTTP=false;
        }
    }
    return XMLHTTP;
}

function GetPage(method, url)
{
    var XMLHTTP = GetXMLHTTPObject();
    
    if(logged_in)
    {
        if(url.indexOf("?") == -1)
        {
            url += "?";
        }
        else
        {
            url += "&";
        }
        
        url += "verify=" + veriCode;
    }
    
    XMLHTTP.open(method, url, true);
    XMLHTTP.setRequestHeader("Cookies", "SessionGuid=" + document.cookie["sessionGuid"]);
    XMLHTTP.onreadystatechange = function(){
        if (XMLHTTP.readyState == 4) {
            document.getElementById("body").innerHTML = XMLHTTP.responseText;
        }
    }
    XMLHTTP.send(null);
}

function GetSubPage(method, url, target)
{
    var XMLHTTP = GetXMLHTTPObject();

    if(logged_in)
    {
        if(url.indexOf("?") == -1)
        {
            url += "?";
        }
        else
        {
            url += "&";
        }
        
        url += "verify=" + veriCode;
    }

    XMLHTTP.open(method, url, true);
    XMLHTTP.setRequestHeader("Cookies", "SessionGuid=" + document.cookie["sessionGuid"]);
    XMLHTTP.onreadystatechange = function(){
        if (XMLHTTP.readyState == 4) {
            document.getElementById(target).innerHTML = XMLHTTP.responseText;
        }
    }
    XMLHTTP.send(null);
}

function Highlight(itm, color)
{
	itm.style.color = color;
}

function PopupMenu(itm, mnu, e)
{
    var menu = document.getElementById("menu");
    menu.innerHTML = GetMenuHtml(mnu);

    var docWidth;
    //Opera Netscape 6 Netscape 4x Mozilla 
    if (window.innerWidth){ 
        docWidth = window.innerWidth; 
    } 
    //IE Mozilla 
    if (document.body.clientWidth){ 
        docWidth = document.body.clientWidth; 
    }

    menu.style.left = ((docWidth / 2) - 324) + "px";
    menu.style.top = "173px"; 
    
    timeout = setTimeout("StartHideMenu();", 3000);
    doLinks();
    ShowMenu();
}

function ShowMenu()
{
    window.clearTimeout(tmrMenu);
    document.getElementById("menu").style.visibility = "visible";
}

function StartHideMenu()
{
    tmrMenu = window.setTimeout("HideMenu();", 1000, "JavaScript");
}

function HideMenu()
{
    document.getElementById("menu").style.visibility = "hidden";
}

function GetMenuHtml(mnu)
{
    var html, i;
    
    html = "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" onmouseover=\"ShowMenu();\">";
    
    for(i = 1; i < mnu.length; i++)
    {
        html += "<tr><td class=\"menu\"><a href=\"" + mnu[i][1] + "\" class=\"link\" style=\"color:#6E9B94;\" onmouseover=\"Highlight(this, '#777765');\" onmouseout=\"Highlight(this, '#6E9B94');\" onmouseup=\"HideMenu();\">Room " + i + " - " + mnu[i][0] + "</a></td></tr>";
    }
       
    html += "</table>";
    
    return html;
}

function ShowVirtualTour()
{
    var tour = document.getElementById("virtualTour").style;
    
    var docWidth, docHeight;
    //Opera Netscape 6 Netscape 4x Mozilla 
    if (window.innerWidth){ 
        docWidth = window.innerWidth; 
        docHeight = window.innerHeight; 
    } 
    //IE Mozilla 
    if (document.body.clientWidth){ 
        docWidth = document.body.clientWidth; 
        docHeight = document.body.clientHeight; 
    }
    
    tour.left = ((docWidth - Math.abs(tour.width.replace("px", ""))) / 2) + "px";
    tour.top = ((docHeight - Math.abs(tour.height.replace("px", ""))) / 2) + "px";
    tour.visibility = "visible";
}

function HideVirtualTour()
{
    document.getElementById("virtualTour").style.visibility = "hidden";
}

function ShowRoomImage(img)
{
    document.getElementById("room_img_large").src = "images/rooms/large/" + img;
}

function SubmitEnquiry(frm)
{
    var emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
    var phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/
    var numRe = /\D/;
        
    if(frm.name.value == "")
    {
        alert("Please enter your name!");
        return false;
    }
    
    if(frm.email.value == "")
    {
        alert("Please enter your email address!");
        return false;
    }
    else
    {      
        if(!emailRe.test(frm.email.value))
        {
            alert("Please enter a valid email address!");
            return false;
        }
    }
    
    if(frm.fax.value == "")
    {
        alert("Please enter a valid fax number!");
        return false;
    }
    else
    {
        if(!phoneRe.test(frm.fax.value))
        {
            alert("Please enter a valid fax number!");
            return false;
        }
    }
    
    if(!CheckDate(frm.arrival_year.value, frm.arrival_month.value, frm.arrival_day.value))
    {
        alert("Please enter a valid arrival date!");
        return false;
    }
    
    if(!CheckDate(frm.departure_year.value, frm.departure_month.value, frm.departure_day.value))
    {
        alert("Please enter a valid departure date!");
        return false;
    }
    
    var arrival = new Date(frm.arrival_year.value, Math.abs(frm.arrival_month.value) - 1, frm.arrival_day.value);
    var departure = new Date(frm.departure_year.value, Math.abs(frm.departure_month.value) - 1, frm.departure_day.value);
    var now = new Date();
    
    if(arrival < now)
    {
        alert("Please enter an arrival date that is in the future!");
        return false;
    }
    
    if(departure < now)
    {
        alert("Please enter a departure date that is in the future!");
        return false;
    }
    
    if(arrival >= departure)
    {
        alert("Your arrival date must be before your departure date!");
        return false;
    }
    
    var i;
    var form = "";
    
    for(i = 0; i < frm.length; i++)
    {
        form += frm[i].name + "=" + frm[i].value + "&";
    }
    
    form = form.substring(0, form.length - 1);
    
    var XMLHTTP = GetXMLHTTPObject();
    
    XMLHTTP.open("POST", "enquiry_email.asp", false);
    XMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    XMLHTTP.send(form);
    
    if(XMLHTTP.responseText == "Success")
    {
        document.getElementById("mail_response").innerHTML = "Thank you for your request.";
    }
    else
    {
        document.getElementById("mail_response").innerHTML = "We've experienced a technical problem. Please try again later,<br />or contact us directly on info@primi-seacastle.com";
    }
}

function CheckDate(year, month, day)
{
    if(month == 1 | month == 3 | month == 5 | month == 8 | month == 10 | month == 12)
    {
        return true;
    }
    
    if(month == 2)
    {
        if((year % 4) != 0)
        {
            if(day > 28)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            if(day > 29)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    
    if(month == 4 | month == 6 | month == 7 | month == 9 | month == 11)
    {
        if(day > 30)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

function SubmitAdminLogin(frm)
{
    if(frm.username.value == "")
    {
        alert("Please enter a valid user name!");
        return false;
    }
    
    if(frm.password.value == "")
    {
        alert("Please enter a valid password!");
        return false;
    }

    var XMLHTTP = GetXMLHTTPObject();
    
    XMLHTTP.open("POST", "admin_login.aspx", false);
    XMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    XMLHTTP.send("username=" + frm.username.value + "&" + "password=" + frm.password.value);
    
    if(XMLHTTP.responseText.indexOf("Success") != -1)
    {
        logged_in = true;
        
        document.getElementById("menu_default").style.display = "none";
        document.getElementById("menu_admin").style.display = "block";
        document.getElementById("admin_login").innerHTML = "admin logout";
        
        var parts = XMLHTTP.responseText.split("|");
        eval(parts[1]);
            
        GetPage("GET", "admin.aspx");
    }
    else
    {
        alert("Login failed! Please try again.");
    }
}

function AdminLoginLogout()
{
    if(!logged_in)
    {
        GetPage('GET', 'admin_login.aspx');
    }
    else
    {
        logged_in = false;
        
        var XMLHTTP = GetXMLHTTPObject();
        
        XMLHTTP.open("GET", "admin_logout.aspx", false);
        XMLHTTP.send(null);
        
        document.getElementById("menu_admin").style.display = "none";
        document.getElementById("menu_default").style.display = "block";
        document.getElementById("admin_login").innerHTML = "admin login";
        
        GetPage("GET", "home.aspx");
    }
}
