// JavaScript Document
fixFormLabelsFF();
$(document).ready( function() {
	$("#validate").click( function() {
		var msg="";
		$("#validateform *").each( function() {
			if($(this).attr("required")=="required"){
				if(!$(this).val() && !msg){
					msg = $(this).attr("title") + " is a required field";
					$(this).focus();
					$(this).css({backgroundColor:"yellow"});
				}
		
				if($(this).attr("type") == "checkbox" && $(this).attr("checked") != true && !msg){
					msg = $(this).attr("title") + " is a required field";
					$(this).focus();
					$(this).css({backgroundColor:"yellow"});
				}
				if($(this).val() && $(this).attr("id") == "captcha_code" && !msg){
					var cap = $.ajax({
					   url: "/fwo.php?overlord=fwCheckCaptcha&captcha_code="+$(this).val(),
					   async: false
					}).responseText;
					if(cap==0){
						msg = $(this).attr("title") + " is a invalid";
						$(this).focus();
						$(this).css({backgroundColor:"yellow"});
						$(this).val("");
						$("#captcha_image").attr("src","/fwo.php?"+Math.round(Math.random()*1000000)+"&id=b3ZlcmxvcmQ9ZndTaG93Q2FwdGNoYQ==");
					}
				}
				
				if($(this).attr("validateas")=="email" && !msg){
					if(!echeck($(this).val())){
						msg = "Email is a invalid";
						$(this).focus();
						$(this).val("");
					}
				}
				
				if($(this).attr("action") && !msg){
					var check = $.ajax({
						   url: "/fwo.php?action="+$(this).attr("action")+"&"+$("#validateform").serialize(),
						   async: false
					}).responseText;
					if(check){
						msg = check+" "+$(this).attr("title");
						$(this).focus();
						$(this).css({backgroundColor:"yellow"});
						$(this).val("");
					}
				}
			}
			if($(this).attr("sameas") && !msg){
					if($(this).val() != $("#"+$(this).attr("sameas")).val()){
						msg = "Passwords do not match";
						$(this).focus();
						$(this).css({backgroundColor:"yellow"});
						$(this).val("");
						$("#"+$(this).attr("sameas")).val("");
					}
				}
			
		});
		if(!msg) {
			
			if($(this).attr("validatetype") == "alert"){
			var msg = $.ajax({
					   url: "/fwo.php?"+$("#validateform").serialize(),
					   async: false
			}).responseText;
			alert(msg);	
			} else $("#validateform").submit(); 
		}
		else alert(msg);
	});
});
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){

		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    
		    return false
		 }

 		 return true					
}
function fixFormLabelsFF(){
	if( jQuery.browser.mozilla ) {
		// do when DOM is ready
		$( function() {
			// search form, hide it, search labels to modify, filter classes nocmx and error
			$(".cmxform").hide().find( 'p label:not(.nocmx):not(.error)' ).each( function() {
				var $this = $(this);
				var labelContent = $this.html();
				var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
				// create block element with width of label
				var labelSpan = $("<span>")
					.css("display", "block")
					.width(labelWidth)
					.html(labelContent);
				// change display to mozilla specific inline-box
				$this.css("display", "-moz-inline-box")
					// remove children
					.empty()
					// add span element
					.append(labelSpan);
			// show form again
			}).end().show();
		});
	};
}
