// Opens links in new (smaller) window
var newwindow = '';
function openwin(page)
{
    if (!newwindow.closed && newwindow.location) {
		newwindow.location.href = page;
	}
	else {
		newwindow=window.open(page,'od','height=640,width=670,toolbar=yes,scrollbars=yes, resizable=yes, top=75, left=200');
		if (!newwindow.opener) newwindow.opener = self;
	}

	if (window.focus) {newwindow.focus()}
}

//*********************************************************************************
//The following functions are used in mail_results.php
var text = "";
AddTxt = "";

function validate(theform) {
	if (theform.message.value=="" || theform.subject.value=="") {
		alert("Please complete the subject and message fields.");
		return false;
	} 
	
	if (theform.sender_name.value=="" || theform.sender_email.value=="") {
		alert("Please complete the sender name and email fields.");
		return false;
	}
	
	var confirmation = confirm("Are you sure you are ready to send this email?");
	if (!confirmation) {
		return false;
	}
}

function stat(thevalue) {
        document.email_results_form.status.value = eval(thevalue+"_text");
}

//*********************************************************************************
//The following functions are used in user_registration.php


function underAge(theform) {
	var ageLimit = "";
	var d = new Date();
	var date = d.getDate();	
	var month = d.getMonth() + 1;
	var year = d.getFullYear();
	var user_date = theform.birthday.value;
	var user_month = theform.month_id.value;
	var user_year = theform.birthyear.value;
	
	if (theform.province_id.value=="AB" || theform.province_id.value=="MB" || theform.province_id.value=="SK" || theform.province_id.value=="ON" || theform.province_id.value=="QC" || theform.province_id.value=="PE") {
		ageLimit = 18;
	} else {
		ageLimit = 19;
	}
	
	if (user_month < month) {
		if (!((year-user_year) >= ageLimit)){
			var confirmation = confirm("If you are under the age of majority in your province or territory we are legally required to get your parent's consent before you can participate. Minors who have not received parental permission to participate will not be eligible to win a scholarship.\n\nIf any winner is a minor, the parent/guardian must accept the prize on the behalf of the minor and execute all necessary documents.  All winners will be required to supply proof of age.\n\nYou are under the age of majority.\n\nDo you have your parent's permission to participate in this contest?");
			if (!confirmation) {
				return false;
			}
		} 
	} else {
		if (user_month == month) {
			if (user_date <= date) {
				if (!((year-user_year) >= ageLimit)) {
					var confirmation = confirm("If you are under the age of majority in your province or territory we are legally required to get your parent's consent before you can participate. Minors who have not received parental permission to participate will not be eligible to win a scholarship.\n\nIf any winner is a minor, the parent/guardian must accept the prize on the behalf of the minor and execute all necessary documents.  All winners will be required to supply proof of age.\n\nYou are under the age of majority.\n\nDo you have your parent's permission to participate in this contest?");
					if (!confirmation) {
						return false;
					}
				}
			} else {
				if (!((year-(user_year+1)) >= ageLimit)) {
					var confirmation = confirm("If you are under the age of majority in your province or territory we are legally required to get your parent's consent before you can participate. Minors who have not received parental permission to participate will not be eligible to win a scholarship.\n\nIf any winner is a minor, the parent/guardian must accept the prize on the behalf of the minor and execute all necessary documents.  All winners will be required to supply proof of age.\n\nYou are under the age of majority.\n\nDo you have your parent's permission to participate in this contest?");
					if (!confirmation) {
						return false;
					}
				}
			}
		} else {
			if (!((year-(user_year+1)) >= ageLimit)) {
				var confirmation = confirm("If you are under the age of majority in your province or territory we are legally required to get your parent's consent before you can participate. Minors who have not received parental permission to participate will not be eligible to win a scholarship.\n\nIf any winner is a minor, the parent/guardian must accept the prize on the behalf of the minor and execute all necessary documents.  All winners will be required to supply proof of age.\n\nYou are under the age of majority.\n\nDo you have your parent's permission to participate in this contest?");
				if (!confirmation) {
					return false;
				}
			}
		}
	}

}

//*********************************************************************************
//The following functions are used in contest_school_registration.php

function student_leader() {

	document.school_register.leader_firstname.value='';

}

//*********************************************************************************
//The following functions are used in user_registration.php
var text = "";
AddTxt = "";

function user_validate(thisform) {
	if (!(thisform.underage_permission[0].checked || thisform.underage_permission[1].checked)) {
		alert("Please choose an answer.");
		return false;
	} 
}

function end_load() {
	document.getElementById("loading").innerHTML = " ";
}

function xmlhttpPost(strURL) {
	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    var form     = document.forms['user_registration'];
    var word = form.userid.value;
    qstr = 'username=' + escape(word)  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str){
    document.getElementById("username_error").innerHTML = str;
}
