// JavaScript Document

function swapHeader(varNavi,varHeader2,varMain) {
	// varNavi = URL to reload the navi bar
	// varHeader2 = URL to the new header to load
	// varMain = URL of page to load in the main div
	getDataNoLoading(varNavi,'navbar');
	getDataNoLoading(varHeader2,'qHeader');
	if (varMain) {
		getDataNoLoading(varMain,'main');
	}
}

function wNewSwapHeader(varHeader2,varMain) {
  getDataNoLoading('navbar/navbar_home.asp','navbar');		// close all folders
  getDataNoLoading(varHeader2,'qHeader');					// swap header out
  getDataNoLoading(varMain,'main');							// load the page in the main div
}

function openSub(varNavi,varMain) {
	// varNavi = URL to reload the navi bar
	// varMain = URL of page to load in the main div
	getDataNoLoading(varNavi,'navbar');
	getDataNoLoading(varMain,'main');
}

function validateLogin() {
	var a = 0;
	if (isRequired('txtUsername','divUsername',msg='You must enter an email address.') == false) {
	  a++;
	}
	if (isRequired('txtPassword','divPassword','Password is required.') == false) {
	  a++;
	}
	if (a > 0) {
	  //alert('there\'s an error');
	  return false;
	}else{
	  //alert('everything is ok');
	  return true;
	}
}

/*******************northern center welcome.asp javascript***************************************/
function MM_preloadImagesNorth() { //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_preloadImagesNoarth.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_swapImgRestoreNorth() { //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_findObjNorth(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_findObjNorth(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImageNorth() { //v3.0
  var i,j=0,x,a=MM_swapImageNorth.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObjNorth(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
/*******************northern center welcome.asp javascript***************************************/

/*******************southern center welcome.asp javascript***************************************/

function MM_preloadImagesSouth() { //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_swapImgRestoreSouth() { //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_findObjSouth(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_findObjSouth(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImageSouth() { //v3.0
  var i,j=0,x,a=MM_swapImageSouth.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObjSouth(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/*******************southern center welcome.asp javascript***************************************/




function isRequired(foobar,div,msg) {
  if (document.getElementById(foobar).value.length < 1) {
    //setStyleById(label,"color","red");
	//alert("field is required");
	if (msg == null) {
		txtMsg = 'This field is required.';
	}else{
		txtMsg = msg
	}
	document.getElementById(div).innerHTML = txtMsg;
	return false;
  }else{
    //setStyleById(label,"color","black");
	document.getElementById(div).innerHTML = '';
	//alert('this is true');
	return true;
  }
}

// drop down menu version dropdown.selectedItem.value = ""
function isRequiredDD(foobar,div,msg) {
  if (document.getElementById(foobar).options.selectedIndex == 0) {
    //setStyleById(label,"color","red");
	//alert("field is required");
	if (msg == null) {
		txtMsg = 'This field is required.';
	}else{
		txtMsg = msg
	}
	document.getElementById(div).innerHTML = txtMsg;
	return false;
  }else{
    //setStyleById(label,"color","black");
	document.getElementById(div).innerHTML = '';
	//alert('this is true');
	return true;
  }
}

function isInteger(foobar,div,msg) {
	if ((isNaN(document.getElementById(foobar).value)) || (document.getElementById(foobar).value < 0)) {
		if (msg == null) {
			txtMsg = 'This field must be a number';
		}else{
			txtMsg = msg;
		}
		document.getElementById(div).innerHTML = txtMsg;
		return false;
	}else{
		document.getElementById(div).innerHTML = '';
		return true;
	}
}

function isEmail(foobar,div,msg) {
	apos = document.getElementById(foobar).value.indexOf("@")
	dotpos = document.getElementById(foobar).value.lastIndexOf(".")
	if (apos < 1 || dotpos - apos < 2) {
		if (msg == null) {
			txtMsg = 'Invalid e-mail address';
		}else{
			txtMsg = msg;
		}
		document.getElementById(div).innerHTML = txtMsg;
		return false;
	}else{
		document.getElementById(div).innerHTML = '';
		return true;
	}
}


// sample of a function
function checkAll() {
	//isEmpty('text1','msg1');
	//isNumber('text1','msg1');
	isEmail('text1','msg1');
	processForm('page.asp','divName','frmName');
}
// end sample

function validateNewUser() {
    var a = 0;
	if (isRequired('first_name','divFirstName') == false) {
	  a++;
	}
	if (isRequired('last_name','divLastName') == false) {
	  a++;
	}
	if (isRequiredDD('center_id','divCenterID') == false) {
	  a++;
	}
	if (isRequiredDD('county_id','divCountyID') == false) {
	  a++;
	}
	if (isRequiredDD('district_id','divDistrictID') == false) {
	  a++;
	}
	if (isRequiredDD('school_id','divSchoolID') == false) {
	  a++;
	}
	if (isEmail('email','divEmail') == false) {
	  a++;
	}
	if (isRequired('password','divPassword') == false) {
	  a++;
	}
	/*if (isRequiredDD('ATID','divAType') == false) {
	  a++;
	}*/
	if (a > 0) {
	  //alert('there\'s an error');
	  return false;
	}else{
	  //alert('everything is ok');
	  processForm('/qeia2/users_new/register_return.asp','main','frmNew');
	  return true;
	}
  }

// clears any pre-populated drop down values - eliminates mismatches between the ID of center, county, district, and school
function processNewUserCenter() {
  processForm('/qeia2/users_new/counties.asp','mnuCounty','frmNew');
  getData('/qeia2/users_new/districts.asp','mnuDistrict');
  getData('/qeia2/users_new/schools.asp','mnuSchool');
}

function processNewUserCounty() {
  processForm('/qeia2/users_new/districts.asp','mnuDistrict','frmNew');
  getData('/qeia2/users_new/schools.asp','mnuSchool');
}

function processNewUserDistrict() {
  processForm('/qeia2/users_new/schools.asp','mnuSchool','frmNew');
}



function validateEditUser() {
    var a = 0;
	if (isRequired('first_name','divFirstName') == false) {
	  a++;
	}
	if (isRequired('last_name','divLastName') == false) {
	  a++;
	}
	if (isRequiredDD('center_id','divCenterID') == false) {
	  a++;
	}
	if (isRequiredDD('county_id','divCountyID') == false) {
	  a++;
	}
	if (isRequiredDD('district_id','divDistrictID') == false) {
	  a++;
	}
	if (isRequiredDD('school_id','divSchoolID') == false) {
	  a++;
	}
	if (isEmail('email','divEmail') == false) {
	  a++;
	}
	/*if (isRequired('password','divPassword') == false) {
	  a++;
	}*/
	/*if (isRequiredDD('ATID','divAType') == false) {
	  a++;
	}*/
	if (a > 0) {
	  //alert('there\'s an error');
	  return false;
	}else{
	  //alert('everything is ok');
	  processForm('/qeia2/users_edit/edit_return.asp','main','frmEdit');
	  return true;
	}
  }

// clears any pre-populated drop down values - eliminates mismatches between the ID of center, county, district, and school for edit section
function processEditUserCenter() {
  processForm('/qeia2/users_edit/counties.asp','mnuCounty','frmEdit');
  getData('/qeia2/users_edit/districts.asp','mnuDistrict');
  getData('/qeia2/users_edit/schools.asp','mnuSchool');
}

function processEditUserCounty() {
  processForm('/qeia2/users_edit/districts.asp','mnuDistrict','frmEdit');
  getData('/qeia2/users_edit/schools.asp','mnuSchool');
}

function processEditUserDistrict() {
  processForm('/qeia2/users_edit/schools.asp','mnuSchool','frmEdit');
}



window.onload = setStyle;


// ----------------------------------------------
// Javascript Style Switcher
// - - - - - - - - - - - - - - - - - - - - - - -
// For details, visit AListApart:
// http://www.alistapart.com/stories/alternate/
// ----------------------------------------------

function setActiveStyleSheet(title, reset) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (reset == 1) {
	  createCookie("wstyle", title, 365);
  }
  
  // set the Text Size images if this is NOT the printer-friendly version
  if (location.href.indexOf("print=yes") < 1) {

    // change the 'A' images based on the text size the user has selected
    if (title == "Small Text") {
	    //alert("swap to normal");
	    document.SmallA.src = "/icons/text_sm2.gif";
	    document.NormalA.src = "/icons/text_med.gif";
	    document.LargeA.src = "/icons/text_lg.gif";
    }
    if (title == "Normal Text") {
  	  //alert("swap to medium");
  	  document.SmallA.src = "/icons/text_sm.gif";
	  document.NormalA.src = "/icons/text_med2.gif";
	  document.LargeA.src = "/icons/text_lg.gif";
    }
    if (title == "Large Text") {
	  //alert("swap to large");
	  document.SmallA.src = "/icons/text_sm.gif";
	  document.NormalA.src = "/icons/text_med.gif";
	  document.LargeA.src = "/icons/text_lg2.gif";
    }
  }
}

function setStyle() {
	var style = readCookie("wstyle");
	if (style != null) {
		setActiveStyleSheet(style, 0);
	}
	else {

         // set the Text Size images if this is NOT the printer-friendly version
         if (location.href.indexOf("print=yes") < 1) {
	    // default to normal;
	    document.SmallA.src = "/icons/text_sm.gif";
	    document.NormalA.src = "/icons/text_med2.gif";
	    document.LargeA.src = "/icons/text_lg.gif";
	  }
	}
}

// ----------------------------------------------
// Cookie functions
// ----------------------------------------------

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = ";expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+";domain=.qeia.org;path=/;";
  //document.cookie = name+"="+value+expires+"; path=/;
}

// ----------------------------------------------

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function getCookieFlash(name) {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
}

function ddCategories(catID) {
  getData('resources/categories.asp?category_id='+catID,'main');
}