/*
==========================================================
InnerLoop Creative Solutions - Javascript function library
Date Created: 22 Dec 2004 - Last Modified: 08 Aug 2006

Notes: This library contains all javascript functions we use, and is included
in the top of the page at the same place as the stylesheets.

TO INCLUDE:
<script type="text/javascript" src="../Javascript/ScriptLibrary.js"></script>
==========================================================
*/

// Pop up a new window (customizable)
function popUpWindow(URLStr, left, top, width, height, resize, scrolling) {
	
	if (left == -1 && top == -1) {
		var left = (screen.width/2) - width/2;
    	var top = (screen.height/2) - height/2;
	}

  popUpWin = window.open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+scrolling+',resizable='+resize+',copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

// Pop up a new window - extended function (transitional)
function popUpWindowExt(URLStr, name, left, top, width, height, resize, scrolling) {
	
	if (left == -1 && top == -1) {
		var left = (screen.width/2) - width/2;
    	var top = (screen.height/2) - height/2;
	}
	
	if (name == "") {
		name = "popUpWin";
	}

  popUpWin = window.open(URLStr, name, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+scrolling+',resizable='+resize+',copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

// Display a message in a new, variable size size window (using the main websites stylesheet)
function messageWindow(title, msg, width, height, resize, scrolling, alignment) {
  var left = (screen.width/2) - width/2;
  var top = (screen.height/2) - height/2;
  if (!alignment) { alignment = "center"; }
  var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar='+scrolling+',resizable='+resize+',copyhistory=yes,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
  var msgWindow = window.open("","msgWindow", styleStr);
  var msgHead = '<html><head><title>'+title+'</title><link href="../CSS/StyleMain.css" rel="stylesheet" type="text/css"></head>';
  var msgBody = '<body style="padding: 8px;"><p align="'+alignment+'">'+msg+'</p><p align="'+alignment+'"><form><input class="formSubmit" type="button" value="   Close this window   " onClick="self.close()"></form></p></body></html>';
  msgWindow.document.write(msgHead + msgBody);
}

// Display a message in a new, variable size size window (using the admin stylesheet)
function messageWindowAdmin(title, msg, width, height, resize, scrolling, alignment) {
  var left = (screen.width/2) - width/2;
  var top = (screen.height/2) - height/2;
  if (!alignment) { alignment = "center"; }
  var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar='+scrolling+',resizable='+resize+',copyhistory=yes,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
  var msgWindow = window.open("","msgWindow", styleStr);
  var msgHead = '<html><head><title>'+title+'</title><link href="../CSS/StyleAdmin.css" rel="stylesheet" type="text/css"></head>';
  var msgBody = '<body style="padding: 8px;"><p align="'+alignment+'">'+msg+'</p><p align="'+alignment+'"><form><input class="formSubmit" type="button" value="   Close this window   " onClick="self.close()"></form></p></body></html>';
  msgWindow.document.write(msgHead + msgBody);
}

// Change an image at any time...this is called using the image's name attribute.
function changeImage(imageName,fileName) {
	document.all(imageName).src = fileName;
}

// Resize a window to fixed width/height
function resizeWindow(width,height) { 
	if (navigator.appVersion.charAt(0) >=4) { 
		window.resizeTo (width,height)
	}
}

// Toggle z-level of a div (targetID) with z-level value
function changeDivLevel( targetID, zLevel ) {
	target = document.all( targetID );
	target.style.zIndex = zLevel;
}

// Go back one step in history
function goBack() { 
	history.back() 
}

// A (supposedly) Spam-proof method of displaying (and linking) an email address.
function showEmail(name, domain) {
document.write('<a href="mailto:' + name + '@' + domain + '">');
document.write(name + '@' + domain + '</a>');
}

// Form Validation
function MM_validateForm() { //v3.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (val!=''+num) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

// Auto-Jump Menu
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

// The two generic <div> functions for hiding & showing various layers
// The non-generic one (commented here) needs to be copied and pasted into the page itself and configured with the <div> id's.
// Also, don't forget to put the InitPageLayout(); function in the onload param of the <body> tag.

//<!--
//function InitPageLayout() {	
//	readDivHeightCookie("divName");
//	readDivHeightCookie("divName2");
//}
//-->

function readDivHeightCookie(cookieName) {
	
	thisCookie = document.cookie.split("; ");

	for (i=0;i<thisCookie.length; i++) {
		if (cookieName == thisCookie[i].split("=")[0]) {
			changeDivHeight(cookieName, thisCookie[i].split("=")[1]);
			//alert ("Cookie Name: " + cookieName + "=" + thisCookie[i].split("=")[1]);
		}
	}
		
}

function changeDivHeight(target,height) {
	
	expireDate = new Date;
	expireDate.setMonth(expireDate.getMonth()+12);
	
	document.getElementById(target).style.height = height;
	document.cookie = target + "=" + height + ";expires=" + expireDate.toGMTString();

}


// Create Generic SetCookie for form fields
function setCookie(fieldname) {
	theValue = document.form.elements[fieldname].value;
	document.cookie = fieldname + "=" + theValue + ";";
}

// SetCookie for radiobuttons/checkboxes
function setRadioCookie(fieldname, theValue) {
	document.cookie = fieldname + "=" + theValue + ";";
}

// Set recurring SetCookie for radiobuttons/checkboxes
function setRecursiveRadioCookie(fieldname, theValue, formName) {
	
	// Set the cookie the first time. else append to the cookie
	if (document.cookie.split('=')[0] == "PHPSESSID") {
		document.cookie = fieldname + "=" + theValue + ";";
	} else {
		
		// Isolate the appropriate cookie and it's value into 'oldCookieValue'
		firstCookie = document.cookie.split("; ")[0];
		oldCookieValue = firstCookie.split("=")[1];
		
		// Create the new value for the cookie by appending old and new
		newCookieValue = oldCookieValue + "," + theValue;
		
		// Replace the instance of 'undefined' if it's there
		newCookieValue = newCookieValue.replace('undefined,','');
		
		// If the value of the current form item's 'check' property is false, then remove them from the list.
		if (formName.checked == false) {
			while (newCookieValue.match(theValue)) {
				newCookieValue = newCookieValue.replace(theValue, '');
			}
			
			while(newCookieValue.match(',,')) {
				newCookieValue = newCookieValue.replace(',,',',');
			}
		}
		
		// Check for trailing and leading lost commas
		newCookieValueFirstChar = newCookieValue.substr(0, 1);
		if (newCookieValueFirstChar == ",") {
			newCookieValue = newCookieValue.replace(",", "");
			//alert ("First Comma Replaced ... new cookievalue: " + newCookieValue);
		}
		
		newCookieValueLastChar = newCookieValue.substr((newCookieValue.length - 1), 1);
		if (newCookieValueLastChar == ",") {
			newCookieValue = newCookieValue.substr(0, (newCookieValue.length - 1));
			//alert ("Last Comma Replaced ... new cookievalue: " + newCookieValue);
		}
		
		// Now, re-set the cookie with the appended value
		document.cookie = fieldname + "=" + newCookieValue + ";";
		//alert (document.cookie);

	}
}

function clearCookie(fieldname) {
		expireDate = new Date;
		expireDate.setDate(expireDate.getDate()-1);
		
		document.cookie= fieldname + "=; expires" + expireDate.toGMTString();
		//alert (document.cookie);
}



// Select All toggle ... add different types in the case to handle other form elements.
function SelectAllToggle(theForm, valueToggle){

var formElements = theForm; 

	for(i = 0; i < formElements.length; i++){ 
		switch(formElements[i].type){
			case "checkbox":
				formElements[i].checked = valueToggle;
				setRecursiveRadioCookie('SaveCheckBoxCookie', formElements[i].value, formElements[i]);
				break;
		}
	}
	
}

function alertCookie() {
	alert (document.cookie);
}

