//--------------------------------------------------------------------
/*
form2php(form)
AUTHOR			:	vivepascal
DATE				:	080203
VERSION			:	v1.1
PURPOSE			:	scan a form (var form) and send an array to a ajaxed php.
VAR					:	- form : a html object <form>
							- ajax : "ajax" or nothing => will we have to send the form by ajax or by simlpe submit
							- content_result : str (the div content result)
							- phpfile : str (the php file called by ajax
							- endfunc : str (the func to be launched at the end of ajax process
DEP. FILE		:	vp.ajax.v2.02.js
CALL				:	<input type="button" value="GO"  onClick="f_form(this.parentNode)">

NOTES				:	if ajax not set, we submit the form

*
* 03-avril-10, ECZ, ré-écriture et gestion des checkbox multiples : nom 'champ[]'
* 25-mai-10, ECZ, Encodage des '%' et des '+'. Réécriture des regexp.
* 03-juin-10, ECZ, Supprimé la conversion d'un apostrophe en guillemet
*/

function convertValue(
	str_value
) {
	/*
	 * 25-mai-10, ECZ, Encodage des '%' et des '+'. Réécriture des regexp.
	 */
	var ret_value = str_value.replace( /&/g,'|#and#|');
	ret_value = ret_value.replace( /%/g, "|#percent#|");
	ret_value = ret_value.replace( /\+/g, "|#plus#|");
	// we deal with the special Word chars.
	// 03-juin-10, ECZ, Supprimé la conversion d'un apostrophe en guillemet
	ret_value = ret_value.replace( /[“”]/g,"\"");
	return ret_value;
}

function form2php(form_id, ajax, content_result, phpfile, endfunc){
	
	// If it exists, we ask tinyMCE to send its value to the related textarea
	if (isset("tinyMCE")) tinyMCE.triggerSave();
	
	form = document.getElementById(form_id);
	
	vars2send = "";
	
	var l = form.elements;
	
	for (var i=0; i<l.length; i++){
		var tag = form.elements[i];
		//alert( "'"+tag.name+"', type = '"+tag.type+"', multiple = "+(tag.multiple ? "OUI" : "NON"));
		// we keep only the usable form tags
		if (
			tag.tagName!=undefined
			&&
			(
			tag.tagName == "INPUT" ||
			tag.tagName == "TEXTAREA" ||
			tag.tagName == "SELECT"
			) 
			&& 
			tag.type.toUpperCase() != "BUTTON"
		){
			
			// exeption for checkboxes : if checked => value = 1, else value = 0
			var tag_type_upper = tag.type.toUpperCase();
			switch (tag_type_upper) {
				case "CHECKBOX" :
					// Checkbox à nom de la forme 'champ[]'
					if (tag.name.substring( tag.name.length-2,tag.name.length) == "[]"){
						//alert( "****'"+tag.name+"' - '"+tag.value+"' checked = "+(tag.checked ? "OUI": "NON")+"****");
						if (tag.checked) {
							var name_array = tag.name.substring(0,tag.name.length-2);
							vars2send += "form["+name_array+"][]="+tag.value+"&";
						}
					}
					else {
						vars2send += "form["+tag.name+"]="+(tag.checked ? "1" : "0")+"&";
					}
				break;
				case "RADIO" :
					if (tag.checked) {
						vars2send += "form["+tag.name+"]="+convertValue( tag.value)+"&";
					}
				break;
				case "SELECT" :
					if (tag.multiple) {
						var tag_name_tmp;
						if (tag.name.substring(tag.name.length-2,tag.name.length) == "[]"){
							// security : to avoid a conflict with already formated select name
							tag_name_tmp = tag.name.substring(0,tag.name.length-2);
						}else{
							tag_name_tmp = tag.name;
						}
						var ls = tag.childNodes;
						for (n=0; n<ls.length; n++) {
							if (ls[n].selected){
								vars2send += "form["+tag_name_tmp+"][]="+ls[n].value+"&";
							}
						}
					}
				break;
				default :
					vars2send += "form["+tag.name+"]="+convertValue( tag.value)+"&";
				break
			}
//			alert( "'"+tag.name+"' - vars2send : '"+vars2send+"'");
		}
	}
	//debug(vars2send);
	// exit :
	if (arguments.length>1){
		if(ajax == "ajax"){
			// we send to vp.ajax.v2.02.js
			ajax_send_form(	content_result,
											phpfile,
											vars2send,
											endfunc
											);
		}
	}else{
		// DOES NOT WORK
		form.submit();
	}
	
}
//--------------------------------------------------------------------
/*

HISTORIQUE :
v 1.2
091221
Take a better care of the select multiple (return something that will be recognize as an array in php

v1.0 :
function form2php(form, ajax, content_result, phpfile, endfunc){
	vars2send = "";
	var l = form.elements;
	for (var i=0; i<l.length; i++){
		var tag = form.elements[i];
		// we keep only the usable form tags
		if (	tag.tagName!=undefined &&
					(tag.tagName == "SELECT" ||
					tag.tagName == "INPUT" ||
					tag.tagName == "TEXTAREA") && 
					tag.type.toUpperCase() != "BUTTON"){
			
			// exeption for checkboxes : if checked => value = 1, else value = 0
			if (tag.type.toUpperCase()=="CHECKBOX"){
				tag.checked ? tag.value = 1 : tag.value = 0;
			}
			// exeption for radio button : we keep only if checked;
			if ((tag.type.toUpperCase() == "RADIO" && tag.checked==true) ||
				 	(tag.type.toUpperCase()!="RADIO")){
				vars2send+= "form["+tag.name+"]="+tag.value+"&";
			}
		}
	}
	
	if(ajax="ajax"){
		// we send to ajax_post.js
		ajax_send_form(	content_result,
										phpfile,
										vars2send,
										endfunc
										);
	}else{
		form.submit();
	}
	
	
}
*/
