// Insert personalization values  tk 9/11/03
// aField specifies which field name the personalization value inserts to.
// aForm specifies which form name to insert into.
//var aField = 'html';

// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_moz = 0;

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);


//ADD PREDEFINED PERSONALIZATION
function insertText(aSelect, aForm) {
   eval("document." + aForm + "." + aField + ".focus()");
   var vTag = aSelect[aSelect.selectedIndex].value;
   if(vTag == "") return;
   if ((clientVer >= 4) && is_ie && is_win) {
       document.selection.createRange().text = vTag;
       return;
   } else if (eval("document." + aForm + "." + aField + ".setSelectionRange")) {
       // Firefox section follows
       eval("document." + aForm + "." + aField + ".value = document." + aForm + "." + aField + ".value.substring(0,document." + aForm + "." + aField + ".selectionStart) + vTag + document." + aForm + "." + aField + ".value.substring(document." + aForm + "." + aField + ".selectionEnd,document." + aForm + "." + aField + ".value.length)");
       return;
   } else {
     document.aForm.aField.value += vTag;
     return;
   }
}

function popup(page,name,w,h,scroll){

LeftPosition=(screen.width)?(screen.width-w)/2:100;
TopPosition=(screen.height)?(screen.height-h)/2.5:100;

settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',status=no,resizable=yes';
var newwindow=window.open(page,name,settings);
if (window.focus) {newwindow.focus()}
	return false;
}
function confirmthis(text) { 
	if (window.confirm(text)) { 
	return true;
	}
	else{
	return false;
	}
}

function redirect(page) {
	window.location.href=page.options[page.selectedIndex].value;
}
 
 //Browser Check 
 function browserCheck(){
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=this.agent.indexOf("Opera 5")>-1
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bc=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}
var bc=new browserCheck()
function popmousemove(e){descx=bc.ns4||bc.ns6?e.pageX:event.x; descy=bc.ns4||bc.ns6?e.pageY:event.y}

//Capture Mouse
if(bc.ns4)document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = popmousemove;

var descx = 0;
var descy = 0;

function showMenu(id,left,top){
	
	//var px = bc.ns4||window.opera?"":"px";

	//if(bc.ie5||bc.ie6) descy = descy+document.body.scrollTop

	if (document.all) { 
	document.all[id].style.visibility = "visible";
	document.all[id].style.position = "absolute";
	document.all[id].style.left = left;
	document.all[id].style.top = top;
	} 
	if(document.layers) { 
	document.layers[id].visibility = "show" ;
	document.layers[id].style.position = "absolute";
	document.layers[id].style.left = left;
	document.layers[id].style.top = top;
	} 
	if(document.getElementById) {
	document.getElementById(id).style.visibility = "visible";
	document.getElementById(id).style.position = "absolute";
	document.getElementById(id).style.left = left;
	document.getElementById(id).style.top = top;
	} 
} 

function showColor(id,left,top){
	
	var px = bc.ns4||window.opera?"":"px";

	if(bc.ie5||bc.ie6) descy = descy+document.body.scrollTop

	if (document.all) { 
	document.all[id].style.visibility = "visible";
	document.all[id].style.position = "absolute";
	document.all[id].style.left = (descx+left)+px;
	document.all[id].style.top = (descy+top)+px;
	} 
	if(document.layers) { 
	document.layers[id].visibility = "show" ;
	document.layers[id].style.position = "absolute";
	document.layers[id].style.left = (descx+left)+px;
	document.layers[id].style.top = (descy+top)+px;
	} 
	if(document.getElementById) {
	document.getElementById(id).style.visibility = "visible";
	document.getElementById(id).style.position = "absolute";
	document.getElementById(id).style.left = (descx+left)+px;
	document.getElementById(id).style.top = (descy+top)+px;
	} 
} 
function switchTab(id,num){

	if(document.getElementById){
		document.getElementById('tab1').className = 'genTab';
		for(i=1;i<=num;i++){
			document.getElementById('tab'+i).className = 'midTab';
			document.getElementById('bot'+i).className = 'bot';
		}
		if(id == 1){
			document.getElementById('tab1').className = 'genOver';
			document.getElementById('bot1').className = 'botOver';
		}
		else{
			document.getElementById('tab'+id).className = 'midOver';
			if(id == 2){
			document.getElementById('tab1').className = 'genRight';
			}
			else{
			document.getElementById('tab1').className = 'genTab';
			document.getElementById('tab'+(id-1)).className = 'midRight';
			}
			document.getElementById('bot'+id).className = 'botOver';
		}
		for(i=1;i<=num;i++){
		document.getElementById('layer'+i).style.visibility="hidden";
		document.getElementById('layer'+i).style.position='absolute';
		}
		document.getElementById('layer'+id).style.visibility="visible";
		document.getElementById('layer'+id).style.position="relative";
	}
}
function switchLayer(showLayer,hideLayer){
	if(document.getElementById){
	document.getElementById(showLayer).style.visibility="visible";
	document.getElementById(showLayer).style.position="absolute";
	document.getElementById(hideLayer).style.visibility="hidden";
	document.getElementById(hideLayer).style.position='absolute';
	}
	else if(document.all){
	document.all[showLayer].style.visibility="visible";
	document.all[showLayer].style.position="absolute";
	}
	else if(document.layers){
	document.layers[showLayer].visibility="show";
	document.layers[showLayer].style.position="absolute";
	}
}
function showLayer(id){
	if(document.getElementById){
	document.getElementById(id).style.visibility="visible";
		if(id=='palette_layer'){
		document.getElementById(id).style.position="absolute";
		}
		else{
		document.getElementById(id).style.position="relative";
		}
	}
	else if(document.all){
	    document.all[id].style.visibility="visible";
	    document.all[id].style.position="relative";
	}
	else if(document.layers){
	        document.layers[id].visibility="show";
	        document.layers[id].style.position="relative";
        }
}


function hideLayer(id){
	if(document.getElementById){
	document.getElementById(id).style.visibility="hidden";
	document.getElementById(id).style.position='absolute';
	}
	else if(document.all){
	    document.all[id].style.visibility="hidden";
	    document.all[id].style.position="absolute";
	}
	else if(document.layers){
	        document.layers[id].visibility="hide";
	        document.layers[id].style.position="absolute";
        }
}


function showHelpTip(e, sHtml, bHideSelects) {

	// find anchor element
	var el = e.target || e.srcElement;
	while (el.tagName != "A")
		el = el.parentNode;
	
	// is there already a tooltip? If so, remove it
	if (el._helpTip) {
		helpTipHandler.hideHelpTip(el);
	}

	helpTipHandler.hideSelects = Boolean(bHideSelects);

	// create element and insert last into the body
	helpTipHandler.createHelpTip(el, sHtml);
	
	// position tooltip
	helpTipHandler.positionToolTip(e);

	// add a listener to the blur event.
	// When blurred remove tooltip and restore anchor
	el.onblur = helpTipHandler.anchorBlur;
	el.onkeydown = helpTipHandler.anchorKeyDown;
}

var helpTipHandler = {
	hideSelects:	false,
	
	helpTip:		null,
	
	showSelects:	function (bVisible) {
		if (!this.hideSelects) return;
		// only IE actually do something in here
		var selects = [];
		if (document.all)
			selects = document.all.tags("SELECT");
		var l = selects.length;
		for	(var i = 0; i < l; i++)
			selects[i].runtimeStyle.visibility = bVisible ? "" : "hidden";	
	},
	
	create:	function () {
		var d = document.createElement("DIV");
		d.className = "help-tooltip";
		d.onmousedown = this.helpTipMouseDown;
		d.onmouseup = this.helpTipMouseUp;
		document.body.appendChild(d);		
		this.helpTip = d;
	},
	
	createHelpTip:	function (el, sHtml) {
		if (this.helpTip == null) {
			this.create();
		}

		var d = this.helpTip;
		d.innerHTML = sHtml;
		d._boundAnchor = el;
		el._helpTip = d;
		return d;
	},
	
	// Allow clicks on A elements inside tooltip
	helpTipMouseDown:	function (e) {
		var d = this;
		var el = d._boundAnchor;
		if (!e) e = event;
		var t = e.target || e.srcElement;
		while (t.tagName != "A" && t != d)
			t = t.parentNode;
		if (t == d) return;
		
		el._onblur = el.onblur;
		el.onblur = null;
	},
	
	helpTipMouseUp:	function () {
		var d = this;
		var el = d._boundAnchor;
		el.onblur = el._onblur;
		el._onblur = null;
		el.focus();
	},	
	
	anchorBlur:	function (e) {
		var el = this;
		helpTipHandler.hideHelpTip(el);
	},
	
	anchorKeyDown:	function (e) {
		if (!e) e = window.event
		if (e.keyCode == 27) {	// ESC
			helpTipHandler.hideHelpTip(this);
		}
	},
	
	removeHelpTip:	function (d) {
		d._boundAnchor = null;
		d.style.filter = "none";
		d.innerHTML = "";
		d.onmousedown = null;
		d.onmouseup = null;
		d.parentNode.removeChild(d);
		//d.style.display = "none";
	},
	
	hideHelpTip:	function (el) {
		var d = el._helpTip;
		/*	Mozilla (1.2+) starts a selection session when moved
			and this destroys the mouse events until reloaded
		d.style.top = -el.offsetHeight - 100 + "px";
		*/		
		
		d.style.visibility = "hidden";
		//d._boundAnchor = null;

		el.onblur = null;
		el._onblur = null;
		el._helpTip = null;
		el.onkeydown = null;
		
		this.showSelects(true);
	},
	
	positionToolTip:	function (e) {
		this.showSelects(false);		
		var scroll = this.getScroll();
		var d = this.helpTip;
		
		// width
		if (d.offsetWidth >= scroll.width)
			d.style.width = scroll.width - 10 + "px";
		else
			d.style.width = "";
		
		// left
		if (e.clientX > scroll.width - d.offsetWidth)
			d.style.left = scroll.width - d.offsetWidth + scroll.left + "px";
		else
			//derrick modified 245 from 10
			d.style.left = e.clientX - 245 + scroll.left + "px";
		
		// top
		if (e.clientY + d.offsetHeight + 18 < scroll.height)
			d.style.top = e.clientY + 18 + scroll.top + "px";
		else if (e.clientY - d.offsetHeight > 0)
			d.style.top = e.clientY + scroll.top - d.offsetHeight + "px";
		else
			d.style.top = scroll.top + 5 + "px";
			
		d.style.visibility = "visible";
	},
	
	// returns the scroll left and top for the browser viewport.
	getScroll:	function () {
		if (document.all && typeof document.body.scrollTop != "undefined") {	// IE model
			var ieBox = document.compatMode != "CSS1Compat";
			var cont = ieBox ? document.body : document.documentElement;
			return {
				left:	cont.scrollLeft,
				top:	cont.scrollTop,
				width:	cont.clientWidth,
				height:	cont.clientHeight
			};
		}
		else {
			return {
				left:	window.pageXOffset,
				top:	window.pageYOffset,
				width:	window.innerWidth,
				height:	window.innerHeight
			};
		}
		
	}

};

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function selectAllCheckboxes( checkObj, searchText ) {
//this is fired by a user clicking the Select All checkbox and it will toggle
//all checkboxes accordingly
   
   if (searchText == 'add') document.getElementById('excAll').checked = false;
   if (searchText == 'exc') document.getElementById('addAll').checked = false;

   var allCheckboxes = document.getElementById('more_lists').getElementsByTagName('input');

   for (var i=2;i<allCheckboxes.length;i++) {
      var prefix = allCheckboxes[i].name;
      if ( (checkObj.checked != allCheckboxes[i].checked) && (prefix.substring(0,3) == searchText) ) {
         //if needed, toggle the checkbox
         allCheckboxes[i].checked = checkObj.checked;
         }
      else if ( (allCheckboxes[i].checked) && (checkObj.checked) && (prefix.substring(0,3) != searchText) ) {
         //if an include checkbox is being checked, uncheck the corresponding exclude box, and vice versa
         allCheckboxes[i].checked = false;
         }
   }
}

function validateSelectAllCheckboxIsCorrect() {
//this function checks that both Select All checkboxes are correct in relation to what other
//checkboxes are selected.  It's called onload and when any checkbox is clicked

   var allCheckboxes = document.getElementById('more_lists').getElementsByTagName('input');
   var addAreAllChecked = true;
   var addAreNoneChecked = true;
   var excAreAllChecked = true;
   var excAreNoneChecked = true;

   for (var i=2;i<allCheckboxes.length;i++) {
      if (i % 2 == 0) {
         if (allCheckboxes[i].checked) addAreNoneChecked = false;
         if (!allCheckboxes[i].checked) addAreAllChecked = false;
      } else {
         if (allCheckboxes[i].checked) excAreNoneChecked = false;
         if (!allCheckboxes[i].checked) excAreAllChecked = false;      
      } //close if
   } //close for loop
   if (addAreAllChecked) document.getElementById('addAll').checked = true;
   if (addAreNoneChecked) document.getElementById('addAll').checked = false;
   if ( (!addAreAllChecked) && (!addAreNoneChecked) ) document.getElementById('addAll').checked = false;
   if (excAreAllChecked) document.getElementById('excAll').checked = true;
   if (excAreNoneChecked) document.getElementById('excAll').checked = false;
   if ( (!excAreAllChecked) && (!excAreNoneChecked) ) document.getElementById('excAll').checked = false;
} //close function

function checkbox_action(checkboxObj, elt) {
//triggered by the user clicking a checkbox

   // Only let one of the two checkboxes in each row be checked
   inverseCheckbox = document.getElementById(elt);
   if (inverseCheckbox.checked) inverseCheckbox.checked = false;

   //check whether the click affects the Select All checkbox
   validateSelectAllCheckboxIsCorrect();
}

function showMore(whichEl) {
   if( document.getElementById(whichEl).style.display) {
      if( document.getElementById(whichEl).style.display == "none") {
         document.getElementById(whichEl).style.display = "block";
      } else {
         document.getElementById(whichEl).style.display = "none";
      }
   }
}

