// Site-Wide JS Template
// copyright 2008, RWD
function quickEditContent(pageID){
	window.location.href = 'zca.cfm?go=zPageEdit&pageID='+pageID;
}

function quickProductEditContent(productID){
	window.location.href = 'zca.cfm?go=zProductEdit&productID='+productID;
}

function zxResults(r){
	eval(r);
}

function zPageAction(action,pageID,parentID){
	proceed = 1;
	if (action == 'delete'){
		if (confirm('Delete This Page?')){
			proceed = 1;
		}else 
		{ proceed = 0; }
	}
	if (proceed == 1){
	DWRUtil.useLoadingImage(ajaxLoading);
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'PageAction', pageID, parentID, action, zxResults);
	}
}

function zQuickPageEdit(pageID){
	DWRUtil.useLoadingImage(ajaxLoading);
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'QuickPageEdit', pageID, zxResults);
}

function zQuickProductEdit(productID){
	DWRUtil.useLoadingImage(ajaxLoading);
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'QuickProductEdit', productID, zxResults);
}

function zPageActionLive(action,pageID,parentID){
	proceed = 1;
	if (action == 'delete'){
		if (confirm('Delete This Page?')){
			proceed = 1;
		}else 
		{ proceed = 0; }
	}
	if (proceed == 1){
	DWRUtil.useLoadingImage(ajaxLoading);
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'PageAction', pageID, parentID, action, zQuickResults);
	}
}
function zShowImage(ni){
		if(window.pageEdit === undefined)
		{
			DWRUtil.useLoadingImage(ajaxLoading);
			DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'zSwapImage', ni, zImageProcess);
			
		}else{
			// do nothing in admin display
		}
}

function zShowPImage(pID, nextSortID, productID, type){
		
			DWRUtil.useLoadingImage(ajaxLoading);
			DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'zSwapPImage', pID, nextSortID, productID, type, zImageProcess);
			
		
}

function zImageProcess(r){
	eval(r);
}
	
function gotoURL(dest){
		window.location = dest; return false;
}
//create function, it expects 2 values.

function insertAfter(newElement,targetElement) {	
	//target is what you want it to go after. Look for this elements parent.	
	var parent = targetElement.parentNode; 	
	
	//if the parents lastchild is the targetElement...	
	if(parent.lastchild == targetElement) {		
	//add the newElement after the target element.		
	parent.appendChild(newElement);		
	} else {		
	// else the target has siblings, insert the new element between the target and it's next sibling.		
	parent.insertBefore(newElement, targetElement.nextSibling);		
	}
}
function hide(did,sc){
	if (!sc){ var sc = 'false';}else{this.SetCookie(group,0);}
	txt = document.getElementById(did);	
	txt.style.display = "none";	 
}

function show(did,sc){
	if (!sc){ var sc = 'false';}else{this.SetCookie(group,1);}
	txt = document.getElementById(did);
	txt.style.display = "block";
}

function showinline(did){
	txt = document.getElementById(did);
	txt.style.display = "inline";
}

function hideshow(group,sc) {
if (!sc){ var scc = 'false';}else{var scc = 'true';}
var txt = document.getElementById(group);
 if (txt.style.display == "none") {	
   txt.style.display="block";
   if (scc == 'true'){this.SetCookie(group,0); }
   }
   else{
   txt.style.display="none";
   if (scc == 'true'){this.SetCookie(group,1); }
   }
}
function recallCookie(group) {
	try{
	var x = document.cookie;
	var txt = document.getElementById(group);
	
		y = this.getCookie(group);
		if (y == 1) {
			txt.style.display="none";
		} else {
			txt.style.display="block";
		}
	}
	catch(e){
		alert('recallCookie error: ' + e.message + '\n\n' + e.description);
	}
}
// [Cookie] Sets value in a cookie
function SetCookie(cookieName, cookieValue, expires, path, domain, secure) {
	if (expires == null) {
		var cookie_date = new Date();  // current date & time		
		var month = cookie_date.getMonth() + 1
		expires = new Date ( cookie_date.getFullYear(), month, cookie_date.getDate() );		
	}	
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');		
}

// [Cookie] get Cookie Values
function getCookie(cookieName){
	var results = document.cookie.match ( cookieName + '=(.*?)(;|$)' );
	// alert('cookie:'+cookieName+' value='+results);
	if (results) {
		return (unescape(results[1])); }
	else
		{ return null; }
}

// JS Trim String
function trim(str){
 return str.replace(/^\s*|\s*$/g,"");
}

// Pop-Up Position

var cX = 0; var cY = 0; var rX = 0; var rY = 0;

function UpdateCursorPosition(e){ cX = e.pageX; cY = e.pageY;}
function UpdateCursorPositionDocAll(e){ cX = event.clientX; cY = event.clientY;}

if(document.all) { document.onmousemove = UpdateCursorPositionDocAll; }
else { document.onmousemove = UpdateCursorPosition; }

function AssignPosition(d) {
if(self.pageYOffset) {
	rX = self.pageXOffset;
	rY = self.pageYOffset;
	}
else if(document.documentElement && document.documentElement.scrollTop) {
	rX = document.documentElement.scrollLeft;
	rY = document.documentElement.scrollTop;
	}
else if(document.body) {
	rX = document.body.scrollLeft;
	rY = document.body.scrollTop;
	}
if(document.all) {
	cX += rX; 
	cY += rY;
	}
d.style.left = (cX+175) + "px";
d.style.top = (cY-100) + "px";
}

function ajaxSaveForm(formid,action){
	
	ColdFusion.Ajax.submitForm(formid,action,ajaxSubReturn);
}

function ajaxSubReturn(r){
	eval(r);
}

//<![CDATA[

//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2001 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

// Determine browser and version.

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

//]]>
// end move a div code....

function refreshPage(path){
	if (confirm('Are you Sure?\n\nAny un-saved changes will be lost')){
	window.location.href = path;
	}
}

function exitEdit(href){
	if (!href){ var href = "zca.cfm?go=adminHome";}
	if (href == '' || href == 0){ var href = "zca.cfm?go=adminHome";}
	
	window.location.href = href;
}

function zResults(r){
	eval(r);
}



function setPointer(theRow, thePointerColor){
    if (thePointerColor == '' || typeof(theRow.style) == 'undefined') { return false; }
    if (typeof(document.getElementsByTagName) != 'undefined') {
        var theCells = theRow.getElementsByTagName('td');
    }else if (typeof(theRow.cells) != 'undefined') {
        var theCells = theRow.cells;
    }else { return false;}
    var rowCellsCnt  = theCells.length;
    for (var c = 0; c < rowCellsCnt; c++) {
        theCells[c].style.backgroundColor = thePointerColor;    }
    return true;
	}

function toggleEditOption(n,id,iid){
try{
	count = 1;
	while (document.getElementById(id+count) != null)
	{
		aimg = document.getElementById(iid+count)
		aimg.src = SysimgDir+"/dot.gif";
		hide(id+count);
		
		count ++;
	}
	aimg = document.getElementById(iid+n)
	aimg.src = SysimgDir+"/arrow-right.gif";
	show(id+n);
}
catch(e){
	alert(e.message);
}
}