// calls isDate, alerts() in the right language and returns true or false.
// input is a mm/dd/yyy string
function chkDate(dateStr,fieldname) {
	isdateres = isDate(dateStr);
	switch(isdateres) {
		case -1:
			alert(document.contacterrmsg[document.lg]["dateformat"] + (fieldname==""?"":" ("+fieldname+")"));
			return false;
		case -2:
			alert(document.contacterrmsg[document.lg]["errmonth"] + (fieldname==""?"":" ("+fieldname+")"));
			return false;
		case -3:
			alert(document.contacterrmsg[document.lg]["errday"] + (fieldname==""?"":" ("+fieldname+")"));
			return false;
		case -4:
			msg = formatStr(document.contacterrmsg[document.lg]["yearbtwn"], document.today.year,(document.today.year+2) + (fieldname==""?"":" ("+fieldname+")"));
			alert(msg);
			return false;
		case -5:
			alert(document.contacterrmsg[document.lg]["errdate"] + (fieldname==""?"":" ("+fieldname+")"));
			return false;
	}
	if(!isAfterToday(dateStr)) {
		alert(document.contacterrmsg[document.lg]["dateinpast"] + (fieldname==""?"":" ("+fieldname+")"));
		return false;
	}
	return true;
}

function submitform(userformname) {
	// creates hidden form
	var sent_form = window.document.createElement("form");
	sent_form.name = 'hidden_form';
	sent_form.method="post";
	sent_form.enctype="multipart/form-data";
	sent_form.action = document.forms[userformname].action;

	// copy all fields in hidden form
	for(key in document.formfields) {
		user_field = eval("document." + userformname + "." + key);
		if(typeof(user_field)==="undefined") continue;
		var newElem = window.document.createElement("input");
		newElem.type = "hidden";
		newElem.name = key;
		varform = getSingleInputValue(user_field);
		if((user_field.type=="text" || user_field.type=="textarea")
			&& varform==document.formfields[key].defaultvalue) {
			varform="";
		}
		newElem.value = varform;
		sent_form.appendChild(newElem);
	}
	// var with the form name
	var newElem = window.document.createElement("input");
	newElem.type = "hidden";
	newElem.name = 'formname';
	newElem.value = userformname;
	sent_form.appendChild(newElem);
	document.body.appendChild(sent_form);

	sent_form.submit();
}

function sendrequest(formname,lg) {
	cleanedemail = ctrlemail(document.forms[formname].email.value);
	if(cleanedemail===false) {
		alert(document.contacterrmsg[document.lg]["erremail"]);
		return ;
	}
	// controls mandatory fields
	ctrlmsg1 = ctrlmsg(formname);
	if(ctrlmsg1!="") {
		msg = formatStr(document.contacterrmsg[document.lg]["errmsg"], document.formfields[ctrlmsg1].label);
		alert(msg);
		return ;
	}

	// if first date is filled
	if(document.forms[formname].arrival_month && (document.forms[formname].arrival_month.value!="" || document.forms[formname].arrival_day.value!="" || document.forms[formname].arrival_year.value!="")) {
		// checks first date validity
		if(!chkDate(parseInt(document.forms[formname].arrival_month.value) + "/" + document.forms[formname].arrival_day.value + "/" + document.forms[formname].arrival_year.value, document.formfields['arrival_day'].label)) return;

		// check second date validity
		if(!chkDate(parseInt(document.forms[formname].departure_month.value) + "/" + document.forms[formname].departure_day.value + "/" + document.forms[formname].departure_year.value, document.formfields['departure_day'].label)) return;

		// checks second date after first one.
		date1 = new Date(document.forms[formname].arrival_year.value,(parseInt(document.forms[formname].arrival_month.value)),document.forms[formname].arrival_day.value);
		date2 = new Date(document.forms[formname].departure_year.value,(parseInt(document.forms[formname].departure_month.value)),document.forms[formname].departure_day.value);
		if(date2<=date1) {
			alert(document.contacterrmsg[document.lg]["depbeforearr"]);
			return;
		}
	} // end if first date is filled

	tmp = submitform(formname);
}

// controls this is a valid email (if there is one) and returns the cleaned email
function ctrlemail(emailtxt) {
	// removes %20, %0A, \r and \n
	forbid = new Array("%0A","%0a","%20","\r","\n","\R","\N");
	for(i=0;i<forbid.length;i++) {
		pos = emailtxt.indexOf(forbid[i]);
		emailtxt = (pos>=0?emailtxt.substring(0,pos):emailtxt);
	}

	if(emailtxt=="") {
		return false;
	}

	if(emailtxt.search(' ')>=0) {
		return false;
	}
	posarobace = emailtxt.search('@');
	poslastdot = emailtxt.lastIndexOf('.');
	if((poslastdot<0)||(poslastdot<posarobace)||(posarobace<0)) {
		return false;
	}

	return(emailtxt);
}
// controls mandatory fields... Returns field name of first mandatory field with an empty value. Else return "".
function ctrlmsg(formname) {
	for(key in document.formfields) {
		if((!(typeof(document.formfields[key].mandatory)==="undefined")) && (document.formfields[key].mandatory == "1") && document.forms[formname][key].value=="") return key;
	}
	return "";
}

// reads the content of a form element of type radio, checkbox, text, hidden, textarea, select
function getSingleInputValue (obj) {
	switch(obj.type) {
	case 'radio':
	case 'checkbox':
		return (obj.checked?obj.value:"");
	case 'text':
	case 'hidden':
	case 'textarea':
		return obj.value;
	case 'password': return obj.value;
	case 'select-one':
		if(obj.options==null) {return "";}
		if(obj.selectedIndex<0) {return "";}
		return obj.options[obj.selectedIndex].value;
	case 'select-multiple':
		if(obj.options==null) {return null;}
		var values=new Array();
		for(var i=0;i<obj.options.length;i++) {
			if(obj.options[i].selected) {
				values[values.length]=obj.options[i].value;
			}
		}
		return(values.length==0)?"":values;
	}
	return "";
}

// writes the content of a form element of type radio, checkbox, text, hidden, textarea, select
function setSingleInputValue(obj, newvalue) {
	switch(obj.type) {
	case 'radio':
	case 'checkbox':
		// TODO
		return;
	case 'text':
	case 'hidden':
	case 'textarea':
		obj.value = newvalue;
	case 'select-one':
		if(obj.options==null) {return;}
		for(var i=0;i<obj.options.length;i++) {
//alert(i + " " + obj.name + " " + obj.options[i].value + " =? " + newvalue);
			if(obj.options[i].value==newvalue) {
				obj.options[i].selected = true;
				return;
			}
		}
		return;
	case 'select-multiple':
		// TODO
		return;
	}
	return null;
}

// restores default value if field is empty, and erases it if contains the default value.
function erasedefaultcontent(obj) {
//alert(obj.name + " " + obj.value + " " + document.formfields[obj.name].defaultvalue);
	// content of calling field
	if(obj.value==document.formfields[obj.name].defaultvalue) {
		setSingleInputValue (obj, "");
		obj.className = "formtext";
	}
	// the other fields (restore to default value if empty)
	for(key in document.formfields) {
		if(key==obj.name) continue;
		tmp = eval("document.user_form." + key);
		if(tmp.value=="") {
			tmp.value = document.formfields[key].defaultvalue;
			tmp.className = "formtextfaded";
		}
	}
}

