/**
 * @author My IT Guy
 * @version 1.3
 *  - Updated Jan 27, 2010
 */
var cancelForm = false;
function validateForm() {
	if (cancelForm) { return true; }
	missing = "";
	var pass = [];
	var y = 0;
	var tags = new Array('input','textarea','select');
	for (a=0;a<tags.length;a++) {
		inps = document.getElementsByTagName(tags[a]);
		for (x=0;x<inps.length;x++) {
			if ((inps[x].className.indexOf("req")!=-1) && (elementVisible(inps[x]))){
				if ((inps[x].value=="") || (inps[x].value=="NULL")) {
					missing+=" - "+(((inps[x].alt!=undefined) && (inps[x].alt!='')) ? inps[x].alt : (((inps[x].title != undefined) && (inps[x].title != '')) ? inps[x].title : inps[x].name))+"\n";
					inps[x].className+=' reqSkipped';
				}
				if (inps[x].type=='password') {
					pass[y++]=inps[x].value;
				} else if (inps[x].type=='radio') {
					if (validateRadio(inps[x]) == '') {
				  	missing += " - " + (((inps[x].title != undefined) && (inps[x].title != '')) ? inps[x].title : inps[x].name) + "\n";
				  }
				}
			}
		}
	}
	if (missing!="") {
		alert("You must complete all required fields:\n"+missing);
		return false;
	}
	if ((pass.length>1) && (pass[0]!=pass[1])) {
		alert("Passwords do not match.");
		return false;
	}
	return true;
}

function validateRadio(el) {
	var form = getParentByTagName(el,'FORM');
	return getCheckedValue(document.forms[form.name].elements[el.name]);
}

function getParentByTagName(el,tagName) {
	el = el.parentNode;
	if (el.tagName == tagName) {
  	return el;
  } else {
  	return getParentByTagName(el,tagName);
  }
}

function elementVisible(jselement) {
	do {
		if (jselement.style.display.toUpperCase() == 'NONE') return false;
		jselement=jselement.parentNode;
	} while (jselement.tagName.toUpperCase() != 'BODY');
	return true;
}

function chooseProgram(n,prog) {
	var dob = document.forms['winter_program']['mem'+n+'_dob'].value;
	if (dob=='yyyy/mm/dd') { alert('You must first enter a valid Date of Birth.'); return; }
	if (prog!='') {
		prog = '&priceid='+prog;
	}
	popUpWindow('chooseProgram.php?mem=' + n + '&dob=' + dob + prog,800,600);
}

function addProgram(n,prog) {
	var dob = document.forms[0]['dob'].value;
	if (dob=='yyyy/mm/dd') { alert('You must first enter a valid Date of Birth.'); return; }
	if (prog!='') {
		prog = '&priceid='+prog;
	}
	popUpWindow('chooseProgram.php?prog=' + n + '&dob=' + dob + prog,800,600);
}

function validProg() {
	for (x=1;x<=6;x++) {
		if (document.forms['winter_program']['mem'+x+'_dob'].value!='yyyy/mm/dd') {
			if (document.forms['winter_program']['mem'+x+'_programPrice'].value=='') {
				alert('You must choose a program for each member.');
				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 "";
}

function popUpWindow(url,width,height) {
	window.open(url,'myitguyPopupWindow','width='+width+',height='+height+',scrollbars,resizable');
	return false;
}

/**
 * On page load, call specified function
 * @param functionName
 **/
function onPageLoad(functionName) {
  //check IE's proprietary DOM members.  If both are undefined, it's firefox (or at least, not IE)...
	if(document.uniqueID==document.expando) {
		// handle multiple functions
		fns = functionName.split(';');
		for (x=0;x<fns.length;x++) {
			if (fns[x].length>3) {
				document.addEventListener("DOMContentLoaded", eval(fns[x].replace('()','')), false);
			}
		}
		return;
	}
  //you can create any tagName, even customTag like <document:ready />
  var tempNode = document.createElement('document:ready');
  (function(){
      try {

        //wait until document.readyState === 'complete';
        if(document.readyState != 'complete') return setTimeout(arguments.callee, 0);

        //see if it throws errors until after ondocumentready
        tempNode.doScroll('left');

        //call your function which catch window.onDocumentReady
        eval(functionName);

        //relaese some memory, if possible
        tempNode = null;

      }catch (err){
        setTimeout(arguments.callee, 100);
      }
  })();
};

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
function confirmDelete() {
	return confirm('Are you sure you want to delete this?');
}

function gotoPage(id) {
	var s = location.href.indexOf('s=');
	if (s!=-1) {
		location.href = location.href.substring(0,s+2)+(id*20);
	} else {
		location.href = location.href+'&s='+(id*20);
	}
}