﻿
  var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
      
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('content').innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function post(obj) {
	  
	   var poststr = "nom=" + encodeURI( document.getElementById("nom").value );
	   poststr = poststr + "&cognom1=" + encodeURI( document.getElementById("cognom1").value );
	   poststr = poststr + "&cognom2=" + encodeURI( document.getElementById("cognom2").value );
	   poststr = poststr + "&nif=" + encodeURI( document.getElementById("nif").value );
	   poststr = poststr + "&poblacio=" + encodeURI( document.getElementById("poblacio").value );
	   poststr = poststr + "&professio=" + encodeURI( document.getElementById("professio").value );
	   poststr = poststr + "&treball=" + encodeURI( document.getElementById("treball").value );
	   poststr = poststr + "&telf=" + encodeURI( document.getElementById("telf").value );
	   poststr = poststr + "&email=" + encodeURI( document.getElementById("email").value );
	   
	   var i 
   		for (i=0;i<document.dadesUsuari.dia21.length;i++){ 
      	 if (document.dadesUsuari.dia21[i].checked) 
         	 break; 
       }
   		poststr = poststr + "&dia21=" + encodeURI( document.dadesUsuari.dia21[i].value );
	    poststr = poststr + "&pregunta=" + encodeURI( document.getElementById("pregunta").value );
	   
	   for (i=0;i<document.dadesUsuari.activitats.length;i++){ 
      	 if (document.dadesUsuari.activitats[i].checked) 
         	 break; 
       }
       poststr = poststr + "&activitats=" + encodeURI( document.dadesUsuari.activitats[i].value );
 		
 		for (i=0;i<document.dadesUsuari.dia22.length;i++){ 
      	 if (document.dadesUsuari.dia22[i].checked) 
         	 break; 
       }
       poststr = poststr + "&dia22=" + encodeURI( document.dadesUsuari.dia22[i].value );
		
		for (i=0;i<document.dadesUsuari.sopar.length;i++){
			if(document.dadesUsuari.sopar[i].checked)
			break;
		}
		
		poststr = poststr + "&sopar=" + encodeURI(document.dadesUsuari.sopar[i].value);
		for (i=0;i<document.dadesUsuari.dinar.length;i++){
			if(document.dadesUsuari.dinar[i].checked)
			break;
		}
		
		poststr = poststr + "&dinar=" + encodeURI(document.dadesUsuari.dinar[i].value);
		for (i=0;i<document.dadesUsuari.autocar.length;i++){
			if(document.dadesUsuari.autocar[i].checked)
			break;
		}
		
		poststr = poststr + "&autocar=" + encodeURI(document.dadesUsuari.autocar[i].value);
		poststr = poststr + "&participacio=" + encodeURI( document.getElementById("participacio").value );

		
	   makePOSTRequest('post_dades.php', poststr);

   }
   
function comprovaCamps(){
	//VEIFIQUEM CAMPS
	document.getElementById("errorMsg").value="";
	document.getElementById("errorMsg").style.visibility="hidden";
	if(!Empty(document.getElementById("nom").value)){
		document.getElementById("errorMsg").value="S'han d'omplir tots els camps";
		document.getElementById("errorMsg").style.visibility="visible";
	}else{
		if(!Empty(document.getElementById("cognom1").value)){
			document.getElementById("errorMsg").value="S'han d'omplir tots els camps";
			document.getElementById("errorMsg").style.visibility="visible";
		}else{
			if(!Empty(document.getElementById("cognom2").value)){
				document.getElementById("errorMsg").value="S'han d'omplir tots els camps";
				document.getElementById("errorMsg").style.visibility="visible";
			}else{
			if(!Empty(document.getElementById("nif").value)){
				document.getElementById("errorMsg").value="S'han d'omplir tots els camps";
				document.getElementById("errorMsg").style.visibility="visible";
			}else{
				if(!Empty(document.getElementById("poblacio").value)){
					document.getElementById("errorMsg").value="S'han d'omplir tots els camps";
					document.getElementById("errorMsg").style.visibility="visible";
				}else{
					if(!Empty(document.getElementById("professio").value)){
						document.getElementById("errorMsg").value="S'han d'omplir tots els camps";
						document.getElementById("errorMsg").style.visibility="visible";
					}else{
						if(!Empty(document.getElementById("pregunta").value)){
							document.getElementById("errorMsg").value="S'han d'omplir tots els camps";
							document.getElementById("errorMsg").style.visibility="visible";
						}else{
							if(!Empty(document.getElementById("participacio").value)){
								document.getElementById("errorMsg").value="S'han d'omplir tots els camps";
								document.getElementById("errorMsg").style.visibility="visible";
							}else{
									document.getElementById("submit").disabled=false;
									document.getElementById("errorMsg").value="";
									document.getElementById("errorMsg").style.visibility="hidden";
									
								
							}
						}
					}
				}
			}
		}
	}
	}
}


function Empty(q){
	var i;
	for(i=0;i<q.length;i++){  
    	if(q.charAt(i) != " " ){  
        	return true; 
        }  
   	 }  
   	 return false;
} 

function enviar(){
	document.getElementById("dadesUsuari").submit;
}

function comprovaCheckBox(checkBox){
	var check = false;

	return check;
}

function cargarContenido(fuente_datos, contenedor_id) {

	var url = encodeURIComponent(fuente_datos);
	var params = '';

	new Ajax.Request(url, {
	method: 'get', parameters: params,

	onSuccess: function(transport) {

	if(transport.responseText.length > 0){
		document.getElementById(contenedor_id).innerHTML=transport.responseText;
	}}});
}

function info(url){
	window.open(url,"Informació","width=300,height=400,status=no,toolbars=no,menubar=no,resizable=no,left=400,top=200"); 
}

function cridaInfo(text){
	comprovaCamps();
	switch(text){
		case "ciutat":	document.getElementById("infoActivitat").innerHTML= "<h2>Visita a la Ciutat:</h2> <br> Informació sobre la visita a la ciutat.";
						break;
		case "mercat" : document.getElementById("infoActivitat").innerHTML="<h2>Mercat d'experiències:</h2> <br> Informació sobre el mercat.";
						break;
		case "taller" : document.getElementById("infoActivitat").innerHTML="<h2>Taller de creativitat i innovació:</h2> <br> Informació sobre el taller.";
						break;
		case "caminada" :	document.getElementById("infoActivitat").innerHTML="<h2>Caminada amb Bastons: </h2><br> Informació sobre la caminada amb bastons.";
							break;
		default :document.getElementById("infoActivitat").innerHTML="";
				break;
	}

}

function calculaTotal(){
	var total = 60;
	var costdinar = 0;
	var costsopar = 0;
	for (i=0;i<document.dadesUsuari.dinar.length;i++){
			if(document.dadesUsuari.dinar[i].checked){
				if(document.dadesUsuari.dinar[i].value == "si"){
					costdinar = 10;
				}
			}
		}
	for (i=0;i<document.dadesUsuari.sopar.length;i++){
			if(document.dadesUsuari.sopar[i].checked){
				if(document.dadesUsuari.sopar[i].value == "si"){
					costsopar = 30;
				}
			}
		}	
	total = total + costdinar + costsopar;
	document.getElementById("total").innerHTML = "<h2> COST TOTAL DE LA INSCRIPCI&Oacute; : "  +  total + " &euro;.</h2>";
	comprovaCamps();
}




