function getByIdOrName(id, context) {
	if(context && jQuery("#" + context + " *[name="+id+"]")) return jQuery("#" + context + " *[name="+id+"]").get(0);
	if(document.getElementsByName(id)) return document.getElementsByName(id)[0];
	if(document.getElementById(id)) return document.getElementById(id);
	
	return false;
}

function check(conditions, form) {
	var context = "";
	if(form && form.id) context = form.id;
	for(var index=0; index<conditions.length; index++ ) {
		var cond = conditions[index]
		var is = cond['is'].toLowerCase();
		var name = cond['name'];
		var title = cond['title'];
		var msg = cond['error'];
		if(!title) title = name;
		
		if(cond['when'] == 0 || cond['when'] == false) {
			continue;
		}/* else if (cond['when'] != "" && typeof cond['when'] != 'undefined') {
			alert(msg);
			return false;
		}*/
		
		var ele = getByIdOrName(name, context);
		if(!ele) continue;

		if(cond['match']) {
			if(ele.value.search(cond['match']) + 1) {
				alert(msg);
				return false;
			}
		}
		if(cond['not_match']) {
			if(ele.value.search(cond['not_match']) == -1) {
				alert(msg);
				return false;
			}
		}
		if(is == 'empty') {
			var error = "Some mandatory fields are not filled.";
			if(!ele.value) {
				if(msg) error = msg;
				else if(title) error = "The " + title +" is not provided";
				alert(error);
				ele.focus();
				return false;
			}
		} else if(is == 'not') {
			if(ele.value != cond.value) {
				alert(msg);
				return false;
			}
		} else if(is == 'equal') {
			if(ele.value == cond.value) {
				alert(msg);
				return false;
			}
		} else if(is == 'greater') {
			if(ele.value > cond.value) {
				alert(msg);
				return false;
			}
		} else if(is == 'lesser') {
			if(ele.value < cond.value) {
				alert(msg);
				return false;
			}
		} else if(is == 'file') { //The valid file types should be given in the 'value' field as a comma seperated list
			if(ele.value) {
				var parts;
				if(ele.value.indexOf("/") + 1) parts = ele.value.split("/");
				else parts = ele.value.split("\\");
	
				var ext = parts[parts.length-1].split(".");
				ext = ext[ext.length-1].toLowerCase();
				var allowed = cond.value.toLowerCase().split(",");
	
				var found = false;
				for(var i in allowed) {
					if(ext == allowed[i]) {
						found = true;
						break;
					}
				}
				if(!found) {
					if(msg) error = msg;
					else if(title) error = "The " + title +" should be any of these file types : " + cond.value;
					else error = "Invalid file type";
					alert(error);
					return false;
				}
			}
		} else if(is == 'nan' || is == 'not_number') { //Warning: Decimals will get thru
			if(isNaN(ele.value)) {
				if(msg) error = msg;
				else if(title) error = "The " + title +" should be a number";
				alert(error);
				ele.focus();
				return false;
			}
		} else if(is == 'not_email') { //If the field does not match the email regexp, an error is shown
			if(ele.value.search(/^[\w\-\.]+\@[\w\-\.]+\.[a-z\.]{2,5}$/) == -1) {
				if(msg) error = msg;
				else if(title) error = "The " + title +" should be a valid email address";
				else error = "Invalid Email address provided"
				alert(error);
				ele.focus();
				return false;
			}
		} else if(is == 'has_weird') { //Check for weird chars
			if(ele.value.search(/^[\w\-]*$/) == -1) {
				if(msg) error = msg;
				else if(title) error = "The " + title +" should not have weird characters";
				else error = "Weird characters where found in the input"
				alert(error);
				ele.focus();
				return false;
			}
		} else if(is == 'not_name') { //Check for chars that cannot appear in a title
			if(ele.value.search(/^[\w\'\(\)\,\.\/ ]*$/) == -1) {
				if(msg) error = msg;
				else if(title) error = "The " + title +" has invalid characters";
				else error = "Invalid characters where found in the input"
				alert(error);
				ele.focus();
				return false;
			}
		} else if(is == 'not_url') { //See if the input is an URL
			if(ele.value.search(/^https?\:\/\/.+\..+/) == -1) {
				if(msg) error = msg;
				else if(title) error = "The " + title +" is not an URL";
				else error = "Invalid URL"
				alert(error);
				ele.focus();
				return false;
			}
		}
	}

	return true;
}


function stopEvent(e) {
	if(!e) var e = window.event;
	
	//e.cancelBubble is supported by IE - this will kill the bubbling process.
	e.cancelBubble = true;
	e.returnValue = false;

	//e.stopPropagation works only in Firefox.
	if (e.stopPropagation) {
		e.stopPropagation();
		e.preventDefault();
	}
	return false;
}
