/*********************************************************************/
// script.js : JavaScript include file
// zetta.in. All rights reserved.
// Author: Sreekandakumar Pillai, sree@zetta.in
/*********************************************************************/  

/* Functions for AJAX Contact Us Form */
function mailIt(form){
var url = "zettacms/contact-send-mail.php";
var loadingimg = "zettacms/files/themes/zetta../images/loading.gif";
var XHRequest = false;

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

	if(XHRequest){
		XHRequest.open("POST", url);
		XHRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		XHRequest.onreadystatechange = function(){
		if(XHRequest.readyState < 4){
			document.getElementById("status").innerHTML = "<img src='"+loadingimg+"'> Sending ...";
		} else if(XHRequest.readyState == 4 && XHRequest.status == 200){
			document.getElementById("status").innerHTML = XHRequest.responseText;
			delete XHRequest;
			XHRequest = null;
		}
	}
		XHRequest.send("to=" + form.to.value + "&from=" + form.from.value + "&subject=" + form.subject.value + "&body=" + form.body.value);
	}
}

//Expand - Collapse Script from http://willworkforart.net/demos/unobtrusive-navigation/index.html
function init(){
	//Check to see if we can use the DOM
	if(!document.getElementById) return;
	if(!document.getElementsByTagName) return;
	if(!document.createElement) return;
	
	//Get all the UL's in the Navigation
	var navigation = document.getElementById('sidenav');
	var navSub = navigation.getElementsByTagName('ul');
	
	//Go through all the Sub Nav's - give them a hidden class, inject in the toogle graphic
	for (i=0; i<navSub.length; i++){
		
		//Create the Image to inject in
		var toggleImage = document.createElement('img');
		toggleImage.setAttribute('src', '../images/expand.gif');
		toggleImage.style.cursor = "pointer";
		toggleImage.onclick = function() {
			toggleNav(this);
		}

		//Get the Parent of the UL, and insert the Image before the first child
		navSub[i].parentNode.insertBefore(toggleImage, navSub[i].parentNode.firstChild);
		
		//Hide the Sub Navigation using a CSS Class and assign a class to the parent for styling
		navSub[i].style.display="none";
		navSub[i].parentNode.className = "expandable";
	}
	
	var expandLink = document.createElement('li');
	expandLink.innerHTML = "<a href='#' onclick='toggleNav(this)' id='expandAll'>Expand All</a>"
	
	var collapseLink = document.createElement('li');
	collapseLink.innerHTML = "<a href='#' onclick='toggleNav(this)' id='collapseAll'>Collapse All</a>"
		
	//Add them to the Bottom of the Navigation
	navigation.appendChild(expandLink);
	navigation.appendChild(collapseLink);
}

function toggleNav(whichOne){
	if (whichOne.getAttribute('id') == "expandAll") {
		var navigation = document.getElementById('sidenav');
		var navigationULs = navigation.getElementsByTagName('ul');
		var allImages = navigation.getElementsByTagName('img');
		for (i = 0; i < navigationULs.length; i++) {
				navigationULs[i].style.display = "block";
				allImages[i].setAttribute('src', '../images/contract.gif')
		
		}
	}
	else if (whichOne.getAttribute('id') == "collapseAll"){
		var navigation = document.getElementById('sidenav');
		var navigationULs = navigation.getElementsByTagName('ul');
		var allImages = navigation.getElementsByTagName('img');
			for (i = 0; i < navigationULs.length; i++) {
				navigationULs[i].style.display = "none";
				allImages[i].setAttribute('src', '../images/expand.gif')
			}
	}
	else {
		var theParent = whichOne.parentNode;
		var theParentULs = theParent.getElementsByTagName('ul');
		var theParentImage = theParent.getElementsByTagName('img');
		
		//Grab just the first UL and the first toggle image so that sub-sub UL navs/image don't expand too
		if (theParentULs[0].style.display == "none") {
			theParentULs[0].style.display = "block";
			theParentImage[0].setAttribute('src', '../images/contract.gif');
		}
		else {
			theParentULs[0].style.display = "none";
			theParentImage[0].setAttribute('src', '../images/expand.gif');
		}
	}
}

window.onload = init;
