//Adds a function to be run when the page finally loads
//Adoped from http://www.tek-tips.com/faqs.cfm?fid=4862
function addOnloadEvent(fnc) {
	if (typeof window.addEventListener != "undefined") {
 		window.addEventListener("load", fnc, false);
	} else if (typeof window.attachEvent != "undefined") {
		window.attachEvent("onload", fnc);
	} else {
		if (window.onload != null) {
			var oldOnload = window.onload;
			window.onload = function (e) {
				oldOnload(e);
				window[fnc]();
			};
		} else  {
			window.onload = fnc;
		}
	}
}

// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
// (From microsoft.com)
function getInternetExplorerVersion() {
	var rv = -1; // Return value assumes failure
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null) {
			rv = parseFloat(RegExp.$1);
		}
 	}
	return rv;
}

//IE 6/7 use a different attribute name for the ID attribute of tags than other browsers
function getElementClassAttributeName() {
	//Get the version of IE, as version 6 requires a different attribute name
	var ver = getInternetExplorerVersion();
	var classId = "class";
	if (ver >= 6.0) { 
		classId = "className";
	}
	return classId;
}

// Gets the query strong from browser.
// Author: Pete Freitag (pete@cfdev.com)
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}

var colorList;
function initColorClass() {
	colorList = new Array();
	colorList[0] = "tableBG_Blue";
	colorList[1] = "tableBG_Gold";
}
initColorClass();

function getNextColorClass(colorClass) {
	//Determine index
	var currentIndex;
	for (var i = 0; i < colorList.length; i++) {
		if (colorList[i] == colorClass) { currentIndex = i; }
	}
	
	currentIndex = (currentIndex + 1) % colorList.length;
	colorClass = colorList[currentIndex];
	return colorClass;
}


function addEvent(obj, evType, fn) {
	var result = false;
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, false);
		result = true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on" + evType, fn);
		result = r;
	}
	return result;
}

/**
 * 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 ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }
 
//Formats a price depending on whether it's positive or negative
function displayPrice(inputPrice) {
	var inputString = "$" + inputPrice.toFixed(2);
	var outputString = inputString;
	if (inputPrice < 0) { outputString = "<span class=\"importantNotice\">(" + inputString + ")</span>"; }
	return outputString;
}