var ALERT_TITLE = "Oops!";
var ALERT_BUTTON_TEXT = "Ok";

/*if(document.getElementById) {
	window.alert = function(txt) {
		createCustomAlert(txt);
	}
}*/
function createCustomAlert(txt) {
	// shortcut reference to the document object
	d = document;
	// if the modalContainer object already exists in the DOM, bail out.
	if(d.getElementById("modalContainer")) return;

	// create the modalContainer div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	// center the alert box
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(ALERT_TITLE));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));
	msg.appendChild(d.createTextNode(txt));

	// create an anchor element to use as the confirmation button.
	btn = alertObj.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
	btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() { removeCustomAlert();return false; }

	
}

// removes the custom alert from the DOM
function removeCustomAlert() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}

function toggleTextOut(e,txtval) {
	if(e.value=="") e.value = txtval;

}
function toggleTextIn(e,txtval) {
	if(e.value==txtval) e.value="";
	else e.select();
}

sfHover = function() {
	if(document.getElementById("nav")!=null){
	var sfEls = document.getElementById("nav").getElementsByTagName("li");


	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";

		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

// email - print

function GetData(id)
{

	var content = document.getElementById(id);
	if(content == null)
		return("");
	return(content.innerHTML);
}
function Print(printPage, id, title)
{
	if(title == '')
		title = document.title;
	var url = printPage + "?id=" + id + "&title=" + title
	var p = window.open(url, "print", "width=610, height=600, left=100, top=100, location=no, menubar=no, resizable=no, scrollbars=yes, status=no, toolbar=no, fullscreen=no");
}

function PrintThisPage(id, printId)
{

	var content = document.getElementById(printId);

	if(content == null)
		return;
	if(window.opener == null)
		return;
	var html = window.opener.GetData(id);
	html = DisableHyperlinks(html);
	content.innerHTML = html;
	window.print();
}

function DisableHyperlinks(html)
{
	return(html.replace(/href\s*=\s*\"*[^\">]*"/ig,"href=\"#\""));
}

/* email specific functions */

function Email(url)
{
	url = url + "&page_title=" + document.title;
	var p = window.open(url, "email", "width=405, height=425, left=100, top=100, location=no, menubar=no, resizable=no, scrollbars=no, status=no, toolbar=no, fullscreen=no");
}
function isValidEmail(str)
	{
		filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		return(filter.test(str));
	}
	function AllowOnly(e,type,allow_space)
	{
		if (allow_space==null) allow_space=false; else allow_space=true;
		if(e.charCode==0) return true;
		var unicode=e.charCode? e.charCode : e.keyCode;
		if(unicode==13) return true;
		if(allow_space && unicode==32)
		return true;
		switch(type)
		{
			case 'NUM_ONLY':
			if(unicode < 48 || unicode > 57) return false; else return true;
			case 'ALPHA_ONLY':
			if(unicode <65 || unicode >90)
			if(!(unicode >= 97 && unicode <=122))
			return false;
			else
			return true;
			case 'ALPHA_NUM_ONLY':
			if(unicode >= 65 && unicode <= 90)

			return true;
			if(unicode >= 97 && unicode <= 120)
			return true;
			if(unicode >= 48 && unicode <= 57)
			return true;
			else
			return false;

		}
	}
function trimAll(sString)
{
		while (sString.substring(0,1) == ' ')
		{
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' ')
		{
			sString = sString.substring(0,sString.length-1);
		}
	return sString;
}

function searchboxValidate(){

	sb = document.getElementById('txtSearchBox').value;
	sb=trimAll(sb);
	if(sb=="Search here.." || sb==""){
	alert('Please enter a search query');
	document.getElementById('txtSearchBox').focus();
	return false;
	}
	return true;
}

function  showPressAdd(url,curDiv,loading_image) {
	divEl = document.getElementById(curDiv);
	divEl.innerHTML='<img src="'+loading_image+'" width="20" height="20" class="loading" />';
	makeRequest(url,curDiv);
}
function makeRequest(url,id) {

	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}

	http_request.onreadystatechange = function() { traffic(http_request,id); };
	http_request.open('GET', url, true);
	http_request.send(null);

}
function traffic(http_request,id) {
	if (http_request.readyState == 4) {
		document.getElementById(id).innerHTML = "";
		if (http_request.status == 200) {
			document.getElementById(id).innerHTML=http_request.responseText;
			document.getElementById(id).className='';
		} else {
			alert('There was a problem with the request.');
		}
	}

}