//////////////////////////////////////////////////////////////////////////////////////
// checkInput.js
// 2003-2009
//

//////////////////////////////////////////////////////////////////////////////////////
// onKeyPress functions for form elements.
// removed.

//////////////////////////////////////////////////////////////////////////////////////
// submit and reset buttons...
// mostly removed.

function goSubmit( formname, mode ) {
	if (!formname) { formname = "myForm"; }
	if( mode == "admin" || checkRequiredElements(formname,0) ) { document[formname].submit(); }
}

//////////////////////////////////////////////////////////////////////////////////////
// Checks that required elements have been filled in.  
// Second parameter decides if an 'alert' message should be displayed.
//
var RequiredArray = new Array();
function checkRequiredElements(formname,no_alert) {

	if (!formname) { formname = "myForm"; }
	var missing_an_input = 0;
	var c = 'Please fill in the required fields.\nRequired fields are indicated by a *.\t\nThese include the following:\n\n';
	if (RequiredArray[formname].length) {
		for (var i=0 ; i<RequiredArray[formname].length ; i++ ) {
			a = RequiredArray[formname][i];
			i++;
			b = RequiredArray[formname][i];
			L = document[formname][b].length;
			t = document[formname][b].tagName;
			x = document[formname][b].value;
			if (L&&!t) {	// assuming checkbox or radio if there is a length but no tagName found
				for (var ii=0 ; ii<L ; ii++ ) {
					if (document[formname][b][ii].checked) { x="is set"; }
				}				
			}
			if (!x) { c = c + "\t" + a +"\t\t\n"; missing_an_input = 1; }
		}
	}
	if (missing_an_input == 1) {
		if (no_alert == 0) {
			alert (c+"\n");
		}
		return false;
	} else {
		return true;
	}
}

//////////////////////////////////////////////////////////////////////////////////////
// Checks inputs against regular expression
//
	function checkRegExp(referer,myRegExp,Label,Params,e) { 
		if ( referer.value != '' ) {
			global_referer = referer;
			var reg = eval("/^" + myRegExp + "$/");
			var test = reg.exec(referer.value);
			if ( !test ) {
				if ( Label ) {
					alert ( "The \"" + Label + "\" field requires the following:" + "\n" + Params + "\n" );
				} else {
					alert ( "This field requires the following:" + "\n" + Params + "\n" );
				}
				if ( document.all ) { setTimeout (" global_referer.select(); ", 20); }
			}
		}
		return; 
	}

//////////////////////////////////////////////////////////////////////////////////////
// Checks dates
//
	var month_days = new Array('',31,28,31,30,31,30,31,31,30,31,30,31);	
	function check_dates(datename,formname) { 
		if (!formname) { formname = "myForm"; }
		month = document.forms[formname][datename+"_month"].value;
		day   = document.forms[formname][datename+"_day"].value;
		year  = document.forms[formname][datename+"_year"].value;
		if ( year && ( ( year%4 == 0 && year%100 != 0 ) || year%400 == 0 ) ) { month_days[2] = 29; }
		if ( !year && month == 2 ) { month_days[2] = 29; }
		if ( !month ) { month=1; }
		document.forms[formname][datename+"_day"].options.length = 0;
		document.forms[formname][datename+"_day"].options[0] = new Option('Day','');
		for(i=1;i<=month_days[month];i+=1) { document.forms[formname][datename+"_day"].options[i] = new Option(i,i); }
		if ( day <= month_days[month] ) {
			document.forms[formname][datename+"_day"].options.selectedIndex = day;
		}
		month_days[2] = 28;
	}

//////////////////////////////////////////////////////////////////////////////////////
// Check "Confirm E-Mail"
//
	function check_e2(e1,e2,formname) { 
		if (!formname) { formname = "myForm"; }
		if ( document.forms[formname][e1].value != document.forms[formname][e2].value ) {
			document.forms[formname][e2].focus();
			alert("Please check that your e-mail address has been entered correctly in both boxes.");
		}
	}

//////////////////////////////////////////////////////////////////////////////////////
// Checks stores by state
//
	function checkStateField() { 
		if ( document.forms['myForm'] ) {
			show_store_by_state('t4m_favorite_store',document.forms['myForm']['state_t4m_favorite_store'].value,'');
		}
	}
	function show_store_by_state(storefield,state,formname) { 
		if (!formname) { formname = "myForm"; }
		document.forms[formname][storefield].options.length = 0;
		var temp = "Stores:\n";
		var ii = 0;
		for(i=0;i<=StoreArray.length;i+=3) { 
//			alert(i+" "+StoreArray[i+2]+" "+StoreArray[i+1])
			if ( StoreArray[i] == state ) {
				document.forms[formname][storefield].options[ii] = new Option(StoreArray[i+2],StoreArray[i+1]); 
				ii++;
			}
		}
		document.forms[formname][storefield].value = document.forms[formname]["temp_"+storefield].value
	}

//////////////////////////////////////////////////////////////////////////////////////
//	checks birthday for month/day, so we can record the info
//
	function check_member_fields(formname) {
		if (!formname) { formname = "myForm"; }
			var fn = document.forms[formname]["t4m_name_first1"].value;
			if ( fn.length < 2 ) {
				alert("Please supply a full first name so we can properly address you!");
				document.forms[formname]["t4m_name_first1"].focus();
				return false;
			}
			var e1 = document.forms[formname]["t4m_email"].value;
			var e2 = document.forms[formname]["t4m_confirm_email"].value;
			if ( e1 != e2 ) {
				alert("Please check that your email address has been entered correctly in both boxes.");
				document.forms[formname]["t4m_confirm_email"].focus();
				return false;
			}
			var bm = document.forms[formname]["t4m_birthday1_month"].value;
			var bd = document.forms[formname]["t4m_birthday1_day"].value;
//			if ( ( bm != "" || bd != "" ) && ( bm == "" || bd == "" ) ) {
			if ( ( bm == "" || bd == "" ) ) {
				alert("Please supply at least a day and month for your birthday. We wouldn't want to miss it!");
				document.forms[formname]["t4m_birthday1_day"].focus();
				return false;
			}
		return true;
	}

//////////////////////////////////////////////////////////////////////////////////////
//	checks the kids' info to be sure we have enough info to add them to the bday club
//
	function check_relations_fields(formname,mode) {
		if (!formname) { formname = "myForm"; }
		if ( mode == 'admin' ) { return true; }
		for ( var i=2;i<7;i++ ) {
			var fn = document.forms[formname]["t4m_name_first" + i].value;
			var bm = document.forms[formname]["t4m_birthday" + i + "_month"].value;
			var bd = document.forms[formname]["t4m_birthday" + i + "_day"].value;
			var by = document.forms[formname]["t4m_birthday" + i + "_year"].value;
//			var rl = document.forms[formname]["t4m_name_first" + i].value || "your kids";
			var rl = "your family members";
			if ( ( fn != "" || bm != "" || bd != "" || by != "" ) && ( fn.length < 2 || bm == "" || bd == "" || by == "" ) ) {
//				alert("We need at least a first name and a full birth date to enroll "+rl+" in the birthday club!");
				alert("Please supply at least a first name and a full birth date for any additional family members.");
				document.forms[formname]["t4m_name_first" + i].focus();
				return false;
			}
		}
		return true;
	}

//////////////////////////////////////////////////////////////////////////////////////
// For clearing a secondary user's row
	function clear_row(row) {
		document.forms.myForm["t4m_name_first"+row].value = "";
		document.forms.myForm["t4m_name_last"+row].value = "";
		document.forms.myForm["t4m_birthday"+row+"_month"].value = "";
		document.forms.myForm["t4m_birthday"+row+"_day"].value = "";
		document.forms.myForm["t4m_birthday"+row+"_year"].value = "";
		if ( document.forms.myForm["t4m_opt_news_list"+row] ) {
			document.forms.myForm["t4m_opt_news_list"+row].checked = 0;
		}
		document.forms.myForm["t4m_opt_birthday_list"+row].checked = 0;
	}

//////////////////////////////////////////////////////////////////////////////////////
//	add an event handler
function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
//		alert('What browser are you using?  Nothing is going to work here!')
		return false; 
	} 
}

