<!--
/* Form Validation script 
 * Written by Carlos R Colon 12/12/05 for Victory2 Multimedia Database program
 * Extensive processing done to authenticate data
 * Further processing will include:
 *  email verification
 *  city verification
 *  drop down menu for birthdate day relative to month & year
 *  textfield entries must be min three characters long
 * Currently all but the above possible errors are covered
 */
 //this array is related to the order listed in the guest list form. 
 // The location in this array matches the option value in the form
 // The value in each location is related to the say of the week. 5-friday, 6-saturday, 7-sunday
var d_arr = new Array(5,5);//The index is mapped to the value of the option. And Events: 0 is Sun - 6 is sat
var err_msg = new Array('');// error messages 'queue' storage
var mnth = new Array("","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var states = new Array('[Select]','ALABAMA','ALASKA','ARIZONA','ARKANSAS','CALIFORNIA','COLORADO','CONNECTICUT','DELAWARE','FLORIDA','GEORGIA','HAWAII','IDAHO','ILLINOIS','INDIANA','IOWA','KANSAS','KENTUCKY','LOUISIANA','MAINE','MARYLAND','MASSACHUSETTS','MICHIGAN','MINNESOTA','MISSISSIPPI','MISSOURI','MONTANA','NEBRASKA','NEVADA','NEW HAMPSHIRE','NEW JERSEY','NEW MEXICO','NEW YORK','NORTH CAROLINA','NORTH DAKOTA','OHIO','OKLAHOMA','OREGON','PALAU','PENNSYLVANIA','PUERTO RICO','RHODE ISLAND','SOUTH CAROLINA','SOUTH DAKOTA','TENNESSEE','TEXAS','UTAH','VERMONT','VIRGINIA','WASHINGTON','WEST VIRGINIA','WISCONSIN','WYOMING');// valid US STATES

var nat = new Array('Afghans','Albanians','Algerians','Americans','Andorrans','Angolans','Argentines','Armenians','Artsakh','Assyrians','Australians','Austrians','Azeris','Bahamians','Bahrainis','Bangladeshis','Batswana','Belarusians','Belgians','Belizeans','Beninese','Bhutanese','Bosnians','Brazilians','Scotish','Welsh','Bruneians','Bulgarians','Burkinabès','Burmese','Burundians','Cambodians','Cameroonians','Canadians','Cape Verdeans','CAR nationals','Chadicans','Chileans','Chinese','Colombians','Comorians','Congolese','Costa Ricans','Croatians','Cubans','Cypriots','Czechs','Danes','Dominicans (Republic)','Djiboutian nationals','(DR)Congolese','East Timorese','Ecuadorians','Egyptians','Emiratis','Guinea','Eritreans','Estonians','Ethiopians','Finns','Fijians',' Filipinos','French','Martinicans','Gabonese','Gambians','Georgians','German','Ghanaians','Greeks','Grenadians','Guatemalans','Guianese (French)','Guinea-Bissau nationals','Guyanese','Haitians','Hondurans','Hungarians','Icelanders','Indians','Indonesians','Iranians','Iraqis','Iranians','Irish people','Israelis','Italians','Ivoirians','Jamaicans','Japanese people','Jordanians','Kazakhs','Kenyans','I-Kiribati','Kosovars','Kuwaitis','Kenyans','Koreans','Kyrgyz','Lao','Latvians','Lebanese','Libyans','Liechtensteiners','Lithuanians','Luxembourgeois','Malagasy','Malawians','Malaysians','Maldivians','Malians','Maltese','Mauritanians','Mauritians','Mexicans','Micronesians','Moldovans','Monegasque people','Mongolians','Moroccans','Mozambicans','Namibians','Nepalese',' Dutch people','New Zealanders','Nicaraguans','Nigeriens','Nigerians','Norwegians','Omanis','Pakistanis','Palauans','Palestinians','Panamanians','Papua New Guineans','Paraguayans','Peruvians','Poles','Portuguese','Puerto Ricans','Qataris','Romanians','Russians','Rwandans','Salvadorans','Saudis','Senegalese','Serbs','Sierra Leoneans','Singaporeans','Slovaks','Slovenes','Somalis','South Africans','Spaniards','Stateless persons','Sri Lankans','Sudanese','Swazis','Swedes','Swiss people','Taiwanese','Tajiks','Tanzanians','Thais','Tibetans','Togolese','Tongans','Tunisians','Turkmen','Turks','Tuvaluans','Ugandans','Ukrainians','Uruguayans','Uzbeks','Venezuelans','Vietnamese','Yemenis','Zambians','Zimbabweans');
var nat_ind = new Array(4,0,3,0,4,4,1,4,4,4,0,0,4,3,4,2,4,4,0,4,4,4,0,1,0,1,4,0,4,4,4,2,4,0,4,4,3,1,2,1,4,3,2,0,1,0,0,0,1,4,4,3,1,4,4,3,4,0,3,0,2,2,0,3,4,3,0,0,3,0,3,1,0,4,4,3,1,0,0,3,2,0,4,4,0,0,0,4,3,2,4,4,3,4,0,4,3,2,4,2,0,4,4,0,0,0,0,0,2,4,4,0,4,4,1,2,4,4,2,4,3,3,2,0,0,1,4,3,0,4,3,4,4,1,4,1,1,0,4,1,4,0,0,3,1,4,3,0,3,2,0,0,3,3,1,4,2,3,3,0,0,2,4,3,2,2,4,4,2,0,0,4,3,0,1,0,1,2,4,3,3);
var evnt_ages = new Array(21,21,21,21);
var add_names = new Array();
var m = 0;//store month
var m_val = 3;// maximum 
$(document).ready(function() {
	init();
});
/* Display month for event date and birthday fields
*/
function disp_month(i, id) {
	if(id=="bday_mnth") {
		for( j=i; j<13; j++ ) {// fill months for birthday list
			if( i < 10 )	{
				$('#'+id).append('<option value=' + j + '>' + mnth[j] + '</option>');
//				document.write('<option value=' + j + '>' + mnth[j] + '</option>');
			}
			else {
				$('#'+id).append('<option value=' + j + '>' + mnth[j] + '</option>');
//document.write('<option value=' + j + '>' + mnth[j] + '</option>');
			}
		}
	} else {// set current month
		$('#'+id).html('<option value=' + (i+1) + '>' + mnth[i+1] + '</option>');
		//<option value=' + (i+1) + '>' + mnth[i+1] + '</option>'
	}
}
/* Display the next three event dates for selected event
*/
function disp_dates() {
	var i = 1, v = parseInt($('#evnt').val());
	n_val = 0;
	
	val = evalDay(d_arr[v]);//store first date
	var dateObj = new Date();
	temp = '<select name="c_dates">';
	
	// this is a comment
	/** this is a comment **/
	/*
		this is a comment
	 */
	
	/****************** EDIT HERE FOR NEW EVENT / EVENT DATE | V is the event index in guest list *************/
	if( v == 2 ) {// v is the selected option index for events
		temp += '<option value="30">30</option>';  
	} /*************************************** END ********************************************************/
	else { //comment me if no special dates
		temp += '<option value="">---</option>';
		temp = temp + '<option value=' + val + ' selected>' + val + '</option>';	
		while( i != m_val ){
			dateObj.setDate(val+7);// fill the next 3 dates
			val = dateObj.getDate();
			if(	n_val < val ){ 
				temp = temp + '<option value=' + val + '>' + val + '</option>';
				n_val = val;
			}
			i++;
		}
	}//comment me if no special dates
	temp += '</select>';
	$("#dates").html(temp);
}
//display approriate nationalities according to race
function disp_nationality(n) {
	temp = '<select name="nationality"><option value="-1">[Select]</option>';
	j = 0;
	for(; j<nat.length; j++ )// fill nationalities
		if( n == nat_ind[j] )// if it belongs to the selected race 
			temp += '<option value=' + j + '>' + nat[j] + '</option>';//add nationality to list
	temp += '<option value=' + (++j) + '>' + 'Other' + '</option>' + '</select>';//close list
	document.getElementById("nationality").innerHTML = temp;
}
/* Get the next event date for selected day
*/
function evalDay(tDay) { 
	var dateObj2 = new Date();
	var aDay = dateObj2.getDay();//current day
	if(tDay<aDay)//if today is after thursday, get next event
		dateObj2.setDate( dateObj2.getDate()+7-(aDay - tDay));
	else //if today is before thursday, get this weeks event date
		dateObj2.setDate( dateObj2.getDate()+(tDay - aDay) );
	m = dateObj2.getMonth();//get month of event 
	return dateObj2.getDate();//return date 
}
/* Display current year for event date
*/
function disp_year(id) { 
	m_age = 21;
	///alert(id);
	var i=new Date().getFullYear();// get current month
	if(id=='years') {
		yStr = '<option value=' + i + '>' + i + '</option>';
	} else {
		//if( document.signup_fm.events.value == 2 ) {// spirit event is selected
			//if( document.signup_fm.gender[1].checked )// if its a female
				//m_age = 18;
			//}
			//else // other events
			//m_age = 21;
		yStr = "<select name='bday_yr'><option value=''>---</option>";
		for( b=1960; b < (i-m_age); b++ )
			yStr = yStr + '<option value=' + b + '>' + b + '</option>';
		yStr = yStr + "</select>";
		//document.getElementById('bday_yrs').innerHTML = yrs;
	}
	$('#'+id).html(yStr);
}
/* Display States list
*/
function disp_state() {
	var t = '';
	for( var s = 0; s < states.length; s++ )
		t += '<option value=' + s + '>' + states[s] + '</option>'
	document.write(t);
}
function gen_customer_id() {
	document.signup_fm.customer_id.value = document.signup_fm.h_firstset.value + '' + document.signup_fm.h_secondset.value + '-' +
	document.signup_fm.email.value.substring(0,document.signup_fm.email.value.indexOf('@'));
	document.signup_fm.customer_id.value = document.signup_fm.customer_id.value.toUpperCase();
	return document.signup_fm.customer_id.value;
}
function gen_form_id() {
	var array1 = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
	var array2 = new Array();
	var k = -1;
	var matrix = '';
	for(i=0; i=26; i++) {
		k++;
		for(j=0; j=26; j++) {
			if( k >= 26 ) k = k % 26;
			array2[i][j] = array1[k];
			matrix += array2[i][j] + '';
		}
		matrix += '<br>';
	}
	alert(matrix);
	//document.signup_fm.customer_id.value = document.signup_fm.customer_id.value.toUpperCase();
	return  '';//document.signup_fm.customer_id.value;
}
function encrypt_char( n ){ 
}
/********************************************* End of display methods *****************************************************/

/********************************************* validation script **********************************************************/
/* Check for proper zip code input (5 digits)
*/
function zipcode_field() {
	if( document.signup_fm.zip_code.value.length == 5 ) {// valid size
		if( !is_number(document.signup_fm.zip_code.value) ) {// not a valid number
			set_msg(0, document.signup_fm.zip_code.value + ' is an invalid zipcode');
			document.signup_fm.zip_code.value = '';
			return false;
		}
	}
	else if( document.signup_fm.zip_code.value != '' ) {// not empty but less than 5 
		set_msg(0, document.signup_fm.zip_code.value + ' is an invalid zipcode'); 
		document.signup_fm.zip_code.value = '';
		return false;
	}
	else {
		set_msg(0, 'Enter a zip code'); 
		return false;
	}
	return true;
}
/* Check for properly formatted phone number
*/
function ph_num_field(ind) {
	
	val = new Object();
	if( ind == 1 ) { val = document.signup_fm.h_areacode; }
	else if( ind == 2 ) { val = document.signup_fm.h_firstset; }
	else if( ind == 3 ) { val = document.signup_fm.h_secondset; }
	else if( ind == 4 ) { val = document.signup_fm.c_areacode; }
	else if( ind == 5 ) { val = document.signup_fm.c_firstset; }
	else { val = document.signup_fm.c_secondset; }
	// last set expected size is 3 or 4
	if( ( ( (ind == 3) || (ind == 6) ) && (val.value.length == 4) ) || ( ( (ind == 1) || (ind == 2) || (ind == 4) || (ind == 5) ) && (val.value.length == 3) ) ) {
		if( !is_number(val.value) ) {
			set_msg(0,'Invalid phone number');
			return false;
		}
	}
	// not empty but invalid size
	else if( val.value != '' ) {
		if( ind < 4 )
			set_msg(0,'Invalid home phone number');
		else
			set_msg(0,'Invalid cell phone number');
		//val.value = '';
		return false;
	}
	else {
		if( ind < 4 )
			set_msg(0,'Enter a complete home phone number');
		else
			set_msg(0,'Enter a complete cell phone number');
		return false;
	}
	//alert(val.value);
	return true;
}
/* Check if a number is valid
*/
function is_number(n) {
	m = 1;
	for(; m < (n.length+1); m++ ) {//check value
		if( !parseFloat(n.charAt(m-1) ) && parseFloat(n.charAt(m-1) ) != 0 ) //not including 0 every other digit should return false
			return false;
	}
	return true;
}
// print message on queue
// for simplicity i places break and only write to the first location of the array 
// ideally this function can return multiple error messages appended by the sequene of events by the form processing.
function print_msg(k) { 
	output = '';
	if( k == null )
		k = err_msg.length
	for( m=0; m < k; m++ ) {
		if( err_msg[m] != '' ) {// continue until you find an error message
			output += err_msg[m] + " ";// append error message
			break;
		}
	}
	document.getElementById('error_mess').innerHTML = output;// display messages
	// empty error message array for next attempt (NOT NECESSARY FOR A SINGLE MESSAGE BUT Saves some minor space)
	for( m=0; m < k; m++ ) 
		err_msg[m] = '';
		
}
// set a single message given index and message
function set_msg(ind, msg) {
	//err_msg[ind] = msg;
	alert(msg);
}
// following are self explanitory checks
function chk_event() {// event is selected
	//if( document.signup_fm.events.selectedIndex != 0 ) return true;
	//set_msg(0, 'Select the event you would like to attend');
	return true;
}
function chk_date() {
	if( $('#dates select').val() != '' ) return true;
	set_msg(0, 'Select the date of the event');
	return false;
}
function chk_name() {// first and last name fields are filled
	if( document.signup_fm.f_name.value == '' ) { set_msg(0, 'Enter your first name'); return false; }
	else if( document.signup_fm.l_name.value == '' ) { set_msg(0, 'Enter your last name'); return false; }
	return true;
}
function chk_nat() {// nationality is selected
	if( document.signup_fm.race.selectedIndex != 0 ) return true;
	set_msg(0, 'Select your Race');
	return false;
}
/* Check for proper birthday date input
 *
 */
function bday_field() {
	if( document.signup_fm.bday_date.value.length == 1 ) {// is a single digit
		if( parseFloat(document.signup_fm.bday_date.value) ) {// digit is a number
			document.signup_fm.bday_date.value = '0' + document.signup_fm.bday_date.value;// proper format
			return false;// no errors
		}
		else {// is not a digit
			set_msg(0, document.signup_fm.bday_date.value + ' is an invalid value');
			return true;// has format errors
		}
	}
	else if( (document.signup_fm.bday_date.value.length == 2) ) {// is a 2 digit number 
		if( document.signup_fm.bday_date.value == 'dd' ) {// textfield value is dd
			document.signup_fm.bday_date.value = '';// clear textfield for user input  
			return false;
		}
		else if( !is_number( document.signup_fm.bday_date.value ) ) { // invalid number
			set_msg(0, document.signup_fm.bday_date.value + ' is an invalid value');
			return true;
		}
	}
	else {
		set_msg(0, document.signup_fm.bday_date.value + 'Enter the date of your birthday');
		return true;		
	}
	return false;
}
function chk_bday() {// birthday is filled
	if( (document.signup_fm.bday_mnth.selectedIndex == 0 ) ) { set_msg(0, 'Enter the month of your birthday'); return false; }//month
	else if( bday_field() ) { return false; }// date
 	else if( document.signup_fm.bday_yr.selectedIndex == 0 ) { set_msg(0, 'Enter the year of your birthday');  return false };//year
	return true;
}
function email_field() {
	if( document.signup_fm.email.value != '' ) {
		if( document.signup_fm.email.value.indexOf('@') == -1 ) {
			set_msg(0, 'invalid email, missing @' );
			return false;
		}
		/*else if( document.signup_fm.email.value.indexOf('.') != (document.signup_fm.email.value.length-4) ) {
			set_msg(0, 'invalid email, missing .' );
			return false;
		}*/
	}
	else {
		set_msg(0, 'Enter your email');
		return false;
	}
	return true;
}
function chk_address() {
	if( document.signup_fm.st_add1.value == '' ) { set_msg(0, 'Enter your address, use both lines if necessary'); return false; }
	return true;
}
function chk_city() {
	if( document.signup_fm.city.value == '' ) { set_msg(0, 'Enter your home city'); return false; }
	return true;
}
function chk_state() {
	if( document.signup_fm.state.selectedIndex == 0 ) { set_msg(0, 'Select your home State'); return false; }
	return true;
}
function chk_zip_code() {
	if( (document.signup_fm.zip_code.value != '') || (document.signup_fm.zip_code.value.length == 5) ) return true;
	return false;
}
function chk_ph_num() {
	if( ph_num_field(1) && ph_num_field(2) && ph_num_field(3) && ph_num_field(4) && ph_num_field(5) && ph_num_field(6) )
		return true;
	return false;
}
function chk_friends() {
	if( document.signup_fm.num_of_friends.value == "" && document.signup_fm.othernames.value != "" ) { set_msg(0, 'Enter # of friends you have listed below'); return false; }
	else if( document.signup_fm.num_of_friends.value != "" && document.signup_fm.othernames.value == "" ) { set_msg(0, 'Enter of name and emails of the ' + document.signup_fm.num_of_friends.value + ' friends you would like to invite.'); return false; }
	else if( document.signup_fm.num_of_friends.value != "" && document.signup_fm.othernames.value.indexOf('@') == -1 ) { set_msg(0, 'Enter an email for the' + document.signup_fm.num_of_friends.value + ' friends you would like to invite.'); return false; }
	return true;
}
function chk_frm() {
	//var temp2 = chk_nat() + "-" + chk_nat() + "-" + chk_bday();
	if( chk_event() && chk_name() && chk_nat() && chk_bday() && email_field() && chk_address() && chk_city() && chk_state() && zipcode_field() && chk_ph_num() && chk_friends() ) {//&& chk_survey() removed manditory survey
		//get_friends();
		//gen_form_id();
		if( gen_customer_id() != "" )
			return true;
		else 
			alert('try again');
		//document.signup_fm.submit();
	}
	else
		return false;
		print_msg();
}
function chk_survey() {// event is selected
	if( document.signup_fm.radiobox.selectedIndex == 0 || document.signup_fm.salary.selectedIndex == 0 || 
	   document.signup_fm.venue.selectedIndex == 0 || document.signup_fm.music.selectedIndex == 0 || 
	   document.signup_fm.vodka.selectedIndex == 0 || document.signup_fm.beer.selectedIndex == 0 ||
	   document.signup_fm.drink.selectedIndex == 0 || document.signup_fm.card.selectedIndex == 0 ) {		
		set_msg(0, 'Plese complete the small survey');
		return false;
	}
	return true;
}
function disp_fields( num ) {
	if( parseFloat(num) ) {
		var output = '<table width=80% align="center" cellpadding="5" cellspacing="0" border="0" bgcolor="#CAA52A">' +				'<tr><td align="center" colspan="2"><strong>Guest list</strong><br>Please fill in their information</td></tr>';	
		for( i=0; i<parseFloat(num); i++) {
			output += '<tr><td align="right">*First Name:</td><td align="left"><input type="text" name="fname_"' + i + ' size="12"></td></tr>';
			output += '<tr><td align="right">*Last Name:</td><td align="left"><input type="text" name="lname_"' + i + ' size="12"></td></tr>'
			output += '<tr><td align="right">*Email:</td><td align="left"><input type="text" name="email_"' + i + ' size="40"><br><br></td></tr>'; 
		}
		output += '</table>';
		document.getElementById('friends').innerHTML = output;
	}
	else
		alert("Please enter how many friends you would like to invite");
}
// check name format
	function chk_names() {
	if( ( document.signup_fm.othernames.value.indexOf(' ') != -1 ) && 
		( document.signup_fm.othernames.value.indexOf(' ') != document.signup_fm.othernames.value.length-1 ) ) {
		return true;
	}
	return false;
}
/******************************************* end *****************************************************/
/********************************************** testing *******************************************************/
function fill()  {
	obj = document.signup_fm;
	obj.events.selectedIndex = 1;
	obj.c_dates.selectedIndex = 1;
	// name="months" - drop down menu
	// name="years"  - drop down menu
	obj.f_name.value = 'carlos';
	obj.l_name.value = 'colon';
	// name="gender" - radio
	obj.race.selectedIndex = 2;
	obj.bday_mnth.selectedIndex = 6;
	obj.bday_date.value = '23';
	obj.bday_yr.selectedIndex = 21;
	obj.email.value = 'djexplosivo@hotmail.com';
	obj.st_add1.value = '187 ten eyck walk apt 4a';
	obj.st_add2.value = '';
	obj.num_of_friends.value = 5;
	obj.city.value = 'brooklyn';
	obj.state.selectedIndex = 32;
	obj.zip_code.value = '11206';
	obj.c_areacode.value = '212';
	obj.c_firstset.value = '779';
	obj.c_secondset.value = '2222';
	obj.h_areacode.value = '212';
	obj.h_firstset.value = '779';
	obj.h_secondset.value = '2222';

}

function init() {
	disp_month(new Date().getMonth(), "mnths");//display current month for event date fields
	disp_month(1,"bday_mnth");//
	disp_dates(document.signup_fm.events.value);
	disp_year("bday_yrs");
	disp_year("years");
}
/******************************************* end *****************************************************/

-->