// JavaScript Document

//determining whether msie or netscape being used and what versions 
var IE4 = (document.all && !document.getElementById) ? 1 : 0;
var NS4 = (document.layers) ? 1 : 0;
var IE5 = (document.all && document.getElementById) ? 1 : 0;
var N6 = (document.getElementById && !document.all) ? 1 : 0;
var ie = (IE4 || IE5) ? 1 : 0;
var n = (NS4 || N6) ? 1 : 0;
var IE5MAC = false;
var NS4MAC = false;
var tempVar;

var MAC = (navigator.userAgent.indexOf("Mac")) // determines platform

if (MAC != -1) {
    if (IE5) {
        IE5MAC = true; //determines if ie5 on a mac
    }
    else {
        if (NS4 || N6) {
            NS4MAC = true; // determines if net 4 or 6 on mac
        }
    }
}
else {
}

// error handling 
window.onerror = errorHandler;
function errorHandler() {
    return true;
}


/* this is a work a round for the netscape resize bug */
if (document.layers) {
    origWidth = innerWidth;
    origHeight = innerHeight;
}

function correctResize() {
    if (innerWidth != origWidth || innerHeight != origHeight)
        location.reload();
}
if (document.layers) onResize = correctResize;

function populateHeight() {
    if (screen.height > 768) {
        if (document.body.clientHeight) {
            if (navigator.userAgent.indexOf("Firefox") != -1) {
                document.getElementById("centerContent").style.minHeight = screen.height - 570 + "px";
            } else if (navigator.userAgent.indexOf("Opera") != -1) {
                document.getElementById("centerContent").style.minHeight = screen.height - 600 + "px";
            } else if (navigator.userAgent.indexOf("Netscape") != -1) {
                document.getElementById("centerContent").style.minHeight = screen.height - 600 + "px";
            } else if (navigator.userAgent.indexOf("MSIE 7.0") != -1) {
                document.getElementById("centerContent").style.minHeight = screen.height - 540 + "px";
            } else {
                document.getElementById("centerContent").style.height = screen.height - 510 + "px";
            }
        }
    }
}

function checkSearchBox() {

    if (document.getElementById("searchBox").value.length > 0) {
        if (document.getElementById("searchBox").value == "please type a phrase here") {
            document.getElementById("searchBox").value = "";
        }
    }
    else {
        document.getElementById("searchBox").value = "please type a phrase here";
    }
}

function loadSearchBoxEvents() {
    document.getElementById("searchBox").onfocus = function() { checkSearchBox(); };
    document.getElementById("searchBox").onblur = function() { checkSearchBox(); };
}

function checkSelected() {
    if (document.getElementById("quickLinks").value != "") {
        window.location = document.getElementById("quickLinks").value;
    }
}

function displayQuickLinks() {
    document.write("<select onchange=\"checkSelected()\" name=\"quickLinks\" id=\"quickLinks\"><option value=\"\" selected=\"selected\">Quick links...</option><option value=\"/choosing_its/why_us.htm\">Why Us?</option><option value=\"/solutions.htm\">Solutions</option><option value=\"/solutions/ongoing_trusteeship.htm\">Ongoing Trusteeship</option><option value=\"/solutions/corporate_transactions.htm\">Corporate Transactions</option><option value=\"/solutions/communications_services.htm\">Communications Services</option><option value=\"/contact_us.htm\">Contact Us</option><option value=\"/media_centre/latest_brochure.htm\">Latest Brochure</option></select>");
}

function emailcheck(str) {

    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)

    if ((str == null) || (str == "")) {
        return false;
    }

    if (str.indexOf(at) == -1) {
        return false;
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false;
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        return false;
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        return false;
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        return false;
    }


    if (str.indexOf(dot, (lat + 2)) == -1) {
        return false;
    }

    if (str.indexOf(dot) + 1 == lstr) {
        return false;
    }

    if (str.indexOf(" ") != -1) {
        return false;
    }

    return true;
}

function validate(e) {
    var check;
    var count;
    var current;
    var error = false;
    count = 0;
    var order = false;

    for (var i = 0; i < e.elements.length; i++) {

        if (e.elements[i].type == "text" || e.elements[i].type == "textarea" || e.elements[i].type == "password") {
            if (e.elements[i].name == "city_town" || e.elements[i].name == "county") {

                continue;
            } else if (e.elements[i].value == "" || e.elements[i].value == e.elements[i].defaultValue) {
                if (document.all)
                    alert("Please enter a value for \n\"" + eval("L" + e.elements[i].name + ".innerText") + "\"");
                else
                    alert("Please enter a value for \n\"" + eval("document.getElementById('L" + e.elements[i].name + "').textContent") + "\"");

                error = true;
                e.elements[i].focus();
                break;
            }

            if (e.elements[i].name == "email" || e.elements[i].name == "confirm_email" || e.elements[i].name == "email2") {
                if (!(emailcheck(e.elements[i].value))) {
                    alert("Please enter a valid email address");
                    error = true;
                    e.elements[i].focus();
                    break;
                }
            }

        } else if (e.elements[i].type == "radio" || e.elements[i].type == "checkbox") {
            /*
            check = false;
            count = 0;
            current = i;
            for (var j=0; j < eval("e." + e.elements[i].name + ".length"); j++) {
            if (eval("e." + e.elements[i].name + "[" + j + "].checked")) {
            check = true;
            count++;
            }
            }		
					
					if (!check) {
            alert("Please select a value");
            error = true;
            e.elements[i].focus();
            break;
            }*/
            //i = i + 1;		

        } else if (e.elements[i].type == "select-one") {
            if (e.elements[i].value == "" || e.elements[i].value == e.elements[i].defaultValue) {
                alert("Please enter a value for \n\"" + eval("L" + e.elements[i].name + ".innerText") + "\"");
                error = true;
                e.elements[i].focus();
                break;
            }

        }

    }


    if (!error) {
        return true;
    } else {
        return false;
    }
}

function init() {
    //populateHeight();

    var allPageTags = new Array();
    var allPageTags = document.getElementsByTagName("a");
    for (i = 0; i < allPageTags.length; i++) {  //Pick out the tags with our class name  
        if (allPageTags[i].className == "popup") {
            allPageTags[i].target = "_blank";
        }
    }
}


window.onload = init;
