// Global declarations
var mid=-1;
var pheight=246;
var pwidth=234;
var ppheight=0;
var ppwidth=40;

g_LoggedInFlag = false;
g_loginWindow = false;
g_forgotWindow = false;
g_loginPID = null;
g_loginTrayMode = false;  // false for closed | true for open
g_resetLoginColorsPID = false;

g_forgotClosurePID = false;

// aWindow class
activeInstance = null;

// onload Arrays
onloadRegistry = new Array();
onloadObjectRegistry = new Array();

showForgotPassword = function(){
	$("forgotPasswordInputButton").style.display = "block";
	$("cancelForgotEmail").style.display = "block";
	
	$("forgotEmailFieldContainer").style.display = "block";
	$("forgotResponseMessage").style.display = "none";
	
	ypSlideOutMenu.hideByForce('logindd');
	$("forgotPasswordBox").style.display = "block";
	$("forgot_email").focus();
	
	$("forgot_email").style.backgroundColor = "#ffffff";
	
	$("forgotPasswordBox").onmouseover = function(){
		if (g_forgotClosurePID){
			clearInterval(g_forgotClosurePID);
		}
	};
	
	$("forgotPasswordBox").onmouseout = function(){
		if (g_forgotClosurePID){
			clearInterval(g_forgotClosurePID);
		}
		
		g_forgotClosurePID = setTimeout("$('forgotPasswordBox').style.display = 'none';",6000);
	};
};

validateForgotPasswordForm = function(evt){
	evt = (document.all) ? event : evt;
	
	var email 		= $("forgot_email");
	
	if (email.value.trim().length > 0){
		
		$("forgot_email").disabled = "disabled";
		
		new Ajax.Request("../include/php/login.php",{
			method:"post",
			parameters:{
				action:"forgot",
				email:email.value.trim()
			},
			onComplete: function(t){
				if (t.responseText.trim() == "OK"){
					//Good
					$("forgot_email").disabled = "";
					$("forgot_email").style.backgroundColor = "#a6e38e";
					
					$("forgotPasswordInputButton").style.display = "none";
					$("cancelForgotEmail").style.display = "none";
					
					$("forgotEmailFieldContainer").style.display = "none";
					$("forgotResponseMessage").style.display = "block";
					
					setTimeout("$('forgotPasswordBox').style.display = 'none';",4000);
					
				} else if (t.responseText.trim() == "NOTOK") {
					$("forgot_email").disabled = "";
					$("forgot_email").style.backgroundColor = "#fc9c9c";
				} else {
					$("forgot_email").disabled = "";
					$("forgot_email").style.backgroundColor = "#fc9c9c";
				}
			}
		});
		
	} else {
		$("forgot_email").disabled = "";
		$("forgot_email").style.backgroundColor = "#fc9c9c";
	}
	
	evt.returnValue = false;
	return false;
};

window.onload = function(){
	var i;
	for (i=0; i<onloadRegistry.length;i++){
		eval(onloadRegistry[i] + "();");
	}
	for (i=0; i<onloadObjectRegistry.length;i++){
		anObj   = onloadObjectRegistry[i].o;
		aMethod = onloadObjectRegistry[i].m;
		eval("anObj." + aMethod + "();");
	}		
};

RelocateScreen = function(id){
	var obj = $(id);
	obj.style.display = "block";
	obj.style.left = "-1000px";
	obj.style.top = "-1000px";
	return;
};

validateLoginForm = function(evt){
	evt = (document.all) ? event : evt;
	
	var email 		= $("login_email");
	var password 	= $("login_password");
	
	if (email.value.trim().length > 0 &&
		password.value.trim().length > 0){
		
		$("login_email").disabled = "disabled";
		$("login_password").disabled = "disabled";
		
		new Ajax.Request("../include/php/login.php",{
			method:"post",
			parameters:{
				action:"authenticate",
				email:email.value.trim(),
				password:password.value.trim()
			},
			onComplete: function(t){
				if (t.responseText.trim() == "OK"){
					// Log the user in
					
					$("login_email").disabled = "";
					$("login_password").disabled = "";
					$("login_email").style.backgroundColor = "#a6e38e";
					$("login_password").style.backgroundColor = "#a6e38e";
					
					$("loginForm").submit();
				} else if (t.responseText.trim() == "NOTOK") {
					$("login_email").disabled = "";
					$("login_password").disabled = "";
					$("login_email").style.backgroundColor = "#fc9c9c";
					$("login_password").style.backgroundColor = "#fc9c9c";
					if (g_resetLoginColorsPID){
						clearInterval(g_resetLoginColorsPID);
					}
					setTimeout("resetLoginFieldsColor();",10000);
				} else {
					$("login_email").disabled = "";
					$("login_password").disabled = "";
					$("login_email").style.backgroundColor = "#fc9c9c";
					$("login_password").style.backgroundColor = "#fc9c9c";
					if (g_resetLoginColorsPID){
						clearInterval(g_resetLoginColorsPID);
					}
					setTimeout("resetLoginFieldsColor();",10000);
				}
			}
		});
		
	} else {
		$("login_email").disabled = "";
		$("login_password").disabled = "";
		$("login_email").style.backgroundColor = "#fc9c9c";
		$("login_password").style.backgroundColor = "#fc9c9c";
		
		if (g_resetLoginColorsPID){
			clearInterval(g_resetLoginColorsPID);
		}
		setTimeout("resetLoginFieldsColor();",10000);
	}
	
	evt.returnValue = false;
	return false;
};

resetLoginFieldsColor = function(){
	$("login_email").style.backgroundColor = "#ffffff";
	$("login_password").style.backgroundColor = "#ffffff";
};

setForgotGoodMessage = function(aMsg,aTimeout){
	if (!g_loginTrayMode){
		setForgotTray(true);
	}
		
	$("badLoginMessageForgot").style.display = "none";
	$("goodLoginMessageForgot").style.display = "block";
	$("goodLoginMessageForgot").innerHTML = aMsg;
	
	if (g_loginPID){
		clearInterval(g_loginPID);
	}
	g_loginPID = setTimeout('$("goodLoginMessageForgot").style.display = "none"; setForgotTray(false); setForgotForm(false); ',(aTimeout*1000)+400);
};

setLoginGoodMessage = function(aMsg,aTimeout){
	if (!g_loginTrayMode){
		setLoginTray(true);
	}
	
	$("processingLoginMessage").style.display = "none";
	$("badLoginMessage").style.display = "none";
	$("goodLoginMessage").style.display = "block";
	$("goodLoginMessage").innerHTML = aMsg;
	
	if (g_loginPID){
		clearInterval(g_loginPID);
	}
	g_loginPID = setTimeout('$("goodLoginMessage").style.display = "none"; setLoginTray(false); setLoginForm(false); ',(aTimeout*1000)+400);
};

setLoginAuthenticating = function(){
	if (!g_loginTrayMode){
		setLoginTray(true);
	}
	
	$("processingLoginMessage").style.display = "block";
	$("badLoginMessage").style.display = "none";
	$("goodLoginMessage").style.display = "none";
};

setForgotErrorMessage = function(aMsg,aTimeout){
	if (!g_loginTrayMode){
		setForgotTray(true);
	}
	
	$("badLoginMessageForgot").style.display = "block";
	$("goodLoginMessageForgot").style.display = "none";
	$("badLoginMessageForgot").innerHTML = aMsg;
	
	if (g_loginPID){
		clearInterval(g_loginPID);
	}
	g_loginPID = setTimeout('$("badLoginMessageForgot").style.display = "none"; setForgotTray(false);',(aTimeout*1000)+400);
};

setLoginErrorMessage = function(aMsg,aTimeout){
	if (!g_loginTrayMode){
		setLoginTray(true);
	}
	$("processingLoginMessage").style.display = "none";
	$("badLoginMessage").style.display = "block";
	$("goodLoginMessage").style.display = "none";
	$("badLoginMessage").innerHTML = aMsg;
	
	if (g_loginPID){
		clearInterval(g_loginPID);
	}
	g_loginPID = setTimeout('$("badLoginMessage").style.display = "none"; setLoginTray(false);',(aTimeout*1000)+400);
};

setLoginTray = function(aBool){
	if (aBool && !g_loginTrayMode){
		new Effect.Morph('loginStatusTray', {
		  style: 'height:20px;', // CSS Properties
		  duration: 0.4, // Core Effect properties
		  transition:Effect.Transitions.linear
		});
		setTimeout('g_loginTrayMode = true;' , 400);
	} else if (!aBool && g_loginTrayMode){
		new Effect.Morph('loginStatusTray', {
		  style: 'height:0px;', // CSS Properties
		  duration: 0.4, // Core Effect properties
		  transition:Effect.Transitions.linear
		});
		setTimeout('g_loginTrayMode = false;' , 400);
	}
	return;
};

setForgotTray = function(aBool){
	if (aBool && !g_loginTrayMode){
		new Effect.Morph('forgotStatusTray', {
		  style: 'height:20px;', // CSS Properties
		  duration: 0.4, // Core Effect properties
		  transition:Effect.Transitions.linear
		});
		setTimeout('g_loginTrayMode = true;' , 400);
	} else if (!aBool && g_loginTrayMode){
		new Effect.Morph('forgotStatusTray', {
		  style: 'height:0px;', // CSS Properties
		  duration: 0.4, // Core Effect properties
		  transition:Effect.Transitions.linear
		});
		setTimeout('g_loginTrayMode = false;' , 400);
	}
	return;
};

centerElement = function(obj){
	var h = (document.all) ? document.documentElement.clientHeight : window.innerHeight;
	var w = (document.all) ? document.documentElement.clientWidth : window.innerWidth;
	var sTop = document.documentElement.scrollTop;
	
	var boxWidth = obj.offsetWidth;
	var boxHeight = obj.offsetHeight;
	
	var bxCord = Math.floor((w-boxWidth)/2);
	var byCord = Math.floor((h-boxHeight)/2) + sTop;
	
	obj.style.left = bxCord + "px";
	obj.style.top = byCord + "px";
	return;
};

getCenterCords = function(obj){
	var h = (document.all) ? document.documentElement.clientHeight : window.innerHeight;
	var w = (document.all) ? document.documentElement.clientWidth : window.innerWidth;
	var sTop = document.documentElement.scrollTop;
	
	var boxWidth = obj.offsetWidth;
	var boxHeight = obj.offsetHeight;
	
	var bxCord = Math.floor((w-boxWidth)/2);
	var byCord = Math.floor((h-boxHeight)/2) + sTop;
	
	return {x:bxCord,y:byCord};
};

function performObjectMethodOnLoad(obj,methodName){
	var anObj = new Object();
	anObj.o = obj;
	anObj.m = methodName;
	onloadObjectRegistry.push(anObj);
}

function addLoadEvent(func){
	onloadRegistry.push(func);
}

if (document.all && !window.XMLHttpRequest){
	new ypSlideOutMenu("mn_0dd", "down", 20, 33, 150, 140);
	new ypSlideOutMenu("mn_1dd", "down", 128, 33, 150, 152);
	new ypSlideOutMenu("mn_2dd", "down", 270, 33, 150, 47);
	new ypSlideOutMenu("logindd", "down", 800, 130, 180, 150);
} else {
	new ypSlideOutMenu("mn_0dd", "down", 10, 33, 150, 150);
	new ypSlideOutMenu("mn_1dd", "down", 118, 33, 150, 152);
	new ypSlideOutMenu("mn_2dd", "down", 260, 33, 150, 47);
	new ypSlideOutMenu("logindd", "down", 800, 130, 180, 150);
}

var dd_vis;
var mn_images = ['tooltip','spacer'];
var mn_active = Array();

function initImages() {
	for (var i=0; i< mn_images.length; i++){
		mn_active[i]=0;
	}
}

function swapImage(element, act, evt) {
	if ( document.getElementById(element+'ddContainer') ) {
		if (act == 1)
			ypSlideOutMenu.showMenu(element+'dd');
		else
			ypSlideOutMenu.hideMenu(element+'dd');
	}
}

function select_innerHTML(objeto,innerHTML){
/******
* select_innerHTML - corrige o bug do InnerHTML em selects no IE
* Veja o problema em: http://support.microsoft.com/default.aspx?scid=kb;en-us;276228
* Versão: 2.1 - 04/09/2007
* Autor: Micox - Náiron José C. Guimarães - micoxjcg@yahoo.com.br
* @objeto(tipo HTMLobject): o select a ser alterado
* @innerHTML(tipo string): o novo valor do innerHTML
*******/
    objeto.innerHTML = ""
    var selTemp = document.createElement("micoxselect")
    var opt;
    selTemp.id="micoxselect1"
    document.body.appendChild(selTemp)
    selTemp = document.getElementById("micoxselect1")
    selTemp.style.display="none"
    if(innerHTML.indexOf("<option")<0){//se não é option eu converto
        innerHTML = "<option>" + innerHTML + "</option>"
    }
    innerHTML = innerHTML.replace(/<option/g,"<span").replace(/<\/option/g,"</span")
    selTemp.innerHTML = innerHTML
      
    
    for(var i=0;i<selTemp.childNodes.length;i++){
  var spantemp = selTemp.childNodes[i];
  
        if(spantemp.tagName){     
            opt = document.createElement("OPTION")
    
   if(document.all){ //IE
    objeto.add(opt)
   }else{
    objeto.appendChild(opt)
   }       
    
   //getting attributes
   for(var j=0; j<spantemp.attributes.length ; j++){
    var attrName = spantemp.attributes[j].nodeName;
    var attrVal = spantemp.attributes[j].nodeValue;
    if(attrVal){
     try{
      opt.setAttribute(attrName,attrVal);
      opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
     }catch(e){}
    }
   }
   //getting styles
   if(spantemp.style){
    for(var y in spantemp.style){
     try{opt.style[y] = spantemp.style[y];}catch(e){}
    }
   }
   //value and text
   opt.value = spantemp.getAttribute("value")
   opt.text = spantemp.innerHTML
   //IE
   opt.selected = spantemp.getAttribute('selected');
   opt.className = spantemp.className;
  } 
 }    
 document.body.removeChild(selTemp)
 selTemp = null
}

function actimage(id, state) {
    if (mid==id)
	state=1;
    if (state == 0) {
	return mn_images[id]+'.gif';
    } else {
	return mn_images[id]+'_.gif';
    }
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function setSelectByValue(o,val){
	if (o.tagName.toLowerCase() != "select"){
		return false;
	}
	
	for (var i=0;i<o.options.length;i++){
		if (o.options.item(i).value == val){
			o.selectedIndex = i;
			return true;
		}
	}
	return false;
}

String.prototype.br2nl = function (){
	return this.replace(/<br[^>]*>/gi,'\n');
};

String.prototype.nl2br = function (){
	return this.replace(/(\r\n|\n)/gi,'<br />');
};
