function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,Label,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=4) { test=args[i+2]; Label=args[i+3]; val=MM_findObj(args[i]);
 	
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) {
      	if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(val)!= true){
 			errors+='- '+Label+' invalid email address.\n';
		}
      }
      else if (test.indexOf('isLicSafeStr')!=-1) {
	      	if(/^[\w\d\. -]+$/.test(val)!= true){
	 			errors+='- '+Label+' contains illegal character(s).\n';
			}
      }
      else if (test.indexOf('isChecked')!=-1) {
      	var j_items = MM_findObj(args[i]).length;
      	var j_checked = false;
      	for(j=0;j<j_items;j++){
      		if(MM_findObj(args[i])[j].checked){j_checked = true;}
      	}
      	if(j_checked!= true){
 						errors+='- '+Label+' is required.\n';
					}
      }
      else if (test.indexOf('is3Phone')!=-1) {
			  if (document.step2.phone_country.value == "") {
				   errors += '- Country Phone Code is required.\n';
				}
				if (document.step2.phone_area.value == "") {
				   errors += '- Area Phone Code is required.\n';
				}
				if (val == "") {
				   errors += '- '+Label+' is required.\n';
				}
				var stripped = document.step2.phone_country.value+document.step2.phone_area.value+val;
		    if (/^\d+$/.test(stripped)!= true) {
		       errors += '- '+Label+' contains illegal character(s).\n';
		    }
		    if (stripped.length <= 3) {
					errors += '- '+Label+' format is invalid (e.g.:+1-440-5555555).\n';
		    }
		}
      else if (test.indexOf('isPhone')!=-1) {
			  if (val == "") {
				   errors += '- '+Label+' is required.\n';
				}
			  var stripped = val.replace(/[\(\)\+\-\ ]/g, '');
		    if (/^\d+$/.test(stripped)!= true) {
		       errors += '- '+Label+' contains illegal character(s).\n';
		    }
		    if (stripped.length <= 9) {
					errors += '- '+Label+' format is invalid (e.g.:+1-440-5555555).\n';
		    }
		}
		else if (test.indexOf('isPass')!=-1) {
			  if (val == "") {
				   errors += '- '+Label+' is required.\n';
			  }
		     if (val.length <= 5) {
					errors += '- Your password is less than 6 characters.\n';
		     }
		     val2=MM_findObj(args[i+4]);
		     if(val!=val2.value){
		     	errors += '- '+ ' Passwords mismatch. Please try again.\n';
		     }
		}
       else if (test!='R') { num = parseFloat(val);
        if (isNaN(val) && test.indexOf('isState')==-1) errors+='- '+Label+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+Label+' must contain a number between '+min+' and '+max+'.\n';
    	}
	 }
	}
	else if (test.charAt(0) == 'R')
		if (test.indexOf('isZip')!=-1) {
			  var iCountry = "document.step2.country";
			  eval("var iSel = "+iCountry+".selectedIndex;");
			  if(iSel == "1" || iSel == "3" || iSel == "39")
			errors += '- '+Label+' is required.\n';
		}
		else if (test.indexOf('isState')!=-1) {
					  var iCountry = "document.step2.country";
					  eval("var iSel = "+iCountry+".selectedIndex;");
					  if(iSel == "1" || iSel == "15" || iSel == "40")
					errors += '- '+Label+' is required.\n';
		}

		else{
			errors += '- '+Label+' is required.\n';
		}
	}
 
  
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function ValidateLogOff(){
	try{
		if(confirm("Do you really want to log out?")){
			document.LogOffRes = true;
		}
		else{document.LogOffRes = false;}
	}catch(e){}
}

function ValidateLoginPass(){
		$("#signinloading").hide();
	var orig=$("#signinloading").html();
	$("#l_userpass").blur(function() {
	$("#signinloading").html(orig);
	$("#signinloading").show();
	loadTextPage("signinloading","/checkusr/"+$("#l_useremail").val()+"/"+$("#l_userpass").val()+"/");
});
}

function loadTextPage($pDivID,$pURL) {
	$.ajax({
	url:$pURL,
	dataType:'html',
	success:function (txt) {
	$('#'+$pDivID).html(txt);
	}
});

$(document).ready(function()
{
	$("#email").blur(function()
	{
		$("#msgbox").removeClass().addClass('email_status').text('Checking...').fadeIn(1000);
		$.post("/checkemail",{ uemail:$(this).val() } ,function(data)
        {
		  if(data=='YES')
		  {
		  	$("#msgbox").fadeTo(200,0.1,function()
			{
			  $(this).html('This Email Already exists').addClass('email_status_err').fadeTo(900,1);
			});
          }
		  else
		  {
		  	$("#msgbox").fadeTo(200,0.1,function()
			{
			  $(this).html('Email available to register').addClass('email_status_ok').fadeTo(900,1);
			});
		  }
        });
	});
});
}

function collapsElement(id) {
    $(".fb_desc").removeClass('selected');
    $("#"+id).toggleClass('selected');
    $("#"+id).scrollTo(500);
	
}

function CheckStates(iVal, iState){
	if(typeof formstyle == "undefined"){formstyle = "submit_form_large";}
  	$.getJSON("/get_stateslist4country/"+iVal+"/",{ajax: 'true'}, function(j){
  		var options = '';
		if(j.length > 1){
			for (var i = 0; i < j.length; i++) {
				zz=(iState == j[i].optionValue)? " selected" : "";
	        options += '<option value="' + j[i].optionValue + '"' +zz+ '>' + j[i].optionDisplay + '</option>';
	        $("#state_field").html("<select name='state' class='"+formstyle+"' tabindex='8'>"+options+"</select>");
	      }
	   }else{
	   		$("#state_field").html("<input name='state' type='text' class='"+formstyle+"' tabindex='10' value='' />");
	   	}
  	});
}

jQuery.fn.maxLength = function(max){  
	this.each(function(){  
		var type = this.tagName.toLowerCase();  
		var inputType = this.type? this.type.toLowerCase() : null;  
		if(type == "input" && inputType == "text" || inputType == "password"){  
			this.maxLength = max;  
  		}  
		else if(type == "textarea"){  
			this.onkeypress = function(e){  
				var ob = e || event;  
				var keyCode = ob.keyCode;  
				var hasSelection = document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;  
				return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);  
  			};  
  			this.onkeyup = function(){  
				if(this.value.length > max){  
  					this.value = this.value.substring(0,max);  
  				}  
  			};  
  		}  
  	});  
};  