function openCopyright(URL) {
copyrightWindow=window.open(URL,"copyrightwindow","toolbar=no,width=735,height=145,status=no,scrollbars=yes,resize=yes,menubar=no");
}
function openPrivacy(URL) {
privacyWindow=window.open(URL,"privacywindow","toolbar=no,width=650,height=420,status=no,scrollbars=yes,resize=yes,menubar=no");
}
function openScreen(URL) {
crsWindow=window.open(URL,"crswindow","toolbar=no,width=1260,height=700,status=no,scrollbars=yes,resize=yes,menubar=no");
}
function openReceipt(URL) {
page2Window=window.open(URL,"page2window","toolbar=no,width=700,height=520,status=no,scrollbars=yes,resize=yes,menubar=no");
}

//condense the blank check with a function (check for null blank)
function check_for_nb(field,fieldname,phrase) {
if (!phrase) var phrase = "The " + fieldname + " field appears to be blank, this must be entered before proceeding";
if ((field === null) || (field.value == '') || typeof(field.value) === 'undefined') {
alert(phrase);
try {
field.focus();
}
catch (e) {
}
return false;
}
return true;
}

function toggleLayer( whichLayer , force) {
  //options for force are 'hide'/'show'
  if (!force) var force = '';
  
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if (vis.display=='' && elem.offsetWidth != undefined && elem.offsetHeight != undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
  if (force == 'show') vis.display='block';
  if (force == 'hide') vis.display='none';
}


function subProceed(message) {
	if (confirm(message)) {
		return true;
	}
	else {
		return false; 
	}
}

function ValidatePwd(pw1,pw2) {

var minLength = 8; // Minimum length
// check for a value in both fields.
if (pw1.value == '' || pw2.value == '') {
alert('Please enter your password twice.');
return false;
}

// check for minimum length
if (pw1.value.length < minLength) {
alert('Your password must be at least ' + minLength + ' characters long. Try again.');
return false;
}
var empty_string = /^\s*$/; //create RegExp object for re-use

//check for whitespaces
ws = new RegExp(/\s/g);
if (ws.test(pw1.value)) {
alert("Sorry, spaces are not allowed in the password fields.");
return false;
}

if (pw1.value != pw2.value) {
pw1.value = '';
pw2.value = '';
alert ("Sorry but your passwords did not match, please re-enter them and try again.");
return false;
}

return true;
}
function CheckAccept(lang,inst) {
var instr = document.getElementById('chkInstructions');
var terms = document.getElementById('chkTerms');
if (inst == 'ph') { 
var interview = document.getElementById('chkInterview');
if(instr.checked == false || terms.checked == false || interview.checked == false) {
if (lang == 'e') alert('You must tick the checkboxes stating you have read the instructions and agree to the documents required for interview and terms and conditions');
if (lang == 'i') alert('Ní mór duit na ticbhoscaí a thiceáil chun a thaispeáint gur léigh tú na treoracha agus go n-aontaíonn tú leis the documents required for interview agus leis na téarmaí agus leis na coinníollacha');
instr.focus();
return false;
}
}
else{
if (instr.checked == false || terms.checked == false) {
if (lang == 'e') alert('You must tick the checkboxes stating you have read the instructions and agree to the terms and conditions');
if (lang == 'i') alert('Ní mór duit na ticbhoscaí a thiceáil chun a thaispeáint gur léigh tú na treoracha agus go n-aontaíonn tú leis na téarmaí agus leis na coinníollacha');
instr.focus();
return false;
}
}
//all is well
return true;
}
function CheckAccept_pe(lang) {
var instr = document.getElementById('chkInstructions');
var eligible = document.getElementById('chkEligible');
if (instr.checked == false || eligible.checked == false) {
if (lang == 'e') alert('You must tick the checkboxes stating you have read the instructions and the additional information');
if (lang == 'i') alert('Ní mór duit na ticbhoscaí a thiceáil chun a thaispeáint gur léigh tú na treoracha agus the additional information');
instr.focus();
return false;
}
//all is well
return true;
}

function ValidatePPS(ppsn) {
var pps = ppsn;
if (pps != null) {
	if(pps.length !=8) {
		return false;
	}
	else {
		var end = pps.substring(7,8);
		var end = end.toUpperCase().charCodeAt(0);
		var end = (end-64);
		var number=pps.substring(0,7);
		var multiply=8;
		var temp=0;
		for(i=0;i<7;i++) {
			temp=temp+(number.charAt(i))*multiply;		
			multiply=multiply-1;			
		}
		var mod = 23;
		var result=temp%mod;
		if(result==end) {
			return true;
		}
		else {
			return false;
		}
	}
}
return true;
}

//short email function checker using filtering
function CheckEmail(str){
if ((str === null) || (str=="")) var email = "";
else var email = str.value;

var filter=/^(([^<>()[\]\\.,;:\s@""]+(\.[^<>()[\]\\.,;:\s@""]+)*)|("".+""))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i
if (filter.test(email)){
return true;
}
else{
alert("Please input a valid email address!");
str.focus();
return false;
}
return true;
}

function CheckResetForm() {
var email = document.getElementById('txtEmail');
if (!CheckEmail(email)) {
return false;
}

var passworda = document.getElementById('txtPassword');
var passwordb = document.getElementById('txtConfirmPassword');
if (!ValidatePwd(passworda,passwordb)) {
	return false;
}
return true;
}

function CheckLoginForm(ln) {
var email = document.getElementById('txtLoginEmail');
if ((email === null) || (email.value == '')) {
if (ln == 'i') { alert("Is cosúil nach bhfuil aon seoladh ríomhphoist, ní mór é seo a iontráil sula dtéann tú ar aghaidh"); }
else { alert("The email address appears to be blank, this must be entered before proceeding"); }
email.focus();
return false;
}

if(!CheckEmail(document.getElementById('txtLoginEmail'))){
return false;
}

var pass = document.getElementById('txtLoginPass');
if ((pass === null) || (pass.value == '')) {
if (ln == 'i') { alert("Is cosúil nach bhfuil aon phasfhocal iontráilte, ní mór é seo a iontráil sula dtéann tú ar aghaidh"); }
else  { alert("The password appears to be blank, this must be entered before proceeding"); }
pass.focus();
return false;
}

if (pass.value.length < 8) {
if (ln == 'i') { alert('Ní mór go mbeadh 8 gcarachtar ar a laghad i do phasfhocal. Triail arís é.'); }
else { alert('Your password must be at least 8 characters long. Try again.'); }
return false;
}
return true;
}


function update_strength(input_field,output_field,ln) {
var input_field = document.getElementById(input_field);
var output_field = document.getElementById(output_field);
var input_str = input_field.value;
var input_length = input_str.length;
var strength = 0;
var number_re = new RegExp("[0-9]");
if(number_re.test(input_str)) {
	strength++;
}
non_alpha_re = new RegExp("[^A-Za-z0-9]");
if(non_alpha_re.test(input_str)) {
	strength++;
}
upper_alpha_re = new RegExp("[A-Z]");
if(upper_alpha_re.test(input_str)) {	
	strength++;
}
if(input_length >= 8) {
	strength++;
}

if (ln == 'i') {
	var strength_str = "&nbsp;Láidreacht an phasfhocail" + ': ';
	if(strength <= 1) {
		strength_str = strength_str + '<strong style = "color:#EE4444;">' + "Lag" + '</strong>';
	}
	else if(strength <= 2) {
		strength_str = strength_str + '<strong style = "color:#FF9C21;"> '+ "Idir eatarthu" + '</strong>';
	}
	else {
	strength_str = strength_str + '<strong style = "color:#94C65A;">' + "Láidir" + '</strong>';
	}
	if(input_str.length < 8) {
		strength_str = '<strong style = "color:#FFC700;">' + "&nbsp;Róghearr" + '</strong>';
	}
}
else {
	var strength_str = "&nbsp;Password strength" + ': ';
	if(strength <= 1) {
		strength_str = strength_str + '<strong style = "color:#EE4444;">' + "Weak" + '</strong>';
	}
	else if(strength <= 2) {
		strength_str = strength_str + '<strong style = "color:#FF9C21;"> '+ "Medium" + '</strong>';
	}
	else {
	strength_str = strength_str + '<strong style = "color:#94C65A;">' + "Strong" + '</strong>';
	}
	if(input_str.length < 8) {
		strength_str = '<strong style = "color:#FFC700;">' + "&nbsp;Too short" + '</strong>';
	}
}
output_field.innerHTML = strength_str;
}

function UpperC(control_id) {
var elm = document.getElementById(control_id).value;
document.getElementById(control_id).value = elm.toLowerCase();
}

/*
The following functions are ajax and used to check email without
navigating away from the current page.
*/
function toggle_email(userid) {
    var XMLHttpFactories =[function () {return new XMLHttpRequest()},function () {return new ActiveXObject("Msxml2.XMLHTTP")},function () {return new ActiveXObject("Msxml3.XMLHTTP")},function () {return new ActiveXObject("Msxml4.XMLHTTP")},function () {return new ActiveXObject("Msxml5.XMLHTTP")},function () {return new ActiveXObject("Msxml6.XMLHTTP")},function () {return new ActiveXObject("Microsoft.XMLHTTP")}];
    for (var i = 0; i < XMLHttpFactories.length; i++){
        try {http = XMLHttpFactories[i]();}
        catch (e){continue;}
        break;
    }

    handle = document.getElementById(userid);
    var url = 'ajax_email.php?';
    if(handle.value.length > 0) {
        var fullurl = url + 'do=check_email_exists&email=' + encodeURIComponent(handle.value);
        http.open("GET", fullurl, true);
        http.send(null);
        http.onreadystatechange = statechange_email;	
    }else{
        document.getElementById('email_exists').innerHTML = '';
    }
}

function statechange_email() {
    if (http.readyState == 4) {
       	var xmlObj = http.responseXML;
	var html = xmlObj.getElementsByTagName('result').item(0).firstChild;
	if (html === null) {
	//do nothing
	document.getElementById('email_exists').innerHTML = '';
	}
	else {
	document.getElementById('email_exists').innerHTML = html.data;
	}
    }
}
/*
end of ajax code
*/

function check_upload_form(lang) {
var course_code = document.getElementById('course_code');
var doc_type = document.getElementById('doc_type');
var upload = document.getElementById('uploadedfile');
var error = "";

if (course_code.value == "noselect") {
error += "* Course code must be selected\n";
}
if (doc_type.value == "noselect") {
error += "* Please specify a document type\n";
}
if (upload.value == "") {
error += "* No file was chosen\n";
}
var ext = upload.value.substring((upload.value.lastIndexOf(".")+1),upload.value.length);
var extensions = "doc docx txt pdf pjpeg jpeg jpg gif png rtf html";
if (extensions.indexOf(ext) == -1) {
error += "* The file is not of an allowed type\n";
}
if (error !== "") {
alert("There was a problem with your upload, the following error(s) occurred:\n\n" + error);
return false;
}
var temp = confirm('The upload process may take some time, please do not navigate away from this page until it has completed.\n\nNavigating away will cause your document upload to fail.\n\nTo proceed, click "Ok", to cancel click "No".');
if (!temp) {
return false;
}
document.getElementById('div_wait').setAttribute('style','margin:0px auto;text-align:center;display:block;');
return true;
}

