
//alert('jaja');

function valida_correo(email){
    regx = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
    return regx.test(email);
}

function requestInscripcio() {
	
	document.getElementById('message_box2').innerHTML = '';
	document.getElementById('message_box2').style.border='0px'

	var all_ok = true;
	
	if(all_ok){

		var oForm = document.getElementById('forminscripcio');
		var sBody = getRequestBodyInscrip(oForm);
		
		var oXmlHttp = zXmlHttp.createRequest();
		oXmlHttp.open('post', oForm.action, true);
		oXmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		oXmlHttp.onreadystatechange = function () {
			if (oXmlHttp.readyState == 4) {
				if (oXmlHttp.status == 200) {
					showResultsInscripcio(oXmlHttp.responseText);
				} else {
					showResultsInscripcio('<p>An error occurred: <i>' + oXmlHttp.statusText + '</i></p>');
				}
			}else{
				var divResults = document.getElementById('formulari');
				divResults.innerHTML = '<p style="vertical-align:middle;"><img src="../img/ajax-loader.gif"> Processant petició</p>';
			}
		}
		oXmlHttp.send(sBody);
		
	}

}


function getRequestBodyInscrip(oForm) {
	var aParams = new Array();

	for (var i=0 ; i < oForm.elements.length; i++) {
		var sParam = encodeURIComponent(oForm.elements[i].name);
		sParam += "=";
		if(oForm.elements[i].name == 'dadesfachab'){
			if(document.forminscripcio.dadesfachab[1].checked){
				sParam += encodeURIComponent('N');
			}else{
				sParam += encodeURIComponent('S');
			}
		}else{
			if(oForm.elements[i].name == 'formapagcol'){
				if(document.forminscripcio.formapagcol[1].checked){
					sParam += encodeURIComponent('N');
				}else{
					sParam += encodeURIComponent('S');
				}
			}else{
				if(oForm.elements[i].name == 'soccolegiat'){
					if(document.forminscripcio.soccolegiat[1].checked){
						sParam += encodeURIComponent('N');
					}else{
						sParam += encodeURIComponent('S');
					}
				}else{
					if(oForm.elements[i].name == 'formapag'){
						if(document.forminscripcio.formapag[0].checked){
							sParam += encodeURIComponent('E');
						}else if(document.forminscripcio.formapag[1].checked){
							sParam += encodeURIComponent('X');
						}else if(document.forminscripcio.formapag[2].checked){
							sParam += encodeURIComponent('T');
						}else if(document.forminscripcio.formapag[3].checked){
							sParam += encodeURIComponent('D');
						}
					}else{
						if(oForm.elements[i].name == 'formapagNOcol'){
							if(document.forminscripcio.formapagNOcol[0].checked){
								sParam += encodeURIComponent('E');
							}else if(document.forminscripcio.formapagNOcol[1].checked){
								sParam += encodeURIComponent('X');
							}else if(document.forminscripcio.formapagNOcol[2].checked){
								sParam += encodeURIComponent('T');
							}else if(document.forminscripcio.formapagNOcol[3].checked){
								sParam += encodeURIComponent('D');
							}
						}else{
							sParam += encodeURIComponent(oForm.elements[i].value);
						}
					}
				}
			}
		}
		aParams.push(sParam);
	}

	return aParams.join("&");
}

function showResultsInscripcio(sMessage) {
	var divResults = document.getElementById("formulari");
	divResults.innerHTML = sMessage;
}


function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  var errorsmail='';
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { 
      	p=val.indexOf('@' && '.');
        if (p<1 || p==(val.length-1)) errors+='- La dirección de E-mail no es correcta.\n';
        if (p<1 || p==(val.length-1)) errorsmail+='- La dirección de E-mail no es correcta.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' tiene que ser un numero.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- Obligatorio => '+nm+'.\n'; }
  } if (errors){
  	if(errorsmail){
  		alert('Si us plau, empleni TOTS els camps marcats amb (*):\n\n'+errorsmail);
  	}else{
  		alert('Si us plau, empleni TOTS els camps marcats amb (*)');
  	}
    }
    //if (errors==false) document.forms.forminscripcio.submit();
    if (errors==false) requestInscripcio();
  document.MM_returnValue = (errors == '');
}


