String.prototype.trim = function()
{
	// replace leading and trailing spaces with the empty string
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

	function valid_query(f) {
	
				var ValidElements = $(f).find('*');
				var ValidElements2 = $(f).find('*');
				var TheReturn = true;
				var ElementFocus;
				
				ValidElements.each
				(
				function()
					{
						 if ($(this).attr('title').trim() && $(this).attr('title').trim() != "")
								{
							if (!($(this).css("display")) || (($(this).css("display")) && !($(this).css("display") == "none"))){
							 //if($(this).css("display") != 'none') {
							 if($(this).attr('type') == 'select-one'){
							if ($(this).attr("selectedIndex") == -1 || $(this).attr("selectedIndex") == 0 )
								{
						if($(this).parent().parent().attr('class') != 'warning' && $(this).parent().attr('class') != 'warning'){
					addWarning($(this));
					}
									TheReturn = false;
								}
								else{
								removeWarning($(this));
								TheReturn = true;
								}
							 }
						if($(this).attr('type') == 'select-multiple'){
							if ($(this).attr("selectedIndex") == -1)
								{
									if($(this).parent().parent().attr('class') != 'warning' && $(this).parent().attr('class') != 'warning'){
					addWarning($(this));
					}
									TheReturn = false;
								}
								else{
								removeWarning($(this));
								TheReturn = true;
								}
							 }
							 
						if($(this).attr('type') == 'text' || $(this).attr('type') == 'textarea' || $(this).attr('type') == 'password'){
							if ($(this).attr('value') == "")
								{
									if($(this).parent().parent().attr('class') != 'warning' && $(this).parent().attr('class') != 'warning'){
					addWarning($(this));
					}
									TheReturn = false;
								}
								else{
								removeWarning($(this));
								TheReturn = true;
								}
							}
					}
								
							
					}
						}
				);
				
				
				ValidElements2.each
				(
				function()
					{
						//var input_title = $(this).attr('title');
						 if ($(this).attr('title').trim() && $(this).attr('title').trim() != "")
								{
							if (!($(this).css("display")) || (($(this).css("display")) && !($(this).css("display") == "none"))){
							 if($(this).attr('type') == 'select-one'){
							if ($(this).attr("selectedIndex") == -1 || $(this).attr("selectedIndex") == 0 )
								{
									//addWarning($(this));
									$(this).focus();
									TheReturn = false;
									return false;
									
								}
								else{
								TheReturn = true;
								}
								
							 }
						if($(this).attr('type') == 'select-multiple'){
							if ($(this).attr("selectedIndex") == -1)
								{
									$(this).focus();
									TheReturn = false;
									return false;
									
								}
								else{
								TheReturn = true;
								}
							 }
							 
						if($(this).attr('type') == 'text' || $(this).attr('type') == 'textarea' || $(this).attr('type') == 'password'){
							if ($(this).attr('value') == "")
								{
									$(this).focus();
									TheReturn = false;
									return false;
									
								}
								else{
								TheReturn = true;
								}
								
							}
					}
								

					}
						}

				);
				//alert(TheReturn);
				if (document.saveForm) {	
				Check_leavePage(f);
			
				if(TheReturn == true){
					if (document.saveForm.email && document.saveForm.pwd) {
						if(Check_email() == true && Check_password() == true){
						TheReturn = true}
						else{
						TheReturn = false;
						}
					}
				}
		}
				return TheReturn;
				}
				
		$(document).ready
		(
			function()
			{
				/*var labels = $(".req").parent().parent().find('label');
				
				labels.each
				(
					function()
					{
						var id = $(this).attr('for');
						
						if ($(this).parent().find("input.req[id=" + id + "], select.req[id=" + id + "]").size())
						{
							$(this).addClass('req');
						}
						else if ($(this).parent().parent().find("input.req[id=" + id + "], select.req[id=" + id + "]").size())
						{
							$(this).addClass('req');
						}
					}
				);*/
				
				
				$("input").blur (
				function(){
				var StopRemoveWarning = 0;
				if ($(this).attr('title').trim() && $(this).attr('title').trim() != ""){
							 if (!($(this).css("display")) || (($(this).css("display")) && !($(this).css("display") == "none"))) {
					if($(this).attr('type') == 'text' || $(this).attr('type') == 'textarea' || $(this).attr('type') == 'password'){
					var parentEls = $(this).parent().children()
                          .map(function () { 
							 if($(this).attr('type') == 'select-one'){
								if ($(this).attr("selectedIndex") == -1 || $(this).attr("selectedIndex") == 0 ){
								StopRemoveWarning = 1;
									}
								}
				else if($(this).attr('type') == 'text' || $(this).attr('type') == 'textarea' || $(this).attr('type') == 'password'){
					if ($(this).attr('value') == '' ){
						StopRemoveWarning = 1;
							}
						}
                              }
							  )
					if ($(this).attr('value') == '' ){
					if($(this).parent().parent().attr('class') != 'warning' && $(this).parent().attr('class') != 'warning'){
					addWarning($(this));
					}
				}
				else {
				if(StopRemoveWarning == 0 && ($(this).attr('name') != 'altemail') && ($(this).attr('name') != 'pwd1')){removeWarning($(this));}
				
				}
				//valid_nonsubmit($(this));
						}
						}
						}
						}
				);
				
				$("textarea").blur (
				function(){
				if ($(this).attr('title').trim() && $(this).attr('title').trim() != ""){
							 if (!($(this).css("display")) || (($(this).css("display")) && !($(this).css("display") == "none"))) {
					if($(this).attr('type') == 'text' || $(this).attr('type') == 'textarea' || $(this).attr('type') == 'password'){
					if ($(this).attr('value') == '' ){
					if($(this).parent().parent().attr('class') != 'warning' && $(this).parent().attr('class') != 'warning'){
					addWarning($(this));
					}
				}
				else {
				removeWarning($(this));
				}
				//valid_nonsubmit($(this));
						}
						}
						}
						}
				);
				
				$("select").blur (
				function(){
				var StopRemoveWarning = 0;
				if ($(this).attr('title').trim() && $(this).attr('title').trim() != ""){
							 if (!($(this).css("display")) || (($(this).css("display")) && !($(this).css("display") == "none"))){
							 if($(this).attr('type') == 'select-one'){
							var parentEls = $(this).parent().children()
                          .map(function () { 
							 if($(this).attr('type') == 'select-one'){
								if ($(this).attr("selectedIndex") == -1 || $(this).attr("selectedIndex") == 0 ){
								StopRemoveWarning = 1;
									}
								}
				else if($(this).attr('type') == 'text' || $(this).attr('type') == 'textarea' || $(this).attr('type') == 'password'){
					if ($(this).attr('value') == '' ){
						StopRemoveWarning = 1;
							}
						}
                              }
							  )
							if ($(this).attr("selectedIndex") == -1 || $(this).attr("selectedIndex") == 0 ){
				if($(this).parent().parent().attr('class') != 'warning' && $(this).parent().attr('class') != 'warning'){
					addWarning($(this));
					}
				}
				else {
				if(StopRemoveWarning == 0){removeWarning($(this));}
				}
				}
				}
				}
				}
				);
				
				$("select").blur (
				function(){
				if ($(this).attr('title').trim() && $(this).attr('title').trim() != ""){
							 if (!($(this).css("display")) || (($(this).css("display")) && !($(this).css("display") == "none"))) {
							 if($(this).attr('type') == 'select-multiple'){
							if ($(this).attr("selectedIndex") == -1){
				if($(this).parent().parent().attr('class') != 'warning' && $(this).parent().attr('class') != 'warning'){
					addWarning($(this));
					}
				}
				else {
				removeWarning($(this));
				}
				}
				}
				}
				}
				);
				
				$("select").change (
				function(){
				var StopRemoveWarning = 0;
				if ($(this).attr('title').trim() && $(this).attr('title').trim() != ""){
							 if (!($(this).css("display")) || (($(this).css("display")) && !($(this).css("display") == "none"))){
							 if($(this).attr('type') == 'select-one'){
							var parentEls = $(this).parent().children()
                          .map(function () { 
							 if($(this).attr('type') == 'select-one'){
								if ($(this).attr("selectedIndex") == -1 || $(this).attr("selectedIndex") == 0 ){
								StopRemoveWarning = 1;
									}
								}
				else if($(this).attr('type') == 'text' || $(this).attr('type') == 'textarea' || $(this).attr('type') == 'password'){
					if ($(this).attr('value') == '' ){
						StopRemoveWarning = 1;
							}
						}
                              }
							  )
							if ($(this).attr("selectedIndex") == -1 || $(this).attr("selectedIndex") == 0 ){
				if($(this).parent().parent().attr('class') != 'warning' && $(this).parent().attr('class') != 'warning'){
					addWarning($(this));
					}
				}
				else {
				if(StopRemoveWarning == 0){removeWarning($(this));}
				}
				}
				}
				}
				}
				);
				
				$("select").change (
				function(){
				if ($(this).attr('title').trim() && $(this).attr('title').trim() != ""){
							 if (!($(this).css("display")) || (($(this).css("display")) && !($(this).css("display") == "none"))) {
							 if($(this).attr('type') == 'select-multiple'){
							if ($(this).attr("selectedIndex") == -1){
				if($(this).parent().parent().attr('class') != 'warning' && $(this).parent().attr('class') != 'warning'){
					addWarning($(this));
					}
				}
				else {
				removeWarning($(this));
				}
				}
				}
				}
				}
				);
			}
		);
		
		
		function addWarning(input_elem)
		{
			if ($(input_elem).parent().find('label').size())
			{
				$(input_elem).parent().addClass('warning');
			}
			else
			{
				$(input_elem).parent().parent().addClass('warning');
			}
			
			if (!$(input_elem).next().hasClass('msg'))
			{
				//var message = $(input_elem).attr('title');
				var message = 'Required Field';
				$(input_elem).parent().append("<div class='msg'>" + message + "</div>");
			}
		}
		
		
		function removeWarning(input_elem)
		{
			if ($(input_elem).parent().find('label').size())
			{
				$(input_elem).parent().removeClass('warning');
			}
			else
			{
				$(input_elem).parent().parent().removeClass('warning');
			}
			$(input_elem).siblings('.msg').remove();
		}
		
		function valid_nonsubmit(input_elem)
		{
		var ValidElements = $(document).find('*');
				var TheReturn = true;
				ValidElements.each
				(
				function()
					{
							if ($(this).attr('title') && $(this).attr('title') != "")
								{
							 if($(this).css("display") != 'none') {
							 if($(this).attr('type') == 'select-one'){
							if ($(this).attr("selectedIndex") == -1 || $(this).attr("selectedIndex") == 0 )
								{
									addWarning($(this));
								}
								else{
								removeWarning($(this));
								}
							 }
						if($(this).attr('type') == 'select-multiple'){
							if ($(this).attr("selectedIndex") == -1)
								{
									addWarning($(this));
								}
								else{
								removeWarning($(this));
								}
							 }
							 
						if($(this).attr('type') == 'text' || $(this).attr('type') == 'textarea' || $(this).attr('type') == 'password'){
							if ($(this).val() == "")
								{
									addWarning($(this));
								}
								else{
								removeWarning($(this));
								}
							}
					}
								
							
					}
							if($(this).attr('name') == $(input_elem).attr('name')){
										return false;}				
											
										}
					);
									
		}
function addWarning_Check_email(input_elem,messagetext)
{
	if ($(input_elem).parent().find('label').size())
	{
		$(input_elem).parent().addClass('warning');
	}
	else
	{
		$(input_elem).parent().parent().addClass('warning');
	}
	
	if (!$(input_elem).next().hasClass('msg'))
	{
		//var message = $(input_elem).attr('title');
		//var message = 'Emails do not match';
		var message = messagetext;
		$(input_elem).parent().append("<div class='msg'>" + message + "</div>");
	}
}
		
function Check_email(elm) {
	var f = document.saveForm;
	var theemail;
	var filter =/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (f.email && f.altemail1) {
		if(f.email.value.trim() != f.altemail1.value.trim()){
			alert("Emails do not match.");
			addWarning_Check_email(f.altemail1, 'Emails must match');
			if (elm == 1) {
			f.email.focus();
			}
			theemail = false;
			return(false);
		}
		else{
		removeWarning(f.altemail1);
		theemail = true;
		}
	}
		if(f.email){
		if((f.email.value.trim() != '') && (theemail == true)){
			if (filter.test(f.email.value)){
			   removeWarning(f.email);
			   theemail=true
				}
			   else {
			   addWarning_Check_email(f.email,"Please input a valid email address!");
				//alert("Please input a valid email address!")
				if(elm == 1){
				f.email.focus();
				}
				theemail=false;
				return(false);
				}
		}
	
	else{theemail = true;}
return(theemail);

}
}

function addWarning_Check_password(input_elem)
{
	if ($(input_elem).parent().find('label').size())
	{
		$(input_elem).parent().addClass('warning');
	}
	else
	{
		$(input_elem).parent().parent().addClass('warning');
	}
	
	if (!$(input_elem).next().hasClass('msg'))
	{
		var message = 'Passwords do not match';
		$(input_elem).parent().append("<div class='msg'>" + message + "</div>");
	}
}
		
function Check_password() {
	var f = document.saveForm;
	var thepassword;
	thepassword = true;
	if (f.pwd && f.pwd1) {
		if((f.pwd.value.trim() != f.pwd1.value.trim()) || (f.pwd.value.trim() == '') || (f.pwd1.value.trim() == '')){
			//alert("Passwords do not match.");
			addWarning_Check_password(f.pwd1);
			f.pwd.focus();
			thepassword = false;
			return(false);
		}
		else{
		removeWarning(f.pwd1);
		thepassword = true;
		}
	}
return(thepassword);	
}

function Check_leavePage(f) {
			//	alert(f.name);
			if(f.name == "saveForm") {
				//If submitting form..disable Leave Page only for form page
				window.onbeforeunload = "";
			//	return true;
			}
}

function Check_functions(f){
	var returnthis;
	if (f.name == "saveForm") {
					if(Check_email() == true && Check_password() == true){
				Check_leavePage(f);
					returnthis = true}
					else{
					returnthis = false;
					}
			}
	else {
		returnthis = true;
	}
}
