var domain = "";
var ordersignup = false;
var bwindowH =  0;
var popPosT = 0;
$j(document).ready(function(){	
	bwindowH =  $j(window).height();						
	domain = "http://"+document.domain;
	try{ $j("#afname").alphanumeric(); } catch(e){}
	$j("#signupForm input[type='text'],#signupForm input[type='password'],#ss_signup_tb input[type='text']").bind("blur",checkAffiliate);	
	$j("#al_signup").bind("click",addAffiliate);
	$j("#sl-tabs li").bind("click",changeSingupTab);
	$j("#al_login").bind("click",SLogin);
	$j("#al_recover").bind("click",recoverPassword);
	$j("#admin-tabs li").bind("click",changeAdminTab);
	$j("#ss_al_signup #afemail").bind("keyup",validWebsite);
	$j('#username,#password').keyup(function(e){
		if(e.keyCode==13){
			if(($j("#username").val()!="") && ($j("#password").val()!=""))$j("#al_login").trigger("click");
		}
	});	
	$j("a[id='terms_of_use']").bind("click",popUpTerms);
	
	$j("#signupa").click(function(){$j("#sl-tabs li[tab='signup']").trigger("click")});
	$j("div[id='user-prof'] a[action]").bind("click",userAction);
	
	$j("div[id='user-prof'] li span[name]").bind("click",setDfPrf);
	$j("div[id='user-prof'] li img").bind("click",userAction);
	$j("#user-profA,.user-profA").click(function(){			
		//alert($j(this).parent().html())
		$j(this).parent().find("ul").toggle();
		//$j("#user-prof ul").toggle();
	})
	
	autoChangeSignupTab();
	$j("#feedbackB a,#feedbackI").bind("click",giveFeedback)
	try{
		popPosT =  ( bwindowH - 137 )/2;	
		$j('#feedbackB').css({'top':popPosT});
	}catch(e){}
});
function validWebsite(){
	var afemail = $j(this).val().split("@");
	if(afemail[1])$j("#vwebsite").html("www."+afemail[1]);
}
function giveFeedback(){
	window.parent.$j(".popup-contentbody").html('<iframe id="popup-ifrm" src="feedbackifr.php" width="400" height="300" frameborder="0" scrolling="no"></iframe>');
	window.parent.showPopDiv();	
}

function autoChangeSignupTab(){
	var option = $j("#option").val();
	var confirmsg = $j("#confirmsg").val();
	var tab = (option=="s") ? "signup" : "login";
	$j("#sl-tabs li[tab='"+tab+"']").trigger("click");
	if(confirmsg && confirmsg!="")$j("div[sltab='"+tab+"'] #message").html(confirmsg).show();
}
function changeAdminTab(){
	var tab = $j(this).attr("id");	
	window.location = "dashboard-"+tab+".htm";
}
function changeSingupTab(){
	var tab = $j(this).attr("tab");
	$j("#sl-tabs li").removeClass("sl-active");
	$j(this).addClass("sl-active");
	$j("div[sltab]").hide();
	$j("div[sltab='"+tab+"']").show();
}
var inptype = "";
function checkAffiliate(){	
	inpthis = $j(this);
	inptype = $j(this).attr("name");	
	email = (inptype=="afcemail") ? "&"+$j("#afemail").serialize() : "";
	passwd = (inptype=="afcpasswd") ? "&"+$j("#afpasswd").serialize() : "";
	if(inptype!="captcha"){
		$j.ajax({
		   dataType: 'jsonp',
		   jsonp: 'jsonp_callback',
		   url: "user/alSignup.php",
		   data: inpthis.serialize()+"&type="+inptype+""+email+""+passwd,
		   success: function(msg){			
			if(!msg[1]){
				inpthis.parent().addClass("ss_al_error");
				inpthis.next().find("div[id='ss_al_pr_left']:first").html(msg[0]).show();
				inpthis.parent().next().find("img:first").attr("src","images/ss_al_error.png").removeClass("spacer");
			}else{
				inpthis.parent().removeClass("ss_al_error").addClass("ss_al_suc");
				inpthis.next().find("div[id='ss_al_pr_left']:first").html("&nbsp;").hide();
				inpthis.parent().next().find("img:first").attr("src","images/ss_al_success.png").removeClass("spacer");
			}
		   }
		 }); 
	 }	
}
function recoverPassword(){
	//al_recover
	$j.ajax({
	   dataType: 'jsonp',
	   jsonp: 'jsonp_callback',
	   url: "user/pwdremind.php",
	   data: "email="+$j("#email").val(),
	   success: function(data){
		
			if(data.status=="failure"){
				alert(data.msg);
			}else if(data.status=="success"){
				window.location = "dashboard.php?option=recover&task=step2";	
			}
	   }
	 }); 
}
function addAffiliate(){
	try{
		var alerrorL = $j("#signupForm .ss_al_error").length;
		var imgsL = $j('body * > img[class="spacer"]').length;	
		var userid = $j("#user_id").val();
		var submitF = false;
		var params = "";
		var position = 0;

		if(alerrorL)$j("#signupForm .ss_al_error:first").find("input").focus();
		else if(imgsL)$j("#signupForm img[class='spacer']:first").parent().parent().find("[class=signup_inp]:first input").trigger("blur").focus();
		else if(!$j("#afaccept").is(":checked") && (!userid)){
			alert("Check I agree to continue signup");
			$j("#afaccept").parent().css("color","#bd1a00");
		}	
		else submitF=true;	
		if(!submitF){
			$j("#sl-err-msg").show();
			setTimeout('$j("#sl-err-msg").hide()',5000);
		}
	}catch(e){
		
	}
	if(submitF){		
		$j("#afaccept").parent().css("color","#3ab1ed");
		
		$j(".captcha_error").hide();
		$j.ajax({
		   dataType: 'jsonp',
		   jsonp: 'jsonp_callback',
		   url: "user/alSignup.php",
		   data: $j('#signupForm').serialize(),
		   success: function(msg){			
			if(msg[1]=="captcha"){
				$j("#signup_captcha").find("iframe").attr("src","http://www.tm.biz/captcha.php");
				$j(".captcha_error").html(msg[0]).show();
			}else if(msg[0]=="updated"){
				$j("#msg").html("Updated..")
				position = (bwidth-$j("#msg").width())/2;
				$j("#msg").css("left",position).show();		
				setTimeout('$j("#msg").hide()',5000);			
			}else if(msg[1]=="msg"){
				/*document.getElementById('signupForm').reset();
				$j("#signupForm img").attr({"src":"images/spacer.gif","class":"spacer"})
				$j("div[sltab='signup'] #amessage").html("Request recieved.  We will let you know as soon as we process your request.").show();
				setTimeout(function(){
					$j("div[sltab='signup'] #amessage").html(" ").hide();
				},10000);*/
				$j("#fp-content").html("Request received.  We will let you know as soon as we process your request.").show();
				$j("#signup-login").remove();				
				if(ordersignup){					
					window.parent.$j("#ordsigfrm").height("265px");
					window.parent.showPopDiv();
				}
			}
			else{				
				window.parent.location = msg[1];	
			}
		   }
		 });
	 }
}
function SLogin(){		
	var skey = $j(".search").val();	
	$j("input[id='search']").val(skey);	
	$j.post("user/vAuthenticate.php",$j("div[sltab='login'] input").serialize(),
			function(data){				
				if(parseInt(data["status"])){
					window.location.replace(data["redirect"]);
				}
				else{
					$j("div[sltab='login'] #message").html("Sorry, your login was incorrect.").show();
					setTimeout(function(){
						$j("div[sltab='login'] #message").html(" ").hide();
					},10000); 
				}
	},"json"); 
}
function submitbutton(pressbutton) {	
	$j("form[name='adminForm']").submit();	
}
function submitform(pressbutton) {	
	$j("form[name='adminForm']").submit();	
}
function popUpTerms(){	
	window.open(domain+"/termsi.htm","mywindow","location=0,status=0,scrollbars=1,width=960,height=400");
}
function userAction(){
	var action = $j(this).attr("action");	
	var dbd = $j(this).parent().find("span").attr("uid");
	var params = "";
	$j("#user-prof ul").hide();
	if(action!="new"){
		id = $j(this).parent().find("span").attr("id");
		params = "&id="+id+"&action="+action+"&dbd="+dbd;
	}
	$j(".popup-contentbody").html('<iframe id="popup-ifrm" src="dashboardNHF.php?option=account&task=newedit'+params+'" width="300" height="150" frameborder="0" scrolling="no"></iframe>');
	showPopDiv();
}
function setDfPrf(){
	//window.location.reload()
	var filter = {};
	var prfname = $j(this).attr("name"); 
	var uid = $j(this).attr("id"); 
	var dbd = $j(this).attr("uid"); 	

	$j.extend(filter,{"type": "aasprf","action":"set","prfname":prfname,"uid":uid,"dboard":dbd});		
	$j.post("actions.php",filter,
			function(data){	
				window.location.reload();
	});
}
function setPrfAction(){
		
}
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

