/****************************************************************************
*
* Copyright 2007 dynamIt Technologies, LLC.
*
* The following code is for the exclusive use of dynamIt Technologies, LLC.
* Any use of this code with written permission from dynamIt is prohibited.
*
* Report an abuse of this copyright to
*	dynamIt Technologies, LLC
*	4612 Sawmill Road
*	Columbus, Ohio 43220 USA
*	+1.614.538.0095
*
****************************************************************************/

/**************************************
* Some basic browser detection
**************************************/
var IE7 = false;
var IE6 = false;
var FF = false;
if (window.XMLHttpRequest) {
	if(document.epando){
		//IE7
		IE7 = true;
	}else{
		//mozilla, safari, opera, etc
		FF = true;
	}
} else {
	// IE6, older browsers
         IE6 = true;
}
/**************************************
* FIX IE 6 bullshit
**************************************/
var dialogOpen = false;
var dialogScroll = 0;
window.onscroll = function() {

        if(IE6) {
                if(dialogOpen) {
                        window.self.scrollTo(0, dialogScroll);
                }
        }

}

function scrollY() {

        var scroll = 0;
        if(document.body.scrollTop) scroll = document.body.scrollTop;
        else if(document.documentElement.scrollTop) scroll = document.documentElement.scrollTop;
        else if(window.pageYOffset) scroll = window.pageYOffset;

        return scroll;
}



/**************************************
* This method will initialize the dynamIt Ajax
* system by placing the
**************************************/
window.onload = dynamItInit;
function dynamItInit() {
	var pos = (IE6) ? "absolute" : "fixed";

	var ca = document.createElement("div");
	var pu = document.createElement("div");
	var sl = document.createElement("div");
	ca.setAttribute("id", "coverAll");
	ca.style.cssText = "position: " + pos + "; width: 1px; height: 1px; top: 0px; left: 0px; background-color: #000000; visibility: hidden; z-index: 1000; opacity: .60; filter: alpha(opacity=60);";
	pu.setAttribute("id", "popUpWin");
	pu.style.cssText = "position: " + pos + "; width: 1px; background-color: white; border: 0px solid #b9b9b9; font-size: 14px; font-weight:bold; visibility: hidden; z-index: 1001; padding: 0px;";
	sl.setAttribute("id", "simpleLoading");
	sl.style.cssText = "color: #cc0000; font-size: 11px; font-weight: bold; background-color: white; border: 1px solid black; padding: 8px; position: absolute; visibility: hidden; opacity: .80; filter: alpha(opacity=80);";

	var body = document.body;
	body.insertBefore(ca, body.firstChild);
	body.insertBefore(pu, ca);
	body.insertBefore(sl, pu);

}


/**************************************
* This method and variables will allow for
* the capturing of the mouse position at any time
**************************************/
document.onmousemove = dynamItMouse;

var xmouse = 0;
var ymouse = 0;
function dynamItMouse(e) {
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		xmouse = e.pageX;
		ymouse = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		xmouse = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		ymouse = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

}

/**************************************
* This method and variables will allow for
* the capturing of the current window size.
**************************************/
var windowWidth;
var windowHeight;
function getWindowSize() {
	if( typeof(window.innerWidth) == 'number' ) {
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	} else {
		windowWidth = screen.width;
		windowHeight = screen.height;
	}
}


/**************************************
* This method creates the object necessary to carry
* out an ajax request. This function will be used by
* the sendRequest and postRequest functions.
**************************************/
function GetXmlHttpObject(handler) {
	var objXMLHttp = null;

	if(window.XMLHttpRequest) {
		objXMLHttp = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	return objXMLHttp;
}


/**************************************
* This method will generate the necessary object
* and carry out the AJAX request via GET.
* url - the script url to execute.
* loadfcn - the function to be called on state change.
**************************************/
function sendRequest(url, loadfcn) {
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

	xmlHttp.onreadystatechange = loadfcn;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


/**************************************
* This method will generate the necessary object
* and carry out the AJAX request via POST.
* url - the script url to execute.
* data -  the url excoded data to send.
* loadfcn - the function to be called on state change.
**************************************/
function postRequest(url, data, loadfcn) {
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

	xmlHttp.onreadystatechange = loadfcn;
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send(data);
}

/**************************************
* This method will return true if the given xmlHttp
* object has a readyState of 'complete'
**************************************/
function isComplete(xmlHttp) {
	return (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete");
}

function isAjaxComplete(xmlHttp) {
	return isComplete(xmlHttp);
}

/**************************************
* This method takes in a reference to an html element
* and returns the element obj. If the parameter passed
* is the object it self, it will simply return it back
* to the caller. Otherwise, this function will attempt
* to retrieve the element by ID. Thus, this method also
* serves as an alias for document.getElementById().
**************************************/
function dynamItElem(obj) {
    try {
        if(typeof(obj) == 'object')
            return obj;
        else
            return document.getElementById(obj);
    }
    catch(e) {
        return null;
    }
}


/************************************
* The following methods deal with showing and hiding
* elements on the page.
************************************/
function dynamItShowMe(objId) {
	dynamItElem(objId).style.display = 'block';
}

function dynamItHideMe(objId) {
	dynamItElem(objId).style.display = 'none';
}

function dynamItToggleMe(objId) {
	if(dynamItElem(objId).style.display == 'none') {
		dynamItShowMe(objId);
	} else {
		dynamItHideMe(objId);
	}
}


/************************************
* The following methods deal with the dynamIt AJAX
* dialog box.
************************************/

function openDialog(html, w, h) {
         if(IE6) {
                hideSelects();
                dialogScroll = scrollY();
        }

		dialogOpen = true;

         var cover = dynamItElem('coverAll');
         cover.style.visibility = 'visible';
	var box = dynamItElem('popUpWin');
         box.style.visibility = 'visible';

         positionDialog(w, h);
         box.innerHTML = html;

}

	function showSelects() {
		for(var i = 0; i < mySelects.length; i++) {
			mySelects[i].style.visibility = "visible";
		}
	}
	var mySelects;
	function hideSelects() {
		mySelects = new Array();
		var sels = document.getElementsByTagName('select');
		for(var i = 0; i < sels.length; i++) {
			sels[i].style.visibility = "hidden";
			mySelects[mySelects.length] = sels[i];
		}
	}

function positionDialog(w, h) {

         var cover = dynamItElem('coverAll');
	var box = dynamItElem('popUpWin');

	w = parseInt(w, 10);
	h = parseInt(h, 10);
	wx = (isNaN(w) || w == 0) ? 'auto' : w + 'px';
	hx = (isNaN(h) || h == 0) ? 'auto' : h + 'px';

	box.style.width = wx;
	box.style.height = hx;

	getWindowSize();

         var left = (windowWidth/2) - (w/2);
         var top = (hx == 'auto') ? 150 : (windowHeight/2) - (h/2);

        if(IE6) {
                top  = top + dialogScroll;
                cover.style.top = dialogScroll + 'px';
        }

	box.style.left = left + 'px';
	box.style.top = top + 'px';

	cover.style.width = '100%';
	cover.style.height = '100%';

	if(IE6) {
		cover.style.height = windowHeight + 'px';
	}

}

function waitDialog(message) {

	var html = '';
	html += '<div class="pane-dlg">';
	html += '	<div class="pane-title"><span>Please wait...<span></div>';
	html += '	<div class="pane-body">';

	html += '<div style="text-align:center"><img src="img/ajax-loader.gif" alt="Loading..." /></div>';
	html += '<div>&nbsp;</div>';
	html += '<div style="text-align:center">' + message + '</div>';

	html += '	</div>';
	html += '	<div class="pane-bottom">&nbsp;</div>';
	html += '</div>';


	var box = dynamItElem('popUpWin');
	box.innerHTML = html;
}

function confDialog(message) {
	var html = '';

	html += '<div class="pane-dlg">';
	html += '	<div class="pane-title"><span>&nbsp;<span></div>';
	html += '	<div class="pane-body">';

	html += '<div>&nbsp;</div>';
	html += '<div style="text-align:center">' + message + '</div>';
	html += '<div>&nbsp;</div>';
         html += '<div style="text-align:center"><input type="button" class="btn" value="   Ok   " onclick="closeDialog();" id="confOkButton" /></div>';

	html += '	</div>';
	html += '	<div class="pane-bottom">&nbsp;</div>';
	html += '</div>';


	var box = dynamItElem('popUpWin');
	box.innerHTML = html;

	dynamItElem('confOkButton').focus()

}

function closeDialog() {
	// We must hide the box and the cover,
	// but also shrink them, otherwise, cursor
	// will not appear in text boxes in Firefox

	var box = dynamItElem('popUpWin');
	box.style.visibility = 'hidden';
	box.style.width = '1px';
	box.style.height = '1px';
	box.style.top = '1px';
	box.style.left = '1px';

	var cover = dynamItElem('coverAll');
	cover.style.visibility = 'hidden';
	cover.style.width = '1px';
	cover.style.height = '1px';

	if(IE6) showSelects();
	dialogOpen = false;

}


/**************************************
* These methods will open up a very simple loading
* box at the mouse position and then close it again
* when done.
**************************************/
function openSimpleLoading() {
	var loadBox = dynamItElem('simpleLoading');
	loadBox.style.visibility = 'visible';
	loadBox.style.left = xmouse + 'px';
	loadBox.style.top = ymouse + 'px';
}

function closeSimpleLoading() {
	var loadBox = dynamItElem('simpleLoading');
	loadBox.style.visibility = 'hidden';
}


/**************************************
* This method given a url will use AJAX to retrieve
* the output at that URL and load them into the
* specificed HTML element
**************************************/
function dynamItLoad(url, pane, w, h, onComplete) {

	var container = dynamItElem(pane);

	if(dynamItElem(pane)) {
                 openSimpleLoading();
	} else {
		openDialog('', 400, 300);
		waitDialog('Loading...Please wait.');
	}

	sendRequest(url, function() {
		if(isComplete(xmlHttp)) {
			if(container) {
				closeSimpleLoading();
				container.innerHTML = xmlHttp.responseText;
			} else {
				openDialog(xmlHttp.responseText, w, h);
			}
			if(onComplete.length) { eval(onComplete); }
		}
	});

}


function dynamItLoadIfEmpty(url, pane, onComplete) {
	var loaded;
	pane = dynamItElem(pane);
	if(pane.innerHTML.length == 0) {
		dynamItLoad(url, pane, null, null, onComplete);
		loaded = true;
	} else {
		loaded = false;
	}
	dynamItShowMe(pane);
	return loaded;
}


/**************************************
* This method take as its parameter and ordinary
* HTML form object and submits it via AJAX
* The script run by the ajax needs to echo
* back a javascript function call and nothing else.
**************************************/
function dynamItPublish(f) {
	var url = f.action;
	var pageData = dynamItFormData(f);

	if (f.method.toUpperCase() == "POST") {
		postRequest(url, pageData, function () {
			if(isComplete(xmlHttp)) {
				setTimeout(xmlHttp.responseText, 1);
			}
		});
	} else {

		url = url + "?" + pageData;
		sendRequest(url, function () {
			if(isComplete(xmlHttp)) {
				setTimeout(xmlHttp.responseText, 1);
			}
		});
	}

	return false;
}

/**************************************
* This method will turn an ordinary form into
* query string data.
**************************************/
function dynamItFormData(f) {
	var e;
	var data = new Array;
	for (var j = 0; j < f.elements.length; j++) {
		e = f.elements[j];
		if (e.name.length) {
			data.push(e.name + "=" + urlencode(e.value));
	        }
	}
	return data.join("&");
}

/**************************************
* This method acts much like the dynamItPublish
* function but will visit a URL rather than
* publish a form. Data sent using GET
**************************************/
function dynamItSend(url) {
	sendRequest(url, function () {
		if(isComplete(xmlHttp)) {
			setTimeout(xmlHttp.responseText, 1);
		}
	});
}

function dynamItSendOnConfirm(url, message) {

	url = str_replace('\'', '\\\'', url);

	var html = '';
	html += '<div class="pane-dlg">';
	html += '	<div class="pane-title"><span>&nbsp;<span></div>';
	html += '	<div class="pane-body">';

	html += '<div style="text-align:center;">' + message + '</div>';
	html += '<div>&nbsp;</div>';
	html += '<div align="center"><input type="button" class="btn" value=" &nbsp; Ok &nbsp; " onclick="dynamItSend(\'' + url + '\');" /> &nbsp; ';
	html += '<input type="button" class="btn" value=" Cancel " onclick="closeDialog();"></div>';

	html += '	</div>';
	html += '</div>';

	openDialog(html);

}

function dynamItLinkOnConfirm(url, message) {
	url = str_replace('\'', '\\\'', url);

	var html = '';
	html += '<div class="pane-dlg">';
	html += '	<div class="pane-title"><span>&nbsp;<span></div>';
	html += '	<div class="pane-body">';

	html += '<div style="text-align:center;">' + message + '</div>';
	html += '<div>&nbsp;</div>';
	html += '<div align="center"><input type="button" class="btn" value=" &nbsp; Ok &nbsp; " onclick="location.href=\'' + url + '\';" /> &nbsp; ';
	html += '<input type="button" class="btn" value=" Cancel " onclick="closeDialog();"></div>';

	html += '	</div>';
	html += '</div>';

	openDialog(html);
}

/**************************************
* These methods aid in dynamically adding and
* removing HTML elements from the page.
**************************************/
function addDiv(container, id, classname, inner) {
	var e = document.createElement('div');

	e.setAttribute('id', id);
	e.className = classname;
	e.innerHTML = inner;

	container = dynamItElem(container);
	container.appendChild(e);
}

function addDivTop(container, id, classname, inner) {
	var e = document.createElement('div');

	e.setAttribute('id', id);
	e.className = classname;
	e.innerHTML = inner;

	container = dynamItElem(container);
	var first = container.firstChild;
	container.insertBefore(e, first);
}

function remDiv(id) {
	var o = dynamItElem(id);

	var p = o.parentNode;
	p.removeChild(o);
}

function moveDiv(divId, newCont) {
	var d = dynamItElem(newCont);
	var o = dynamItElem(divId);
	var oldCont = o.parentNode;
	var o = oldCont.removeChild(o);
	d.appendChild(o);
}

function changeLink(linkId, href, html) {
	var l = dynamItElem(linkId);
	l.setAttribute('href', href);
	l.innerHTML = html;
}

/**************************************
* PHP functions you know and love implemeneted
* in JavaScript.
**************************************/
function str_replace(find, replace, search) {
	return search.split(find).join(replace);
}

function urlencode(str) {
	return encodeURIComponent(str);
}

function substr(string, start, length) {
	l = parseInt(length);
	if(isNaN(l)) {
		return string.substring(start);
	} else {
		return string.substring(start, start + length);
	}
}

function stripslashes(str) {
	str = str_replace('\\\\', '\\', str);
	str = str_replace('\\\'', '\'', str);
	str = str_replace('\\\"', '\"', str);
	return str;
}

function in_array(needle, haystack) {
	for(var j = 0; j < haystack.length; j++) {
		if(needle == haystack[j])
			return true;
	}
	return false;
}

/*****************************
* Method for AJAX scroll pane
*****************************/
function dynamItScroll(id, current, offset) {

	var url = eval(id + 'Action');
	var last = eval(id + 'Last');
	var scrollheight = eval(id + 'ScrollHeight');
	var pane = dynamItElem(id + '_page_' + current);

	if(current == 0 && offset == -1) return current; //cannot scroll up if at the top
	if(current == last && offset == 1) return current; //cannot scroll down if at the bottom

	dynamItHideMe(pane);

	current += offset;
	pane = dynamItElem(id + '_page_' + current);

	url = str_replace('$page', current, url);
//	dynamItLoadIfEmpty(url, pane, 'addDHTML(\'' + url +'&draggable=1\')');
	dynamItScrollPaneLoad(url, id + '_page_' + current);

dynamItPositionScrollBar(id, current, last, scrollheight);

	return current;
}

function dynamItPositionScrollBar(id, current, last, scrollheight) {
	var bar = dynamItElem(id + 'ScrollBar');
	var top = parseInt(current * scrollheight / last);
	bar.style.top = top + 'px';
}

function dynamItScrollPaneLoad(url, pane) {
	var loaded;
	var pane_id = pane;
	pane = dynamItElem(pane);
	if(pane.innerHTML.length == 0) {
		dynamItSend(url + '&pane=' + pane_id);
		loaded = true;
	} else {
		loaded = false;
	}
	dynamItShowMe(pane);
	return loaded;
}

function addDHTML(url) {

	sendRequest(url, function() {
		if(isComplete(xmlHttp)) {
			nm = xmlHttp.responseText.split(',');
			for(var i = 0; i < nm.length; i++) {
				ADD_DHTML(nm[i]);
			}
			hideRated(nm);
		}
	});

}
