// W2LIB.JS

// GLOBALS:
var Sess = "0";
var Dip = "";
var GUser = "";
var CurPage = "";
var HOMEPAGE = "http://www.web2coders.com/samples/ashleesays/";
var SavedTitle = "";
var SavedPage = "";
var Feedno = 1;
var PrevT = 0;

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);					
}
// ===================================================================================================================
// === MAIN
// ===================================================================================================================
function js_home()
{
	window.location = HOMEPAGE;
}
function js_jumpto(section)
{
//	alert ("js_jumpto:"+section);
	sajax_target_id = 'main'; 
	x_p_jumpto(Sess,section); 
	sajax_target_id = '';
}
// ===================================================================================================================
// === 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 SessCookie()
{
	var rts = getCookie('assess');
	if (rts != null && rts != "")	// cookie exists: we use that session instead (but Php does NOT know)
		Sess = rts;
	else
		setCookie('assess',Sess,1);	// Cookie lasts for 1 day
}
function DelSessCookie()
{
	setCookie("assess","",-1);
}
// ===================================================================================================================
// === RSS FEEDS
// ===================================================================================================================
function js_start_feeds()
{
	Feedno = 17;
//	setInterval("js_next_feed()", 30000);
}
function js_next_feed()
{
	Feedno++;
	if (Feedno == 26)
		Feedno = 17;
//	alert("Changing feed to: "+Feedno);
	sajax_target_id = 'rssfeed'; 
	x_p_get_rss(Feedno); 
	sajax_target_id = '';
}
// ===================================================================================================================
// === CONTACTS
// ===================================================================================================================
function js_send_contact(theForm)
{
	showform(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 = 'rightpage'; 
	x_p_send_contact(txt); 
	sajax_target_id = '';
}
// ===================================================================================================================
// === LOGIN
// ===================================================================================================================
function js_login(theForm)
{
//	showform(theForm,"js_login",0);
	if (theForm.elements['user'] == "")
		return;
	if (theForm.elements['pwd'] == "")
		return;
	setCookie("assess",Sess,1)
	var txt = "";
	for (var i=0; i<theForm.elements.length; i++)
		txt += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
	txt += "s="+Sess+"&";
//	alert("login: "+txt);
	sajax_target_id = 'login'; 
	x_p_login(txt); 
	sajax_target_id = '';
//	setTimeout("js_check_logged()",2000);
//	setTimeout("js_nav()",2000);
}
function js_logoff()
{
//	alert("Log-off");
	sajax_target_id = 'login'; 
	x_p_logoff(Sess); 
	sajax_target_id = '';
	DelSessCookie();
//	alert ("CurPage: "+CurPage);
	if (CurPage != "home")
		window.location = HOMEPAGE;
}
function js_register()
{
//	alert ("js_register");
	sajax_target_id = 'main'; 
	x_p_register(Sess); 
	sajax_target_id = '';
}
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 lowercase 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 Lowercase 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+ Lowercase 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",0);
	if (js_check_user_form(theForm)) return true;
	var txt = "";
	for (var i=0; i<theForm.elements.length; i++)
		txt += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
	txt += "s="+Sess+"&";
//	alert (txt + "-> to p_new_user");
	sajax_target_id = 'boxcontents'; 
	x_p_new_user(txt); 
	sajax_target_id = '';
//	setTimeout("js_nav()",1700);
}
function js_auto_log()	// just registered, Sess has been written in w2cusers
{
	window.location = HOMEPAGE;
}
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_my_details()
{
	sajax_target_id = 'main'; 
	x_p_my_details(Sess); 
	sajax_target_id = '';
}
function js_update_details(theForm)
{
	showform(theForm,"js_update_details",0);
	if (theForm.elements['npwd'].value != theForm.elements['rnpwd'].value)
	{
		pop_err("Unequal 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+"&";
//	alert(txt);
	sajax_target_id = 'boxcontents'; 
	x_p_update_user_details(txt); 
	sajax_target_id = '';
	setTimeout(js_refresh_main(),3500);
}
function js_refresh_main()
{
	window.location = HOMEPAGE;
}
// ===================================================================================================================
// === BLOG
// ===================================================================================================================
function js_new_thread_form(typ)
{
	sajax_target_id = "new_thread"; 
	x_p_new_thread_form(Sess,typ); 
	sajax_target_id = '';
}
function js_post_new_thread(theForm)
{
	showform(theForm,"js_post_new_thread",0);
	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 = "main"; 
	x_p_post_new_thread(txt); 
	sajax_target_id = '';
}
function js_show_comments(tref)
{
	if (PrevT != 0)
	{
		sajax_target_id = PrevT; 
		x_p_clear_thread(); 
		sajax_target_id = '';
	}
	sajax_target_id = "comt"+tref; 
	x_p_show_comments(Sess,tref); 
	sajax_target_id = '';
	PrevT = "comt"+tref;
}



function js_submit_comment(theForm)
{
//	showform(theForm,"js_submit_comment",0);
	var x = theForm.elements['comt'];
	if (x.value == "")
		return;
	var id = theForm.elements['tref'].value;
	var txt = "";
	for (var i=0; i<theForm.elements.length; i++)
		txt += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
	txt += "s="+Sess+"&";
//	alert("Id: "+id);
	sajax_target_id = id; 
	x_p_submit_comment(txt); 
	sajax_target_id = '';
}

function js_post_agony(theForm)
{
	showform(theForm,"js_post_agony",0);
	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 = "theBlog"; 
	x_p_post_agony(txt); 
	sajax_target_id = '';
}
function js_view_answer(tref)
{
	if (PrevT != 0)
	{
		sajax_target_id = PrevT; 
		x_p_clear_thread(); 
		sajax_target_id = '';
	}
	sajax_target_id = tref; 
	x_p_view_answer(Sess,tref); 
	sajax_target_id = '';
	PrevT = tref;
}
function js_agony_answer(tref)
{
	sajax_target_id = "reply"+tref; 
	x_p_agony_answer(Sess,tref); 
	sajax_target_id = '';
}
function js_post_agony_ans(theForm)
{
	showform(theForm,"js_post_agony_ans",0);
	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 = "theBlog"; 
	x_p_post_agony_ans(txt); 
	sajax_target_id = '';
}