
errorClr = "#ffff00";

function checke(e) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(h=0; h < e.length ;h++){
		if(ok.indexOf(e.charAt(h))<0){ 
			return (false);
		}		
	} 
	if(document.images){
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
			return (-1);		
		} 
	}
}

function ucFirst(l,a){
	if(l){
		f = l.charAt(0);
		f = f.toUpperCase();
		l = f + "" + l.substr(1);
	}
	if(a == 3 && document.images){ // apply to all
		ALLCAPS = new Array();
		ALLCAPS = l.split(" ");
		if(ALLCAPS[0]){
			str = "";
			for(AC = 0; AC < ALLCAPS.length; AC++){
				ALLCAPS[AC] = ucFirst(ALLCAPS[AC]);
				str += ALLCAPS[AC] + " ";
			}
			l = str.substr(0,str.length - 1);
		}
		delete ALLCAPS;
	}
	return (l);
}

function checkZip(z){
	if(document.images){
		z.value = z.value.replace(/\D+/, "");
	}
	if(z.value.length != 5 && z.value.length != 9){
		alert("Whoops, please check your zip code");
		if(document.all || document.getElementById){
			z.style.background = errorClr;
		}
		z.focus();
		return (false);
	}
	if(z.value.length > 5){
		z.value = z.value.substr(0,5) + "-" + z.value.substr(5);
	}
	return (z);
}

function noSpace(s){
	// no spaces in id
	if(s.value.match(/\s+/)){
		alert("Whoops, no spaces allowed in Event ID"); 
		if(document.all || document.getElementById){
			s.style.background = errorClr;
		}
		s.focus();
		return (false);
	}
	return true;
}

function matchFields(a,b){
	if(a.value != b.value){
		alert("Whoops, the two values you entered do not match");
		if(document.all || document.getElementById){
			a.style.background = errorClr;
		}
		a.value = "";
		b.value = "";
		a.focus();
		return (false);
	}
	return true;
}

function illChars(a,b){
	if(a.value.match(/\W+/)){
		alert("Whoops, illegal character found in password"); 
		if(document.all || document.getElementById){
			a.style.background = errorClr;
		}
		a.value = "";
		b.value = "";
		a.focus();
		return (false);
	}
	return true;
}

// args = form object, array element, check e-mail if value
function checkField(f,w,e){
	// upper case first
	if(w[1] > 0){
		f[w[0]].value = ucFirst(f[w[0]].value, w[1]);
	}
	// all fields must have value
	if(w[3] == "Text"){
		if(f[w[0]].value.length < w[2]){
			alert("Whoops, you missed field: " + w[4]);
			if(document.all || document.getElementById){
				f[w[0]].style.background = errorClr;
			}
			f[w[0]].focus();
			return (false);
		}
	} else if(w[3] == "Select"){
		if(f[w[0]].selectedIndex < 1){
			alert("Whoops, you missed field: " + w[4]);
			if(document.all || document.getElementById){
				f[w[0]].style.background = errorClr;
			}
			f[w[0]].focus();
			return (false);
		}
	} else if(w[3] == "Email"){
		if(e){
			if(!checke(f[w[0]].value) && f[w[0]].value.length > 0){
				alert("Whoops, please enter a valid e-mail address."); 
				if(document.all || document.getElementById){
					f[w[0]].style.background = errorClr;
				}
				f[w[0]].focus();
				return (false);
			}
		} else {
			if(!checke(f[w[0]].value)){
				alert("Whoops, please enter a valid e-mail address."); 
				if(document.all || document.getElementById){
					f[w[0]].style.background = errorClr;
				}
				f[w[0]].focus();
				return (false);
			}
		}
	} else if(w[3] == "Radio"){
		isRad = 0;
		for(r = 0; r < f[w[0]].length; r++){
			if(f[w[0]][r].checked){
				isRad++;
			}
		}
		if(!isRad){
			alert("Whoops, you missed field: " + w[4]);
			if(document.all || document.getElementById){
				f[w[0]][0].style.background = errorClr;
			}
			f[w[0]][0].focus();
			return (false);
		}
	}
	return true;
}

// member sign up reqs
REQSms = new Array();
//           field name,  ucfirst, min length, type, label
REQSms[0]  = ["mbr_firstname",    2, 1, "Text",   "First Name"];
REQSms[1]  = ["mbr_lastname",     2, 1, "Text",   "Last Name"];
REQSms[2]  = ["mbr_companyname",  3, 1, "Text",   "Company Name"];
REQSms[3]  = ["mbr_companytype",  3, 1, "Text",   "Type of Business"];
REQSms[4]  = ["mbr_address1",     3, 1, "Text",   "Address 1"];
REQSms[5]  = ["mbr_address2",     3, 0, "Text",   "Address 2"];
REQSms[6]  = ["mbr_city",         3, 1, "Text",   "City"];
REQSms[7]  = ["mbr_state",        0, 1, "Select", "State"];
REQSms[8]  = ["mbr_zip",          0, 5, "Text",   "Zip Code"];
REQSms[9]  = ["mbr_country",      0, 1, "Select", "Country"];
REQSms[10] = ["country_code",     0, 1, "Text",   "Country Code"];
REQSms[11] = ["area_code",        0, 1, "Text",   "Area Code"];
REQSms[12] = ["mbr_phone",        0, 3, "Text",   "Phone"];
REQSms[13] = ["mbr_email",        0, 5, "Email",  "E-mail Address"];
REQSms[14] = ["mbr_email_conf",   0, 5, "Email",  "E-mail Address Confirmation"];
REQSms[15] = ["mbr_pwd",          0, 3, "Text",   "Password"];
if(inEdit){
	REQSms[16] = ["mbr_pwd_conf",     0, 3, "Text",   "Password Confirmation"];
	REQSms[17] = ["mbr_pref_contact", 0, 1, "Radio",  "Contact me by"];
} else {
	REQSms[16] = ["mbr_pwd_conf",     0, 0, "Text",   "Password Confirmation"];
	REQSms[17] = ["mbr_pref_contact", 0, 0, "Radio",  "Contact me by"];
}

function check_ms(f){
	for(R = 0; R < REQSms.length; R++){
		if(!checkField(f,REQSms[R])){
			return false;
		}
	}
	if(!checkZip(f.mbr_zip)){
		return false;
	}
	
	// check confirmation fields
	if(!matchFields(f.mbr_email,f.mbr_email_conf)){
		return false;
	}
	if(!matchFields(f.mbr_pwd,f.mbr_pwd_conf)){
		return false;
	}
	// no illegal chars in passwords
	if(!illChars(f.mbr_pwd,f.mbr_pwd_conf)){
		return false;
	}
	
	// don't submit form
	//alert("All ok!");
	//return false;
	return true;
}

// step 1 reqs
REQS1 = new Array();
REQS1[0]  = ["event_name",             3, 1, "Text",   "Event Name"];
REQS1[1]  = ["event_id",               0, 1, "Text",   "Event ID"];
REQS1[2]  = ["event_CP_name",          3, 1, "Text",   "Contact Name"];
REQS1[3]  = ["event_CP_addr1",         3, 1, "Text",   "Contact Address 1"];
REQS1[4]  = ["event_CP_city",          3, 1, "Text",   "Contact City"];
REQS1[5]  = ["event_CP_state",         0, 1, "Select", "Contact State"];
REQS1[6]  = ["event_CP_zip",           0, 1, "Text",   "Contact Zip"];
REQS1[7]  = ["event_CP_country",       0, 1, "Select", "Contact Country"];
REQS1[8]  = ["event_CP_phone_c_code",  0, 1, "Text",   "Contact Phone Country Code"];
REQS1[9]  = ["event_CP_phone_a_code",  0, 1, "Text",   "Contact Phone Area Code"];
REQS1[10] = ["event_CP_phone",         0, 3, "Text",   "Contact Phone"];
REQS1[11] = ["event_CP_email",         0, 5, "Email",  "Contact E-mail"];
REQS1[12] = ["event_CP_email_compare", 0, 5, "Email",  "Contact E-mail comparison"];
REQS1[13] = ["event_CP_pwd",           0, 3, "Text",   "Admin Password"];
REQS1[14] = ["event_CP_pwd_confirm",   0, 3, "Text",   "Admin Password Comparison"];

function check_Step1(f){
	for(R = 0; R < REQS1.length; R++){
		if(!checkField(f,REQS1[R])){
			return false;
		}
	}
	
	if(!noSpace(f.event_id)){
		return false;
	}
	if(!checkZip(f.event_CP_zip)){
		return false;
	}
	
	// check confirmation fields
	if(!matchFields(f.event_CP_email,f.event_CP_email_compare)){
		return false;
	}
	if(!matchFields(f.event_CP_pwd,f.event_CP_pwd_confirm)){
		return false;
	}
	// no illegal chars in passwords
	if(!illChars(f.event_CP_pwd,f.event_CP_pwd_confirm)){
		return false;
	}
	
	// don't submit form
	//alert("All ok!");
	//return false;
	return true;
}

// step 2 reqs
REQS2 = new Array();
REQS2[0]  = ["event_month_start",      0, 1, "Select", "Event Start Month"];
REQS2[1]  = ["event_day_start",        0, 1, "Select", "Event Start Day"];
REQS2[2]  = ["event_year_start",       0, 1, "Select", "Event Start Year"];
REQS2[3]  = ["event_month_end",        0, 1, "Select", "Event End Month"];
REQS2[4]  = ["event_day_end",          0, 1, "Select", "Event End Day"];
REQS2[5]  = ["event_year_end",         0, 1, "Select", "Event End Year"];
			
REQS2[6]  = ["event_hour_start",       0, 1, "Select", "Event Start Hour"];
REQS2[7]  = ["event_minute_start",     0, 1, "Select", "Event Start Minute"];
REQS2[8]  = ["event_ampm_start",       0, 1, "Select", "Event Start Meridiem"];
REQS2[9]  = ["event_hour_end",         0, 1, "Select", "Event End Hour"];
REQS2[10] = ["event_minute_end",       0, 1, "Select", "Event End Minute"];
REQS2[11] = ["event_ampm_end",         0, 1, "Select", "Event Meridiem"];

REQS2[12] = ["venue_name",             3, 1, "Text",   "Venue Name"];
REQS2[13] = ["venue_addr1",            3, 1, "Text",   "Venue Address 1"];
REQS2[14] = ["venue_city",             3, 1, "Text",   "Venue City"];
REQS2[15] = ["venue_state",            0, 1, "Select", "Venue State"];
REQS2[16] = ["venue_zip",              0, 1, "Text",   "Venue Zip"];
REQS2[17] = ["venue_country",          0, 1, "Select", "Venue Country"];
REQS2[18] = ["venu_phone_c_code",      0, 1, "Text",   "Venue Phone Country Code"];
REQS2[19] = ["venu_phone_a_code",      0, 1, "Text",   "Venue Phone Area Code"];
REQS2[20] = ["venue_phone",            0, 3, "Text",   "Venue Phone"];
REQS2[21] = ["send_email_addr",        0, 5, "Email",  "RSVP Notification Email"];
REQS2[22] = ["confirm_email_addr",     0, 5, "Email",  "RSVP Notification Email comparison"];

function check_Step2(f){
	// check for leap years
	YEAR = new Date();
	YEAR = YEAR.getYear();
	
	DATES = new Array(); // + 1 for all because of first option
	if(YEAR % 4 == 0){
		DATES[0] = [2 , 29];
	} else {
		DATES[0] = [2 , 28];
	}
	DATES[1] = [4 , 30];
	DATES[2] = [6 , 30];
	DATES[3] = [9 , 30];
	DATES[4] = [11, 30];

	for(D = 0; D < DATES.length; D++){
		if(f.event_month_start.selectedIndex == DATES[D][0]){
			if(f.event_day_start.selectedIndex > DATES[D][1]){
				alert("Whoops, you have entered an invalid start date");
				if(document.all || document.getElementById){
					f.event_day_start.style.background = errorClr;
				}
				f.event_day_start.focus();
				return (false);
			}
		}
		if(f.event_month_end.selectedIndex == DATES[D][0]){
			if(f.event_day_end.selectedIndex > DATES[D][1]){
				alert("Whoops, you have entered an invalid end date");
				if(document.all || document.getElementById){
					f.event_day_end.style.background = errorClr;
				}
				f.event_day_end.focus();
				return (false);
			}
		}
	}
	
	for(R = 0; R < REQS2.length; R++){
		if(!checkField(f,REQS2[R],1)){
			return false;
		}
	}
	
	if(!noSpace(f.event_id)){
		return false;
	}
	if(!checkZip(f.venue_zip)){
		return false;
	}
	if(!matchFields(f.send_email_addr,f.confirm_email_addr)){
		return false;
	}
	
	// check event start end date
	shour = f.event_ampm_start.selectedIndex == 2 ? 
	f.event_hour_start.selectedIndex + 12 : f.event_hour_start.selectedIndex;
	ehour = f.event_ampm_end.selectedIndex == 2 ? 
	f.event_hour_end.selectedIndex + 12 : f.event_hour_end.selectedIndex;
	
	sdate = Date.UTC(f.event_year_start.options[f.event_year_start.selectedIndex].value, f.event_month_start.selectedIndex - 1, f.event_day_start.selectedIndex, shour, f.event_minute_start.selectedIndex - 1, 0);
	edate = Date.UTC(f.event_year_end.options[f.event_year_end.selectedIndex].value, f.event_month_end.selectedIndex - 1, f.event_day_end.selectedIndex, ehour, f.event_minute_end.selectedIndex - 1, 0);
	
	if(sdate > edate){
		alert("Whoops, Event's Start Time/Date comes after Event's End Time/Date"); 
		f.event_month_start.focus();
		return (false);
	}
	
	// check sign in closing time against event start time
	if(f.signin_hour_start.selectedIndex > 0 || f.signin_hour_end.selectedIndex > 0){
		signinCloseHour = f.signin_ampm_end.selectedIndex == 2 ? 
		f.signin_hour_end.selectedIndex + 12 : f.signin_hour_end.selectedIndex;
		
		signinCloseTime = Date.UTC(f.event_year_start.options[f.event_year_start.selectedIndex].value, f.event_month_start.selectedIndex - 1, f.event_day_start.selectedIndex, signinCloseHour, f.signin_minute_end.selectedIndex - 1, 0);
		
		if(signinCloseTime > sdate){
			alert("Whoops, Sign in closing time comes after Event's Start Time/Date"); 
			f.signin_hour_start.focus();
			return (false);
		}
	}
	
	// don't submit form
	//alert("All ok!");
	//return false;
	return true;
}


// step 3 reqs
REQS3 = new Array();
REQS3[0]  = ["payment_cardtype",      0, 1,  "Select", "Credit Card Type"];
REQS3[1]  = ["payment_cardnum",       0, 15, "Text",   "Credit Card Number"];
REQS3[2]  = ["payment_cardexp-mon",   0, 1,  "Select", "Expiration Month"];
REQS3[3]  = ["payment_cardexp-yr",    0, 1,  "Select", "Expiration Year"];
REQS3[4]  = ["payment_cardname",      3, 3,  "Text",   "Name of Credit Card"];
REQS3[5]  = ["payment_csv_code",      0, 3,  "Text",   "Credit Card CSV Code"];

function check_Step3(f){
	// remove spaces
	if(document.images){
		f.payment_cardnum.value = f.payment_cardnum.value.replace(/\s+/, "");
	}
	// check for invalid chars
	if(document.images && f.payment_cardnum.value.match(/\D+/)){
		alert("Whoops, invalid character found in Credit Card Number");
		if(document.all || document.getElementById){
			f.payment_cardnum.style.background = errorClr;
		}
		f.payment_cardnum.focus();
		return (false);
	}
	// check length
	if(f.payment_cardnum.value.length > 16){
		alert("Whoops, Credit Card Number is too long");
		if(document.all || document.getElementById){
			f.payment_cardnum.style.background = errorClr;
		}
		f.payment_cardnum.focus();
		return (false);
	}
	
	for(R = 0; R < REQS3.length; R++){
		if(!checkField(f,REQS3[R])){
			return false;
		}
	}
	
	// check expiration
	dObj = new Date();
	YEAR = dObj.getYear();
	MON  = dObj.getMonth();
	
	if(f[REQS3[3][0]].options[f[REQS3[3][0]].selectedIndex].value <= YEAR && f[REQS3[2][0]].options[f[REQS3[2][0]].selectedIndex].value < MON){
		alert("Whoops, your card has expired");
		if(document.all || document.getElementById){
			f[REQS3[2][0]].style.background = errorClr;
		}
		f[REQS3[2][0]].focus();
		return (false);
	}
	
	// don't submit form
	//alert("All ok!");
	//return false;
	return true;
}


// member master event
REQSme = new Array();
//           field name,  ucfirst, min length, type, label
REQSme[0]   = ["guest_fname",          2, 1, "Text",   "First Name"];
REQSme[1]   = ["guest_lname",          2, 1, "Text",   "Last Name"];
REQSme[2]   = ["guest__addr1",         2, 1, "Text",   "Address 1"];
REQSme[3]   = ["guest__addr2",         3, 0, "Text",   "Address 2"];
REQSme[4]   = ["guest__city",          3, 1, "Text",   "City"];
REQSme[5]   = ["guest__state",         3, 1, "Select", "State"];
REQSme[6]   = ["guest__zip",           3, 1, "Text",   "Zip"];
REQSme[7]   = ["guest__country",       3, 1, "Select", "Country"];
REQSme[8]   = ["guest__phone",         0, 5, "Text",   "Phone"];
REQSme[9]   = ["guest__email",         0, 5, "Email",   "E-mail"];
REQSme[10]  = ["guest__email_compare", 0, 5, "Email",   "E-mail comparison"];

function check_me(f){
	for(R = 0; R < REQSme.length; R++){
		if(!checkField(f,REQSme[R])){
			return false;
		}
	}
	if(!checkZip(f.guest__zip)){
		return false;
	}
	
	// check confirmation fields
	if(!matchFields(f.guest__email,f.guest__email_compare)){
		return false;
	}
	
	// don't submit form
	//alert("All ok!");
	//return false;
	return true;
}
