
function emailCheck(emailStr)
{
    var checkTLD = 1;
    var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
    var emailPat = /^(.+)@(.+)$/;
    var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
    var validChars = "\[^\\s" + specialChars + "\]";
    var quotedUser = "(\"[^\"]*\")";
    var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom = validChars + '+';
    var word = "(" + atom + "|" + quotedUser + ")";
    var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray = emailStr.match(emailPat);

    if (matchArray == null)
    {
	    alert("Email address " + emailStr + " seems incorrect (check @ and .'s)");
	    return false;
    }

    var user = matchArray[1];
    var domain = matchArray[2];

    // Start by checking that only basic ASCII characters are in the strings (0-127).

    for (i = 0; i  <user.length; i++)
    {
	    if (user.charCodeAt(i) > 127)
	    {
		    alert("Email address " + emailStr + " contains invalid characters.");
		    return false;
	    }
    }

    for (i = 0; i < domain.length; i++)
    {
	    if (domain.charCodeAt(i) > 127)
	    {
		    alert("Email address " + emailStr + " contains invalid characters.");
		    return false;
	    }
    }

    // See if "user" is valid 

    if (user.match(userPat) == null)
    {
	    // user is not valid

	    alert("The username in " + emailStr + " doesn't seem to be valid.");
	    return false;
    }

    var IPArray = domain.match(ipDomainPat);

    if (IPArray != null)
    {
	    // this is an IP address

	    for (var i = 1; i <= 4; i++)
	    {
		    if (IPArray[i] > 255)
		    {
			    alert("Destination IP address is invalid in " + emailStr + ".");
			    return false;
		    }
	    }
    	
	    return true;
    }

    // Domain is symbolic name. Check if it's valid.
    var atomPat = new RegExp("^" + atom + "$");
    var domArr = domain.split(".");
    var len = domArr.length;

    for (i = 0; i < len; i++)
    {
	    if (domArr[i].search(atomPat) == -1)
	    {
		    alert("The domain name does not seem to be valid in " + emailStr + ".");
		    return false;
	    }
    }
    if (checkTLD && domArr[domArr.length-1].length != 2 && domArr[domArr.length-1].search(knownDomsPat) == -1)
    {
	    alert("The address " + emailStr + " must end in a well-known domain or two letter " + "country.");
	    return false;
    }

    // Make sure there's a host name preceding the domain.

    if (len < 2)
    {
	    alert("Email address " + emailStr + " is missing a hostname!");
	    return false;
    }

    // If we've gotten this far, everything's valid!
    return true;
}

function trim(varStr) {
	return varStr.replace(/^\s+|\s+$/g,"");
}

function MouseHover(x) {
	if (typeof(x) == "undefined") {
		x = this;
	}
	
	x.oldClassName = x.className;
	x.className = x.className + "Hover";
}

function MouseOffHover(x) {
	if (typeof(x) == "undefined") {
		x = this;
	}
	
	x.className = x.oldClassName;
}

function changeLinkColour(colour, linkElement) {
    document.getElementById(linkElement).style.color = colour;
}

function changeLinkColourBack(linkElement) {
    document.getElementById(linkElement).style.color = "";
}

function keyPressed(e, action){
    var keyPressed;
    if(window.event)
        keyPressed = window.event.keyCode; // IE
    else
        keyPressed = e.which; // Firefox

    if (keyPressed == 13) {
        if(action == "Search"){
            search();
        }
        else if (action == "Login"){
            login();
        }
        else if (action == "FreightTracking"){
            freightTracking();
        }
        else if(action == "ContactUs")
        {
            contactsSearch();
        }
    }
}

function keepSessionAlive() {
    jsrsExecute(Application["ApplicationPath"] + "/ServerScript/Default.aspx", null, "KeepSessionAlive", null);
}

function footerHover(img){
    img.src = img.getAttribute("hoverImage");
}

function footerOut(img){
    img.src = img.getAttribute("normalImage");
}

function freightTracking()
{
    if(trim(document.getElementById("txtTrackingCode").value).length == 0 || trim(document.getElementById("txtTrackingCode").value) == "Enter your tracking number")
    {
        alert("Tracking number is a required field.");
        document.getElementById("txtTrackingCode").focus();
        return false;
    }
    document.getElementById("txtAction").value = "FREIGHT_TRACKING";
    document.getElementById("frmHead").submit();
}


function trackingTextBox_OnBlur(title)
{
    if (document.getElementById("txtTrackingCode").value.length == 0)
    {
        document.getElementById("txtTrackingCode").value = title;
    }
}

function clearTextBox(title)
{
    if (document.getElementById("txtTrackingCode").value == title)
    {
        document.getElementById("txtTrackingCode").value = "";
    }
}

function setCode(code)
{
    document.getElementById("txtCode").value = code;
    if(code == "C")
    {
        document.getElementById("txtTrackingDate").style.display = "none";
    }
    else
    {
        document.getElementById("txtTrackingDate").style.display = "block";
    }
}

function login()
{
    if(trim(document.getElementById("txtHeaderUsername").value).length == 0)
    {
        alert("User name is a required field.");
        document.getElementById("txtHeaderUsername").focus();
        return false;
    }
    if(trim(document.getElementById("txtHeaderPassword").value).length == 0)
    {
        alert("Password is a required field.");
        document.getElementById("txtHeaderPassword").focus();
        return false;
    }
    document.getElementById("txtAction").value = "LOGIN";
    document.getElementById("frmHead").submit();
}

function search()
{
    if(trim(document.getElementById("txtSearchKeywords").value).length == 0)
    {
        alert("You must enter your search keywords.");
        document.getElementById("txtSearchKeywords").focus();
        return false;
    }
    if(trim(document.getElementById("txtSearchKeywords").value).length < 3)
    {
        alert("Your search keywords must be at least 3 characters long");
        document.getElementById("txtSearchKeywords").focus();
        return false;
    }
    document.getElementById("txtAction").value = "SEARCH";
    document.getElementById("frmHead").submit();
}

function document_onload()
{
    if(document.getElementById("txtMainchainURL").value.length > 0)
    {
        openWindow(document.getElementById("txtMainchainURL").value, 'Mainchain', 1060, 750, "resizable=yes, scrollbars=yes, menubar=yes, location=yes, toolbar=yes");
        document.getElementById("txtMainchainURL").value = "";
    }
    if(document.getElementById("txtTrackingDate") != null)
    {
	    $('#txtTrackingDate').datepicker({ dateFormat: 'dd M yy' });
    }
}
function showRegionPopUp(e)
{
    //document.getElementById("divRegionPopUp").style.left = e.clientX + document.body.scrollLeft - 115;
    //document.getElementById("divRegionPopUp").style.top = e.clientY + document.body.scrollTop + 13;
    if(document.getElementById("divRegionPopUp").style.display == "block")
    {
        document.getElementById("divRegionPopUp").style.display = "none";
    }
    else 
    {
        document.getElementById("divRegionPopUp").style.display = "block";
    }
}

function gotoRegion(link)
{
    window.location.href = link;
}