// JavaScript Document

var hoveredTool = "" ;


String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}


function init()
{
    var i = 1 ;

    for (i = 1 ; ; i++)
    {
        var id = "footer" + i ;
        var obj = document.getElementById(id) ;

        if (obj == null)
            break ;

        obj.onmouseover=function () { hoverfoot(this.id) ; } ;
        obj.onmouseout=function () { unhoverfoot(this.id) ; } ;

    }
}

function hovertest(id)
{
    alert(id) ;
}
function GetXmlHttpObject()
{
    if (xmlHttp != null)
        return xmlHttp ;

    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
                alert(e);
            }
        }
    }
    return xmlHttp ;
}

function AjaxRequest(page, func)
{
    if (xmlHttp == null)
        xmlHttp = GetXmlHttpObject();

    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }

    var oDate = new Date();
//    page += "&randomseed=" + oDate.getMilliseconds() ;
    var str = page ;
    var x = str.search(/\?/) ;
    if (x > -1)
        page += "&randomseed=" + oDate.getTime() ;
    else
        page += "?randomseed=" + oDate.getTime() ;
//alert(page) ;
    xmlHttp.open("GET", page, true);
    xmlHttp.onreadystatechange = func ;
    xmlHttp.send(null);
}
/*
function getTool()
{
}
*/
var hoveredFoot = "" ;

function hoverfoot(id)
{
    if (hoveredFoot == id)
        return ;

    hoveredFoot = id ;
    HighlightFoot() ;

}

function unhoverfoot(id)
{
    hoveredFoot = "" ;
    HighlightFoot() ;
}


function HighlightFoot()
{
// to hover and unhover footers.
// footer menu ids are footer1 = footer10, consecutively.
// if you need more than 10, change number below in for loop.
// You need two classes, class and classhover.  Doesn't matter what they are called
// as long has the only difference is that hover class is the same name with 'hover' appended.
// like 'foot' and 'foothover'.

    var obj ;
    var id ;
	
    for (var i = 1 ; i < 10 ; i++)
    {
        id = "footer"+i ;

        obj = document.getElementById(id) ;
        if (obj == null)
            break ;

        // grab curren classname.
        var c = obj.className ;
        if (id == hoveredFoot)
        {
            // if current classname does not contain 'hover', append it to hover class to hover it
            if (c.indexOf('hover') == -1)
                c += 'hover' ;
            obj.className = c ;
        }
        else
        {
            // if current class does contain 'hover', remove it.
            var idx = c.indexOf('hover') ;
            if (idx != -1)
                c = c.substring(0, idx) ;

            obj.className = c ;
        }
    }
}

function selectFooter(which)
{
/*  var sel = id ;
    var url = "" ;
    var i = parseInt(id.toString().substring(6)) ;// remove "footer" from "footer1", etc.
*/

    var cur = location.href ;

    var i = cur.indexOf("/business") ;
    if (i == -1)
        i = cur.indexOf("/leisure") ;
    if (i == -1)
        i = cur.indexOf("/groups") ;

    var newloc = "" ;
    if (i != -1)
        newloc = cur.substr(0, i) + "/tools/index.php?u=" + which ;
    else
        newloc = location.href + "/tools/index.php?u=" + which ;

    location.href = newloc ;
}

function selectLeisure(which)
{

    var cur = location.href ;

    var i = cur.indexOf("/business") ;
    if (i == -1)
        i = cur.indexOf("/leisure") ;
    if (i == -1)
        i = cur.indexOf("/groups") ;

    var newloc = "" ;
    if (i != -1)
        newloc = cur.substr(0, i) + "/leisure/index.php?u=" + which ;
    else
        newloc = location.href + "/leisure/index.php?u=" + which ;

    location.href = newloc ;
}

function selectPromos()
{
    var cur = location.href ;

    var i = cur.indexOf("/business") ;
    if (i == -1)
        i = cur.indexOf("/leisure") ;
    if (i == -1)
        i = cur.indexOf("/groups") ;

    var newloc = "" ;
    if (i != -1)
        newloc = cur.substr(0, i) + "/promos" ;
    else
        newloc = location.href + "/promos" ;

    location.href = newloc ;
}


function hovertoolbox(id)
{
    if(hoveredTool == id)
        return ;
    hoveredTool = id ;
    HighlightToolbox() ;
}

function unhovertoolbox(id)
{
    hoveredTool = "" ;
    HighlightToolbox() ;
}

function HighlightToolbox()
{
// to hover and unhover toolbox.
    var obj ;
    var id = "menutoolbox" ;

    obj = document.getElementById(id) ;
    if (obj == null)
        return ;

    // grab curren classname.
    var c = obj.className ;

    if (id == hoveredTool)
    {
        // if current classname does not contain 'hover', append it to hover class to hover it
        if (c.indexOf('hover') == -1)
            c += 'hover' ;
        obj.className = c ;
    }
    else
    {
        // if current class does contain 'hover', remove it.
        var idx = c.indexOf('hover') ;
        if (idx != -1)
            c = c.substring(0, idx) ;

        obj.className = c ;
    }
}


function hoverbar(id)
{
    if(hoveredBarItem == id)
        return ;
    hoveredBarItem = id ;
    HighlightMenu() ;
}

function unhoverbar(id)
{
    hoveredBarItem = "" ;
    HighlightMenu() ;
}


function HighlightMenu()
{
    var obj ;
    var id ;

    for (var i = 1 ; i < 100 ; i++)
    {
        id = "menu"+i ;

        obj = document.getElementById(id) ;
        if (obj == null)
            break ;
/*
        if (id == selectedItem)
        {
            if (selectedItem == hoveredBarItem)
                obj.className="selhover" ;
            else
                obj.className="sel" ;
        }
        else
*/
        if (id == hoveredBarItem)
        {
            obj.className = 'hover' ;
        }
        else
        {
            obj.className = 'non' ;
        }
    }
}

function selectToolbox()
{
    url = "/tools/index.php?u=1" ;

    var newloc = new String(location.href) ;

	var last = newloc.lastIndexOf("/") ;
    last = newloc.lastIndexOf("/", last-1) ;

	var twit = newloc.indexOf("/twitter/") ;
	if (twit > -1)
	{
    	last = newloc.lastIndexOf("/", last-1) ;
    	last = newloc.lastIndexOf("/", last-1) ;
	}

    newloc = newloc.substring(0, last) + url ;
    location.href = newloc ;
}


/* Define email constructor */
function Email(e)
{
    this.emailAddr = e ;
    this.message = "" ;
    this.valid = false ;
}

var regex ;
var _match ;
var user ;
var domain ;

function validate()
{
    // do a basic check for null, zero-length string,".", "@", only one @, and the absence of spaces
    if (this.emailAddr == null ||
        this.emailAddr.length == 0 ||
        this.emailAddr.indexOf(".") == -1 ||
        this.emailAddr.indexOf("@") == -1 ||
        this.emailAddr.indexOf("@") != this.emailAddr.lastIndexOf("@") ||
        this.emailAddr.indexOf(" ") != -1)
    {
        this.valid = false ;
        this.message = "Invalid email address.  Please enter again." ;
        return ;
    }

    /* name part of email cannot begin or end with "." and must have at least two chars */
    regex=/(^(\w{2,}\.)?\w{2,})@/;
    _match = regex.exec(this.emailAddr) ;

    if (_match)
    {
        user = RegExp.$1 ;
//        alert("user: " + user) ;
    }
    else
    {
        this.valid = false ;
        this.message = "Invalid name portion of email address.  Please enter again." ;
        return ;
    }

    //get the domain after the @ char
    //first take care of domain literals like @[19.25.0.1]
    regex = /@(\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\])$/ ;
    _match = regex.exec(this.emailAddr) ;

    if (_match)
    {
        domain=RegExp.$1 ;
        this.valid = true ;
    }
    else
    {
        // the @ followed by at least two chars that are not period (.),
        // followed by a period, followed by zero or one instances of two or more
        // chars ending with a period, followed by two-three chars htat are not periods

        regex = /@(\w{2,}\.(\w{2,}\.)?[a-zA-Z]{2,3})$/ ;
        _match = regex.exec(this.emailAddr) ;

        if (_match)
        {
            domain = RegExp.$1 ;
//            alert("domain: " + domain) ;
        }
        else
        {
            this.valid = false ;
            this.message = "Invalid domain portion of email address.  Please enter again." ;
            return ;
        }
    }// end of domain check

    this.valid = true ;
}

Email.prototype.validate = validate ;

function CheckAddress(val)
{
    var eml = new Email(val) ;
    var url ;
    eml.validate() ;
    if (! eml.valid)
        return false ;

    if (eml.valid)
    {
        return true ;
    }
}
