// JavaScript Document
function subMain(){
	
	if(document.getElementById("subMenus")) {
		
		/*set_links(); */
		/*init_ae();*/
	}
	
	/*if(document.getElementById("product_scroll")) {
		var navigation = document.getElementById('scroll_ctrl');
		var links = navigation.getElementsByTagName("a");
			links[0].onclick = function() { moveElement2("canvas", 136, 20) }
			links[1].onclick = function() { moveElement2("canvas", -136, 20) }
	}*/
}

function defaultFormcheck(no){
	var ret=true;	
	for(var f=0;f<document.forms[no].elements.length; f++){
		if(document.forms[no].elements[f].value=="") ret=false;
	}
	if(ret==false) alert("Please fill in all of the fields please.");
	return ret;
}
function validateForm(whichform){
	ret=true;
	for(var i=0; i<whichform.elements.length; i++){
		var element = whichform.elements[i];
		
		
		if(element.className.indexOf("required")!= -1){
			if(whichform.elements[i].value=="") ret=false;
		}
		
	}
 
 	if(!ret) alert("Please fill in of the required fields");
 	return ret;
	
}
function writeToLayer(id, sHTML) {

// document.getElementById Returns a reference to the first object with the specified value of the ID attribute.

  var el = document.getElementById(id);
  var cntnt = sHTML ; // apply a style to the new text
  if (typeof el.innerHTML!="undefined") {
      el.innerHTML = cntnt;				// this is how IE needs it
  } else if (document.layers) {
			el.document.write(cntnt);	// otherwise this method
			el.document.close();
  }
}


/* ------------------------------------------------------------ */
function set_links(){
		var navigation = document.getElementById('subMenus');
		var links = navigation.getElementsByTagName("a");
		
		for(var i=0; i<links.length;i++) {
				links[i].onclick = function() { submenus(this); return false; }
		}
}

function init_ae(){
	if(window.XMLHttpRequest){
		http_request = new XMLHttpRequest()
	}else if (window.ActiveXObject){
		http_request = new ActiveXObject("Microsoft.XMLHTTP")
	}
	
}
function submenus(whichlink){
 
 		var navigation = document.getElementById('mainNav');
		var links = navigation.getElementsByTagName("a");
		for(var i=0; i<links.length;i++) {
			var id = links[i].setAttribute("id",""); /* clear all links */	
		}
	
	var href = whichlink.getAttribute("href");
	var els = href.split("/");
	var param = els[els.length - 1];
	
	pairs = param.split("&");
	for(var f=0;f<pairs.length;f++){
		var temp = pairs[f].split("=");
		pairs[f] = temp[1];
	}
	

	retstr = "?id=" + pairs[0];
	
	if(pairs[1]) retstr = retstr + "&parent=" + pairs[1];
		
		
			var sstr = "subs.php" + retstr;
			getit(sstr,0);
			
				
		/*	if(pairs[1]){
			
				var sstr = "products.php" + retstr;
				getit(sstr,1);
				
			}*/
}

function getit(sstr,id){
			
			http_request.open("GET", sstr, true); 
            if(id) {
					http_request.onreadystatechange = handleHttpResponse2; 
			}else{
					http_request.onreadystatechange = handleHttpResponse; 
			}
            http_request.send(null);
}
function productinfo(whichlink){
		
		var href = whichlink.getAttribute("href");
		var ar = href.split("=");
		if(ar[3]!= -1){
			
			
		var retstr = "?id=" + ar[1] + "&parent=" + ar[2] + "&pid=" + ar[3];
		var sstr = "products.php" + retstr;
		getit(sstr,1);
		}else{
				
			self.location="add2cart.php?id=" + ar[1] + "&qty=" + ar[2];
			
		}
}
function handleHttpResponse() { 
        if (http_request.readyState == 4 && http_request.status==200) {
			
                var results=http_request.responseText; 
				var descr = document.getElementById("subMenus");
				descr.innerHTML = results;
				/*set_links();*/
				
				var sstr = "products.php" + retstr;
				getit(sstr,1);
        } 
	
}
function handleHttpResponse2() { 
        if (http_request.readyState == 4 && http_request.status==200) { 
			
                var results=http_request.responseText; 
				var descr = document.getElementById("product");
				descr.innerHTML = results;
				set_links();	
				
				/* now we need the other items in category */
				http_request.open("GET", "otheritems.php", true); 
				http_request.onreadystatechange = handleHttpResponse3; 
				http_request.send(null);
				
				
				
        } 
	
}

function handleHttpResponse3() { 
        if (http_request.readyState == 4 && http_request.status==200) {
				document.getElementById("content").innerHTML = http_request.responseText;
				
				/* bread crumb trail */
				http_request.open("GET", "includes/trail.inc.php", true); 
				http_request.onreadystatechange = handleHttpResponse4; 
				http_request.send(null);
				
		}
}
function handleHttpResponse4() { 
        if (http_request.readyState == 4 && http_request.status==200) {
			document.getElementById("crumbs").innerHTML = http_request.responseText;
		}
}

/* -------------------------------------------------------- */
final_y=0;
rows = 0;
cnt = 3;
function moveElement2(elementID, increment, interval){
	
	if(!document.getElementById) return false;
	if(!document.getElementById(elementID)) return false;
	
	var elem = document.getElementById(elementID);
	
	if(elem.movement) clearTimeout(elem.movement);
	
	
	if(!elem.style.left) elem.style.left="0px";
	if(!elem.style.top) elem.style.top="0px";
	
	if(increment!=0) {
		
			if(increment>0){
				
				/* move up */
				if(rows+2 != cnt){
					final_y = final_y - increment;
					rows++;
				}
			}
		
			if(increment<0){
				/* move down */
				if(rows>0){
					final_y = final_y - increment;
					rows--;
				}
			}
			increment = 0;
			
	}
	
	var ypos = parseInt(elem.style.left);
	
	if(ypos == final_y) return true;
	
	
	
	if(ypos < final_y) {
		var dist = Math.ceil((final_y - ypos)/10);
		ypos=ypos+dist;	
	}
	if(ypos > final_y) {
		var dist = Math.ceil((ypos - final_y)/10);
		ypos=ypos-dist;
	}
	
	elem.style.left = ypos + "px";
	
	var repeat = "moveElement2('" + elementID + "'," + increment + "," + interval + ")";
	
	elem.movement = setTimeout(repeat,interval);
}

function ajax(pid){
	http_request.open("GET", "includes/info.inc.php?pid=" + pid, true);
	http_request.onreadystatechange = handleHttpResponse2; 
    http_request.send(null);
	return false;
}
/* ----------------------------------------------------------------- */
function getCookie(){
		if(window.document.cookie)
		{
			var cookiedata = unescape( window.document.cookie );
			var userdata = cookiedata.split(";");
			var name = userdata[0].split("=");
			if(userdata[1]) 
			{
				var email = userdata[1].split("=");
				if(name[0]=="cookiedata") document.writeln(name[1]);
			}
			
		}
}
/* ----------------------------------------------------------------- */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


/* ------------------------------------------------------------------- */
var http_request = false;
addLoadEvent(subMain);