//NAV EDIT FUNCTIONS
	
	function getEventCurrentTarget(e){
		var targ
		
		if (!e) var e = window.event
		
		if (e.currentTarget) targ = e.currentTarget
		
		else if (e.srcElement) targ = e.srcElement
		
		if (targ.nodeType == 3) // defeat Safari bug
			targ = targ.parentNode
			
		return targ;
	}
	
	//SORT ITEM UP
	function moveUp(e){
		moveRow(e, -1);
		return false;
	}

	//SORT ITEM DOWN
	function moveDown(e){
		moveRow(e, 1);
		return false;
	}
	
	
	function sortRows(a, b){
		return a.sort - b.sort;
	}

	function moveRow(e, index){
		var btn = getEventCurrentTarget(e);
		var id = btn.id.replace("btn_", "");
		
		for(var i =0; i < currentSortRows.length; i++){
			if(currentSortRows[i].id == id){
				var tmp = currentSortRows[i].sort;
				currentSortRows[i].sort = currentSortRows[i + index].sort;
				currentSortRows[i + index].sort = tmp;
				break;
			}
		}
		
		currentSortRows.sort(sortRows);
		
		var tbl = document.getElementById("tblSort");
		
		for(var i= tbl.rows.length - 1; i >=0; i--){
			tbl.deleteRow(i);
		}
		
		var tr, td, btn, a, img;
		
		for(var i = 0; i < currentSortRows.length; i++){
			tr = tbl.insertRow(i);
			td = tr.insertCell(0);
			td.noWrap = true;
			td.appendChild(document.createTextNode(currentSortRows[i].name));
			
			td = tr.insertCell(1);
			td.width = "100%";
			
			if(i > 0){
				a = document.createElement("a");
				a.id = "btn_" + currentSortRows[i].id;
				a.onclick = moveUp;
				img = document.createElement("img");
				img.border = 0;
				img.src = "images/arrowUp.gif";
				a.appendChild(img);
				td.appendChild(a);
			}

			if(i < currentSortRows.length - 1){
				a = document.createElement("a");
				a.id = "btn_" + currentSortRows[i].id;
				a.onclick = moveDown;
				img = document.createElement("img");
				img.border = 0;
				img.src = "images/arrowDown.gif";
				a.appendChild(img);
				td.appendChild(a);
			}		
		}
	}

	var currentSortRows = new Array();
	

	function showDivNav(id, y, active, navid, page, sortRows){
	
		//SHOW DIV WINDOW
		var div = document.getElementById(id);
		div.style.display = "";
		div.style.top = y;
			
		//SET PAGE NAME
		var tbPage = document.getElementById("tbPage");
		tbPage.value = page;
		
		//SET NAVID
		var hdnavid = document.getElementById("hdnavid");
		hdnavid.value = navid;
		
		//SET CHECKBOX		
		if(active == 1){		
			var cbActive = document.getElementById("cbActive");
			cbActive.checked = true;
		}
		
		//BUILD SORT TABLE
		var tbl = document.getElementById("tblSort");
		
		//REMOVE ANY OLD DATA IN SORT TABLE
		for(var i= tbl.rows.length - 1; i >=0; i--){
			tbl.deleteRow(i);
		}
		
		if(sortRows){
			var tr, td, btn, a, img;
			for(var i = 0; i < sortRows.length; i++){
				tr = tbl.insertRow(i);
				td = tr.insertCell(0);
				td.noWrap = true;
				
				td.appendChild(document.createTextNode(sortRows[i].name));
				
				td = tr.insertCell(1);
				td.width = "100%";
				
				if(i > 0){
					a = document.createElement("a");
					a.id = "btn_" + sortRows[i].id;
					a.onclick = moveUp;
					img = document.createElement("img");
					img.border = 0;
					img.src = "images/arrowUp.gif";
					a.appendChild(img);
					td.appendChild(a);
				}

				if(i < sortRows.length - 1){
					a = document.createElement("a");
					a.id = "btn_" + sortRows[i].id;
					a.onclick = moveDown;
					img = document.createElement("img");
					img.border = 0;
					img.src = "images/arrowDown.gif";
					a.appendChild(img);
					td.appendChild(a);
				}
				
			
			}
			
			currentSortRows = sortRows;
		}
		
	}
	
	//CLOSE EDIT WINDOW
	function closeEdit(id){
		var div = document.getElementById(id);
		div.style.display = "none";
	}
	
	
	//NAVIGATION ADD
	function showDivAdd(id, y, navid){
	
		//SHOW DIV WINDOW
		var div = document.getElementById(id);
		div.style.display = "";
		div.style.top = y;
		
		//SET NAVID
		var hdnavid = document.getElementById("hdAddnavid");
		hdnavid.value = navid;
	}
	
	//PROMOTION CATEGORY ADD/EDIT
	function showDivPromotionCategory(id, action){
	
		//SHOW DIV WINDOW
		var div = document.getElementById(id);
		div.style.display = "";
		
		//SET PROMOTIONCATEGORY
		ddlPromotionCategory = document.getElementById("ddlPromotionCategory");		
		
		if(ddlPromotionCategory.value > 0 && action == "edit"){	
		
			var myindex  = ddlPromotionCategory.selectedIndex;			
			tbPromotionCategory = document.getElementById("tbPromotionCategory");
			tbPromotionCategory.value = ddlPromotionCategory.options[myindex].text;
			
			hdPromotionCategoryId = document.getElementById("hdPromotionCategoryId");
			hdPromotionCategoryId.value = ddlPromotionCategory.options[myindex].value;
		}	
	}
	
	function checkPromotionCategory(){
	
		tbPromotionCategory = document.getElementById("tbPromotionCategory");
	
		if(tbPromotionCategory == ""){
			var msg = "Promotion Category is blank.";
			alert(msg);
			return false;
		}	
		
		return true;
	}	
	
	
	function checkForm(){
	
		tbPage = document.getElementById("tbPage");
	
		if(tbPage == ""){
			var msg = "Page is blank.";
			alert(msg);
			return false;
		}
	
		var ids = "";
		for(var i = 0; i < currentSortRows.length; i++){
			if(i > 0)
				ids += ",";
			ids += currentSortRows[i].id;
		}
		
		hdSort = document.getElementById("hdSort");
		hdSort.value = ids;
		
		return true;
	}	
	
	
	//IMAGE SWAP FUNCTION
	function SwapImage(itm, i){
		var imgMain = document.getElementById("imgMain");		
		
		var imageURL = "";
		if(i == 0)
			imageURL = "images/ProductImages/" + itm + ".jpg";
		else	
			imageURL = "images/ProductImages/" + itm + "-0" + i + ".jpg";
			
		
		imgMain.src="getproductimage.php?img=" + imageURL + "&h=180&w=350";	

		document.getElementById("hdImageURL").value = imageURL;
	}
	
	
	//OPEN IMAGE IN POPUP, SNAP TO SIZE OF IMAGE
	function openPopup(id){
		var imageURL = document.getElementById(id).value;
	
		myImage = new Image();
		myImage.src = imageURL; 
		
		//var h = myImage.height;
		//var w = myImage.width;
		
		
		newindow=window.open("popupImage.php?file=" + imageURL , null, "height=0, width=0,status= no, resizable= yes, scrollbars=no, toolbar=no,location=no,menubar=no'");		
		newindow.focus();
	}
	
	
	//SHOW BROWSE WINDOW
	function showDivBrowse(id){
	
		//SHOW DIV WINDOW
		var div = document.getElementById(id);
		div.style.display = "";
		//div.style.top = 30;
	}
	
	
	//IMAGE PREVIEW FUNCTION
	function Preview(path, y){
		var imgPreview = document.getElementById("imgPreview");
		imgPreview.src="getproductimage.php?img=" + path + "&h=180&w=300";
		
		//var div = document.getElementById("divImage");
		//div.style.top = y;
	}
	
	
	//SET PATH OF IMAGE IN CLIPBOARD
	function setClipBoard(path){	
	   var trCopy = document.getElementById("trCopy");
	   trCopy.style.display = "";
	   
		var tbCopy = document.getElementById("tbCopy");
		tbCopy.value = path;
	}
	
	
	//PRELOADS IMAGES
	function preloadImage(image){
	
		var img = new Image();
		img.src = image;	
	}
	
	
	//POP UP WINDOW
	function openWindow(locURL, winName, width, height){
	
		if (window.popWindow && !window.popWindow.closed){
			window.popWindow.location.href = locURL;    
			window.articleWin.focus();  
		}  
		
		else{    
			window.articleWin = window.open(locURL,winName,"toolbar=no,menubar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=yes,height=" + height + ",innerheight=" + height + ",width=" + width + ",innerwidth=" + width + ",screenx=125,left=125,screeny=150,top=150");
			window.articleWin.focus();  
		}
	}
