//Site wide javascript

//is the browser IE5.5 or IE6?
IE5_6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.5")!=-1)) || ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? true : false;

//default content image flipping function
//requires the following 2 lines in each template
// 		startPos = Math.round(Math.random() * 11)
//		rollFeatImg(startPos)

imgCounter = 0
pageImages = new Array(12);
pageImages[0] = "/images/img_01.jpg"
pageImages[1] = "/images/img_02.jpg"
pageImages[2] = "/images/img_03.jpg"
pageImages[3] = "/images/img_04.jpg"
pageImages[4] = "/images/img_05.jpg"
pageImages[5] = "/images/img_06.jpg"
pageImages[6] = "/images/img_07.jpg"
pageImages[7] = "/images/img_08.jpg"
pageImages[8] = "/images/img_09.jpg"
pageImages[9] = "/images/img_10.jpg"
pageImages[10] = "/images/img_11.jpg"
pageImages[11] = "/images/img_12.jpg"

function rollFeatImg(i) {
	//reset counter if neccessary
	if (i >= pageImages.length) {
		currentImg = 0
	}else{
		currentImg = i
	}
	
	//apply the filter if IE5/6
	if(IE5_6) {
		document.images["featImg"].filters[0].Apply()
	}
	
	//flip the image
	document.images["featImg"].src = pageImages[currentImg]
	
	//play the filter if IE5/6
	if(IE5_6) {
		document.images["featImg"].filters[0].Play()
	}
	//call self with time delay
	setTimeout("rollFeatImg(currentImg+1)", 3000)
}

//var currentimg = 1;
var imgcount = 4;
var imgpath = "images/";
var imgext = ".jpg";
var imgCounter1 = 1;
var imgCounter2 = 1;
var imgCounter3 = 1;
var minTime = 2000;
var maxTime = 5000;
if (typeof(iList) == "undefined") var iList = "";
if (typeof(iDir) == "undefined") var iDir = "images/";
if (typeof(iExt) == "undefined") var iExt = "gif";
	
function rand() {
	delay = Math.round(Math.random() * maxTime) + minTime;
	return delay;
}

function refreshheader1() {
			if (imgCounter1 > imgcount) {
				imgCounter1 = 1;
			}
			//apply the filter if IE5/6
			if(IE5_6) {
				document.images["home_img01"].filters[0].Apply()
			}
			document.images["home_img01"].src = imgpath + "home_img01_" + imgCounter1 + imgext;
			//apply the filter if IE5/6
			if(IE5_6) {
				document.images["home_img01"].filters[0].Play()
			}
			imgCounter1 = imgCounter1 + 1;
			setTimeout("refreshheader1()", rand());
}

function refreshheader2() {
			if (imgCounter2 > imgcount) {
				imgCounter2 = 1;
			}
			//apply the filter if IE5/6
			if(IE5_6) {
				document.images["home_img02"].filters[0].Apply()
			}
			document.images["home_img02"].src = imgpath + "home_img02_" + imgCounter2 + imgext;
			//apply the filter if IE5/6
			if(IE5_6) {
				document.images["home_img02"].filters[0].Play()
			}
			imgCounter2 = imgCounter2 + 1;
			setTimeout("refreshheader2()", rand());
}

function refreshheader3() {
			if (imgCounter3 > imgcount) {
				imgCounter3 = 1;
			}
			//apply the filter if IE5/6
			if(IE5_6) {
				document.images["home_img03"].filters[0].Apply()
			}
			document.images["home_img03"].src = imgpath + "home_img03_" + imgCounter3 + imgext;
			//apply the filter if IE5/6
			if(IE5_6) {
				document.images["home_img03"].filters[0].Play()
			}
			imgCounter3 = imgCounter3 + 1;
			setTimeout("refreshheader3()", rand());
}

	
function setPointer(theRow, thePointerColor, theNormalBgColor)
{
    var theCells = theRow.getElementsByTagName('td');	
    var rowCellsCnt  = theCells.length;
    var currentColor = theCells[0].style.backgroundColor;
    var newColor     = (currentColor.toLowerCase() == thePointerColor.toLowerCase())
                     ? theNormalBgColor
                     : thePointerColor;
        for (var c = 0; c < rowCellsCnt; c++) {
           	 	theCells[c].style.backgroundColor = newColor;
   			 }

    return true;
}

/* toggles blocks (divs) on and off - eg:
<div id="bubba">Something</div>
onclick="toggle('bubba');return false;"
*/
function toggle( targetId ){
  if (document.getElementById){
  		target = document.getElementById( targetId );
  			if (target.style.display == "none"){
  				target.style.display = "";
  			} else {
  				target.style.display = "none";
  			}
  	}
}

function countChars(caller, reciever) {
	caller = eval('document.form.' + caller);
	reciever = eval('document.form.' + reciever);
	reciever.value = caller.value.length;
	if (reciever.value > 500) {
		alert("Too long!");
	}
}

//Site wide javascript


////////////////////> roll over script </////////////////////////
///////// place the following style script in header page ///////
/*///////////////////////////////////////////////////////////////
<script language="JavaScript1.2" type="text/javascript">
//////////////////////> CONFIGURATION <//////////////////////////
var iDir = "images/"; 					//path to images
var iExt = "gif"; 						//extension of image files
var iList = "nav01,nav02,nav03"; 		//list of image names
/////////////////////////////////////////////////////////////////
</script>
*////////////////////////////////////////////////////////////////
///////// and then place the following in the <a> tags //////////
///// EG: onmouseover="over('nav01'); onmouseout="out('nav01'); //////
/////////////////////////////////////////////////////////////////
var d = document;
var iA = iList.split(',');
if 	(d.images) { 
	var iO = new Array();
	for	(j = 0; j < iA.length; j++) {
		iO[j] = new Image();
		iO[j].src = iDir + iA[j] + "b." + iExt;
	 	}
	}
	
function 	out(iName,newDir,newType) { 
			i = eval("d." + iName);
			i.src = iDir + iName + "a." + iExt;
			}
			
function 	over(iName,newDir,newType) { 
			i = eval("d." + iName);
			i.src = iDir + iName + "b." + iExt;
			}
/////////////////////////////////////////////////////////////////



///////////////> swapType - swap img type script <///////////////
///// this allows you to swap image type from the default////////
//EG:onmouseover="swapType('jpg');b('image01');swapType('gif');//
//this swaps type to jpg does roll over and swaps back to gif ///
/////////////////////////////////////////////////////////////////
function swapType(type){
	iExt = type;
}
/////////////////////////////////////////////////////////////////



//////////////> valForm - form validation script <///////////////
///////// place the following style script in form page /////////
/////////// the fields array can be as long as required /////////
/*///////////////////////////////////////////////////////////////
<script language="JavaScript" type="text/javascript">
function initValForm(){
	thisForm = document.forms[0];
	errMsg = "You have not filled out all the required fields \ncorrectly. Please double check the following \nfields: \n\n";	
	fieldsArray[0] = new fieldRecord("entrantFname","First Name","str");
	fieldsArray[1] = new fieldRecord("stateId","State","int");
	fieldsArray[2] = new fieldRecord("entrantEmail","Email","eml");
	}
</script>
*////////////////////////////////////////////////////////////////
////////// and then place the following in the form tag /////////
///////////////// onsubmit="return valForm()" ///////////////////
/////////////////////////////////////////////////////////////////

var errMsg;
var thisForm;
var fieldsArray = new Array();;
var errorSwitch;

//master function
function valForm(){
	errorSwitch = 0;
	initValForm();
	testVals();
	outputMsg();
	if 	(errorSwitch){
		alert(errMsg);
		return false
		}
	}

// set up function for second level arrays
function fieldRecord(fieldName, logicalName, test) {
	this.fieldName = fieldName;
	this.logicalName = logicalName;
	this.test = test;
	this.error = 0; //default error to false
	}

//loop through fieldsArray and run the appropriate test for each field
//update the error status for each field
function testVals(){
	for (i=0;i<fieldsArray.length;i++){
		var thisType = eval("thisForm." + fieldsArray[i].fieldName + ".type");
		var thisError = fieldsArray[i].error;
		var thisTest = fieldsArray[i].test;
		
		if 	(thisType == "select-one") {
			var thisVal = eval("thisForm." + fieldsArray[i].fieldName + ".options[thisForm." + fieldsArray[i].fieldName + ".selectedIndex].value")
			} else {
			var thisVal = eval("thisForm." + fieldsArray[i].fieldName + ".value")
			}
		
		if 	(thisTest == "str") {
				thisError = strTest(thisVal);
			} else if	(thisTest == "int") {
				thisError = intTest(thisVal);			
			} else if	(thisTest == "eml") {
				thisError = emlTest(thisVal);
			} else {
				alert('unknown test');
				break
			}
			
		fieldsArray[i].error = thisError;
		}
	}

//strTest tests that the field is not blank
function strTest(thisVal){
	thisError = 0;
	if 	(thisVal == "") {
		thisError = 1;
		}
	return thisError;
	}

//intTest tests that the field is not blank and that it is a number
function intTest(thisVal){
	thisError = 0;
	if 	(thisVal == "" || isNaN(thisVal)) {
		thisError = 1;
		}
	return thisError;
	}

//emlTest tests that the field is not blank and that it contains @ & .
function emlTest(thisVal){
	thisError = 0;
	if 	((thisVal == "" || thisVal.indexOf('@', 0) == -1) || thisVal.indexOf('.') == -1) {
		thisError = 1;
		}
	return thisError;
	}

//check the error code on each field and build error message
function outputMsg(){
	for (i=0;i<fieldsArray.length;i++){
	 if (fieldsArray[i].error){
	 		errorSwitch = 1;
	 		errMsg = errMsg + "- " + fieldsArray[i].logicalName + "\n";
	 		}
		}
	}
/////////////////////////////////////////////////////////////////