function strip_space(thisURL) {
	var strip = thisURL;
	strip.value = strip.value.replace(/\s+/g,'');
	thisURL.value = strip.value;
}

function validate_review() {
	 var rating = document.getElementById('comment_0_rating');
	 var comment = document.getElementById('comment_0_comment');
	 var email = document.getElementById('metadata_field_text_2296_value');
	 var name = document.getElementById('metadata_field_text_2295_value');
	 var terms = document.getElementById('metadata_field_select_2754');
	 if (rating.value > 0) {
		 if (terms.checked) {
			 if (comment.value.length > 0) {
				 if (email.value.length == 0) {
					alert("You need to enter an email address if you have provided a comment");
					return false;
				 }
				 else{
		             if (!(email_checker(email.value))){
		                alert("The email you provided does not appear to be in a valid format");
		                return false;
		             }
	            }
				if (name.value.length ==0){
					alert("You need to enter a name if you have provided a comment");
					return false;
				}
			 }
			 if (comment.value.length > 3000) {
				 alert("The length of your comment needs to be less than 3000 characters");
				 return false;
			 }
			return true;
		 }
		 else{
			alert("You must agree to the terms and conditions to create a review");
			return false;
		 }
	 }
	 else
	 {
		 alert("You need to enter a rating to create a review");
		 return false;
	 }
 }
 
 function validate_edit(asset_id) {
	 var thisAsset = asset_id;
	 var title = document.getElementById('diird_resource_'+thisAsset+'_1798');
	 var shortTitle = document.getElementById('diird_resource_'+thisAsset+'_1804');
	 var URL = document.getElementById('diird_resource_'+thisAsset+'_1803');
	 var description = document.getElementById('metadata_field_text_2061_value');
	 var producedby = document.getElementById('metadata_field_text_2271_value');
	 var email = document.getElementById('diird_resource_'+thisAsset+'_1808');
	 
	 if (!(document.getElementById('diird_resource_'+thisAsset+'_1798'))){
		return true;							   
	 }
	 if (title.value.length == 0) {
		alert("You cannot submit a resource without a page name");
		return false;
	 }
	 if (shortTitle.value.length == 0) {
		alert("You cannot submit a resource without a page short name");
		return false;
	 }
	 if (URL.value.length == 0) {
		alert("You cannot submit a resource without a URL");
		return false;
	 }
	 else {strip_space(URL);}
	 if (description.value.length == 0) {
		alert("You cannot submit a resource without a description");
		return false;
	 }
	 if (producedby.value.length == 0) {
		alert("You cannot submit a resource without a produced by name");
		return false;
	 }
	 if (email.value.length == 0) {
		alert("You cannot submit a resource without an email address");
		return false;
	 }
	 if (!(email_checker(email.value))){
		alert("The email you provided does not appear to be in a valid format");
		return false;
	 }
	 return true;
 }
 
 function validate_res(submission) {
	var title = document.getElementById('diird_resource_0_1798');
	var URL = document.getElementById('diird_resource_0_1803');
	var description = document.getElementById('metadata_field_text_2061_value');
	var producedby = document.getElementById('metadata_field_text_2271_value');
	var location = document.getElementsByName('location[]')[0];
	var email = document.getElementById('diird_resource_0_1808');
	var i;
	if (submission!="internal"){
		var terms = document.getElementById('metadata_field_select_2754');
		if (!(terms.checked)) {
			alert("You must agree to the terms and conditions to create a new resource");
			return false;
		}
		if (description.value.length > 3000) {
		    alert("The length of your description needs to be less than 3000 characters");
			return false;
		}
	}
	if (location.value != "") {
		for (i=0; i<location.options.length; i++) {
		 if (location.options[i].selected) {
			if (location.options[i].value == 1766){
				alert("Business planning is a category, please select only sub categories prefixed by a -");
				return false;
			}
			if (location.options[i].value == 1770){
				alert("Marketing is a category, please select only sub categories prefixed by a -");
				return false;
			}
		  }
		}
	}
	else {
		alert("You need to select a sub-cateogry to create a new resource");
		return false;
	}
	if (title.value.length == 0) {
	alert("You need to enter a title to create a new resource");
	return false;
	}
	if (URL.value.length == 0) {
	alert("You need to enter a URL to create a new resource");
	return false;
	}
	else {strip_space(URL);}
	if (description.value.length == 0) {
	alert("You need to enter a description to create a new resource");
	return false;
	}
	if (producedby.value.length == 0) {
	alert("You need to enter a producer to create a new resource");
	return false;
	}
	if (email.value.length == 0) {
	alert("You need to enter an email address to create a new resource");
	return false;
	}
	else{
		if (!(email_checker(email.value))){
		alert("The email you provided does not appear to be in a valid format");
		return false;
		}
	}
	return true;
 }
 
 function email_checker(myemail) {
	    var str=myemail;
		var at="@"
		var dot="."
		var lat=myemail.indexOf(at)
		var lstr=myemail.length
		var ldot=myemail.indexOf(dot)
		if (myemail.indexOf(at)==-1){
		   return false
		}

		if (myemail.indexOf(at)==-1 || myemail.indexOf(at)==0 || myemail.indexOf(at)==lstr){
		   return false
		}

		if (myemail.indexOf(dot)==-1 || myemail.indexOf(dot)==0 || myemail.indexOf(dot)==lstr){
		    return false
		}

		 if (myemail.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (myemail.substring(lat-1,lat)==dot || myemail.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (myemail.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (myemail.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
	
function validate_cat(submission) {
	var name = document.getElementById('q2279_q1');
	var email = document.getElementById('q2279_q2');
	var category = document.getElementById('q2279_q3');
	var terms = document.getElementById('q2279_q6_0');
	
	if (!(terms.checked)) {
			alert("You must agree to the terms and conditions to suggest a category");
			return false;
		}
	if (name.value.length == 0) {
	alert("You need to enter a name to suggest a category");
	return false;
	}
	if (email.value.length == 0) {
	alert("You need to enter an email to suggest a category");
	return false;
	}
	else{
		if (!(email_checker(email.value))){
		alert("The email you provided does not appear to be in a valid format");
		return false;
		}
	}
	if (category.value.length == 0) {
	alert("You need to enter a category to suggest a category");
	return false;
	}
	
	return true;
}