//
// submit Form function (POST with ajax)
//
  var formID = null;
  var formBtnText = null;
  var formOKfunction = null;
  
  
 function submitForm(ID, submitOK)
  {	
  	formID = ID;
	formOKfunction = submitOK;
	


 	$(formID).request({
		onLoading: function() {
 					if($(formID + 'Loading'))
 						$(formID + 'Loading').addClassName('isLoading');
					$(ID).disable();
				},
 		onSuccess: function(transport){
		    	
			var validationResult = transport.responseJSON;
			
			//comprobar si se fixo un test nas ultimas 24 horas
			if(validationResult.Check)
			{
				Dialog.info($('maxtest').innerHTML , {className:"alphacube", width:560, zIndex:150} );
				return null;
			}		
				
			
			// check if json is all NULL
			if(!validationResult)
			{
				alert("Se ha producido un error. Por favor, inténtelo más tarde.");
				return false;
			}
			// clear all alerts on form
			clearFormAlerts();
			
			// show form validation
			if(validationResult.errorlist)
				showFormValidation(validationResult);
			else
				formOKfunction();
			// enable form
			$(ID).enable();
			
			
 		}
 	});	

    return false;
  }
  
//
//	Aplicates validation on form
//
function showFormValidation(validationResult)
{
	if(validationResult.errorlist != 1) {
		validationResult.errorlist.each(function(s){
			if($(s['id']))
				$(s['id']).addClassName('alertERROR');

			if(s['id']=='ctc_calle' && s['opt']) citySelector(s['opt']); // Only for Ono: Generate a city Selector from JSON result
		});
	}


	
	
	// close waiting dialog
	if($(formID + 'Loading'))
		$(formID + 'Loading').removeClassName('isLoading');

	
}
  




//
//	Default way when form is OK
//
function defaultSubmitOK( OKformID, returnMSG )
{
	var rtMSG = ""
	for(c=6;returnMSG[c];c++)
		rtMSG = rtMSG + returnMSG[c];
		
	rtMSG = "<div id='rtMSG'>" + rtMSG + "</div>";
	
	$( OKformID+'_Container').innerHTML = rtMSG;
}


//
//	implode checklist
//
function implodeChecklist(checklistDIV, inputRes)
{
	$(inputRes).value = '';
	first=true;
	$$('#'+checklistDIV+' input').each( function(e){
		if (e.checked) {
			if(first==false) $(inputRes).value += ',';
			$(inputRes).value += e.name;
			first = false;
		}

	}.bind(this));
}


//
//	Disable Form
//
function disableForm(formID, isdisable)
{
	$$('#'+formID+'Container input').each(function(s, index) {
  		s.disabled=isdisable;
	});	
}

//
//	Clear data Alerts
//
function clearFormAlerts(){
	$$('#'+formID+'Container .alertERROR').each(function(s, index) {
  		s.removeClassName('alertERROR');
	});	
}

