// JavaScript Document
/* validation of the classifieds form */
// Create new main array. Good for empty text fields
var txt_name = new Array() 
// Form field names is listed first followed by a descriptive name to be show in the js pop up if left blank
txt_name[0] = new Array("NAME","Name") 
txt_name[1] = new Array("ADDRESS","Address") 
txt_name[2] = new Array("PHONE","Phone Number")
txt_name[3] = new Array("AD_CONTENT","Ad Content")
txt_name[4] = new Array("AD_LIFE","How many weeks should the ad be listed?");


/* THESE FIELD ARE NOW RADIO BUTTONS 092104
var class_drop = new Array()
class_drop[0] = new Array("TYPE_CLASSIFIEDS", "Category of the Classified Ad")
*/


// check for blank text fields
function missing_content(){
	// check the regular text fields for empty content
	var j;
	var error_ct=0; // a counter variable for the errors;
	var missing_empty = "";


	// [01] CHECK THE TEXT FIELDS FROM THE ARRAY ABOVE
	for (j=0; j<txt_name.length; j++){
		if (document.classifieds[txt_name[j][0]].value == "") {
			missing_empty+= txt_name[j][1] + "\n";
		}
	}
	return missing_empty;
}

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



// function for the value of the dropdown field
function drop_valid(drop_name) { // this will only work when the first value is set to: ""
	var d;
	var missing_drop = "";
	
	for (d=0; d<drop_name.length; d++){
		if (document.classifieds[drop_name[d][0]].selectedIndex == ""){
			missing_drop+= drop_name[d][1] + "\n";
		}
	}
	return missing_drop;
}



function validation(){
	var missing = "";

	// ck for blank fields
	missing = missing_content();

	// ck and validate the email address
	email = checkEmail (document.classifieds["EMAIL"].value);
	
	if (email == false){
		missing+= "Invalid Email Address\n";
	}

	//ck the type of classified ad
	/* NOW RADION BUTTONS 092104
	class_typ = drop_valid(class_drop);
	missing += class_typ; */
	var class_type = "";
	var class_ck = 0;
	for (c=0; c < document.classifieds["TYPE_CLASSIFIEDS"].length; c++) {
		if (document.classifieds["TYPE_CLASSIFIEDS"][c].checked){
			class_ck++;
		}
	}
	
	if (class_ck == 0) missing+= "Category of the Classified Ad\n";

// CHECK TO SEE IF THE ITEM FOR SALE IS LESS THAN 100.00, IF SO THEN IT IS FREE
if (!document.classifieds["UNDER_100"].checked) {
	
	// ck the type of payment method
	// a. get the value of the type of payment

	var radio_choice = "";
	for (counter = 0; counter < document.classifieds["PAYMENT_TYPE"].length; counter++) {
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (document.classifieds["PAYMENT_TYPE"][counter].checked){
			radio_choice = document.classifieds["PAYMENT_TYPE"][counter].value;
		}
	}

	if (radio_choice == ""){
		missing+= "Payment Method not selected\n";
	}

	if (radio_choice == "m" || radio_choice == "v"){
		// a - get the values for the month and the day dropdown figures
		the_month = window.document.classifieds.CC_MONTH;
		for (m=0; m<the_month.length;m++){
			if (the_month.options[m].selected) {
				month_value = the_month.options[m].value;
			}
		};

		// b - get the values for the year
		the_year = window.document.classifieds.CC_YEAR;
		for (y=0; y<the_year.length;y++){
			if (the_year.options[y].selected) {
				year_value = the_year.options[y].value;
			}
		}

		// return missing:
		cc_valid = validateCard(document.classifieds['CC_NUMBER'].value,radio_choice,month_value,year_value);
		if (cc_valid != ""){ missing+=cc_valid; };
	};

// ENDING THE CHECK IF THE ITEM IS LESS THAN 100.00
}
	// FINALE: is anything missing?
	if (missing != ""){
		missing_hdr = "The Following Errors Have Occurred:\n";
		alert (missing_hdr + missing);
		return false;
	} else {	
		return true;
	}
}