//	formcheck.js
//	upd 21.03.2006 mh

/*
	If no RequireList in form, form is submitted

	more options:

	Text to alert if required fields omitted:
	<input type="hidden" name="fcMsg" value="Bitte f(ue)llen Sie diese Felder aus:" />
	OR
	<input type="hidden" name="fcMsg" value="<%=gl("fcMsg")%>:" />

	Check two fields for identical content
	<input type="hidden" name="fcTwinEmail" value="email,email2" />
*/

var jslib;if(!jslib){jslib=[];}

clearColour="#fff";
var mailPopup=false;			//	launch form post into a popup window
var doubleEmail,windowNameDefault;
var formSent=false;


function formCheck(theform){
	if(!document.getElementById){return false}
	if((typeof theform).toLowerCase()!="object"){theform=document.getElementById(theform)}
	formMessage=""
	if(fieldsOK(theform)){
		doubleEmail=theform.elements["fcTwinEmail"]
		if(doubleEmail){
			aEmailCheck=doubleEmail.value.split(";")
			mailCompare(theform,aEmailCheck[0],aEmailCheck[1])
		}
		if(formMessage==""){
			sendMail(theform)
			return false
		}
	}
	if(formMessage!=""){deAlert((theform.elements["fcMsg"]?theform.elements["fcMsg"].value:"Bitte f(ue)llen Sie diese Felder aus:")+"\n\n"+formMessage)}
	return false
}

function fieldsOK(a){
	if(formSent){deAlert("Formular wurde schon gesendet.");return false}
	doChange=document.getElementById
	formMessage=""
	resetFields(a)
	aRequired=getRequired(a)
	if((!aRequired)||(aRequired.length==0)){return true}	//	no fields to check
	for(n=0;n<aRequired.length;n++){
		thefield=a.elements[aRequired[n]]
		if(thefield){
			noValue=formFieldValue(thefield);
			if(noValue){
				if(doChange&&thefield.type){if(!jslib.browser.iemac){thefield.className+=" fehler"}}
				if(thefield.length&&!thefield.type){thefield=thefield[0]}	//	use the first radio button in a set
				formMessage+="- "+(thefield.title?thefield.title:thefield.name)+"\n"
			}
		}
	}
	return formMessage==""?true:false
}

function mailCompare(f,a,b){
	// compare two email addresses
	if(f.elements[a]&&f.elements[b]){
		if(f.elements[a].value==f.elements[b].value){return}
		else{
			formMessage+=formMessage!=""?"\n":""
			formMessage+="Bitte vergewissern Sie sich, dass die beiden E-Mail-Adressen (ue)bereinstimmen"
		}
	}
	return
}

function getRequired(theform){
	// create array of required form fields from input element RequireList
	srcfld=theform.elements["RequireList"]
	if((!srcfld)||(srcfld.value=="")){return false}	//	no RequireList
	return srcfld.value.split(",")
}

function resetFields(theform){
	// reset styles of all form fields
	if((typeof theform).toLowerCase()!="object"){theform=document.getElementById(theform)}
	if(document.getElementById){
		for(n=0;n<theform.elements.length;n++){
			if((theform.elements[n].type!="radio")&&(theform.elements[n].type!="submit")&&(theform.elements[n].type!="reset")&&(theform.elements[n].type!="hidden")){
				if(!jslib.browser.iemac){
					if(theform.elements[n].type!="undefined"){	//	not fieldset	//	mh 13.3.2006
						theform.elements[n].className=replaceAll(theform.elements[n].className,"fehler","");
						theform.elements[n].onchange=function(){if(this.value!=""){this.style.backgroundColor=clearColour;}}
					}
				}else{
					theform.elements[n].style.backgroundColor=clearColour;
				}
			}
		}
	}
	return false
}

function sendMail(theform){
	// set up return address and submit form (into popup window if mailPopup=true)
	if(theform.elements["emailReplyTo"]&&theform.elements["email"]){
		theform.elements["emailReplyTo"].value=theform.elements["email"].value;
	}	
	if(mailPopup){
		confWin=p('','mailConfirm',360,325,cWindows["options"][7]);
		theform.target="mailConfirm";
	}
	disable(theform);
	protectandserve(theform);
	theform.submit();
	formSent=true
}

function checkAbo(a){
	// checks abo formular and submits form if all OK
	goOn=true
	if((a.elements["Zustellungsdatum"][1].checked)&&(a.elements["Datum"].value=="")){
		deAlert("Bitte f(ue)llen Sie das Feld 'Datum' aus.")
		goOn=false
	}
	if(goOn){formCheck(a)}
	return false
}

function searchCheck(theform,submitPopup){
	// checks search form and launches results in popup window
	if(fieldsOK(theform)){
		if(submitPopup){
			if(!self.window.name){self.window.name=windowNameDefault}
			formWin=p('','formPopup',680,480,cWindows["options"][5]);
			theform.target="formPopup";
		}
		disable(theform)
		theform.submit();
		return false;
	}else{
		pre=theform.elements["fcMsg"]?theform.elements["fcMsg"].value:"Bitte f(ue)llen Sie diese Felder aus:";
		deAlert(pre+"\n"+formMessage);
		return false;
	}
	return false
}

function setVerlag(a){
	// dynamically sets value of main email contact
	if(a.alt){
		targetField=document.getElementsByTagName("form")[0].elements["emailTo"]
		targetField.value=a.alt
	}
}

function checkFileName(uploadfield){
	//	make sure upload form field has a valid suffix and has a filename
	if(uploadfield.value===""){return true}
	filepath=uploadfield.value.split("/")
	filename=filepath[filepath.length-1]
	if(!filename){return false}
	if(filename.indexOf(".")==filename.length-4){return true}
	else{deAlert("Bitte (ue)berpr(ue)fen Sie der Dateinname und\ngarantieren Sie, dass es einen g(ue)ltigen Suffix gibt.")}
}

function disable(o){
//	if(o&&o.elements){i=0;while(o.elements[i]){o.elements[i].disabled="disabled";i++;}}
}

protectandserve=function(o,action){
	if(action){o.action=action;}
	oC=document.createElement("INPUT");oC.style.display="none";o.appendChild(oC);
	oC.name="protectandserve";oC.value="protectandserve";
}
