/*   ========================================================================
Name:		Common.js

Notes:		This Javascript file contains commonly used functions.

History:
mm/dd/yyyy	Author	Description
----------	------	----------------------------------------------------
04/16/2001	CAF		Created
========================================================================    */

// A function to validate the E-mail
 function validEmail(email) {
    invalidChars = " /:,;"

  if (email == "") {      // cannot be empty
   return false
  }
  for (i=0; i<invalidChars.length; i++) { // does it contain any invalid characters?
   badChar = invalidChars.charAt(i)
   if (email.indexOf(badChar,0) > -1) {
    return false
   }
  }
  atPos = email.indexOf("@",1)   // there must be one "@" symbol
  if (atPos == -1) {
   return false
  }
  if (email.indexOf("@",atPos+1) != -1) { // and only one "@" symbol
   return false
  }
  periodPos = email.indexOf(".",atPos)
  if (periodPos == -1) {     // and at least one "." after the "@"
   return false
  }
  if (periodPos+3 > email.length) {  // must be at least 2 characters after the "."
   return false
  }
  return true
}

// Check form field content.
function checkField (thisform, fieldvalue) {
	if (thisform.BookmarkName.value == "") {
	   alert ('Please enter ' +  fieldvalue);
	   return false;
	}
	return true;
}

// Check form for E-mail addresses
 function checkForm (emailform) {
 	  if (emailform.email_target_email.value == "") {
	      alert ('Please enter "Send to" address.');
		  return false
	  }
	  else {
		   if (!validEmail(emailform.email_target_email.value)) {
		    alert('Invalid "Send to" email address')
		    emailform.email_target_email.focus()
		    emailform.email_target_email.select()
		    return false
		   }
	  }

	   if (emailform.email_from.value == "") {
	      alert ('Please enter "Your email" address.');
		  return false
	   }
	   else {
	   	   if (!validEmail(emailform.email_from.value)) {
		    alert('Invalid email address')
		    emailform.email_from.focus()
		    emailform.email_from.select()
		    return false
		   }
	   }

	  return true
 }

// Check thisform's multiple select list and give an error if selected
// items count is more than maxcount.
function checkSelectionCount (thisform, selname, maxcount) {
   var j = 0;


   for (var i=0; i<thisform.elements[selname].options.length; i++) {

   	   if(thisform.elements[selname].options[i].selected) {
		   	if ((thisform.elements[selname].options[i].value == 'PUBLIC') ||
				(thisform.elements[selname].options[i].value == 'PRIVATE')) {
				alert('Do not select the school type identifiers - "PUBLIC" or "PRIVATE"');
				return false;
			}
	   }

   }


   for (var i=0; i<thisform.elements[selname].options.length; i++) {
   	   if(thisform.elements[selname].options[i].selected) j++;
   }

   if (j > maxcount) {
	   alert("Please select " + maxcount + " or fewer items.\nCurrently selected items: " + j);
       return false;
   }
   else {
       return true;
   }

}

// Pops up a child window with the given window name and dimensions
/*
function popupWindow(winname,w,h) {
	if (winname == null) winname = "newWindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	cwin=window.open("", winname,
	"nomenubar,status,scrollbars,noresizable,width=" + w + ",height=" + h);
return true;
}
*/

// Pops up a child window with the given window name and dimensions.
// Takes the optional arguments to set the menubar, resize, and scrollbars attributes of Window.
function popupWindow(winname,  w, h, menu, resize, scroll) {
	popupWindowURL("", winname,  w, h, menu, resize, scroll);
	return;
}

function popupWindowURL(url, winname,  w, h, menu, resize, scroll, x, y) {
	if (winname == null) winname = "newWindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	if (resize == null) resize = 1;

	menutype   = "nomenubar";
	resizetype = "noresizable";
	scrolltype = "noscrollbars";
	if (menu) menutype = "menubar";
	if (resize) resizetype = "resizable";
	if (scroll) scrolltype = "scrollbars";

	if (x == null || y == null) {
		cwin=window.open(url,winname,"status," + menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h);
	}
	else {
		cwin=window.open(url,winname,"top=" + y + ",left=" + x + ",screenX=" + x + ",screenY=" + y + "," + "status," + menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h);
	}
	if (!cwin.opener) cwin.opener=self;
	cwin.focus();

	return;
}

function SubmitAction(formName,formAction) {
	document[formName].Action.value=formAction;
	document[formName].submit();
	return;
}

// Submits the form associated with "thiswin" window and displays the results in
// "popupname" window of dimensions 'w' and 'h'.  Uses popupWindow function.
function submitClose(thiswin, popupname, w, h, menu, resize, scroll) {
	if (popupname == null) popupname = "resultswindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	popupWindow(popupname, w, h, menu, resize, scroll);

	thiswin.document.forms[0].submit();

	thiswin.close();
}

// Checks(status true) or unchecks(status false) all the checkboxes associated with the form.
function CheckUncheck(status) {
   for (i = 0; i < document.forms[0].length; i++) {
      document.forms[0].elements[i].checked = status
   }
}

// Reads user cookie
function ReadCookie (CookieName) {
  var CookieString = document.cookie;
//  var CookieSet = CookieString.split (';');
  var CookieSet = splitArray(CookieString, ';');
  var SetSize = CookieSet.length;
  var CookiePieces
  var ReturnValue = "";
  var x = 0;

  for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) {

//    CookiePieces = CookieSet[x].split ('=');
    CookiePieces = splitArray(CookieSet[x], '=');

    if (CookiePieces[0].substring (0,1) == ' ') {
      CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);
    }

    if (CookiePieces[0] == CookieName) {
      ReturnValue = CookiePieces[1];
    }

  }

  return ReturnValue;
}

// Swaps one Character for Another
function SwapStr (str, fnd, rpl) {
  if (rpl.indexOf(fnd) != -1) return str;

  while ((pos = str.indexOf(fnd)) != -1) {
    str = str.substring(0,pos) + rpl + str.substring(pos+fnd.length,str.length);
  }

  return str;
}

// A JavaScript 1.0 version of split
function splitArray(joinedString, splitString) {
	var tempArray = new Array(0);
	var arrayCounter = 0;
	var elementBoolean	= true;
	for (var i = 0; i < joinedString.length; i++) {
		if(elementBoolean) {
			tempArray[arrayCounter] = "";
			elementBoolean 		  = false;
			}
		if(joinedString.charAt(i) == splitString) {
			arrayCounter++;
			elementBoolean = true;
			continue;
			}
		tempArray[arrayCounter] += joinedString.charAt(i);
		}
	return tempArray;
	}


//If a checkbox is selected in a form, this will give a
//confirmation as to whether they really want to check
//the box or not. Clicking "Ok" will leave the checkbox alone.
//Clicking "Cancel" will make the checkbox become unchecked.
//
//Inputs:	thisform (the form object identifier)
//		checkname (the name of the form variable for the checkbox)
function VerifyCheckBox(thisform, checkname) {
   if (thisform.elements[checkname].checked) {
	   if (confirm("Are you sure?")) {
		        thisform.elements[checkname].checked=true;
			return true;
		} else {
	   		thisform.elements[checkname].checked=false;
			return false;
		}
	}
}

// Given a message this function will prompt the user with the message
// and submits the form if the user confirms
function VerifyAction(thisForm, message) {
	if (window.confirm(message)) {
		thisForm.submit;
	}
}


//
// clears a form.  give it the number of the form on the page
//
function clear_text() {
	if (clear_text.arguments.length == 0) {
		k = 0;
	}
	else if (document.forms[clear_text.arguments[0]] != null) {
		k = clear_text.arguments[0];
	}
	else {
		k = document.forms.length - 1;
	}
	for(n = 0; n < document.forms[k].elements.length; n++) {
		if (document.forms[k].elements[n].type == "text")
			  document.forms[k].elements[n].value = "";
	}
}


//
// move items up and down on a select
//
function OrderSelect(theSelect, moveDir) {
	var tmpValue = "";
	var tmpText = "";

	for (loop=0; loop < theSelect.options.length; loop++) {
		if (theSelect.options[loop].selected == true) {
			if (moveDir == 1 && loop > 0) {
				tmpValue = theSelect.options[loop-1].value;
				tmpText = theSelect.options[loop-1].text;
				theSelect.options[loop-1].value = theSelect.options[loop].value;
				theSelect.options[loop-1].text = theSelect.options[loop].text;
				theSelect.options[loop].value = tmpValue;
				theSelect.options[loop].text = tmpText;
				theSelect.options[loop].selected = false;
				theSelect.options[loop-1].selected = true;
				break;
			}
			if (moveDir == -1 && loop < (theSelect.options.length -1)) {
				tmpValue = theSelect.options[loop+1].value;
				tmpText = theSelect.options[loop+1].text;
				theSelect.options[loop+1].value = theSelect.options[loop].value;
				theSelect.options[loop+1].text = theSelect.options[loop].text;
				theSelect.options[loop].value = tmpValue;
				theSelect.options[loop].text = tmpText;
				theSelect.options[loop].selected = false;
				theSelect.options[loop+1].selected = true;
				break;
			}
		}
	}
}

//
// move items up and down on a select
//
function SelectToList(theSelect, getAll, Sep) {
	var ValueList = "";

	if (getAll == null) getAll = false;
	if (Sep == null) Sep = ";";
	for (loop=0; loop < theSelect.options.length; loop++) {
		if (theSelect.options[loop].selected == true || getAll) {
			ValueList += theSelect.options[loop].value + Sep;
		}
	}
	return ValueList;
}


// Fireworks 3.0  Dreamweaver 3.0 functions follow below
function MM_findObj(n, d) { //v3.0
  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); return x;
}
/* Functions that swaps images. */
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;
}

/* Functions that handle preload. */
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];}}
}

// SOME FUNCTIONS USED WITH ONLOAD & ONUNLOAD

// Body onload utility (supports multiple onload functions)
function AddOnload(f)
{
	if (typeof document.OnloadFunctions == 'undefined') {
		document.OnloadFunctions = new Array();
		if (window.onload)
			document.OnloadFunctions[0] = window.onload;
		window.onload = RunOnloadFunctions;
	}
	document.OnloadFunctions[document.OnloadFunctions.length] = f;
}
function RunOnloadFunctions()
{
	for (var i=0;i<document.OnloadFunctions.length;i++)
		document.OnloadFunctions[i]();
}

// END FUNCTIONS USED WITH ONLOAD & ONUNLOAD

function RotationSet(BaseSrcURL) {
	// Make sure BaseSrcURL ends in /
	if (BaseSrcURL.length && BaseSrcURL.charAt(BaseSrcURL.length-1) != '/') {
		BaseSrcURL += '/';
	}

	if (typeof document.RotationSets == 'undefined') {
		document.RotationSets = new Array();
	}
	this.RotationSetIndex = document.RotationSets.length;
	document.RotationSets[this.RotationSetIndex] = this;

	//
	// Properties
	this.Index = -1;
	this.BaseSrcURL = BaseSrcURL;
	this.Images = new Array();
	this.Links = new Array();
	// Properties per Rotation Start
	this.ImageObj = null;
	this.LinkObj = null;
	this.TimeoutId = -1;
	this.RotateDelay = null;
	this.RotateInOrder = null;

	//
	// Methods
	this.AddItem = function (Src,Url) {
		this.Images[this.Images.length] = new Image();
		this.Images[this.Images.length-1].src = this.BaseSrcURL + Src;
		this.Links[this.Images.length-1] = Url;
	}

	this.Start = function (ImgName,LinkName,RotateInOrder,RotateDelay) {
		if (this.TimeoutId != -1) return; // Already Started
		if (!LinkName || !LinkName.length) { LinkName = ImgName + 'Link'; }
		this.ImageObj = document[ImgName];
		this.LinkObj = document[LinkName];
		if (!RotateDelay) { RotateDelay = 2000; }
		this.RotateInOrder = RotateInOrder;
		this.RotateDelay = RotateDelay;
		this.Rotate();
	}

	this.Rotate = function () {
		this.Index = (this.RotateInOrder ? (this.Index + 1) : Math.floor(Math.random()*this.Images.length)) % (this.Images.length);
		this.ImageObj.src = this.Images[this.Index].src;
		//this.LinkObj.href = this.Links[this.Index];
		this.TimeoutId = setTimeout("document.RotationSets["+this.RotationSetIndex+"].Rotate()",this.RotateDelay);
	}


}