//functions.js

function menuClick(menuItem)
{
	ele = document.getElementById('menu_'+menuItem);
	menuEle = document.getElementById('menu');
	//menu = ele.parentNode;
	for( i=0; i<menuEle.children.length; i++ ){
		liEle = menuEle.children[i];
		spanEle = liEle.children[0];
		aEle = spanEle.children[0];
		if( liEle == ele ){
			spanEle.className = 'pr_left';
			aEle.className = 'pr_right';
		}
		else{
			spanEle.className = '';
			aEle.className = '';
		}
	}
	if( menuItem == 'books' ){
		makesearchrequest('*','*');
	}
	else{
		getContent(menuItem);
	}
}
function getContent(id)
{
	makerequest('content.php?cont_id='+id,'content_side');	
}
function makesearchrequest(cat1,cat2) {
	sobj = document.getElementById("searchTextBox");
	stTxt = document.getElementById("searchTextBox").value;
	saTxt = document.getElementById("searchAuthor").value;
	//if (searchText == '')
	//	searchText = '*';
		
	
	if (cat1==undefined)
		cat1 = document.cat1;
	if (cat2==undefined)
		cat2 = document.cat2;
	
	
	if (cat1 == undefined)
		cat1 = '*';
		
	if (cat2 == undefined)
		cat2 = '*';
	
	document.cat1 = cat1;
	document.cat2 = cat2;
 
	makerequest('books.php?search=' + stTxt + '&author=' + saTxt + '&cat1=' + cat1 + '&cat2=' + cat2,'content_side');
}

//Create a boolean variable to check for a valid IE instance.
var xmlhttp = createXMLHTTP();

function createXMLHTTP()
{
	xmlhttp = false;
	//Check if we are using IE.
	try {
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	//If not, then use the older active x object.
	try {
		//If we are using IE.
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
		//Else we must be using a non-IE browser.
		xmlhttp = false;
		}
	}
	//If we are using a non-IE browser, create a JavaScript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}


function makerequest(serverPage, objID) {
	var obj = document.getElementById(objID);
	
	//alert (serverPage);
	
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					
				obj.innerHTML = xmlhttp.responseText;
				child = obj.firstChild;
				
				while ( child )
				{
					if ( child.id == "code" )
						eval(child.innerHTML);

					child = child.nextSibling;
				}
				
			}
		}
	xmlhttp.send(null);
}

function postrequest(serverPage) {
	
	xmlhttp.open("POST", serverPage,true);
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				alert ( xmlhttp.responseText );
			}
		}
	xmlhttp.send(null);
}

//xmlhttp.js
//Function to create an XMLHttp Object.
function getxmlhttp (){
	//Create a boolean variable to check for a valid Microsoft active x instance.
	var xmlhttp = false;
	//Check if we are using internet explorer.
	try {
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	//If not, then use the older active x object.
	try {
		//If we are using internet explorer.
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
		//Else we must be using a non-internet explorer browser.
		xmlhttp = false;
	}
	}
	// If not using IE, create a
	// JavaScript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}
//Function to process an XMLHttpRequest.
function processajax (serverPage, obj, getOrPost, str){
	//Get an XMLHttpRequest object for use.
	xmlhttp = getxmlhttp ();
	if (getOrPost == "get"){
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
	} else {
		xmlhttp.open("POST", serverPage, true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(str);
	}
}

//Functions to submit a form.
function getformvalues (fobj, valfunc){
	var str = "";
	aok = true;
	var val;
	//Run through a list of all objects contained within the form.
	for(var i = 0; i < fobj.elements.length; i++){
		if(valfunc) {
			if (aok == true){
				val = valfunc (fobj.elements[i].value,fobj.elements[i].name);
				if (val == false){
					aok = false;
				}
			}
		}
		str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
	}
	//Then return the string values.
	return str;
}
function submitform (theform, serverPage, objID, valfunc){
	var file = serverPage;
	var str = getformvalues(theform,valfunc);
	//If the validation is ok.
	if (aok == true){
		obj = document.getElementById(objID);
		processajax (serverPage, obj, "post", str);
	}
}

function makemultirequest(serverPage, objID) {
	obj = document.getElementById(objID);
	
	obj.xmlhttp = createXMLHTTP();
		
	obj.xmlhttp.div = obj;
	
	obj.xmlhttp.open("GET", serverPage);
	obj.xmlhttp.onreadystatechange = function() {
			if (this.readyState == 4 && this.status == 200) {
			//alert ("Ready" + xmlhttp.readyState);
				this.obj.innerHTML = this.xmlhttp.responseText;
				alert (this.obj.firstChild.outerHTML);
				if ( this.obj.firstChild.code )
				{
					alert ('code');
				}
				
			}
		}
	obj.xmlhttp.send(null);
}


function makepopuprequest(serverPage)
{
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			actionpopuprequest(xmlhttp.responseText);
			}
		}
	xmlhttp.send(null);
}

function actionpopuprequest(responseText)
{
	
	overlay = createOverlay();
	
	popup = document.createElement('div');
	popup.id = 'popup';
	popup.className = 'popup';
	popup.innerHTML = responseText;
	document.body.appendChild( popup );
	
	overlay.popupDiv = popup;
	
	popup.style.position = 'absolute';
	
	//alert (document.scroll);
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	scrollOffsets = document.viewport.getScrollOffsets();
	
	popup.style.top = ( scrollOffsets[1] + 100 ) + 'px';
	popup.style.left = ( scrollOffsets[0] + 100 ) + 'px';
	popup.style.borderStyle = 'solid';
	popup.style.borderColor = 'white';
	popup.style.borderWidth = '10px';
	popup.style.backgroundColor = 'white';
	
	//ele.onmousedown = pickup;
	//ele.onmouseup = drop;
	//overlay.onmousemove = overlayTrackMouse;
}

function scrollingDetector(){

	alert(navigator.appName + "You've scrolled to " + document.body.scrollTop + " pixels.\rYou've scrolled to " + window.pageYOffset + " pixels.\r" + document.viewport.getScrollOffsets() );
}




var pickedUp;
var offsetX;
var offsetY;

function pickup()
{
	window.status = "pickup";
	pickedUp = this;
	
	document.onmousemove = dragIt;
    var offsetX = window.event.offsetX - document.body.scrollLeft;
    var offsetY = window.event.offsetY - document.body.scrollTop;

	//this.style.backgroundColor = 'blue';
	
}

function drop()
{

	//this.style.backgroundColor = 'white';
}

function shiftTo(obj, x, y) {
    obj.style.left = x & 'px';
    obj.style.top = y & 'px';
 
}

function popupClick()
{
	 this.popupDiv.parentNode.removeChild(this.popupDiv);
	 this.parentNode.removeChild(this);
}



//A variable used to distinguish whether to open or close the calendar.
var showCalendar = true;
function showHideCalendar() {
	//The location we are loading the page into.
	var objID = "calendar";
	//Change the current image of the minus or plus.

	//alert ("Show Hide");
	
	if (showCalendar == true){
		//Show the calendar.
		document.getElementById("opencloseimg").src = "images/mins.gif";
		//The page we are loading.
		var serverPage = "calendar.php";
		//Set the open close tracker variable.
		showCalendar = false;
		var obj = document.getElementById(objID);
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					window.status = "Ready";
					obj.innerHTML = xmlhttp.responseText;
				}
			}
		xmlhttp.send(null);
	} else {
		//Hide the calendar.
		document.getElementById("opencloseimg").src = "images/plus.gif";
		showCalendar = true;
		document.getElementById(objID).innerHTML = "aaaaaa";
	}
}

//function addtocart(prod_code){
//	makerequest("addtocart.php?prod_code="+prod_code,"cart_div");
//}

function addToBasket(id,title,author,price){
	makerequest("adjustcart.php?act=add&id="+id+"&price="+price+"&title="+escape(title)+"&author="+escape(author),"cart_div");
}

function removeFromBasket(id){
	makerequest("adjustcart.php?act=remove&id="+id, "cart_div");
}

function viewBasket(){
	makepopuprequest("viewcart.php");
}


function getPageSize() {
	        
	 var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

function createOverlay()
{
	pageSize = getPageSize();
	
	overlay = document.createElement('div');
	overlay.onclick = popupClick;
	overlay.id = "overlay";
	document.body.appendChild(overlay);
	
	overlay.style.backgroundColor = "black";
	overlay.style.opacity = '0.5';
	overlay.style.filter = 'alpha(opacity=50)';
	overlay.style.position = "absolute";
	overlay.style.top = 0;
	overlay.style.left = 0;
	overlay.style.width = pageSize[0] + 'px';
	overlay.style.height = pageSize[1] + 'px';
	
	return overlay;
}

function absoluteTop( ele )
{
	aTop = ele.offsetTop;
	oEle = ele.offsetParent;
	while ( oEle != undefined )
	{
		aTop = aTop + oEle.offsetTop;
		oEle = oEle.offsetParent;
	}
	return aTop;
}

function absoluteLeft( ele )
{
	aLeft = ele.offsetLeft;
	oEle = ele.offsetParent;
	while ( oEle != undefined )
	{
		aLeft = aLeft + oEle.offsetLeft;
		oEle = oEle.offsetParent;
	}
	return aLeft;
}


function editcontent()
{
	bEdit = false;
	ele = window.event.srcElement;
	
	if ( ele.children.length == 0 )
	{
		bEdit = true;
	}
	else
	{
		bEdit = true;
	}
	
	if ( bEdit )
	{
		dataEle = ele;
		while ( dataEle != undefined )
		{
			if ( dataEle.id.substr(0,12) == "dataContent_" )
				break;
			dataEle = dataEle.parentElement;
		}
		
		if ( dataEle != undefined )
		{
			overlay = createOverlay();
			overlay.onclick = editContentClick;
			
			popup = document.createElement('textarea');
			
			popup.id = 'popup';
			popup.className = 'popup';
			popup.editEle = ele;
			popup.dataEle = dataEle;
			
			document.body.appendChild( popup );
			
			overlay.popupDiv = popup;
			
			popup.style.position = 'absolute';
			
			popup.style.top = absoluteTop( ele ) + 'px';
			popup.style.left = absoluteLeft( ele )  + 'px';
			popup.style.width = ele.offsetWidth + 'px';
			popup.style.height = ele.offsetHeight + 'px';
			popup.style.backgroundColor = 'white';
			popup.innerText = ele.innerHTML;
			
			//nicEditors.allTextAreas();
			
		}
	}
}

function editContentClick()
{
	this.popupDiv.editEle.innerHTML = this.popupDiv.value;
	
	contentSave(this.popupDiv.dataEle.id.substr(12), this.popupDiv.dataEle.innerHTML);
	
	this.popupDiv.parentNode.removeChild(this.popupDiv);
	this.parentNode.removeChild(this);
}

function executeSQL(sqlText)
{
	formEle = document.createElement("form");
	formEle.style.position = "absolute";
	textEle = document.createElement("textarea");
	textEle.name = "sqlText";
	textEle.value = sqlText;
	formEle.appendChild(textEle);
	document.body.appendChild(formEle);

	submitform(formEle,'executeSQL.php','content_div');

}



function contentSave(contentID, contentHTML)
{
	
	formEle = document.createElement("form");
	formEle.style.position = "absolute";
	formEle.style.top = 0;
	formEle.style.left = 0;
	
	inpEle = document.createElement("input");
	inpEle.name = "contentID";
	inpEle.value = contentID;
	formEle.appendChild(inpEle);
	
	textEle = document.createElement("textarea");
	textEle.name = "contentHTML";
	textEle.value = contentHTML;
	formEle.appendChild(textEle);
	
	//document.body.appendChild(formEle);

	submitform(formEle,'contentSave.php','content_div');
	

}

