//THIS CONTAINS ALL THE FUNCTIONS THE VARIOUS SCHOOLIES PAGES USE

function popup(mylink, windowname)
{
if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
		href=mylink;
	else
		href=mylink.href;

	window.open(href, windowname.replace(' ',''), 'width=800,height=500,resizable=yes,scrollbars=yes');
	return false;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() 
{ //v3.0
	var d=document;
	if(d.images)
	{ 
		if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
		for(i=0; i<a.length; i++)
		{
			if (a[i].indexOf("#")!=0)
			{ 
				d.MM_p[j]=new Image; 
				d.MM_p[j++].src=a[i];
			}
		}
	}
}
function MM_nbGroup(event, grpName) { //v3.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : args[i+1];
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    if ((nbArr = document[grpName]) != null)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = args[i+1];
      nbArr[nbArr.length] = img;
  } }
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=300,left = 200,top = 150');");
}
function isEmail(emailToCheck)
{
	if (emailToCheck.length > 8)
	{
		if (emailToCheck.indexOf("@") > 0 && emailToCheck.indexOf(".") > 0)
			return true;
		else
			return false;
	}
	else
		return false;
}

function isNumberInt(inputString)
{
  return (!isNaN(parseInt(inputString))) ? true : false;
}

function isNumberFloat(inputString)
{
  return (!isNaN(parseFloat(inputString))) ? true : false;
}

//this is a wrapper function to maintain backwards compatibility
//with the other pages that call the createWindow function  
function createWindow(URL) {
	return createWindowCustom(URL, 500, 450);
}

//this is a new function to open a custom sized window
function createWindowCustom(URL, intWidth, intHeight) {
	var features = "width=" + intWidth + ",height=" + intHeight + ",left=100,top=100,location=0,menubar=0,scrollbars=1,status=0,toolbar=0,resizable=1;"
	popup=window.open(URL, 'Costings', features);
	popup.focus();
	return true;	
}

function printWindow() {
	bV = parseInt(navigator.appVersion);
	if (bV >= 4) window.print();
}

//this is a new function that closes the current window and opens it in the parent
function doOpener()
{
	if (window.opener != null)
	{
		window.opener.location = window.location;
		window.close();
	}
}

function isDate(date)
{
	var dateArray = date.split("/");

	if (dateArray.length != 3)
	{
		return false;
	}

	var ArrMonthNames = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];

    // Apply validation and report failures

	var ExpValDay    = /^(0?[1-9]|[1-2]\d|3[0-1])$/,
                     ExpValMonth  = new RegExp('^(0?[1-9]|1[0-2]|'        +
                                                  ArrMonthNames.join('|') +
                                                  ')$','i'),
                     ExpValYear   = /^(\d{1,2}|\d{4})$/;



    if (ExpValYear.exec(dateArray[2])  == null ||
        ExpValMonth.exec(dateArray[1]) == null ||
        ExpValDay.exec(dateArray[0])   == null ||
		date == 'dd/mmm/yyyy')
	{
		return false;
	}
	else
	{
		return true;
	}
}

//this is a function that checks the form for submitting. It ASSUMES the 
//form to check is called form1 it checks all the fields in the fieldsToCheck 
//variable and all fields with the word "email" in it
//if checkPassword = true then the checker will verify the passwords match
function checkForm(fieldsToCheck,checkPassword) 
{
	var errorCount = 0;
	var emailErrorCount = 0;
	var fieldValidationErrorCount = 0;
	var errorMessage = "The form could not be submitted because of the following error(s): \n\n";
	var i;
	var invalidField;
	var dobErrorCount = 0;
	//var fieldsToCheck = "txtFirstname|txtSurname|txtBPhoneNumber|txtCPhoneNumber|cboSchool|txtPasswordOne|txtPasswordTwo";
	
	for (i = 0; i < document.form1.elements.length; i++)
	{
		if ((document.form1.elements[i].type == "checkbox") && (!document.form1.elements[i].checked) && (document.form1.elements[i].name == "termsandconditions"))
		{	
			errorCount++;
			invalidField = document.form1.elements[i];
		}
		 //The following fields can only contain numbers.
		if ((document.form1.elements[i].name == "postcode") || (document.form1.elements[i].name == "phone") || (document.form1.elements[i].name == "mobile") )
		{
				if ((document.form1.elements[i].value != "") && (document.form1.elements[i].value != "invalid"))
				{
					// replace all whitespace within string.
					var mystring = document.form1.elements[i].value.replace(/\s+/g, "");
					//find character values within string.
					if ((mystring.match(/\D+/g)))  
					{
						fieldValidationErrorCount++;
						invalidField = document.form1.elements[i];
					}  			
				}
		}
	
		// Check DOB field is not set to default value of dd/mmm/yyyy
		if(document.form1.elements[i].name.toUpperCase().indexOf("DOB") > -1 && fieldsToCheck.indexOf(document.form1.elements[i].name) > -1 && !isDate(document.form1.elements[i].value))
		{
			dobErrorCount++;
			invalidField = document.form1.elements[i];		
		}

		if (document.form1.elements[i].type != "hidden" && fieldsToCheck.indexOf(document.form1.elements[i].name) > -1 && (document.form1.elements[i].value == "" || document.form1.elements[i].value == "invalid" || document.form1.elements[i].value == "-1")) 
		{
			errorCount++;
			invalidField = document.form1.elements[i];
		}
		if (document.form1.elements[i].name.toUpperCase().indexOf("EMAIL") > -1 && fieldsToCheck.indexOf(document.form1.elements[i].name) > -1 && !isEmail(document.form1.elements[i].value))
		{
			emailErrorCount++;
			invalidField = document.form1.elements[i];
		}
	}
	if (errorCount > 0)
		errorMessage += "* All fields marked with an (*) are required. There were " + errorCount + " field(s) left empty.\n";
	if (emailErrorCount > 0)
		errorMessage += "* All email addresses must be valid. There were " + emailErrorCount + " email field(s) invalid.\n";	
	if (dobErrorCount > 0)
		errorMessage += "* DOB fields must have correct dates. There were " + dobErrorCount + " DOB field(s) invalid.\n";
	if (fieldValidationErrorCount > 0)
	{
		errorMessage += "* Phone and Postcode fields can only contain numbers.\n";
	}
	if (checkPassword && document.form1.txtPasswordOne.value != document.form1.txtPasswordTwo.value)
	{
		errorCount++;
		errorMessage = errorMessage + "* Both passwords entered must be the same\n";
		invalidField = document.form1.txtPasswordOne;
	}
	if (errorCount > 0 || emailErrorCount > 0 || fieldValidationErrorCount > 0 || dobErrorCount > 0)
	//if (errorCount > 0 || emailErrorCount > 0)
	{
		alert(errorMessage + "\nPlease click OK to try again.");
		invalidField.focus();
		return false;
	}
	else
		return true;
}

// function that checks all options in a given radio list to see if one of them is checked.
function checkRadioControl(strFieldName){
      intControlLength = strFieldName.length
      bolSelected = false;
      for (i=0;i<intControlLength;i++){
      if(strFieldName[i].checked){
      bolSelected = true;
      break;
      }
    }    
     if(! bolSelected){
         return false;
         }
    return true;
}

//this is a function that checks the form for submitting. It ASSUMES the 
//form to check is called form1 it checks all the fields in the fieldsToCheck 
//variable and all fields with the word "email" in it
//if checkPassword = true then the checker will verify the passwords match
function checkAnotherForm(theForm,fieldsToCheck,radioToCheck,checkPassword) 
{
	var errorCount = 0;
	var qErrorCount = 0;
	var emailErrorCount = 0;
	var errorMessage = "The form could not be submitted because of the following error(s): \n\n";
	var i;
	var invalidField;
	var radioList = radioToCheck.split("|");
	//var fieldsToCheck = "txtFirstname|txtSurname|txtBPhoneNumber|txtCPhoneNumber|cboSchool|txtPasswordOne|txtPasswordTwo";
	
	for (i = 0; i < theForm.elements.length; i++)
	{
		if (theForm.elements[i].type != "hidden" && fieldsToCheck.indexOf(theForm.elements[i].name) > -1 && (theForm.elements[i].value == "" || theForm.elements[i].value == "invalid")) 
		{
			errorCount++;
			invalidField = theForm.elements[i];
		}
		if (theForm.elements[i].name.toUpperCase().indexOf("EMAIL") > -1 && fieldsToCheck.indexOf(theForm.elements[i].name) > -1 && !isEmail(theForm.elements[i].value))
		{
			emailErrorCount++;
			invalidField = theForm.elements[i];
		}
	}

    // check any radio buttons to make sure that they have been selected.
	for (i=0; i<radioList.length; i++) {
	  if (!checkRadioControl(theForm[radioList[i]])) {
		qErrorCount++;
		invalidField = theForm.elements[i];
	  }
	}
	
	if (errorCount > 0)
		errorMessage += "* All fields marked with an (*) are required. There were " + errorCount + " field(s) left empty.\n";
	if (emailErrorCount > 0)
		errorMessage += "* All email addresses must be valid. There were " + emailErrorCount + " email field(s) invalid.\n";	
	if (qErrorCount > 0)
		errorMessage += "* All survey questions must be answered. There were " + qErrorCount + " question(s) unanswered.\n";	
	if (checkPassword && theForm.txtPasswordOne.value != theForm.txtPasswordTwo.value)
	{
		errorCount++;
		errorMessage = errorMessage + "* Both passwords entered must be the same\n";
		invalidField = theForm.txtPasswordOne;
	}
	if (errorCount > 0 || emailErrorCount > 0 || qErrorCount > 0)
	{
		alert(errorMessage + "\nPlease click OK to try again.");
		if (errorCount > 0 || emailErrorCount > 0) {
			invalidField.focus();
		}
		return false;
	}
	else
		return true;
}


function focusWindow()
{
	window.focus();
}

function doMap(locationCode)
{
	if (window.opener != null)
	{
		window.opener.location = "AllotmentSearch.asp?buildingcode=" + locationCode;
		window.close();
	}
}

function win( pageToLoad, winName, width, height, center, scrollbars) {
    xposition=0; yposition=0;
    if ((parseInt(navigator.appVersion) >= 4 ) && (center)){
        xposition = (screen.width - width) / 2;
        yposition = (screen.height - height) / 2;
    }
    args = "width=" + width + "," 
    + "height=" + height + "," 
    + "location=0," 
    + "menubar=0,"
    + "resizable=1,"
    + "scrollbars=" + scrollbars + ","
    + "status=0," 
    + "titlebar=0,"
    + "toolbar=0,"
    + "hotkeys=0,"
    + "screenx=" + xposition + ","  //NN Only
    + "screeny=" + yposition + ","  //NN Only
    + "left=" + xposition + ","     //IE Only
    + "top=" + yposition;           //IE Only

    window.open( pageToLoad,winName,args );
	window.name = 'mediaArticles';
}

function validate(string) {
    if (!string) return false;
    var Chars = "0123456789";

    for (var i = 0; i < string.length; i++) {
       if (Chars.indexOf(string.charAt(i)) == -1)
          return false;
    }
    return true;
}

function makePrintable()
{
	var header, navigation, login, footer, passwordforget;

	if (document.getElementById)
	{
		header = document.getElementById('header');
		navigation = document.getElementById('navigation_bar');
		passwordforget = document.getElementById('passwordforget');
		login = document.getElementById('login');
		footer = document.getElementById('footer');

		header.style.display = 'none';
		navigation.style.display = 'none';
		footer.style.display = 'none';
	}
}