/* document.domain="avalon-internet.com" */

function changeCheckbox(form_name,field_name){
obj=eval("document."+form_name+"."+field_name);
if(obj.checked==true){obj.checked=false}
	else{obj.checked=true}
}//END changeCheckbox()


function changeRadio(form_name,field_name,which){
//THIS DOES NOT WORK. KEEPS RESETTING TO TOP RADIO SELECTION. TESTED IN FIREFOX. 
//ACCORDING TO QUIRKSMODE, GENERATED RADIOS CANNOT BE USED IN EXPLORER, WHICH CONSIDERS ALL ON THE PAGE TO BE THE SAME.
obj=eval("document."+form_name+"."+field_name+"["+which+"]");
obj.checked=true;
alert(obj.checked)//OK HERE, BUT THEN GOES BACK
obj.focus();
}

function capitalize(phrase){
x=phrase.substring(0,1);
return x.toUpperCase()+phrase.substring(1);
}

/* TO USE THIS OPTION OF MULTIPLE SUBMIT BUTTON VALIDATION, MUST BE ONE SELECT BOX, MARKED
NOT REQUIRED, VERSUS MULTIPLE ENTRY BOXES */
function setSubmitSelect(selectbox_name){//1
document.forms[0].elements['submit_select'].value = selectbox_name;	
}//1

function unsetSubmitSelect(selectbox_name){//1

document.forms[0].elements[selectbox_name].selectedIndex=-1
document.forms[0].elements['submit_select'].value = "";
}//1


function cbDiv(div)
{//1
if (document.all){return div}
else{return "document.getElementById('"+div+"')"}
}//1

function formValidate(){//1 

missing= new Array();wrong=new Array();
required = new Array();required_element_type =  new Array();
checkname = new Array();checkwith = new Array();label_wording = new Array();
highlight = new Array();
ok = new Array()
name_to_label=new Array();//THIS ASSOCIATIVE ARRAY COUPLES NAMES WITH ERROR LABELS; USED FOR "wrong" ARRAY.

/* SPECIAL SITUATIONS */
if(window.document.forms[0].elements['submit_select']){x=checkForSpecialSelectbox();if(x == true){return true;}}
if(window.document.forms[0].elements['something_checked']){document.forms[0].elements['something_checked'].value = "";//RESET
	checkCheckboxes();}
if(window.document.forms[0].elements['something_selected']){document.forms[0].elements['something_selected'].value = "";//RESET
	checkSelectboxes();}	

validateStringsToArrays();

pass=true;x="";y=0;z=false
// CHECK FOR MISSING REQUIRED PARAMETERS 

for(i=0;i<required.length;i++){//2 
	currentname = required[i];//alert("fs js 69 currentname="+currentname);
	switch(required_element_type[i]){//3 GENERALLY NO HIDDEN FIELDS ARE REQUIRED, EXCEPT TO TRIP NO ENTRIES
		case 'hidden':
		case 'text':
		case 'textarea': x=eval("document.forms[0].elements['"+currentname+"'].value");
			if(!x>""){pass = false;}
			break;
		case 'select': y=eval("document.forms[0].elements['"+currentname+"'].selectedIndex");
			if(y<0){pass=false}break;
		case 'radio': n = eval("document.forms[0].elements['"+currentname+"'].length");
			for (i=0;i<n;i++) {
				if (eval("document.forms[0].elements['"+currentname+"'].checked")) {x="checked";break}
			}
			if(!x>""){pass = false;}
			break;
		case 'checkbox': if(!eval("document.forms[0].elements['"+currentname+"'].checked")){//ASSUMES BOX MUST BE CHECKED
			pass=false;}break;

	}//3 END SWITCH
	name_to_label[currentname] = label_wording[i];
	if(pass==false){missing[missing.length]=label_wording[i];highlight[highlight.length]=required[i];
		pass=true}

}//2	

// CHECK FOR WRONG ENTRY DATA
 
if(checkname.length>0){
for(i=0;i<checkname.length;i++){//2
	if(checkwith[i].indexOf("()")>0){//3 CODE IS FUNCTION TO RUN
		if(checkwith[i].indexOf("Db")>0){continue;}
		eval(checkwith[i])
		//END CHECK FUNCTION
		}else{//CODE IS REGULAR EXPRESSION TO MATCH 
	exp = new RegExp(checkwith[i]);
	whichname=checkname[i];
	y=eval("document.forms[0].elements['"+whichname+"'].value");
	if(y>"" && !y.match(exp)){wrong[wrong.length]=name_to_label[whichname];highlight[highlight.length]=whichname;
		}else{ok[ok.length]=whichname}
	}//3 END REGEX CHECK	
}//2
}

/* CHECK FOR DELETE CONFIRMATIONS BEFORE RETURNING */
if(missing.length==0 && wrong.length==0){
	//x = confirmDelete(); CONFIRMING ON SERVER
	//if(x==true){return true}else{return false}
	return true;
	}else{//2

// MAKE STATEMENTS 
missingstring = makeErrorString(missing,include);

 wrongstring = makeErrorString(wrong,correct);

if(missing.length >0 && wrong.length >0){conjunction=", "+and+" ";
	}else{
	conjunction="";}//USED IN COMBINING BOTH STRINGS

errorstring = capitalize(please)+" "+missingstring+conjunction+wrongstring+".";

// alert("formscript line 80 highlight length is "+highlight.length+" required = "+required);return false;
highlightErrors(highlight,required);

if(errorstring>""){alert(errorstring);
	return false;
	}else{//3
	return true
		}//3
	}//2
	
}//1END formValidate()

function validateStringsToArrays(){
		//JUST USING NUMBERS PRODUCES UNPREDICTABLE READS OF FORM VALUES, POSSIBLY BECAUSE
	// LABELS ARE COUNTED AS ELEMENTS(?). MUST USE ELEMENT NAMES.		
	if(requiredstr.indexOf("QQ")>0){required = requiredstr.split("QQ")}else{if(requiredstr>""){required[0]=requiredstr}}
	if(required_element_typestr.indexOf("QQ")>0){required_element_type = required_element_typestr.split("QQ")
		}else{required_element_type[0]=required_element_typestr}
	if(checknamestr.indexOf("QQ")>0){checkname = checknamestr.split("QQ")}else{if(checknamestr>""){checkname[0]=checknamestr}}
	if(checkwithstr.indexOf("QQ")>0){checkwith = checkwithstr.split("QQ")}else{if(checkwithstr>""){checkwith[0]=checkwithstr}}
	if(label_wordingstr.indexOf("QQ")>0){label_wording = label_wordingstr.split("QQ")
		}else{if(label_wordingstr>""){label_wording[0]=label_wordingstr}}
}//1 END validateStringsToArrays()

function makeErrorString(arraycontent,verb){
	usestring = "";len=arraycontent.length;
	 for(i=0;i<len;i++){//3						
		if(len>2 && i>0 && i<(len-1)){usestring += ", ";}
		usestring += arraycontent[i];
		if(len>1 && i==(len-2)){usestring += " "+and+" ";}
		if(i==(len-1)){
			if(verbplacement=="last"){usestring += verb;}else{usestring = verb+" "+usestring;}
			}
	}//3
return usestring;

}//END makeErrorString()

function highlightErrors(highlight,required){//1

obj="";
if (document.all){return} //MSIE { obj = "document.all.";}	DOESN'T WORK IN IE; FAILURE BREAKS WHOLE JS PAGE. FIX SOMETIME.

//RESET TO BLACK FOR SUBSEQUENT SUBMISSIONS
	for(i=0;i<required.length;i++){
	if(!eval("window.document.forms[0].elements['label_"+highlight[i]+"']")){return}//MAY BE HIDDEN FIELD	
	item="label_"+required[i];	 
	eval(obj+cbDiv(item)+".style.color='#000'");		
	}
	
	for(i=0;i<highlight.length;i++){
	if(!eval("window.document.forms[0].elements['label_"+highlight[i]+"']")){return}//MAY BE HIDDEN FIELD	
	item="label_"+highlight[i];	 
	eval(obj+cbDiv(item)+".style.color='#c60'");
	}
}//END highlightErrors()

/* DOES NOT WORK. CANNOT BOTH CONFIRM THIS IN JS AND ON SERVER, ANYWAY, SO AM LEAVING IT AS IS.
function confirmDelete(){//1
	
var deleting=Array;
for(i=0;i<document.forms[0].length;i++){//2
	if(document.forms[0].elements[i].name.substring(0,5) == "delete" && document.forms[0].elements[i].value == checked){//3
deleting[deleting.length]=document.forms[0].elements[i].name.substring(7);
statement = capitalize(please)+" "+confirm_the_deletion_of+":\n\r\n\r"
	for(i=0;i<deleting.length;i++){//4
		statement += "\n\r"+deleting[i]
		}//4
if(confirm(statement)){return true}else{return false}
	}//3	
}//2
}//1 END confirmDelete()
*/
function checkCheckboxes(){//1
for(i=0;i<document.forms[0].length;i++){//2
	if(document.forms[0].elements[i].type == "checkbox" && document.forms[0].elements[i].checked == true){
		document.forms[0].elements['something_checked'].value = "OK"; return}

}//2	
}//1 END checkCheckboxes()

function checkForSpecialSelectbox(){

/* FOR FORMS WITH A SELECTION BETWEEN A SELECT BOX, PRESENTED FIRST, AND NEW DATA ENTRY FOR THE BOX.
SELECT BOX MUST BE DESIGNATED required=0 IN THE DATABASE, OR NEW ENTRIES WILL NOT PASS VALIDATION. */	

	if(document.forms[0].elements['submit_select'].value>""){
	x=document.forms[0].elements['submit_select'].value
	y=eval("document.forms[0].elements['"+x+"'].value")
	if(y>0){return true}else{//CLICKING TOP BUTTON WIPES OUT VALIDATION STRINGS FOR BOTTOM BUTTON. SAVE THEM ON FORM.
		/* SAVE STRINGS FOR MULTIPLE JS CALLS */
		document.forms[0].elements['requiredstr'].value = requiredstr
		document.forms[0].elements['required_element_typestr'].value = required_element_typestr
		document.forms[0].elements['checknamestr'].value = checknamestr
		document.forms[0].elements['checkwithstr'].value = checkwithstr
		document.forms[0].elements['label_wordingstr'].value = label_wordingstr

		requiredstr = x
		required_element_typestr = "select"
		checknamestr = ""
		checkwithstr = ""
		label_wordingstr = "selection"
		return false
	}
	}else{
	if(document.forms[0].elements['requiredstr'].value>""){
		requiredstr =	document.forms[0].elements['requiredstr'].value
		required_element_typestr = document.forms[0].elements['required_element_typestr'].value
		checknamestr = document.forms[0].elements['checknamestr'].value 
		checkwithstr = document.forms[0].elements['checkwithstr'].value
		label_wordingstr = document.forms[0].elements['label_wordingstr'].value	
	}	
		}
 
}//1 END checkForSpecialSelectbox()

function checkSelectboxes(){//1
for(i=0;i<document.forms[0].length;i++){//2
	if(document.forms[0].elements[i].type == "select-one"){
		if(document.forms[0].elements[i].value > "" && document.forms[0].elements[i].value != "NULL" )
	{document.forms[0].elements['something_selected'].value = "OK"; return}}

}//2	
}//1 END checkSelectboxes()

checked_once=0;

function checkDate(){//1
/* FOR DATES THAT SHOULD BE IN THE FUTURE. EXPECTS CERTAIN FIELD-NAMING CONVENTIONS.
EXPECTS DATES TO BE MARKED REQUIRED, HAVING "calculated" [MUST BE EXACTLY THIS FOR PHP VALIDATION
CODE; DOESN'T MATTER WHAT IT IS FOR JAVASCRIPT.] AS VALUES IN THE
DATABASE, AND TO HAVE checkreject=1 AND checkwith=3 (checkDate()). ERROR STATEMENTS IN
formtext AS USUAL. RECALL THAT JAVASCRIPT NUMBERS MONTHS FROM ZERO; PHP FROM ONE.

NOTE SELECT BOXES SHOULD HAVE TOP SELECTION MARKED AS SELECTED, OR IT WILL NOT BE
REGISTERED IF USER HAS NOT RESELECTED IT.

NOTE THERE IS NO EFFORT TO VALIDATE FOR IMPOSSIBLE DATES (EG FEBRUARY 31)
BECAUSE PHP DATE FUNCTIONS CAN WORK WITH THEM. */

if(window.document.forms[0].elements['date_start']){//2
whichname = checkname[i];
	
	current = new Date();
	current_year = current.getFullYear();
	current_month = current.getMonth();
	current_date = current.getDate();
	today_start = Date.UTC(current_year,current_month,current_date,0,0,0);
	

	start_year = document.forms[0].elements['start_year'].value;
	start_month = (document.forms[0].elements['start_month'].value-1);
	start_date = document.forms[0].elements['start_day'].value;
	real_start_date = Date.UTC(start_year,start_month,start_date,0,0,0);

	end_year = document.forms[0].elements['end_year'].value;
	end_month = (document.forms[0].elements['end_month'].value-1);
	end_date =	document.forms[0].elements['end_day'].value;
	real_end_date = Date.UTC(end_year,end_month,end_date,0,0,0);
	
	if(whichname=="date_start"){entered_date = real_start_date}else{entered_date = real_end_date}	
	
a = entered_date
b = today_start
x = entered_date - today_start
if(checked_once == 0){
if(real_start_date - real_end_date >0){wrong[wrong.length] = ": "+date_order_error}checked_once++;}

if(x >= 0){
	return
	}else{wrong[wrong.length]=name_to_label[whichname];
return}
	
}//2 END CHECK FOR FIELD NAME

}//1 END checkDate()

