$(document).ready(function(){	
		
	function forms(){
		
		var errmsg = '';
		function error(obj){
			$(obj).addClass('notvalid');
			if(obj.tagName.toLowerCase() != 'select'){
				var err = ($(obj).attr('title') != "" ) ? $(obj).attr('title') : errmsg;
				$(obj)
				.val(err)
				.focus(function(){
					var err = ($(obj).attr('title') != "" ) ? $(obj).attr('title') : errmsg;							
					if(!$(this).hasClass('label') && $(this).val()==err) $(this).val('');
				});
				$(obj)
				.val(err)
				.blur(function(){
					var err = ($(obj).attr('title') != "" ) ? $(obj).attr('title') : errmsg;							
					if(!$(this).hasClass('label') && $(this).val()=='') $(this).val(err);
				});
			}
			valid = false;
		};
		
		//2009/01/27 for checkbox only
		function errorCheckbox(obj){
			//$(obj).addClass('notvalid');
            $(obj).parent().parent().addClass('notvalid');
			if(obj.tagName.toLowerCase() != 'select'){
				var err = ($(obj).attr('title') != "" ) ? $(obj).attr('title') : errmsg;
				$(obj)
				.val(err)
				.focus(function(){
					var err = ($(obj).attr('title') != "" ) ? $(obj).attr('title') : errmsg;							
					if(!$(this).hasClass('label') && $(this).val()==err) $(this).val('');
				});
			}
			valid = false;
		};
		
		//2010/02/12 for premium Areas covered checkbox only
		function errorAreaCheckbox(obj){
			$(obj).addClass('notvalid');
			
			valid = false;
		};

		//2009/01/28 check the consistent between password and confirm password
		function checkPasswordConsistent(){
			$('#passwordErrorDiv').hide();

            var confirmPas= $("input[name='password_confirm']").val();
			var pas ='';
                          
            $("input[name='password']").each(function(){	
              var pid = $(this).attr('id');
			  if(pid=='name'){
			      pas = $(this).val();
			  }

			});
			
		    if (pas!= '' && confirmPas!= '' && pas!= confirmPas){
                $('#passwordErrorDiv').show();
			    valid=false;
			}
		
		}

		function check(obj){ 
			if ($(obj).val() == '' || checkLabel(obj) || checkErrMsg(obj)) error(obj);
		};
		
		function checkSelect(obj){
			if ($(obj).attr('title') == "Not mandatory")
			{
				return;
			}
			if ($(obj).val() == '' || $(obj).val() == 'No Selection Made') error(obj);
		};
		
		function checkRegEx(obj,type){
			var regEx,err;
			switch(type){
			case 'url':
				regEx = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
				break;
			case 'phone':
				var regEx = /[\d\s_-]/;
				break;
			default:
				regEx = /^[^@]+@[^@]+[a-z]{2,}$/;
			};
			var val = $(obj).val();
			if (val.search(regEx) == -1 || checkLabel(obj)){
				error(obj);
			};
		};	
		
		function checkLabel(obj){
			var text = $('label[for='+$(obj).attr('id')+']').text();
			return (text == $(obj).val());
		};	
		
		function checkErrMsg(obj){
			var text = ($(obj).attr('title') != "" ) ? $(obj).attr('title') : errmsg;
			return (text == $(obj).val());
		};	
		
		$('input.label,textarea.label').each(function(){
			var text = $('label[for='+$(this).attr('id')+']').text();
			$('label[for='+$(this).attr('id')+']').css('display','none');
			$(this).val(text);
			$(this).focus(function(){ if($(this).val()==text || $(this).val()==$(this).attr('title')  || $(this).val()==errmsg) $(this).val('');});
			$(this).blur(function(){ if($(this).val()=='') $(this).val(text); }); 										
		});		
		
		var pid, pnid;
		$('#login_password').each(function(){								
				pid = $(this).attr('id');
				pnid = pid + '2';	
				label = $('label[for='+ pid +']').text();
				$(this)
				.after('<input type="text" value="'+ label +'" id="'+ pnid + '" maxlength="32" size="16" />')
				.hide()
				.blur(function(){
					if($(this).val() == ''){
						$(this).hide();
						$('#'+ pnid).show();
					};
				});		
				$('#'+ pnid).focus(function(){								
					$(this).hide();
					$('#'+ pid).show().focus();
				})
		});			
				
		$('form').each(function(){

			$(this).submit(function(){
							
				$('.error',this).remove();
				$('.notvalid',this).removeClass('notvalid');
				valid = true;
				
				$(':text.required',this).each(function(){													
					if( $(this).hasClass('email') ){
						checkRegEx(this,'email');
					} else if ( $(this).hasClass('url') ){
						checkRegEx(this,'url');
					} else if ( $(this).hasClass('phone') ){
						checkRegEx(this,'phone');
					} else {
						check(this);
					};	
				});
				$('select',this).each(function(){														
					checkSelect(this);	
				});
				$('textarea.required',this).each(function(){													
					check(this);
				});
				
				$(':password',this).each(function(){														
					check(this);	
				});	
				$(':checkbox.required',this).each(function(){														
					if (!$(this).attr('checked')){
						//error(this);
						errorCheckbox(this);
					}
				});
				$('#'+ pnid, this).addClass('notvalid');

		        //2009/01/28 check the consistent between password and confirm password
				checkPasswordConsistent();
				
				//To check whether areas covered in premium form is checked
				var tp = $('input[name ="field_ft_81"]').val(); 
				if(tp == 'none')
				if($('input[name ="field_ft_81"]').is(".required"))
				{		
					var ts = $("#rright :checked").size();
					if (ts <= 1)
					{
							errorAreaCheckbox("#rright");
					}	
					else{
						$('#rright').removeClass('notvalid');
					}
				}
				
				if($('textarea[name ="field_id_61"]').is(".required")){
					if ($('textarea[name ="field_id_61"]').val() == '' ) {
						$('textarea[name ="field_id_61"]').addClass('notvalid');						
					}
					else{
						$('textarea[name ="field_id_61"]').removeClass('notvalid');
					}
				}
				
				
				return valid;	
			});	
		
		});			
	}	
	
		
	function backgrounds(){

		$('.bgimg').each(function(){
			var cssclass = $(this).attr('id');
			if($(this).val() == '') $(this).addClass(cssclass);
			$(this)
				.focus(function(){
					$(this).removeClass(cssclass);
				})
				.blur(function(){
					if($(this).val() == '') $(this).addClass(cssclass);
				});
		});
	};
	function upload(){
		$(':file').each(function(){	
			if($(':file').val() != ''){			
				alert("upload");
			}
		});
	};
	// init
	forms();
	backgrounds();
	upload();
	
});
