var pluckgbl = {};
pluckgbl.en = 1; // enable

pluckgbl.noRefresh = 1; // don't force a page refresh
pluckgbl.hasCallback = null; // include callback after successful sign in

pluckgbl.cCheck = function (name) {
	var cookies = document.cookie;
	if (cookies.indexOf(name) != -1) {
		var startpos = cookies.indexOf(name)+name.length+1;
		var endpos = cookies.indexOf(";",startpos)-1;
		if (endpos == -2) endpos = cookies.length;
		return unescape(cookies.substring(startpos,endpos));
	}
	else{
		if (name == "HD") {
			return this.cCheck("AT");
		} else {
			return "";
		}
	}
}
pluckgbl.getName = function(c) {
	var name = pluckgbl.cCheck('AT').parseQuery().a;
	if (!name) name = pluckgbl.cCheck('HD').split("|")[1];
	return name;
}
pluckgbl.getUID = function(c) {
	var uid = pluckgbl.cCheck('AT').parseQuery().u;
	if (!uid) uid = pluckgbl.cCheck('HD').split("|")[0];
	return uid;
}

/*
	CFS: The Pluck drop-in widgets don't allow for comments and posts to update via ajax
	The iframe "solution" below acts as a temporary holder for Pluck comments to be copied
	into the main page DOM from.
*/
pluckgbl.updateCommentsHtml = function(r) {
	$('Comments_Container').innerHTML = r;
}
pluckgbl.updateCommentsContainer = function(obj) {
	$('pluck-comments-holder').src = '/fod/pluck-comments-holder.php?sh=' + obj.sh + '&ep=' + obj.ep;
}

pluckgbl.fixSLAuthRetry = 150;

pluckgbl.fixSLAuth = function () {
   if (typeof(gSLAuthProxy) == "object") {
	gSLAuthProxy.SubmitLoginForm = function(form) { 
	  // copied from Proxy.rails with the last line changed
	    var emailElement = this.GetElement("plckEmail");
	    var passwordElement = this.GetElement("password");
	    var emailValidationControl = this.GetElement("emailValidation");
	    var passwordValidationControl = this.GetElement("passwordValidation");
	    
	    emailValidationControl.style.display = "none";
	    passwordValidationControl.style.display = "none";
	
	    var isValid = true;
	    if (!jsValidateEmailAddress(emailElement.value)){
	        emailValidationControl.style.display = "inline";
	        isValid = false;
	    }
	    
	    if (passwordElement.value == ""){
	        passwordValidationControl.style.display = "inline";
	        isValid = false;
	    }
	    
	    if (isValid) {
	  		// copy end
	  		if(pluckgbl.noRefresh == 1) {
				ActiveUser.doSignIn(emailElement.value,passwordElement.value);
			}
			else {
				this.__SubmitForm(form);
			}
		}
	};
	
	gSLAuthProxy.ShowLoginOriginal = gSLAuthProxy.ShowLogin;
	
	gSLAuthProxy.ShowLoginFix = function() {
		$$('td.Table_FloatLoginLeft a').each(function(e,i) {
			if(e.innerHTML == "Register") {
				gSLAuthProxy.foundRegister = e;
			}
		});
		if (gSLAuthProxy.foundRegister) {
			gSLAuthProxy.foundRegister.href = "JavaScript:ActiveUser.doSignUpClick();";
		} else {
			if ( gSLAuthProxy.showLoginFixRetry -- ) {
				setTimeout("gSLAuthProxy.ShowLoginFix();",200);
			}
		}
	};
	
	gSLAuthProxy.ShowLogin = function() {
		this.ShowLoginOriginal();
		gSLAuthProxy.foundRegister = false;
		gSLAuthProxy.showLoginFixRetry = 150;
		this.ShowLoginFix();
	};

   } else {
	if ( pluckgbl.fixSLAuthRetry -- ) {
		setTimeout("pluckgbl.fixSLAuth();",50);
	}
   }
}

pluckgbl.fixSLAuth();

