// Copyright 2005-2009 JIC Design - All Rights Reserved 
// Redistribution and use in source and binary forms, with or without
// modification, are not permitted.By reading this documentation you agree
// to be bound by the terms and conditions set out in our license agreement.

String.prototype.trim = function ()
{
   return this.replace(/^\s+|\s+$/g, '') ;
}

function removeSpaces(string) {
 return string.split(' ').join('');
}

//AJAX Code
function createClient() {
 try {
  client = window.XMLHttpRequest ? new XMLHttpRequest() : 
   new ActiveXObject("Microsoft.XMLHTTP");
 } catch (e) { 
  alert("Sorry, your browser is not AJAX-enabled!"); 
 }
}

function genericUpdate(receiverid, script, query) {
 client.onreadystatechange = function() { callback_genericUpdate(receiverid); };
 client.open('GET',script+"?"+query,true); 
 client.send(null);
}
 
function callback_genericUpdate(receiverid)
{
   if(client.readyState == 1)
   {
      document.getElementById(receiverid).innerHTML = "<img src=/images/ajax-loader.gif align=center>";
   }
   if (client.readyState == 4) 
   {
      if (client.status == 200) 
      {
         document.getElementById(receiverid).innerHTML = client.responseText;
         //if(typeof window.tb_reinit == 'function') {
            // function exists, so we can now call it
            //tb_reinit('a.thickbox');
         //}
      }
      else 
      {
         document.getElementById(receiverid).innerHTML = 'There was a problem with the request.';
      }
      createClient();
   }
}
//END AJAX Code

/** type=0 for oen, type=1 for sisid 
 * year=0 for force year, year=1 for choosing year
**/
function checkStudentLogin(type,year) {
	if (type == 0)
	{
		if ((document.getElementById('user1').value.length != 3) || (document.getElementById('user2').value.length != 3) || (document.getElementById('user3').value.length != 3)) {
			alert("Please enter a username.");
			document.getElementById('user1').focus();
			return false;
		}
	}
	else
	{
		if ((document.getElementById('sisid').value.length != 9)) {
			alert("Please enter a username.");
			document.getElementById('sisid').focus();
			return false;
		}
	}
   if (document.loginForm.password.value == "") {
      alert("Please enter a password.");
      document.loginForm.password.focus();
      return false;
   }
   if (year == 1)
   {
	   if (document.getElementById('year').value == 0)
	   {
	      alert("Please choose a year.");
	      document.loginForm.year.focus();
	      return false; 
	   }
   }
   return true; 
}

function checkLogin() {
   if (document.loginForm.username.value == "") {
      alert("Please enter a username.");
      document.loginForm.username.focus();
      return false;
   }
   if (document.loginForm.password.value == "") {
      alert("Please enter a password.");
      document.loginForm.password.focus();
      return false;
   }
   if (document.getElementById('year').value == 0)
   {
      alert("Please choose a year.");
      document.loginForm.year.focus();
      return false; 
   }
   return true; 
}

function checkStudentLogin() {
   if ((document.getElementById('user1').value.length != 3) || (document.getElementById('user2').value.length != 3) || (document.getElementById('user3').value.length != 3)) {
      alert("Please enter a username.");
      document.getElementById('user1').focus();
      return false;
   }
   if (document.loginForm.password.value == "") {
      alert("Please enter a password.");
      document.loginForm.password.focus();
      return false;
   }
   if (document.getElementById('year').value == 0)
   {
      alert("Please choose a year.");
      document.loginForm.year.focus();
      return false; 
   }
   return true; 
}

function getSchoolYearList()
{
   var schoolyearlist = new Ext.data.JsonStore({
        totalProperty: 'total',
        root: 'results',
        fields : [
            {
                name: 'schoolyearname'
            },
            {
                name: 'schoolyearid'
            }
        ],
        url : '/admin/php/get-schoolyearlist.php',
        autoLoad: true
    });
   return schoolyearlist;
}

/** Core functions */
var browserType;
if (document.layers) {browserType = "nn4"}
if (document.all) {browserType = "ie"}
if (window.navigator.userAgent.toLowerCase().match("gecko")) {
   browserType= "gecko"
}

function hide(divname) {
  if (browserType == "gecko" )
  {
     document.poppedLayer = eval('document.getElementById(divname)');
     document.getElementById(divname).style.display = 'none';
  }
  else if (browserType == "ie")
     document.poppedLayer = eval('document.getElementById(divname)');
  else
     document.poppedLayer = eval('document.layers[divname]');
  document.poppedLayer.style.visibility = "hidden";
}

function show(divname) {
  if (browserType == "gecko" )
  {
     document.poppedLayer = eval('document.getElementById(divname)');
     document.getElementById(divname).style.display = 'inline'; 
  }
  else if (browserType == "ie")
     document.poppedLayer = eval('document.getElementById(divname)');
  else
     document.poppedLayer = eval('document.layers[divname]');         
     document.poppedLayer.style.visibility = "visible";
}


function zeroPad(num,count)
{
  if (num.length != count)
  {
	  var numZeropad = num + '';
	  while(numZeropad.length < count) {
	    numZeropad = "0" + numZeropad;
	  }
	  return numZeropad;
  }
  else
	  return num;
}

function capitalizeFirstLetter(string)
{
  return string.charAt(0).toUpperCase() + string.slice(1);
}





/** Student info related functions */
function setBirthProvince ()
{
   if (document.getElementById("birthcountry").value != 'Canada')
   {
      document.getElementById("birthprovinceother").selected = true;
   }
}

function checkArrivalDate()
{
   if ((document.getElementById("birthcountry").value != 'Canada') ||  (document.getElementById("citizenship").value != 'Canada'))
   {
      show('divcitizenship');
   }
   else if ((document.getElementById("birthcountry").value == 'Canada') &&  (document.getElementById("citizenship").value == 'Canada'))
   {
      hide('divcitizenship');
   }  
}




//Validating code for forms throughout admin and student side
var blank = 0;

// generic function to validate form fields
// type: 1 = string, 2 = int, 3 = select, 4 = email
function validateFormField (id1, type)
{
   for (i=0; i<id1.length; i++)
   {
      if (type[i] == 1)
      {
         validateString(id1[i]);
      }
      if (type[i] == 2)
      {
         validateInt(id1[i]);
      }
      if (type[i] == 3)
      {
         validateSelect(id1[i]);
      }
      if (type[i] == 4)
      {
         validateEmail(id1[i]);
      }
      if (type[i] == 5)
      {
         validatePassword(id1[i]);
      }
      if (blank == 1)
      {
         blank = 0;
         return false;
      }
   }
}

function validateSingleFormField (id1, type)
{
	if (type == 1)
   {
      validateString(id1);
   }
   if (type == 2)
   {
      validateInt(id1);
   }
   if (type == 3)
   {
      validateSelect(id1);
   }
   if (type == 4)
   {
      validateEmail(id1);
   }
   if (blank == 1)
   {
      blank = 0;
      return false;
   }
}

function validateString(id1)
{
   if (document.getElementById(id1).value.trim() == "")
   { 
      alert("The following field is required and has not been completed: " + id1);
      document.getElementById(id1).focus();
      blank = 1;
   }
}

function validateInt(id1)
{
   if (document.getElementById(id1).value.trim() == "" || isNaN(document.getElementById(id1).value)==true || Math.ceil(document.getElementById(id1).value)!=document.getElementById(id1).value)
   {
      alert("The following field must be an Integer : " + id1);
      document.getElementById(id1).focus();
      blank = 1;
   } 
}

function validateSelect(id1)
{
   if ((document.getElementById(id1).value == 0) || (document.getElementById(id1).value == ""))
   { 
      alert("The following field is required and has not been completed: " + id1);
      document.getElementById(id1).focus();
      blank = 1;
   }
}

function validateEmail(id1)
{
	regex = /^([^,></=\.\"\'?@]+(\.[^,></=\.\"\'?@]+)*)+@+([^,><\"\.']+\.)+[a-zA-Z]{2,5}$/;
   if(!document.getElementById(id1).value.match(regex)){
     alert("Invalid email format");
     blank = 1;
   }
}

function validateFieldSize (id2, size)
{
   for (i=0; i<id2.length; i++)
   {
      if (document.getElementById(id2[i]).value.length != size[i])
      {
         alert(id2[i]+" needs to be "+size[i]+" characters.");
         document.getElementById(id2[i]).focus();
         return false;
      }
   }
}

function validateFormFieldAndSize(id1, type2, id2, size)
{
   if ((validateFormField (id1, type2) == false) || (validateFieldSize (id2, size) == false))
   {
     return false;
   }
   else
   {
     return true;
   }
}

function validateStudentRegisterForm(id1, type2, id2, size)
{
   if ((validateFormField (id1, type2) == false) || (validateFieldSize (id2, size) == false))
   {
      return false;
   }
   if (document.getElementById("birthcountry").value != 'Canada' || document.getElementById("citizenship").value != 'Canada')
   {
      if ((document.getElementById("arrivalyear").value == "") || (document.getElementById("arrivalmonth").value == "") || (document.getElementById("arrivalday").value == ""))
      {
         alert("The following field is required: Arrival Date");       
         return false;
      }
   }
   
}

function autoTab (object1, length1, object2)
{
   if (document.getElementById(object1).value.length == length1)
   {
      document.getElementById(object2).focus();
   }
}

function compareStrings(string1,string2)
{
	if (string1 == string2)
		return true;
	else
		return false;
}

function verifySecurityPasswordChange(temppassword,newpassword1,newpassword2)
{
	validateString(temppassword);
	validatePassword(newpassword1);
	validatePassword(newpassword2);
	if (blank == 1)
		return false;
	else
	{
		if (compareStrings(document.getElementById(newpassword1).value,document.getElementById(newpassword2).value) == true)
			return true;
		else
		{
			alert("The passwords entered do not match. Please try again.");
		    document.getElementById(newpassword1).focus();
		    return false;
		}
	}
	
}

function validatePassword(password)
{
	if (document.getElementById(password).value.length < 8 || document.getElementById(password).value.length > 12)
	{
		alert("The password entered must be between 8 and 12 characters. Please try again");
	    document.getElementById(password).focus();
	    blank = 1;
	}
}

