//reset

function reset(formname)
{
var elem = document.getElementById(formname).elements;
	for(var i = 0; i < elem.length; i++)
	{
		elem[i].value = "";
	}
}

// browser detection
var w3c=(document.getElementById)?true:false;
var ns4=(document.layers)?true:false;
var ie4=(document.all && !w3c)?true:false;
var ie5=(document.all && w3c)?true:false;
var ns6=(w3c && navigator.appName.indexOf("Netscape")>=0)?true:false;

// DHTML functions
var DHTML = (document.getElementById || document.all || document.layers);

function lightup(imgName)
 {
   if (document.images)
    {
      imgOn=eval(imgName + "_on.src");
      document[imgName].src= imgOn;
    }
 }

function turnoff(imgName)
 {
   if (document.images)
    {
      imgOff=eval(imgName + "_off.src");
      document[imgName].src= imgOff;
    }
 }

function getObj(name) {
	if (document.getElementById) {
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	} else
	if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	} else
	if (document.layers) {
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

// return document object by id;
function getobject(obj){
if (document.getElementById)
	return document.getElementById(obj)
else if (document.all)
	return document.all[obj]
}

// set obj class style
function setStyle(obj, className) {		
	if(getobject(obj).className != className) {
	getobject(obj).className = className;
	}
}

/// popup window function
function popup(URL,w,h) {

day = new Date();
id = day.getTime();

var width = w;
var height = h;

var left = ((screen.width-width)/2);
var top = ((screen.height-height)/2);

eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + '');");
}

function wpopup(URL,n,w,h,sb) {

day = new Date();

var name = n;
var width = w;
var height = h;
if(sb==null){var scrollbar=1}else{var scrollbar=sb}

var left = ((screen.width-width)/2);
var top = ((screen.height-height)/2);


var features = 'toolbar=0,scrollbars=' + scrollbar + ',location=0,statusbar=0,menubar=0,resizable=0,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top;

//eval("page" + name + " = window.open(URL, '" + name + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + '');");
    
var popWin = window.open(URL, name, features);
popWin.focus();

}

function popWindow(url, name, features) 
{
    var host = location.hostname;
	window.top.name = "opener";
	var popupWin = window.open( url, name, features );
	
	if ( popupWin && popupWin.opener ) {
		if ( openerUrl )
		{
			popupWin.opener.location = openerUrl;
			popupWin.focus();
		}
	}
}


function enable_other()
{
	if (document.getElementById('clubs_other_check').checked)
	{
		document.getElementById('clubs_other').disabled = false;
		document.getElementById('clubs_other').focus();
	}
	else
	{
		document.getElementById('clubs_other').disabled = true;
		document.getElementById('clubs_other').value = "";
	}
}


/////
///onKeyPress="return numbersonly(this, event)";
/////

function numbersonly(myfield, e, dec)
{
	var key;
	var keychar;
	if (window.event){
		key = window.event.keyCode;
	}else if (e){
		key = e.which;
	}else{
		return true;
	}
	keychar = String.fromCharCode(key);
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ){
		return true;
	}else if ((("+1234567890-. ()").indexOf(keychar) > -1)){
		return true;
	}else if (dec && (keychar == ".")){
		myfield.form.elements[dec].focus();
		return false;
	}else{
		return false;
	}
}

///// onKeyUp="numTyped(this.value,p3,'3')"
/// jump phone number fields
function numTyped(current, next,maxlen) {
  var len = current.length;
  if (len==maxlen) {
    next.focus();
  }
}

//// form validation

/// form validation starts here

var new_fieldname = ""

//this is the onclick that calls this function
//
// onClick="return check(form,form.elements.length)"
//


function checkEmail(email) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
	{
		return (true)
	}
	else
	{
		return (false)
	}
}


function count_checkboxes(element_name)
{ 
	box_checked = 0;
	with (document.form) { 
		for (var i=0; i < elements.length; i++) { 
			if (elements[i].name == element_name)
				if (  elements[i].checked )
				{ 
						box_checked += 1;
				}
		}
	}
	return box_checked;
}
 
function chkAll(frm, arr, mark) {
  for (i = 0; i <= frm.elements.length; i++) {
   try{
     if(frm.elements[i].name == arr) {
       frm.elements[i].checked = mark;
     }
   } catch(er) {}
  }
}



















/** Validate Forms - Mark Sargeant, Sean Stevens 9/25/06 **/
function get_required(formid)
{
	
		function add_error(input,message)
		{		
			if(message==null) message = ", ";
		
			required_fields += label.getAttribute('title') + message;
			labelid.className +=" error";
			errorcount++;	
			
			if(input == "input") inputid.className +=" inputerror";	
		}
	
		/*** Format phone number to (xxx) xxx-xxxx format. Sean Stevens 9/26/06 ***/
		function format_phone(number,inputname)
		{
				var returnString = "";		
				var regexp = /\D/g;
				
				returnString = number.replace(regexp,"");
			
				
				if(returnString.substring(0,1)=="1")
				{
					returnString = returnString.substring(1,returnString.length);					
				}
				
				
				var areaCode = returnString.substring(0,3);
				var exchange = returnString.substring(3,6);
				var lastFour = returnString.substring(6,10);
			
				if(returnString){
					if(returnString.length < 10){
						var formattedPhone = returnString;
						add_error(inputname, " too short, ");
					}
					else
					{		
					var formattedPhone = areaCode + '-' + exchange + '-' + lastFour;
					}		
				}
				else 
				{
					var formattedPhone = ""
				}
				
				document.getElementById(inputname).value = formattedPhone;
		}


if(formid != "reg") formid="reg";
var formname = document.getElementById(formid);


if (!document.getElementsByTagName)
{ 
return; 
}
var labels = formname.getElementsByTagName('label');
var inputs = formname.getElementsByTagName('input');
	var required_fields = "";
	var errorcount = 0;
// loop through all label tags
for (var i=0; i<labels.length; i++)
{

var label = labels[i];
var input = inputs[i];

/*** adds tab-away validation. **/
//input.setAttribute("onblur","get_required()",0)
//input.setAttribute("onfocus","get_required()",0);

if (window.attachEvent) 
{
   input.detachEvent("onblur",get_required);
   input.detachEvent("onfocus",get_required);

   input.attachEvent("onblur",get_required);
   input.attachEvent("onfocus",get_required);
   
   
   
} 
else if (window.addEventListener) 
{
    input.addEventListener("focus",get_required,true);
    input.addEventListener("blur",get_required,true);
}


/*** Format phone number ***/
if((input.id=="phone") || (input.id=="alt_phone") || (input.id=="fax"))
				{
					if(input.value != "") format_phone(input.value, input.id);
				}

	var classAttribute = String(label.className);
			
// use the string.match() method to catch 'required' as the class
	if (classAttribute.toLowerCase().match('required'))
	{

		getlabelid = label.getAttribute('id');		
		labelid = document.getElementById(getlabelid);
	
		
		/*** Checks to see if the label id ends in "radio" or "check" because it has to handle the data differently ***/
		if((labelid.id.substr(labelid.id.length-5)=="radio") || (labelid.id.substr(labelid.id.length-5)=="check"))
		{
						
			var unchecked = 0; 
			
			inputid = labelid.id.substr(4,labelid.id.length-10); //Not sure why 4 and 10 work here, but it does.		
			
			for (var x = 0; x < document.getElementsByName(inputid).length; x++) {				 			
				 	if (document.getElementById(inputid + x).checked == false) {
							unchecked++; 
					}	
			}


			/*** Checks to see if a minimum number of boxes need to be checked. If min is absent, it assumes 1 if the field is required. ***/
			/*** I used a custom attribute, so the HTML won't validate, but it won't break the display or anything.   					 ***/
			
			if(labelid.getAttribute('min') > 0){
		
				if(unchecked > (document.getElementsByName(inputid).length - labelid.getAttribute('min')) )
				{
					add_error()				
				}
				else
				{
					labelid.className = "required";						
				}
			}
			
			/*** If the number of unchecked items equals the number of items, then nothing is checked, so it throws an error. ***/
			else if(unchecked == document.getElementsByName(inputid).length)
			{	
				add_error()					
			}
			else
			{
				labelid.className = "required";				
			}
		
		}			
		/*** End radio and checkbutton verification ***/ 
		
		/*** The label id must be the same as the input id, but start with 'lbl_'. This functions chops off the 'lbl_' and looks for an 	***/
		/*** input field with that name.																							    	***/
		else
		{
		   
		   	   
		   inputid = document.getElementById(getlabelid.substr(4));
			if(inputid.value=="")
			{
				add_error('input');	
				//alert('if: ' + inputid.id);

			}
			else
			{
				labelid.className = "required"; //changes the color of the label
				if(input.type!="radio"){
					if(input.className!="shortinput")
					{
						input.className = "input"; //changes the color of the input
					}
					else
					{
						input.className = "shortinput";
						break;
					}
				}
			
			if((inputid.id=="email"))
				{
					/*** Check if email address is valid ***/
					if(checkEmail(inputid.value)==false)
					{
						add_error(input, " not a valid address, ")

					}
					else
					{
							/*** Check to see if emails match ***/
							if((document.getElementById('email').value) != (document.getElementById('confirm_email').value))
							{
									required_fields += "Email addresses do not match. ";
									document.getElementById('lbl_email').className +=" error";
									document.getElementById('email').className +=" inputerror";
									document.getElementById('lbl_confirm_email').className +=" error";
									document.getElementById('confirm_email').className +=" inputerror";
									errorcount++;
							}				
					}

				}
			}	
		}			
	} 
}


/*** Display error messages. ***/
if(errorcount > 0)
	{
		 document.getElementById('errorwarning').innerHTML = "Please correct the following fields: " + required_fields.substr(0,(required_fields.length-2)) + "."; 
		 return false;
	}
	else
	{
		document.getElementById('errorwarning').innerHTML="";
	}

}