var fieldsArray = new Array();
    
    
    	// -=============================================================-
	// -: This function show or Hide a elelment of the page         :-
	// -: We use the id property of the element to identify it!     :-
	// -=============================================================-
	
    	function showDiv(id, bool)
	{
	  var obj;
	  	
	  	obj = document.getElementById(id);
	  	if (bool==true)
		{
			obj.style.display="block";
		}
		else
		{
			obj.style.display="none";	
		}
	}
	
    	// -=============================================================================-
	// -: Redirection 								:-
	// -: This function is called only in the case we have a Linker object 		:-
	// -: alone in the 'form' (in fact there is no form if we have a linker		:-
	// -: but it looks like a form in the html page). The function then make a 	:-
	// -: get on both parameter and a link to the url page asked.			:-
	// -=============================================================================-
	
	function redirection(oElem)
	{
		if (oElem.value != "empty") window.location = oElem.value;	
	}
    
	// -=====================================================================-
	// -: addElemn2Array							:-
	// -: this function add all datas of a new field to an array if this	:-
	// -: fields are not yet saved into the array. If the 'id' of the 	:-
	// -: element already exist, the function does nothing!			:-
	// -=====================================================================-
	
	function addElem2Array(id, type, regEx, isRequired)
	{
	  var workStr = '';
	  var splittedStr = new Array();
	  var found = false;
	  
		// Looking for the Id. If it already exist, do nothing
		for (i = 0; i<fieldsArray.length;i++)
		{
			workStr = fieldsArray[i];
			splittedStr = workStr.split(";");
			if (splittedStr[0] == id){
				found = true;
				break;						//We stop the loop
			} 				
		}
		
		if (! found){						// We did not found anything -> then we need to add the element!
			workStr = id + ";" + type + ";" + regEx + ";" + isRequired;
			fieldsArray.push(workStr);
		}
	}
	
	// -========================================================-
	// -: checkRegEx function				   :-
	// -: This function use the Regular Explression module 	   :-
	// -: from javascript to check if syntaxes use to fill in  :-
	// -: the fied match or not.				   :-
	// -========================================================-
	
	function checkRegEx(regEx, value)
	{
	  var re = new RegExp(regEx)
	  	
	  	if (value.match(re))
		{
	  		return true;
		}
		else
		{
			return false;
		}
	}
	
	// -=====================================================================-
	// -: checkAll function							:-
	// -: Function that will be called when the button sent is clicked! 	:-
	// -: It uses the datas stored in the array fieldsArray to check if all :-
	// -: fields are well filled. If it is the case -> submit. If not some	:-
	// -: error messages are printed to show where are problems. The form	:-
	// -: will not be published until all fields are ok			:-
	// -=====================================================================-
	
	function checkAll()
	{
	  var workStr="";
	  var splittedArray= new Array();
	  var id="";
	  var regEx="";
	  var obj;
	  var errObj;
	  var errorFound=false;
	  var required=false;
	  var bFirstError = true;
	  var tmpStr;

	  	// We have a specific treatment for the "failurl" field. In fact we need to update the value
		document.getElementById("failurl").value = window.location;
		
		if(document.forms['Agfa'].elements['pageurl'] != null) {
			document.forms['Agfa'].elements['pageurl'].value = document.location.href.split("?")[0];
		}
		
		if(document.forms['Agfa'].elements['mailTypeTemp'] != null) {
			if(document.forms['Agfa'].elements['mailTypeTemp'].checked == true)
			{
				document.forms['Agfa'].elements['mailType'].value = "text";
			}
			else
			{
				document.forms['Agfa'].elements['mailType'].value = "html";
			}
		}
		
		/*if(document.forms['Agfa'].elements['url'] != null) {
			document.forms['Agfa'].elements['url'].value = document.forms['Agfa'].elements['url'].value + "&email=" + document.forms['Agfa'].elements['email'].value;
		}*/

		for (i=0;i<fieldsArray.length;i++){
			workStr = fieldsArray[i];
			splittedArray=workStr.split(";");
			id=splittedArray[0];
			regEx = splittedArray[2];
			obj = document.forms['Agfa'].elements[id];
			fieldType = splittedArray[1];
			
			if (document.forms['Agfa'].elements[id] != null) {
				if(fieldType == "DropDown") {
					if (document.forms['Agfa'].elements[id].selectedIndex == 0) {
						fieldValue="";
					} else {
						fieldValue = "Something";
					}
				} else {
					fieldValue = document.forms['Agfa'].elements[id].value;
					if (fieldValue == undefined) {
						fieldValue="";
					}
				}
			} else {
				fieldValue = "";
			}

			if (splittedArray[3]=="yes" || splittedArray[3]=="True")
				required=true
			else
				required=false;

			if (required && fieldValue=="")
			{
				errObj=document.getElementById(id+"Req");
				errObj.style.display="block";
				if(bFirstError) {
					obj.focus();
					bFirstError = false;
				}
				errorFound=true;
			}
			else
			{
				if (required)
				{
					errObj=document.getElementById(id+"Req");
					errObj.style.display="none";
				}
				
				if (id == "conf_pass")
				{
					if(document.forms['Agfa'].elements['password'] != null)
					{
						if(document.forms['Agfa'].elements['password'].value == document.forms['Agfa'].elements['conf_pass'].value)
						{
							errObj=document.getElementById(id+"Err");
							errObj.style.display="none";
						}
						else
						{
							errObj=document.getElementById(id+"Err");
							errObj.style.display="block";
							if(bFirstError) {
								obj.focus();
								bFirstError = false;
							}
							errorFound=true;
						}
					}
				}
				
				
				if ((regEx!="")&&(fieldValue!=""))
				{
					errObj=document.getElementById(id+"Err");
					if (!checkRegEx(regEx,fieldValue))
					{
						errObj.style.display="block";
						if(bFirstError) {
							obj.focus();
							bFirstError = false;
						}
						errorFound=true;
					}
					else
					{
						errObj.style.display="none";
					}
				}
			}
		}
		
		errObj= document.getElementById("mainErrorMsg");
		if (errorFound)
		{
			errObj.style.display="block";
		}
		else
		{
			errObj.style.display="none";
			document.forms.Agfa.submit()
		}
	}
	
	
	function cancelOrder()
	{
		document.forms['Agfa'].action = "/rpt/order.handler";
		document.forms['Agfa'].elements['url'].value = document.location.href.split("?")[0];
		document.forms['Agfa'].elements['method'].value = "del";
		document.forms.Agfa.submit();
	}
	
	function submitOrder()
	{
		document.forms['Agfa'].elements['method'].value = "set";
		document.forms.Agfa.submit();
	}
	
	function submit()
	{
		document.forms.Agfa.submit();
	}
