////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// FORM VALIDATION STUFF
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////


function validateInput(id){
	//alert("validate" + id);
	var toCheck = new Array();
	
	//document.getElementById('sourcesForm').target = 'upload_target';
	if (id == undefined){
		if ($("file") == undefined){
			id = "url";
		}else if ($("url") == undefined){
			id = "file";
		}
	}else if (id =="test"){
		return true;
	}
	
	
	if (id == "file"){
		toCheck.push("nickname");
		toCheck.push("description");
		toCheck.push("file");
		toCheck.push("labels");
		
	}else if (id == "url" ){
		//toCheck.push("file");
		toCheck.push("nickname");
		toCheck.push("isActive");
		toCheck.push("id");
		toCheck.push("description");
		toCheck.push("url");
		toCheck.push("coordinateSystem");
		toCheck.push("labels");
		
	}else{
		//toCheck.push("file");
		//alert("empty");
		toCheck.push("nickname");
		if ($("file") != undefined){
			toCheck.push("file");
		}
	}
	
	for (var i = 0; i < toCheck.length; i++){
		//alert(toCheck[i]+"->"+dwr.util.getValue(toCheck[i])); 
		if ($(toCheck[i]) == undefined){
			//alert("should be checked but wrong" + toCheck[i]);
		}else{
			if (!validate(toCheck[i], dwr.util.getValue(toCheck[i]))){
			//alert(toCheck[i]);
				return false;
			}
		}
	}
	document.getElementById('sourcesForm').setAttribute('target', 'upload_target');
	return true;
}

/**
* Validating the user input. Checks if all fields have been filled and if their
* values make sense (e-mail, URL etc). If a check fails the user will be prompted
* to the erroneous field. 
*/
function validate(id, text){
	// clear old error messages
	
	if($(id) !=  undefined){
		dwr.util.setValue(id+"Help", "");
	}
	// nothing to check for the status
	if (id == "isActive"){
		return true;
	}// check if there is an auto-generated id, if not create on
	else if(id == "id"){
		if(text.length == 0){
			Dasmi.getNextId( function(id) {
				dwr.util.setValue("id", id);
				return true;
			});
		}
	} // nickname present and without special chars 
	else if (id == "nickname"){
		if(text.length == 0){
			dwr.util.setValue(id+"Help", "Please insert a name!");
			setFocus(id);
			return false;
		}
		if (!checkSpecialChars(text)){
			dwr.util.setValue(id+"Help", "Please do not use special characters!");
			setFocus(id);
			return false;
		}
	 } // description present and without special chars 
	else if (id == "description"){
		/*if(text.length == 0){
			dwr.util.setValue(id+"Help", "Please insert insert at least a small description!");
			setFocus(id);
			return false;
		}*/
		if (!checkSpecialChars(text)){
			dwr.util.setValue(id+"Help", "Please do not use special characters!");
			setFocus(id);
			return false;
		}
	} // url present and a valid url 
	else if (id == "url"){
		text = text.replace(/^\s+|\s+$/g,"");
		if(text.length < 8){
			dwr.util.setValue(id+"Help", "Please insert an URL!");
			setFocus(id);
			return false;
		}
		if (!checkUrl(text)){
			dwr.util.setValue(id+"Help", "Please enter a valid URL!");
			setFocus(id);
			return false;
		}
	} // email present and a valid email 
	else if (id == "adminemail"){
		if(text.length == 0){
			dwr.util.setValue(id+"Help", "Please insert an e-mail!");
			setFocus(id);
			return false;
		}
		if (!checkEmail(text)){
			dwr.util.setValue(id+"Help", "Please enter a valid e-mail!");
			setFocus(id);
			return false;
		}
	} // at least one capability selected (also enforce interaction??)
	else if (id == "capabilities"){
        //alert(id+text);
		if(text.length == 0){
			dwr.util.setValue(id+"Help", "Please select at least one capability!");
			setFocus(id);
			return false;
		}
	} // at least one coordinate system selected
	else if (id == "coordinateSystem"){
		if(text.length == 0){
			dwr.util.setValue(id+"Help", "Please select at least one coordinate system!");
			setFocus(id);
			return false;
		}
	} // nothing to check for the labels, not required
	else if (id == "labels"){
		return true;
	}else if (id == "file"){
		//alert(dump(text));
		if (text.length < 1){
			dwr.util.setValue(id+"Help", "Please select a file!");
			setFocus(id);
			return false;
		}
		return true;
	}
	return true;
}





function checkSpecialChars(text){
	var chars = "!@#§²³$%^&*()+=-[]\\\';,./{}|\":<>?";
	for (var i = 0; i < text.length; i++) {
	  	if (chars.indexOf(text.charAt(i)) != -1) {
			return false;
		}
   	}
	return true;
}


function checkUrl(url){
    var urlRegExp = /^(http:\/\/)/i; // works, but not very good
//	var urlRegExp = /^(http:\/\/)?(?:[\w-]+\.)+[a-z]{2,6}$/i; // works, but not very good
	//var urlRegExp = /(http:\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i;
	//var urlRegExp =  /(((ht|f)tp(s?):\/\/)|(www\.[^ \[\]\(\)\n\r\t]+)|(([012]?[0-9]{1,2}\.){3}[012]?[0-9]{1,2})\/)([^ \[\]\(\),;&quot;'&lt;&gt;\n\r\t]+)([^\. \[\]\(\),;&quot;'&lt;&gt;\n\r\t])|(([012]?[0-9]{1,2}\.){3}[012]?[0-9]{1,2})/i; 
	if(!urlRegExp.test(url)){
		return false;
	} else{
		return true;
	}
}


/**
* Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
* http://javascript.internet.com
*/
function checkEmail(email) {
	//var emailRegExp = /^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/i; // works good, but ...
	var emailRegExp = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/i; // the longer the better =)
	if(!emailRegExp.test(email)){
		return false;
	} else{
		return true;
	}
}

