// JScript source code
function isValidEmail(str) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str)){
		return true;
	}
	else {
		return false;
	}
}

function checkenquiry(){
	var ftxt = '';

	if (document.enquiry.Name.value==''){
		ftxt += '\n- Please enter your Name.';
	}
	
	if (document.enquiry.Company.value==''){
		ftxt += '\n- Please enter your Company.';
	}
	
	if (document.enquiry.Telephone.value==''){
		ftxt += '\n- Please enter your Telephone Number.';
	}
	
	if (isValidEmail(document.enquiry.email.value)==false){
		ftxt += '\n- Please enter your Email Address.';
	}
	
	if (document.enquiry.subject.value==''){
		ftxt += '\n- Please select an enquiry Subject.';
	}
	
	if (ftxt!==''){
		alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
		return false;
	}
	else {
		return true;
	}
}


// JS Functions
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function emailCloak() {
    if (document.getElementById) {
        var alltags = document.all ? document.all : document.getElementsByTagName("*");
        for (i = 0; i < alltags.length; i++) {
            if (alltags[i].className == "emailCloak") {
                var oldText = alltags[i].firstChild;
                var emailAddress = alltags[i].firstChild.nodeValue;
                var user = emailAddress.substring(0, emailAddress.indexOf("("));
                var website = emailAddress.substring(emailAddress.indexOf(")") + 1, emailAddress.length);
                var newText = user + "@" + website;
                var a = document.createElement("a");
                a.href = "mailto:" + newText;
                var address = document.createTextNode(newText);
                a.appendChild(address);
                alltags[i].replaceChild(a, oldText);
            }
        }
    }
}
addLoadEvent(emailCloak);


function clearemailfield(fieldVal){
	if (isValidEmail(fieldVal)==false){
		document.newsletterform.email.value='';
	}
}

function clearText(field){
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}

function mainmenu(){
	$(" #uppernavigation ul ").css({display: "none"}); // Opera Fix
	$(" #uppernavigation li").hover(function(){
			$(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown(0);
			},function(){
			$(this).find('ul:first').css({visibility: "hidden"});
			});
	}

 $(document).ready(function(){					
	mainmenu()
});

/* Site Search */
function checksitesearch() {
    if (document.sitesearch.Criteria.value == '') {
        alert('Please enter your Search Criteria.');
        return false;
    }
    else {
        return true;
    }
}

/* Events Quick Panel */
function loadurl(dest) {
    try {
        // Moz supports XMLHttpRequest. IE uses ActiveX.
        // browser detction is bad. object detection works for any browser
        xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        // browser doesn't support ajax. handle however you want		
    }
    // the xmlhttp object triggers an event everytime the status changes
    // triggered() function handles the events
    //xmlhttp.onreadystatechange = triggered;	

    xmlhttp.onreadystatechange = function() {
        if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
            //alert(xmlhttp.responseText)
            document.getElementById("eventsbox").innerHTML = xmlhttp.responseText;
        }
    }

    // open takes in the HTTP method and url.
    xmlhttp.open("GET", dest);
    // send the request. if this is a POST request we would have
    // sent post variables: send("name=aleem&gender=male)
    // Moz is fine with just send(); but
    // IE expects a value here, hence we do send(null);
    xmlhttp.send(null);
}

/* Print Page */
function PrintThisPage() {
    var sOption = "toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes,width=750,height=600,left=100,top=25";
    var winprint = window.open('/Scripts/PrintPage.htm', 'Print', sOption);
    winprint.focus();
}

/* Newsletter Form */
function checknewsletterform() {
    var ftxt = '';

    if (document.newsletterform.ParentsName.value == '') {
        ftxt += '\n- Please enter a Parents Name.';
    }

    if (isValidEmail(document.newsletterform.ParentsEmail.value) == false) {
        ftxt += '\n- Please enter a Parents Email.';
    }

    if (document.newsletterform.StudentsName.value == '') {
        ftxt += '\n- Please enter a Students Name.';
    }

    if (isValidEmail(document.newsletterform.StudentsEmail.value) == false) {
        ftxt += '\n- Please enter a Students Email.';
    }

    if (ftxt !== '') {
        alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
        return false;
    }
    else {
        return true;
    }
}
