// GLOBALS:
var Sess = "";
var Dip = "";
var GUser = "";
var GCurPage = "";
var Scrw = "0";
var Scrh = "0";
var HOMEPAGE = "http://www.web2coders.com/encours/contactcelebs/";
var PrevId = "";
var GSPrevId = "";

btn100on = new Image();
btn100on.src = "pics/btn100on.png";
btn100off = new Image();
btn100off.src = "pics/btn100off.png";
staboff = new Image();
staboff.src = "pics/staboff.gif";
stabon = new Image();
stabon.src = "pics/stabon.gif";

function GetW()
{
	if( typeof( window.innerWidth ) == 'number' ) 
		//Non-IE
		Scrw = window.innerWidth; // - 20;
	else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
		//IE 6+ in 'standards compliant mode'
		Scrw = document.documentElement.clientWidth; //-16;
	else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
		//IE 4 compatible
		Scrw = document.body.clientWidth; // -16;
}
function GetH()
{
	if (parseInt(navigator.appVersion)>3)
	{
		if (navigator.appName=='Netscape')
			winH = window.innerHeight;
		if (navigator.appName.indexOf('Microsoft')!=-1)
			winH = document.body.offsetHeight;
	}
	Scrh = winH - 100;
}
function showform(theForm,a,b)
{
	var txt = "Form name: " + theForm.name + "\r\n";
	txt += "theForm.elements.length = "+theForm.elements.length+"\r\n";
	for (var i=0; i<theForm.elements.length; i++)
		txt += theForm.elements[i].name + " = " + theForm.elements[i].value + "\r\n";
	txt += "a: "+a+"\r\n";
	txt += "b: "+b+"\r\n";
	alert(txt);
}
// === EMAIL VALIDATION =================================================================================================================================
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(true);
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return(true);
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return(true);
	if (str.indexOf(at,(lat+1))!=-1)
		return(true);
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return(true);
	if (str.indexOf(dot,(lat+2))==-1)
		return(true);
	if (str.indexOf(" ")!=-1)
		return(true);
	return (false);					
}

// ===================================================================================================================
// === COOKIES
// ===================================================================================================================
function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}
function setCookie(c_name,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+";expires="+exdate.toGMTString();
}
function UserCookie()
{
	var rts = getCookie('lduser');
	if (rts != null && rts != "")	// cookie exists: we use that session instead (but Php does NOT know)
	{
		if (rts.length > 5)
			GUser = rts;
	}
	else
		GUser = "";
//	alert("GUsercookie: "+GUser);
}
function SessCookie()
{
	var rts = getCookie('ldsess');
	if (rts != null && rts != "")	// cookie exists: we use that session instead (but Php does NOT know)
	{
		if (rts.length > 5)
			Sess = rts;
	}
	else
		Sess = "";
//	alert("Sesscookie: "+Sess);
}
function DelSessCookie()
{
	setCookie("ldsess","",-1);
}
function DelUserCookie()
{
	setCookie("lduser","",-1);
}

// ===================================================================================================================
// === LOGIN
// ===================================================================================================================
function js_login_form(s)
{
//	alert("js_login_form: "+s);
	Sess = s;
	js_pop_open(280,200,130,"login");
	setTimeout("js_field_user()",2000);
}
function js_field_user()
{
	var x = document.getElementById("user");
	x.focus();
}
function js_recover_pwd(user)
{
	sajax_target_id = 'popcontents';
	x_p_recover_pwd(user); 
	sajax_target_id = '';
}
function js_login(theForm)
{
	if (theForm.elements['user'] == "")
	{
		js_pop_close();
		return;
	}
	txt = "";
	Sess = theForm.elements['s'].value;	// the session just created.
	GUser = theForm.elements['user'].value;
	if (theForm.elements['remember'].checked)
		theForm.elements['remember'].value = "set";
	var txt = "user="+theForm.elements['user'].value;
	txt += "&pwd="+theForm.elements['pwd'].value;
	txt += "&remember="+theForm.elements['remember'].value;
	txt += "&s="+Sess;
	sajax_target_id = 'popcontents'; // returns "logged", "wuser", "wpwd"
	x_p_login(txt); 
	sajax_target_id = '';
	setTimeout("js_check_logged()",2000);
}
function js_check_logged()
{
	var r = document.getElementById("remember");
	var remember = r.value;
//	alert ("Remember = "+remember);
	var x = document.getElementById("status");
	var msg = x.value;
	if (msg == "wuser")
		js_login_form(Sess);
	else if (msg == "logged")
	{
		js_pop_close();
		sajax_target_id = 'login'; 
		x_p_logoff_form("",Sess); 
		sajax_target_id = '';
		if (remember == "set")
			setCookie("bguser",document.getElementById("user").value,365);
		setCookie("bgsess",Sess,1);
	}
	window_location = HOMEPAGE;
}
function js_logoff()
{
	js_pop_close();
	sajax_target_id = 'hide'; //login?
	x_p_logoff(Sess); 
	sajax_target_id = '';
	DelSessCookie();
	DelUserCookie();
	Sess = "";
	GUser = "";
	window.location = HOMEPAGE;
}
function js_auto_log(user,s)
{
	GUser = user;
	Sess = s;
	js_pop_close();
	sajax_target_id = 'login'; 
	x_p_logoff_form(GUser,Sess); 
	sajax_target_id = '';
}
// =========================================================================
// === USERS REGISTRATION
// =========================================================================
function js_register_form(s)
{
	Sess = s;
	GUser = "";
	js_pop_open(150,400,350,"register");
}
function js_check_avail(theForm)
{
	var name = theForm.elements['user'].value;
//	alert ("checkavail "+name);
	sajax_target_id = 'poperr'; 
	x_p_check_avail(name); 
	sajax_target_id = '';
}
function js_check_username(name)
{
	if (name == "")
	{
		pop_err("Please, enter a username, min 6 char alphanumeric only.","user");	
		return true;
	}
	var User_N = name;
	User_N = User_N.toLowerCase();	// LOWERCASE ONLY
	var patt1 = /\W/g;
	var uu = "";
	uu = User_N.match(patt1);
	if (uu != null)
	{
		pop_err("Username in Alphanumeric only!","user");	
		return true;
	}
	if (User_N.length < 6)
	{
		pop_err("Username: Six characters or more!","user");
		return true;
	}
	return false;
}
function js_check_passwords(theForm)
{
	var p = theForm.elements["pwd"];
	var pwd = p.value;
	pwd = pwd.toLowerCase();	// LOWERCASE ONLY
	p.value = pwd;
//	alert ("pwd = "+pwd);
	if (p.value == "")
	{
		pop_err("Please, enter a Password.","pwd");
		return true;
	}
	else
	{
		var patt1 = /\W/g;
		var uu = "";
		uu = pwd.match(patt1);
		if (uu != null)
		{
			pop_err("Pwd must be 6+ Alphanumeric only.","pwd");
			return true;
		}
		if (pwd.length < 6)
		{
			pop_err("Pwd must be 6 characters or more.","pwd");
			return true;
		}
		var rp = theForm.elements["rpwd"];
		var rpw = rp.value;
		rpw = rpw.toLowerCase();
		rp.value = rpw;
//		alert ("rpw: "+rpw);
		if (rpw == "")
		{
			pop_err("Please, repeat your password","rpwd");
			return true;
		}
		if (p.value != rpw && rpw != "")
		{
			pop_err("ERROR: your two passwords are not indentical","rpwd");
			return true;
		}
	}
	return false;
}
function js_check_email(theForm)
{
	var email = theForm.elements["email"].value;
	if (echeck(email))
	{
		pop_err("Please, enter a valid email address.","email");
		return true;
	}
	return false;
}
function js_check_user_form(theForm)
{
//	alert ("Checking username");
	if (js_check_username(theForm.elements['user'].value)) return true;
//	alert ("Checking passwords");
	if (js_check_passwords(theForm)) return true;
//	alert ("Checking email");
	if (js_check_email(theForm)) return true;
//	alert ("All checks passed");
	return false;
}
function js_new_user(theForm)
{
//	showform(theForm,"js_new_user calling js_check_user_form",0);
	if (js_check_user_form(theForm)) return true;
//	alert("registering.");
	var txt = "";
	for (var i=0; i<theForm.elements.length; i++)
		txt += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
	txt += "s="+Sess+"&";
	sajax_target_id = 'popcontents'; 
	x_p_new_user(txt); 
	sajax_target_id = '';
	setTimeout("js_check_logged()",2000);
}
function pop_err(txt,field)
{
//	alert ("pop_err "+txt);
	sajax_target_id = 'poperr'; 
	x_p_pop_err(txt); 
	sajax_target_id = '';
	document.getElementById(field).focus();
}
function js_user_mydetails()
{
	js_pop_open(150,400,350,"mydetails");
}
function js_user_update_details(theForm)
{
//	showform(theForm,"js_user_update_details",0);
	if (theForm.elements['npwd'].value != theForm.elements['rnpwd'].value)
	{
		pop_err("Unequal New Passwords! Try again","pwd");
		return;
	}
	var email = theForm.elements['email'].value;
	if (echeck(email))
	{
		pop_err("Please, enter a valid Email!","email");
		return;
	}
	var txt = "";
	for (var i=0; i<theForm.elements.length; i++)
		txt += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
	txt += "s="+Sess+"&";
	sajax_target_id = 'popcontents';
	x_p_user_update_details(txt); 
	sajax_target_id = '';
	setTimeout("js_pop_close()",3000);
}

// =========================================================================
// === ADMIN
// =========================================================================
// =========================================================================
// === CONTACTS
// =========================================================================
function js_send_contact(theForm)
{
	var txt = "";
	for (var i=0; i<theForm.elements.length; i++)
		txt += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
	txt += "s="+Sess+"&";
	sajax_target_id = 'popcontents';
	x_p_contact_send(txt); 
	sajax_target_id = '';
//	setTimeout("js_pop_close()",3000);

}
// =========================================================================
// === CATEGORIES
// =========================================================================
function js_expand_cat(catname)
{
	alert ("expanding "+catname);
}
// =========================================================================
// === TOS
// =========================================================================

// =========================================================================
// === COMMON
// =========================================================================
function js_sdarken(x,txt)
{
	if (GSPrevId != "")
		GSPrevId.style.backgroundImage = "url(pics/staboff.gif)";	// WORKS ON ALL PASSING "THIS"
	x.style.backgroundImage = "url(pics/stabon.gif)";	// WORKS ON ALL PASSING "THIS"
	var dst = document.getElementById("searching");
	dst.value = txt;
	GSPrevId = x;
}
function js_btn100_down(x)
{
	x.style.backgroundImage = "url(pics/btn100on.png)";
}
function js_btn100_up(x)
{
	x.style.backgroundImage = "url(pics/btn100off.png)";
}
// =========================================================================
// === POP WINDOW
// =========================================================================
function js_pop_open(top,w,h,f)
{
	GetW();
	GetH();
//	alert(Scrw+" x "+Scrh);
	var yScroll = getPageScroll();
//	alert("Page scroll: "+yScroll);
	sajax_target_id = 'pop';
	switch (f)
	{
		case "login":
			x_p_pop_open(Scrw,Scrh,f,Sess); 
			break;
		case "register":
			x_p_register_form(Sess,"new");
			break;
		case "mydetails":
			x_p_user_mydetails(Sess);
			break;
		case "contact":
			x_p_contact_form(Sess);
			break;
		case "tos":
			x_p_tos(Sess);
			break;
		case "ps":
			x_p_ps(Sess);
			break;
	}
	sajax_target_id = '';
	var x = document.getElementById("pop");
	var t = yScroll + top;
	x.style.top = t;
	var l = eval((Scrw - w - 40) / 2);
	x.style.position = "absolute";
	x.style.left = l;
	x.style.width = w+40;
	x.style.height = h+40;
	sajax_target_id = 'pop'; 
	x.style.visibility = "visible";
}
function getPageScroll()
{
	var yScroll;
	if (self.pageYOffset) 
		yScroll = self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	else if (document.body) // all other Explorers
		yScroll = document.body.scrollTop;
	return yScroll;
}
function js_pop_close()
{
	var x = document.getElementById("pop");
	x.style.visibility = "hidden";
	sajax_target_id = 'pop';
	x_p_pop_clear(); 
	sajax_target_id = '';
}