//-----------------------------------------------------------------------------
// Define some constants.
//-----------------------------------------------------------------------------

// Define a list of Microsoft XML HTTP ProgIDs.
var XMLHTTPREQUEST_MS_PROGIDS = new Array(
	"Msxml2.XMLHTTP.7.0",
	"Msxml2.XMLHTTP.6.0",
	"Msxml2.XMLHTTP.5.0",
	"Msxml2.XMLHTTP.4.0",
	"MSXML2.XMLHTTP.3.0",
	"MSXML2.XMLHTTP",
	"Microsoft.XMLHTTP"
);

// Define ready state constants.
var XMLHTTPREQUEST_READY_STATE_UNINITIALIZED = 0;
var XMLHTTPREQUEST_READY_STATE_LOADING       = 1;
var XMLHTTPREQUEST_READY_STATE_LOADED        = 2;
var XMLHTTPREQUEST_READY_STATE_INTERACTIVE   = 3;
var XMLHTTPREQUEST_READY_STATE_COMPLETED     = 4;

function Move( Source, Destination ){
	for( var I = Source.options.length-1; I >= 0; I-- ){
		if( Source.options[I] != null && Source.options[I].selected){
			x = Destination.options.length;
			Destination.options.length = x+1;
			Destination.options[x].text = Source.options[I].text;
			Destination.options[x].value =  Source.options[I].value;
			Source.options[I] = null;
		}
	}
}

	function MoveAll( Source, Destination ){
		for( var I = Source.options.length-1; I >= 0; I-- ){
			if( Source.options[I] != null ){
				x = Destination.options.length;
				Destination.options.length = x+1;
				Destination.options[x].text = Source.options[I].text;
				Destination.options[x].value =  Source.options[I].value;
				Source.options[I] = null;
				
			}
		}
	}

	function selectcon(frmsort){
		for( i = 0; i < frmsort.GroupsSend.options.length; i++ ){
			frmsort.GroupsSend.options[i].selected = true;
		}
		for( i = 0; i < frmsort.UsersSend.options.length; i++ ){
			frmsort.UsersSend.options[i].selected = true;
		}
	}


	function selectall(frmsort){
	for( i = 0; i < frmsort.groupsSend.options.length; i++ ){
		frmsort.groupsSend.options[i].selected = true; }
	}
	function selectall2(){
	if(a = document.getElementById("topcatSend")) {
		for( i = 0; i < a.options.length; i++ ){
			a.options[i].selected = true; }
		}
	}
	function select_users() {
		if(users = document.getElementById("docusersSend")) {
			for( i = 0; i < users.options.length; i++ ){
				users.options[i].selected = true; }
		}
		if(groups = document.getElementById("docgroupsSend")) {
			for( i = 0; i < groups.options.length; i++ ){
				groups.options[i].selected = true; }
		}
	}

//-----------------------------------------------------------------------------
// Returns an XMLHttpRequest object.
//-----------------------------------------------------------------------------
function getXMLHttpRequest()
{
	var httpRequest = null;

	// Create the appropriate HttpRequest object for the browser.
	if (window.XMLHttpRequest != null)
		httpRequest = new window.XMLHttpRequest();
	else if (window.ActiveXObject != null)
	{
		// Must be IE, find the right ActiveXObject.
		var success = false;
		for (var i = 0; i < XMLHTTPREQUEST_MS_PROGIDS.length && !success; i++)
		{
			try
			{
				httpRequest = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
				success = true;
			}
			catch (ex)
			{}
		}
	}

	// Display an error if we couldn't create one.
	if (httpRequest == null)
		alert("Error in HttpRequest():\n\nCannot create an XMLHttpRequest object.");

	// Return it.
	return httpRequest;
}

var getPage = getXMLHttpRequest();
function Navigation(url)
{
	
	
	
	getPage.open("GET", url, false);
	getPage.send(null);

	// Fill in the city and state fields, if available.
	try
	{
		var data = getPage.responseText;
		return data;
	}
	catch (ex)
	{}
}

function getRemoteData(url,selectedVal,rewriteElemId,outPutElem){
	url=url+selectedVal+"&nochace="+Math.random()
	data=Navigation(url);
	if (selectedVal=="")
	{
		selectedVal="All";
	}
	document.getElementById("spnSelectedAlpha").innerHTML=selectedVal;
	output="<SELECT CLASS=adminforms NAME=\""+outPutElem+"\" ID=\""+outPutElem+"\" SIZE=5 MULTIPLE STYLE=\"width:95%;font-weight:300;\"  >\n";
	output=output+data;
	output=output+"</SELECT>\n";
	document.getElementById(rewriteElemId).innerHTML=output
}
function lookUpUser(url,selectedVal,searchVal,rewriteElemId,outPutElem){
	url=url+searchVal+"&alpha="+selectedVal+"&nochace="+Math.random()
	data=Navigation(url);
	if (selectedVal=="")
	{
		selectedVal="All";
	}
	document.getElementById("spnSelectedAlpha").innerHTML=selectedVal;
	output="<SELECT CLASS=adminforms NAME=\""+outPutElem+"\" ID=\""+outPutElem+"\" SIZE=5 MULTIPLE STYLE=\"width:95%;font-weight:300;\"  >\n";
	output=output+data;
	output=output+"</SELECT>\n";
	document.getElementById(rewriteElemId).innerHTML=output
}

//START MODIFICATION BY SAM ON 09-13-07 FOR INTEGRATING WITH PRODUCTS MODULE
function ajaxLoad(url){ 
		var url=url+"&nochace="+Math.random()
		data=Navigation(url);
		document.getElementById("dynBlock").innerHTML=data;
}

	function MoveLimited( Source, Destination ){
		var count=0;
		if(Destination.options.length>=3) return;
		for( var I = Source.options.length-1; I >= 0; I-- ){
			if(count==3){
				return;
			}
			if( Source.options[I] != null && Source.options[I].selected){
				x = Destination.options.length;
				Destination.options.length = x+1;
				Destination.options[x].text = Source.options[I].text;
				Destination.options[x].value =  Source.options[I].value;
				Source.options[I] = null;
				count++;
			}
		}
	}
	
	function MoveLimitedAll( Source, Destination ){
		if(Destination.options.length>=3) return;
		var limit=Source.options.length;
		if(limit>=3) limit=3;
		for( var I = limit-1; I >= 0; I-- ){
			if( Source.options[I] != null ){
				x = Destination.options.length;
				Destination.options.length = x+1;
				Destination.options[x].text = Source.options[I].text;
				Destination.options[x].value =  Source.options[I].value;
				Source.options[I] = null;
				
			}
		}
	}

	function selectAllSend(objList){
	for( i = 0; i < objList.options.length; i++ ){
		objList.options[i].selected = true; }
	}

function get_group_role(str)
{
	if(str=='')
		return;	
	var tdStaffRole = document.getElementById('tdStaffRole');	
	var randomnumber=Math.floor(Math.random()*1000);	
	var url = "includes/custom_files/get_group_role.php?id="+str+"&t="+ randomnumber; 
	data='';
	getPage.open("GET", url, false);
	getPage.send(null);
	try
	{
		data = getPage.responseText;
		//alert(url);
		if(data != '')
		{
			tdStaffRole.innerHTML=data;			
		}
		else
		{
			tdStaffRole.innerHTML='';			
		}
	}
	catch (ex)
	{}	
}

function get_schools(str)
{
	if(str=='')
		return;	
	var divSchool = document.getElementById('divSchool');
	var enrollTitle = document.getElementById('schoolEnrollTitle');	
	var randomnumber=Math.floor(Math.random()*1000);	
	var url = "includes/custom_files/get_state_schools.php?id="+str+"&t="+ randomnumber; 
	data='';
	getPage.open("GET", url, false);
	getPage.send(null);
	try
	{
		data = getPage.responseText;
		
		if(data != '')
		{
			divSchool.style.display='';
			enrollTitle.innerHTML='*School to enroll';
			divSchool.innerHTML=data;
			
		}
		else
		{
			tdSchool.innerHTML='';
			
		}
	}
	catch (ex)
	{}	
}

function add_another_child()
{
	var tbl = document.getElementById("tblChild");
	var lastrow = tbl.rows.length;
	var rowcounter = lastrow;
	var row = tbl.insertRow(lastrow);
	var cell1 = row.insertCell(0);
	cell1.innerHTML = '<input type="text" name="cname[]" id="cname" />';
	var cell2 = row.insertCell(1);
	
	cell2.innerHTML = '&nbsp;<input type="text" name="grade[]" id="grade" size="5" />';
	var cell3 = row.insertCell(2);
	cell3.innerHTML = '<input type="text" name="sname[]" id="sname" />';
}
function IsNumeric(strString,customChar) 
{
   if(!customChar)
	   var strValidChars = "0123456789";
   else
      var strValidChars = "0123456789" + customChar;
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
      {
         blnResult = false;
      }
   }
   return blnResult;
}
function frm_enrollment_validation()
{
	var required = {
	"pname": "Parent/Guardian name cannot be left blank",
	"address1": "Address1 cannot be left blank",
	"city": "City cannot be left blank",
	"state":"State cannot be left blank",
	"zip":"Zip code cannot be left blank",
	"email":"Email address cannot be left blank"
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}
	var zip = document.getElementById("zip").value;
	if(!IsNumeric(zip))
	{
		alert("Invalid Zip Code, format should be 1234567891");
		document.getElementById("zip").focus();
		return false;
	}
	if(document.getElementById("sitetype").value!=3)
	{
		if(document.getElementById("school").value=='')
		{
			alert("School can not be left blank");
			document.getElementById("school").focus();
			return false;
		}
	}
	var aphone = (document.getElementById("phone1").value)+(document.getElementById("phone2").value)+(document.getElementById("phone3").value);
	if(aphone=='')
	{
		alert("Phone number can not be left blank");
		document.getElementById("phone1").focus();
		return false;
	}
	if(aphone !='')
	{
		if(aphone.length < 10)
		{
			alert("Incomplete Phone Number, format should be 1234567891");
			document.getElementById("phone1").focus();
			return false;
		}
		else if(!IsNumeric(aphone))
		{
			alert("Invalid Phone Number, format should be 1234567891");
			document.getElementById("phone1").focus();
			return false;
		}
	}
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var email = document.getElementById("email");
	str = email.value;
	if(str!='')
	{
		if(str.match(emailRegEx)){	
		}
		else{
			alert('Invalid e-mail address');
			email.focus();
			return false;
		}
	}	
	var cname = document.getElementsByName("cname[]");
	if(cname[0].value =='')
	{
		alert("Name of first child can not be left blank");	
		cname[0].focus();
		return false;
	}
	
	var grade = document.getElementsByName("grade[]");
	if(grade[0].value =='')
	{
		alert("Grade of first child can not be left blank");	
		grade[0].focus();
		return false;
	}
}

function frm_contactus_validation()
{
	var required = {
	"uname": "Name cannot be left blank",	
	"email":"Email address cannot be left blank"	
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}
	
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var email = document.getElementById("email");
	str = email.value;
	if(str!='')
	{
		if(str.match(emailRegEx)){	
		}
		else{
			alert('Invalid e-mail address');
			email.focus();
			return false;
		}
	}
	
	
}
function populate_sites(str)
{
	if(str==1)
		return;	
	var tdSiteid = document.getElementById('tdSiteid');	
	var randomnumber=Math.floor(Math.random()*1000);	
	var url = "includes/custom_files/get_sites.php?id="+str+"&t="+ randomnumber; 
	data='';
	getPage.open("GET", url, false);
	getPage.send(null);
	try
	{
		data = getPage.responseText;
		
		if(data != '')
		{			
			tdSiteid.innerHTML=data;
			
		}
		else
		{
			tdSiteid.innerHTML='';
			
		}
	}
	catch (ex)
	{}	
}


function validateUser()
{
	var required = {
	"username": "Username can not be left blank",
	"firstname": "First name can not be left blank",
	"lastname": "Last name can not be left blank",
	"email": "E-mail can not be left blank",
	"user_password": "Password can not be left blank",
	"vpassword": "Verification password can not be left blank"
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value.trim()) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	str = document.getElementById("email").value;	
	if(str.match(emailRegEx)){	
	}
	else{
		alert('Invalid e-mail address');
		document.getElementById("email").focus();
		return false;
	}
	var password = document.getElementById("user_password");
	var vpassword = document.getElementById("vpassword");
	if(password.value.trim()!=vpassword.value.trim())
	{
		alert("Password does not match");	
		return false;
	}
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
function validateUserEdit()
{
	var required = {	
	"firstname": "First name can not be left blank",
	"lastname": "Last name can not be left blank",
	"email": "E-mail can not be left blank"	
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value.trim()) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	str = document.getElementById("email").value;	
	if(str.match(emailRegEx)){	
	}
	else{
		alert('Invalid e-mail address');
		document.getElementById("email").focus();
		return false;
	}	
}

function frm_enrollmentrequest_validation()
{
	var required = {
	"pname": "Name cannot be left blank",
	"email":"E-mail address cannot be left blank"/*,
	"school": "School cannot be left blank"	*/
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}
	
	var aphone = (document.getElementById("phone1").value)+(document.getElementById("phone2").value)+(document.getElementById("phone3").value);
	if(aphone=='')
	{
		alert("Phone number can not be left blank");
		document.getElementById("phone1").focus();
		return false;
	}
	if(aphone !='')
	{
		if(aphone.length < 10)
		{
			alert("Incomplete Phone Number, format should be 1234567891");
			document.getElementById("phone1").focus();
			return false;
		}
		else if(!IsNumeric(aphone))
		{
			alert("Invalid Phone Number, format should be 1234567891");
			document.getElementById("phone1").focus();
			return false;
		}
	}
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var email = document.getElementById("email");
	str = email.value;
	if(str!='')
	{
		if(str.match(emailRegEx)){	
		}
		else{
			alert('Invalid e-mail address');
			email.focus();
			return false;
		}
	}	
	var cname = document.getElementsByName("cname[]");
	if(cname[0].value =='')
	{
		alert("Name of first child can not be left blank");	
		cname[0].focus();
		return false;
	}
	
	var grade = document.getElementsByName("grade[]");
	if(grade[0].value =='')
	{
		alert("Grade of first child can not be left blank");	
		grade[0].focus();
		return false;
	}
}

function frm_ice_validation()
{
	var required = {
	"pname": "Name cannot be left blank",
	"sname":"Student name cannot be left blank"
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}
	
	var email = document.getElementById("email");
	var acell = (document.getElementById("cell1").value)+(document.getElementById("cell2").value)+(document.getElementById("cell3").value);

	if(email.value=='' && acell=='')
	{
		alert("Please supply your email or cell phone number so that we can notify you in the case of an emergency.");
		email.focus();
		return false;
	}
	if(email.value!='')
	{
		var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;		
		str = email.value;
		if(str!='')
		{
			if(str.match(emailRegEx)){	
			}
			else{
				alert('Invalid e-mail address');
				email.focus();
				return false;
			}
		}
	}

	if(acell!='')
	{
		if(acell.length < 10)
		{
			alert("Incomplete cell number, format should be 1234567891");
			document.getElementById("cell1").focus();
			return false;
		}
		else if(!IsNumeric(acell))
		{
			alert("Invalid cell number, format should be 1234567891");
			document.getElementById("cell1").focus();
			return false;
		}
	}
}

function frm_ice_unsubscribe_validation()
{
	
	var email = document.getElementById("email");
	var cell = document.getElementById("cell");
	if(email.value=='' && cell.value=='')
	{
		alert("Either email or cell phone must be entered");
		email.focus();
		return false;
	}
	if(email.value!='')
	{
		var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;		
		str = email.value;
		if(str!='')
		{
			if(str.match(emailRegEx)){	
			}
			else{
				alert('Invalid e-mail address');
				email.focus();
				return false;
			}
		}
	}

	if(cell.value!='')
	{
		if(cell.value.length < 10)
		{
			alert("Incomplete cell number, format should be 1234567891");
			cell.focus();
			return false;
		}
		else if(!IsNumeric(cell.value))
		{
			alert("Invalid cell number, format should be 1234567891");
			cell.focus();
			return false;
		}
	}
}
//END OF MODIFICATION
function signupNewsLetter()
{
	var required = {
	"txtfname": "First name can not be left blank",
	"txtlname": "Last name can not be left blank",
	"txtemail": "E-mail can not be left blank"
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value.trim()) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	str = document.getElementById("txtemail").value;	
	if(str.match(emailRegEx)){	
	}
	else{
		alert('Invalid e-mail address');
		document.getElementById("txtemail").focus();
		return false;
	}
}