/* =========================================================================

JavaScript Source File -- Created with SAPIEN Technologies PrimalScript 3.1

NAME: 
 
AUTHOR: Mark J. Roberts , BOF, Inc.
DATE  : 9/14/2006

COMMENT: 

============================================================================ */


var jsevent;

var mouseX = 0;
var mouseY = 0;

var dwin = null;
var debug_ctr = 0;
var debugmode = false;

var joinRequest;

function joinList() {
	var url = "./ajax/main.ajax.php";
	var email = getElement("ml_email");
	if (echeck(email.value) ) {
		req = "submitted=1&action=join&email="+email.value;
		joinRequest = GetXmlHttpObject(cbJoinList);
		joinRequest.open("post",url,true);
		AddPost(joinRequest);
		joinRequest.send(req);

	} else {
		var status = getElement("ml_status");
		status.style.fontWeight = "bold";
		status.style.color = "red";
		replaceText(status,"Invalid E-Mail Address");
	}
	
}

function cbJoinList() {
	if ( CheckState(joinRequest) ) {
		//alert(joinRequest.responseText);
		var json = joinRequest.responseText.parseJSON();
		
		var status = getElement("ml_status");
		status.style.fontWeight = "bold";
		
		if (json['status'] == 1 ) {
			replaceText(status,"Successfully joined the mailing list.");
			status.style.color = "green";
		} else {
			replaceText(status,json['msg']);
			status.style.color = "red";
		}
	}
}

function clearNulls() {
	var inputs = document.getElementsByTagName("input");
	
	if (navigator.userAgent.indexOf("MSIE")>=0){ 

		for ( var i = 0; i < inputs.length; i++ ) {
			var inpt = inputs[i];

			if ( inpt.value == null || inpt.value == "null" ) {
				inpt.value = "";
			}
		}

	}

}


function debug(msg) {
	
	if ( debugmode ) {
	    if ((dwin == null) || (dwin.closed) || debug_ctr == 0) {
			debug_ctr = 0;
			
			dwin = window.open("","debugconsole","scrollbars=yes,resizable=yes,height=300,width=600");
			dwin.document.close();
			dwin.document.open("text/html", "replace");
			dwin.document.write("<pre>");
			dwin.document.writeln("DEBUG: "+document.location);
	    }

	    debug_ctr++;

	    dwin.document.writeln(debug_ctr+ ": " + msg);
	    dwin.scrollTo(0,10000);
	    // dwin.focus();
	    // dwin.document.close();  // uncomment this if you want to see only last message , not all the previous messages
	}
}


function hideSelects() {
	var sels = document.getElementsByTagName("select");
	
	for (var i=0; i<sels.length; i++ ) {
		var sel = sels[i];
		
		sel.style.display="none";
	}
}

function showSelects() {
	var sels = document.getElementsByTagName("select");
	
	for (var i=0; i<sels.length; i++ ) {
		var sel = sels[i];
		
		sel.style.display="";
	}	
}

function setMousePosition(event) {
	
	if (event == null ) {
		event = window.event;
		// alert("is null");
	}
	
	mouseX = event.clientX + document.body.scrollLeft;
	mouseY = event.clientY + document.body.scrollTop;

}

function showElement(id) {
	var obj = document.getElementById(id)
	obj.style.display="";
	obj.style.zIndex=5;
	
	return obj;
}

function setChecks(name,value) {
	var eles = document.getElementsByName(name);
	
	for ( var i = 0; i < eles.length; i++ ) {
		eles[i].checked = value;
	}
}

function selectOption(sel, value) {
	for (i=0;i<sel.options.length;i++) {
		var opt = sel.options[i];
		
		if ( opt.value == value ) {
			sel.selectedIndex=opt.index;
			return true;
			break;
		}
	}
}

// hides Element
function hideElement(id) {
	document.getElementById(id).style.display="none";
}

// returns reference to element
function getElement(id){
	return document.getElementById(id);
}

function appendText(el, text ) {
	if ( el != null ) {
		var txt = document.createTextNode(text);
		el.appendChild(txt);
	}
}

// replace element's text
function replaceText(el, text) {
	if ( el != null ) {
		clearText(el);
		var newNode = document.createTextNode(text)
		el.appendChild(newNode);
	}
}

// clear thje ext of the element
function clearText(el) {
	if ( el != null ) {
		if ( el.childNodes ) {
			for ( var i = (el.childNodes.length-1);i>=0; i-- ) {
				var childNode = el.childNodes[i];
				el.removeChild(childNode);
			}
		}
	}
}

// return text of an element
function getText(el) { 
	var text = "";
	
	if ( el != null ) {
		if ( el.childNodes) {
			for ( var i = 0; i < el.childNodes.length; i++ ) {
				var childNode = el.childNodes[i];
				if ( childNode.nodeValue != null ) {
					text += childNode.nodeValue;
				}
			}
		}
	}
	
}

function showBusy(caption) {
	disableScreen("#ffffff",.85);

	if (navigator.userAgent.indexOf("MSIE")>=0){ 
		hideSelects();
	}

	var div = createElement("div")
	div.id = "status_guy";
	div.align="center";
	div.style.postion="absolute";
	div.style.left="0px";
	div.style.top="0px";
	
	var img = createElement("img");
	img.src = "./images/bigrotation2.gif";
	
	div.appendChild(img);
	
	div.appendChild(createElement("br"));
	
	div.appendChild(document.createTextNode(caption));
	
	document.body.appendChild(div);
	moveElement(div,0,0);
}

function clearBusy() {
	if (navigator.userAgent.indexOf("MSIE")>=0){ 
		showSelects();
	}

	document.body.removeChild(getElement("status_guy"));
	enableScreen();
}

function createElement(tag) {
	return document.createElement(tag);
}

function createAHref(url,text) {
	var a = createElement("a");
	a.href = url;
	replaceText(a,text);
	
	return a;
}

function deleteElement(id,parent) {
	var obj = getElement(id);
	
	parent.removeChild(obj);
}

function moveElement(obj, left, top){
	
	
	obj.style.position="absolute";
	//obj.style.left="0px";
	//obj.style.top="0px";
	
	obj.style.visibility="hidden";
	showElement(obj.id);
	
	if( left == 0 ) { 
		left = (browserWidth()/2)-(elementWidth(obj.id)/2);
	}

	if ( top == 0 ) {
		top = (browserHeight()/2)-(elementHeight(obj.id)/2);
	}

	//hideElement(obj.id);
	// alert("Left: "+left+"\nTop: "+top);

	obj.style.left=left+"px";
	obj.style.top=top+"px";
	
	obj.style.visibility="visible";
	//showElement(obj.id);

}

// highlight a row within a table
function highlightRow(TableId, RowId, Color) {
	var table = document.getElementById(TableId);
	var msg = "";
	
	for ( i = 0; i < table.rows.length; i++ ) {
		row = table.rows[i];
		msg += row.id + "\n";

		if ( row.id.indexOf(RowId) > -1 ) {
			row.bgColor = Color;
			break;
		}
	}	
	
}

/* ========================================================================= 
AJAX Stuff
========================================================================= */
function GetXmlHttpObject(handler) { 
	var objXmlHttp=null

	/*
	if (navigator.userAgent.indexOf("Opera")>=0) {
		alert("This example doesn't work in Opera") 
		return 
	}
	*/

	if (navigator.userAgent.indexOf("MSIE")>=0){ 
		var strName="Msxml2.XMLHTTP"
	
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0) {
			strName="Microsoft.XMLHTTP"
		} try { 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} catch(e) { 
			alert("Error. Scripting for ActiveX might be disabled") 
			return 
		} 
	} 

	if (navigator.userAgent.indexOf("Mozilla")>=0 || navigator.userAgent.indexOf("Opera")>=0) {
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
} 

function AddPost(obj) {
	obj.setRequestHeader('Content-type','application/x-www-form-urlencoded'); 
}

function CheckState(o) {
	if (o.readyState==4 || o.readyState=="complete") { 
		return true;
	}
}

function browserWidth() {
	if ( window.innerWidth > 0 ) {
		return window.innerWidth; 
	} else if ( document.body.clientWidth > 0 ) {
		return document.body.clientWidth;
	} else if ( document.documentElement.clientWidth > 0 ) {
		return document.documentElement.clientWidth;
		
	}
}

function browserHeight() {
	if ( window.innerHeight> 0 ) {
		return window.innerHeight; 
	} else if ( document.body.clientHeight > 0 ) {
		return document.body.clientHeight;
	} else if ( document.documentElement.clientHeight > 0 ) {
		return document.documentElement.clientHeight;
		
	}
}

function elementWidth(id) {
	var element;
	if (document.all)
		element = document.all[id];
	else if (document.getElementById)
		element = document.getElementById(id);
	
	if (element) {
		return element.offsetWidth
	} 
}

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 false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}


function addBreak(obj) {
	obj.appendChild(createElement("br"));
}

function addListItem(ul, text ) {
	var li = createElement("li");
	if ( text == undefined ) {
		text = "";
	}
	
	replaceText(li,text);
	ul.appendChild(li);
	
	return li;
}

function elementHeight(id) {
   var element;
   if (document.all)
     element = document.all[id];
   else if (document.getElementById)
     element = document.getElementById(id);
   if (element) {
     return element.offsetHeight
   } 
}

function disableScreen ( color, opacity ) {
	
	var div = createElement("div");
	div.id = "pageDisabler";
	div.style.position = "absolute";
	div.style.left="0px";
	div.style.top="0px";
	
	//alert("Body.Width: "+elementWidth(body.id)+"\n"+"Body.height: "+elementHeight(body.id));

	div.style.width=pageWidth()+"px";
	div.style.height=pageHeight()+"px";
	div.style.backgroundColor = color;
	div.style.opacity = opacity
	div.style.filter="alpha(opacity="+(opacity*100)+")";

	document.body.appendChild(div);

}

function enableScreen() {
	var div = getElement("pageDisabler");
	try {
		document.body.removeChild(div);
	} catch (err) {
		
	}
}

function pageWidth() {
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight

	if (test1 > test2) { // all but Explorer Mac
		return document.body.scrollWidth;
	} else { // Explorer Mac would also work in Explorer 6 Strict, Mozilla and Safari
		return document.body.offsetWidth;
	}

}

function pageHeight() {
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight

	if (test1 > test2) { // all but Explorer Mac
		return document.body.scrollHeight;
	} else { // Explorer Mac would also work in Explorer 6 Strict, Mozilla and Safari
		return document.body.offsetHeight;
	}		
}