// JavaScript Document
function addLoadEvent(func) {	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

function ButOver(object) {
	var class_name = '';
	class_name = object.className;
	object.className = class_name.replace(/button/, "button_hover");
}

function ButOut(object) {
	var class_name = '';
	class_name = object.className;
	object.className = class_name.replace(/button_hover/, "button");
}

function RowOver(obj) {
	obj.oldClassName = obj.className;
	obj.className = 'row_o';
}

function RowOut(obj) {
	obj.className = obj.oldClassName;
}

function printPreview(url,DefaultWidth,DefaultHeight) {
	var DefaultWidth = (DefaultWidth == null) ? 580 : DefaultWidth;
	var DefaultHeight = (DefaultHeight == null) ? DefaultWidth-200 : DefaultHeight;
	var winl = (screen.width - DefaultWidth) / 2;
	var top = (screen.height - DefaultHeight) / 2;
	
	Width = DefaultWidth+30;
	Height = DefaultHeight;

	if (DefaultWidth > screen.width) {
		Width = screen.width;
	}
	
	if (Height > screen.height) {
		Height = screen.height;
	}
	
	preview = window.open(url, 'printPreviewwindow','width='+Width+',height='+Height+',top='+top+',left='+winl+',toolbar=no,menubar=yes,dependent=yes,resizable=yes,scrollbars=yes');
	preview.focus();
	return preview;
}

function ChangeStates(obCountry) {

	div = "div_state";
	div_us = "div_us_state";
	
	if(obCountry.options[obCountry.selectedIndex].value == 'US') {
		document.getElementById(div).style.display = "none";
		document.getElementById(div_us).style.display = "block";
	} else {
		document.getElementById(div).style.display = "block";
		document.getElementById(div_us).style.display = "none";
	}
}

function SetJSField(Tid,Tvalue) {
	tF = document.getElementById(Tid);
	if(tF){
		if ((tF.type == 'radio') || (tF.type == 'checkbox')) {
			if (Tvalue == 1) tF.checked = true; else tF.checked = false;
		} else {
			tF.value = (Tvalue);
		}
	}
}

function StarChecker() {
	for (i=0;i<document.forms.length;i++) {
		for (y=0;y<document.forms[i].elements.length;y++) {
			curelem = document.forms[i].elements[y];
			if (curelem.className == 'starfield_char') {

				if ((curelem.value.length < 2) || (curelem.value == '-')) curelem.className = 'starfield_notcorrect';

			} else
			if (curelem.className == 'starfield_int') {

				if (parseInt(curelem.value) < 1) curelem.className = 'starfield_notcorrect';
				if (curelem.value.length < 1) curelem.className = 'starfield_notcorrect';
				

			} else
			if (curelem.className == 'starfield_float') {
				if (parseFloat(curelem.value) <= 0) curelem.className = 'starfield_notcorrect';
				if (curelem.value.length < 1) curelem.className = 'starfield_notcorrect';
			}
		}
	}
}

function FormatMoney(value) {
   result="$"+Math.floor(value)+".";
   var cents=100*(value-Math.floor(value))+0.5;
   result += Math.floor(cents/10);
   result += Math.floor(cents%10);
   return result;
}

function SelectItemByValue(select_box,item_) {
	var whichitem = 0;
	if (document.getElementById(select_box) == null) return;
	while (whichitem < document.getElementById(select_box).length) {
		if (document.getElementById(select_box).options[whichitem].value == item_) {
			document.getElementById(select_box).selectedIndex = whichitem; 
			break;
		}
		whichitem++;
	}
}

function SelectItem(select_box,item_) {
	var whichitem = 0;
	if (document.getElementById(select_box) == null) return;
	while (whichitem < document.getElementById(select_box).length) {
		if (document.getElementById(select_box).options[whichitem].text == item_) {
			document.getElementById(select_box).selectedIndex = whichitem; 
			break;
		}
		whichitem++;
	}
}

function getAnElement(id) {
	return document.getElementById?document.getElementById(id):document.all?document.all[id]:document.layers[id];
}

function validate_zip(field) {
	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.length!=5 && field.length!=10) {
		//alert("Please enter your 5 digit or 5 digit+4 zip code.");
		alert("Please enter your 5 digit zip code.");
		return false;
	}
	
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		//if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
			alert("Invalid characters in your zip code. Please try again.");
			return false;
		}
		/*
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			return false;
		}
		*/
	}
	return true;
}
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function set_cookie(name, value, expires) {
	document.cookie = name + '=' + escape(value) + '; path=/' + (typeof expires != 'undefined' ? '; expires=' + expires.toGMTString() : '');
}

function get_cookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end))
		}
	}
	return ""
}