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;
								}
								
							}
					}
								
							
					}
						}
						
				);
				
			//	Check_functions(f);
			if (document.saveform) {
				if(TheReturn == true){
					if(Check_email() == true && Check_password() == true){
				Check_leavePage(f);
					TheReturn = true}
					else{
					TheReturn = false;
					}
				}	
		}
		if (document.saveform) {
			if(document.saveform.pwd1) { //Only Application .. not Edit
			//Saving Phone Values
				if (document.saveform.phone) { mergephone(); }
				if (document.saveform.mobile) { mergephone_mobile(); }
				if (document.saveform.appq_165_1) { mergephone165(); }
			}
		}

				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)
{
	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';
		$(input_elem).parent().append("<div class='msg'>" + message + "</div>");
	}
}
		
function Check_email() {
	var f = document.saveform;
	var theemail;
	if (f.email && f.altemail) {
		if((f.email.value.trim() != f.altemail.value.trim()) || (f.email.value.trim() == '') || (f.altemail.value.trim() == '')){
			//alert("Emails do not match.");
			addWarning_Check_email(f.email);
			f.email.focus();
			theemail = false;
			return(false);
		}
		else{
		removeWarning(f.altemail);
		theemail = true;
return(theemail);
		}
	}
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;
	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.pwd1.focus();
			thepassword = false;
			return(false);
		}
		else{
		removeWarning(f.pwd1);
		thepassword = true;
return(thepassword);	
		}
	}
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;
	}
}
