
function validateEmail(elementValue){
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test(elementValue);
}

function validateNotNullables(notnullables){
	for(var i=0; i < notnullables.length; i++){
		item = document.getElementById(notnullables[i]);
		if (item == null)
			item = document.getElementsByName(notnullables[i])[0];
		aux = item.value.replace(/ /g, "");
		if (aux.length == 0){
			alert("All fields / questions marked with * must be filled in");
			item.focus();
			return false;
		}
	}

	return true;
}

function checkRadioGroup(radio){
	var checked = false;

	for(var i=0; i<radio.length;i++){
		checked = checked || radio[i].checked;

	}

	return checked;
}

function validateForm(form, notnullables){
	var aux;

	if(!form.agree.checked){
		alert("Yo must agree with the conditions");
		return false;
	}

	validateNotNullables(notnullables);

	if(!validateEmail(form.email.value)){
		alert("The given email was not a valid email");
		form.email.focus();
		return false;
	}

	return true;
}

function validateFormContactus(form){
	var notnullables = new Array('name', 'email', 'comment');

	if(!form.politpriv.checked){
		alert("Yo must read and accept the private policy");
		return false;
	}

	var result = validateNotNullables(notnullables);
	if(!result) return false;

	if(!validateEmail(form.email.value)){
		alert("The given email isn't a valid email");
		return false;
	}


	return true;
}

function validateFormAlerts(form){
	var notnullables = new Array("name", "email", "organisation");

	var post = "NONE";
	
	if(!form.politpriv.checked){
		alert("Yo must read and accept the private policy");
		return "SEND_ERROR";
	}

	var result = validateNotNullables(notnullables);
	if(!result) return 0;

	if(!validateEmail(form.email.value)){
		alert("The given email isn't a valid email");
		return "SEND_ERROR";
	}

	if(!form.newsletters.checked && !form.alerts.checked){
		alert("Yo must choose a subscription: Alerts or/and Newsletters");
		return "SEND_ERROR";
	}
	
	if(form.newsletters.checked){
		var newsForm = "EMail=" + form.email.value + "&FirstName=" + form.name.value + 
		"&Company=" + form.organisation.value +"&try=true&RegistrationFormID=76500&key=bBWk8k8f";
		post = httpRequest("POST","http://www.energyxchange.eu/tinc",false,requestResults,newsForm);
		post = "SEND_NEWSLETTER";
	}
	
	if(form.alerts.checked){
		if(post!="NONE"){
			post += "_ALERTS";
		}else{
			post = "SEND_ALERTS";
		}
	}
	return post;
}


function sendRequest(form){
	var access = true;
	var post = validateFormAlerts(form);
	switch(post){
	case "SEND_NEWSLETTER":
	case "SEND_NEWSLETTER_ALERTS":
	case "SEND_ALERTS":
		form.action = "actions/registration.php";
		access = true;
		break;
	default:
		access = false;
		break;
	}
	return access;
}

function requestResults() {
    if (http_request.readyState == 4) {
       if (http_request.status == 200) {
          result = http_request.responseText;
          alert(result);
          return true;
       } else {
          alert('There was a problem with the request.Please, try again later.');
          return false;
       }
    }
 }

function validateFormRequestMaterials(form){

	var notnullables = new Array("firstname","lastname","organisation","email","jobtitle",
		"address", "town", "country", "website");

	if(!validateNotNullables(notnullables))
		return false;

	if(!validateEmail(form.email.value)){
		alert("The given email isn't a valid email");
		return false;
	}

	if(!form.firstquestion.checked){
		alert("You are not allowed to request materials. Please, check your answers.");
		return false;
	}

	if(!form.secondquestion.checked){
		alert("You are not allowed to request materials. Please, check your answers.");
		return false;
	}

	if(!form.thirdquestion.checked){
		alert("You are not allowed to request materials. Please, check your answers.");
		return false;
	}

	var boolvar = false;
	for(var i=0; i<form['materials[]'].length; i++){
		if(form['materials[]'][i].value.length > 0){
			boolvar = true;
			break;
		}
	}

	if(!boolvar){
		alert('You must enter the references of the requested materials');
		return false;
	}

	return true;
}

function validateFormOffer(form){
	var notnullables = new Array("typematerial", "targetarea", "targetsector",
		"arecopies", "firstname", "lastname", "organisation", "email2");

	if(!validateNotNullables(notnullables))
		return false;

	if(!validateEmail(form.email2.value)){
		alert("The given email isn't a valid email");
		return false;
	}

	return true;
}

function validateFormNewOrg(form){
	var notnullables = new Array("name");
	
	if(!validateNotNullables(notnullables))
		return false;
	
	var boolvar = false;
	for(var i=0; i<form['sectors[]'].length; i++){
		if(form['sectors[]'][i].checked){
			boolvar = true;
			break;
		}
	}
	
	if(!boolvar){
		alert('You must select at least a sector');
		return false;
	}
		
	return true;
}

function validateFormNewCamp(form){
	var notnullables = new Array("name");
	
	if(!validateNotNullables(notnullables))
		return false;
	
	
}

function validateMultipleLangText(textname){
	var textbox_en = document.getElementsByName(textname + '_en')[0];
	var textbox_es = document.getElementsByName(textname + '_es')[0];
	var textbox_lt = document.getElementsByName(textname + '_lt')[0];
	
	if(textbox_en.value.length != 0)
		return true; 
	if(textbox_es.value.length != 0)
		return true; 
	if(textbox_lt.value.length != 0)
		return true; 
	
	return false;
}

function validateFormNewMaterial(form){
	var notnullables = new Array("author", "availability", "keywords");
	
	// Validate selects
	var selectorg = document.getElementById('organizationList');
	var selectcamp = document.getElementById('campaignList');
	
	if(selectorg.length == 0 || selectcamp.length == 0){
		alert('You must select an organization and a campaign.');
		return false;
	}
	
	if(!validateNotNullables(notnullables))
		return false;
	
	if(!validateMultipleLangText('title')){
		alert('You must complete the title in one language at least.');
		return false;
	}
	
	if(!validateMultipleLangText('description')){
		alert('You must complete the description in one language at least.');
		return false;
	}
	
	var checkbox = document.getElementsByName('modify_filecontent')[0];
	
	// Content validation
	switch(form.type_content.value){
		case "1": // Text
			if(form.textcontent.textLength == 0){
				alert('You must put text on the text content field.');
				return false;
			}
			break;
		case "2": // File
			
			if(checkbox != null){
				if(checkbox.checked == true){
					var file = form.content.value.replace(/ /g, "");
					if(file.length == 0){
						alert('You must select a content file.');
						return false;
					}
				}
			}
			break;
		case "4": // Youtube
			var link = form.youtubelink.value.replace(/ /g, "");
			if(link.length == 0){
				alert('You must put a youtube link on the text field.');
				return false;
			}
			break; 
	}
	
	return true;
}	



