//////////////////////////////////////////////////
// General Common Functions Library				//
//////////////////////////////////////////////////

// Debug Error Alert
function WB_Error(e){
	if (WB_enable_debug) alert("ERROR : \n" + e);
	//else window.location = "index.php";
}
// Check If Cookies Are Accepted
function WB_CookiesAccepted(alertMessage){
	try {
		var ca = document.cookie;
		if(!ca && alertMessage) alert(alertMessage);
		return ca;
	}catch(e){WB_Error(e);}
}
// Set Focus to given Element
function WB_SetFocus(id){
	try {document.getElementById(id).focus();}
	catch(e){WB_Error(e);}
}
// Toggle Visiblity of Element
function WB_ToggleVisibility(id){
	try {
		var el = document.getElementById(id);
		el.style.visibility = (el.style.visibility=="visible") ? "hidden" : "visible";
	}catch(e){WB_Error(e);}
}
// Alert Box Display
function WB_AlertBox(timer){
	box = document.getElementById("alertBox");
	box.style.left=Math.floor((document.body.clientWidth/2)-(parseInt(box.style.width)/2))+"px";
	box.style.top=Math.floor((document.body.clientHeight/2)-(parseInt(box.style.height)/2))+"px";
	box.style.display = "block";
	window.setTimeout('document.getElementById("alertBox").style.display="none"',timer*1000);
}
//////////////////////////////////////////////////
// Form Fields Validation						//
//////////////////////////////////////////////////

// Remove Field Value Spaces
function WB_Field_RemoveSpaces(fld){
	try{
		fld.value = String(fld.value).replace(/^[\s]+/, "");
		fld.value = String(fld.value).replace(/[\s]+$/, "");
	}catch(e){WB_Error(e);}
}
// String Length
function WB_Valid_StringLength(str, min, max){
	try{
		var sl = String(str).length;
		if (!max || max<min) return (sl >= min);
		else return (sl >= min && sl <= max);
	}catch(e){WB_Error(e);}
}
// Email Format
function WB_Valid_Email(str){
	try{
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		return filter.test(str);
	}catch(e){WB_Error(e);}
}
// Password Format
function WB_Valid_Password(str){
	try{
		var filter=/^[\w_]{6,20}$/i;
		return filter.test(str);
	}catch(e){WB_Error(e);}
}
// Remove Search Wildcards Characters
function WB_Field_RemoveWilcards(fld){
	try{
		var str = new String(fld.value),
			upd = false;
		while (str.substr(0,1)=="?") str = str.substr(1), upd=true;
		while (str.substr(0,1)=="*") str = str.substr(1), upd=true;
		if(upd) fld.value = str;
	}catch(e){WB_Error(e);}
}
//////////////////////////////////////////////////
//	POPUPS										//
//////////////////////////////////////////////////
var popUps = new Array(),
	popUpsBlockerDelay = 2000;
// Open PopUp
function openWindow(url,id,w,h,reload){
	if (popUps[id] && !popUps[id].closed) {
		if(reload!=null) popUps[id].location = url;
		popUps[id].focus();
	}
	else {
		popUps[id] = window.open(url,id,"left="+Math.ceil((screen.width-w)/2)+",top="+Math.ceil((screen.height-h)/2)+",width="+w+",height="+h+",location=no,menubar=no,resizable=yes,scrollbars=auto,status=no,titlebar=no,toolbar=no");
		if(popUps[id]!=null && !popUps[id].closed) popUps[id].focus();
		window.setTimeout("if(popUps['"+id+"']==null)alert(_AlertPopUpBlocker);",popUpsBlockerDelay);
	}
}
// Open Full Screen PopUp
function fullScreen(url,id,reload){
	if (popUps[id] && !popUps[id].closed) {
		if(reload!=null) popUps[id].location = url;
		popUps[id].focus();
	}
	else {
		popUps[id] = window.open(url,id,"type=fullWindow,fullscreen=yes,left=0,top=0,width=" + screen.availWidth + ",height=" + screen.availHeight + ",location=no,menubar=no,resizable=no,scrollbars=auto,status=no,titlebar=no,toolbar=no");
		if(popUps[id]!=null && !popUps[id].closed) popUps[id].focus();
		window.setTimeout("if(popUps['"+id+"']==null)alert(_AlertPopUpBlocker);",popUpsBlockerDelay);
	}
}
// Close Window (optional id = popUp ID)
function closeWindow(id){
	if (id==null) window.close();
	else if (popUps[id] && !popUps[id].closed) {
		popUps[id].close();
		popUps[id] = null;
	}
}
// Check Opener Status (close child if closed)
function checkOpener(){
	if(!opener || opener.closed) closeWindow();
	else window.setTimeout("checkOpener();",2000);
}
