// test.js

// newFunction
function loadForm() {
	//alert("On Load");
	acesAjaxAnywhere.submitAcesAJAX('method=ONLOAD_AJAX');
}

// submitForm
function submitForm() {
	//alert("Submit Form");
}

function clearCsrForm() {
	var frm = document.csrBean;
	frm.customerLastName.value = "";
	frm.clientOrderNumber.value = "";
	frm.garmentNumber.value = "";
	frm.serialNumber.value = "";
}

/**
 * Copyright 2005 Archetype Solutions Inc. All rights reserved
 * This library may not be used without permission.
 *
 * This file contains widget helping functions
 *
 *		swapImage()
 *		showHelp()
 *		hideHelp()
 */

function WidgetHelper() {
	if(typeof COLLECTIONS_VERSION == "undefined"
		|| BROWSER_SNIFFER_VERSION == "undefined")
	{
		alert("WidgetHelper.error: Missing Required Libraries");
		throw new Error("WidgetHelper Missing Required Libraries");
	}
	this.activeHelpDivision = null;
	
}

widgetHelper = new WidgetHelper();

/*! @var IMG_SWAP_LABEL the id marker for a handler to mark it as a image */
var IMG_SWAP_LABEL 	= "d";
/*! @var DIV_LABEL the id marker for a handler to mark it as a label */
var DIV_LABEL 		= "x";

/*!
 @function swapImage
 Switches an R3 answer image for the given element
 @param for_item the item who's image should be swapped (if possible)
*/
WidgetHelper.prototype.swapImage = function(for_item){
	try{
		//name in the form of value(5,26)
		var questionHandle = for_item.name.replace(/[value\(\)]/g, "");

		//get the key with out the comma
		var key = questionHandle.replace(/,/g,"_");
		//look for a Map object with the key in the name
		var aryhndl = eval("img" + key);
		
		//if we've found a map object try to get the image to swap
		if(aryhndl)
		{
			var newimg = aryhndl.get(for_item.value);
			
			var imgelement = document.getElementById(IMG_SWAP_LABEL + questionHandle);
			
			if(imgelement)
			{
				imgelement.src = newimg;
			}
			else
				throw new Error("WidgetHelper.swapImage.error: Cannot find image element");
		}
	}catch(e){
		alert(e.message);	
	}
}

/*! @var SysMouse this holds the current mouse position on the screen */
SysMouse = new Object();
SysMouse.X = 0;
SysMouse.Y = 0;
				
/*
 * gets the current mouse position and puts it into the SysMouse object
 * this is not an API call; however it needs to get envoked on window mouse move
 * events to be of any help.
*/
SysMouse.setMouseCoords = function(e) {
	e = __normalizeEvent(e);
	if (typeof(e) != "undefined")
	{
		if (typeof( e.pageX ) == 'number') {
			//NS 4, NS 6+, Mozilla 0.9+
			var xcoord = e.pageX;
			var ycoord = e.pageY;
		}
		else {
			if(typeof( e.clientX ) == 'number'){
				//IE, Opera, NS 6+, Mozilla 0.9+
				//except that NS 6+ and Mozilla 0.9+ did pageX ...
				var xcoord = e.clientX;
				var ycoord = e.clientY;
				
				//should this be in the sniffer?
				if(!((window.navigator.userAgent.indexOf( 'Opera' ) + 1 )
					|| (window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) 
					|| window.navigator.vendor == 'KDE' )){
					
					if(document.body && (document.body.scrollLeft || document.body.scrollTop)){
						//IE 4, 5 & 6 (in non-standards compliant mode)
						xcoord += document.body.scrollLeft;
						ycoord += document.body.scrollTop;
					}else if(document.documentElement
						&& (document.documentElement.scrollLeft || document.documentElement.scrollTop)){
						//IE 6 (in standards compliant mode)
						xcoord += document.documentElement.scrollLeft;
						ycoord += document.documentElement.scrollTop;
					}
				}
			} 
			else {
				//TOTAL FAILURE, WE HAVE NO WAY OF OBTAINING THE MOUSE COORDINATES
				return;
			}
		}
		
		//populate the object
		SysMouse.X = xcoord;
		SysMouse.Y = ycoord;
	}
};

/*!
 @function __normalizeEvent
 helper function to handle IE bug for event handling - mostly makes just 
 makes sure an event is an event
 @param evt the event to normalize
 @result an event object that is mostly browser independent
*/
function __normalizeEvent(evt)
{
	if ((!evt || evt == null || typeof(evt) == "undefined") 
		&& (document.all && navigator.userAgent.indexOf("Opera")) )
	{
		evt = window.event;
	}
	
	return evt;
}

document.onmousemove = SysMouse.setMouseCoords;






/*
 *	find location of questionHelpImage
 *	find location of mouse 
*/
WidgetHelper.prototype.showHelp = function(questionHelpImage) {
	var questionHandle = questionHelpImage.id.substring(1);
	
	this.activeHelpDivision = document.getElementById(DIV_LABEL + questionHandle);
	
	var yQuestionHelpImage = 0;
	var xQuestionHelpImage = 0;

	/*
	 *	Firefox truncates the offsetWidth if, from prior display,
	 *	it was beyond the document margin. To workaround, position
	 *	help division at left
	*/	
	this.activeHelpDivision.style.left = "0px";
	
	var helpDivisionWidth = this.activeHelpDivision.offsetWidth;
	var helpDivisionHeight = this.activeHelpDivision.offsetHeight;
	
	var wrapperDivision = document.getElementById("wrapper");
	var wrapperDivisionOffsetLeft = wrapperDivision.offsetLeft;

	if(questionHelpImage.y) {
		yQuestionHelpImage = questionHelpImage.y;
		xQuestionHelpImage = questionHelpImage.x;
	} else {
		//alert("XY not available.");
		/*
		 *	recursively ascend the parentage of the target element
		 *	and sum the offset left and top of each. This method
		 *	includes the offset of the wrapper division which must
		 *	be subtracted for correct positioning of the help division 
		*/
		var tot = 0;
		yQuestionHelpImage = __findRealYPosition(questionHelpImage, tot)
		tot = 0;
		xQuestionHelpImage = __findRealXPosition(questionHelpImage, tot)
		
		xQuestionHelpImage -= wrapperDivisionOffsetLeft;

	}
	
	var documentWidth = document.body.offsetWidth - 5;
	var documentHeight = document.body.offsetHeight;
	
	// IE includes the scrollbar in the documentWidth
	if(SysBrowser.explore) {
		documentWidth -= 20;
	}
	
	/*alert("Help Icon Y=" + yQuestionHelpImage + " X=" + xQuestionHelpImage
		+ "\nHelp Division Width=" + helpDivisionWidth + " Height=" + helpDivisionHeight
		+ "\nMouse Y=" + SysMouse.Y + " X=" + SysMouse.X
		+ "\nDocument Width=" + documentWidth + " Height=" + documentHeight
		+ "\nwrapperDivision offsetLeft=" + wrapperDivisionOffsetLeft
		);
	*/
	
	var helpDivisionX;
	var helpDivisionY;
	
	/*
	 *	Display help division to the right of the help icon if possible
	 *	Otherwise, display below the help icon and
	 *	right justified to the window width
	*/
	if((SysMouse.X + helpDivisionWidth) > documentWidth) {
		helpDivisionY = yQuestionHelpImage + 20;
		helpDivisionX = documentWidth -
			(helpDivisionWidth + wrapperDivisionOffsetLeft);
	} else {
		helpDivisionX = xQuestionHelpImage + 20;
		helpDivisionY = yQuestionHelpImage;
	}
	
	this.activeHelpDivision.style.top = helpDivisionY + "px";
	// With the new Indi design I need to subtract from top for IE
	// IE calculates from first relative DIV, while other browsers from top of window
	if(SysBrowser.explore) {
		this.activeHelpDivision.style.top = (helpDivisionY - 81) + "px";
	}
	this.activeHelpDivision.style.left = helpDivisionX + "px";
	this.activeHelpDivision.style.visibility = "visible";
	
	__setAllDropDownVisibility(false, helpDivisionX, helpDivisionY,
		helpDivisionHeight, helpDivisionWidth)
}

function __findRealYPosition(ele,tot)
{
	if(ele.offsetParent){
		tot += __findRealYPosition(ele.offsetParent,tot);
	}
	tot += ele.offsetTop;
	return tot
}

function __findRealXPosition(ele,tot)
{
	if(ele.offsetParent){
		tot += __findRealXPosition(ele.offsetParent,tot);
	}
	tot += ele.offsetLeft;
	return tot
}

/*!
 @function setAllDropDownVisibility
 Sets every drop downs visibility to on or off for the active element. 
 Needed because IEs zOrder is wacky and doesn't let anything go above 
 select boxes
 @param to boolean for on or off
*/
function __setAllDropDownVisibility(to, left, top, height, width){
	var formelement = document.forms[0];
	
	for(var q=0; q<formelement.length; q++)
	{
		if(formelement[q].type == "select-one"){
			if(to)
			{
				formelement[q].style.visibility = "visible";
			}
			else
			{
				var selectWidth = formelement[q].offsetWidth;
				var selectHeight = formelement[q].offsetHeight;
	
				var wrapperDivision = document.getElementById("wrapper");
				var wrapperDivisionOffsetLeft = wrapperDivision.offsetLeft;
			
				if(formelement[q].y) {
					var selectY = formelement[q].y;
					var selectX = formelement[q].x;
				} else {
					//alert("XY not available.");
					/*
					 *	recursively ascend the parentage of the target element
					 *	and sum the offset left and top of each. This method
					 *	includes the offset of the wrapper division which must
					 *	be subtracted for correct positioning of the help division 
					*/
					var tot = 0;
					var selectY = __findRealYPosition(formelement[q], tot)
					tot = 0;
					var selectX = __findRealXPosition(formelement[q], tot)
					
					selectX -= wrapperDivisionOffsetLeft;
			
				}
				
				if((selectX + selectWidth) >= left &&
					(selectY + selectHeight) >= top &&
					selectY < (top + height))
				{
					formelement[q].style.visibility = "hidden";
				}
			
			}
		}
	}
}



WidgetHelper.prototype.hideHelp = function() {
	if(typeof(this.activeHelpDivision) != "undefined")
	{
		this.activeHelpDivision.style.visibility = "hidden";
		__setAllDropDownVisibility(true, 0, 0, 0, 0)	
	}
}

var currentmapobj;
var currentstartimg;
var zoompopup;
/*!
 @function showZoomImage
 Pop open a new windows for an image that can have a "closer view". Note
 The setup for this requires that all the XSLT elements are in place - 
 meaning this is only useful for generated Widgets with zoom images 
 defined in the ADC data
 @param mapkey which question this refers to
 @param strpath the new image to zoom into
*/
function showZoomImage(mapkey, strpath)
{
	//alert(mapkey, strpath);
	currentmapobj = eval("zoomimg" + mapkey.replace(/,/g,"_"));
	currentstartimg = strpath;
	
	firstimg = new Image();
	firstimg.src = strpath;
	
	//if the windows exists we need to close it else this just keeps
	//appending to the open window and makes it more or less useless
	if(zoompopup){
		zoompopup.close();
	}
	
	//first is location...
	var windowHeight = 350;
	if (mapkey == "5,254" || mapkey == "5,28" || mapkey == "5,753" || mapkey == "5,7683"){ //styles
		windowHeight = "500";
	}
	else if (mapkey == "5,5547" || mapkey == "5,5842" || mapkey == "5,208"){ //sanding and crease down
		windowHeight = "425";
	}	
	var windowWidth	 = 300;
	zoompopup = window.open(
		'images/graphics/spacer.gif', 'zoompopup',
		'height=' + windowHeight + ',width=' + windowWidth 
		+ ',scrollbars=no,resizable=yes,left=300,top=100,status=no,toolbar=no,location=no'
	);
	
	zoompopup.focus();
	
	var hed_s = "<html><head><title>Zoom Image</title>";
	var inc2  = "<script language='javascript' src='/ACES_client/scripts/zoomImagePopup.js'>" + "</" + "script>";
	var hed_e = "</head>";
	var bdy_s = "<body style='margin: 0px; padding: 0px;'>";
	
	var bdc =  "<div id=\"ArchetypePopWinContents\">";
	bdc += "	<div id=\"Container\">";
	bdc += "	<div id=\"PhotoContainer\">";
	bdc += "	<img id=\"Photo\" name=\"Photo\" src=\"images/graphics/spacer.gif\" width=\"300\" />";
	bdc += "	</div>";
	bdc += "	</div>";
	bdc += "	<div align=\"center\">";
	bdc += "	<form name=\"formselector\">";
	bdc += "		<select name=\"zoomimage\" id=\"ArchetypeAllZoomImages\" onchange=\"javascript: showNewPhoto(this);\">";
	bdc += "			<option value=\"blank.jpg\"> ... Loading ... </option>";
	bdc += "		</select>";
	bdc += "	</form>";
	bdc += "	</div>";
	bdc += "</div>";
	
	var loader = "<script language='javascript'>setTimeout('fillDropDown();',600);" + "</" + "script>";
	var bdy_e  = "</body></html>";
	
	//bdy = document.getElementById("ArchetypePopWinContents").innerHTML;
	zoompopup.document.write(hed_s + inc2 + hed_e + bdy_s + bdc + loader + bdy_e);
	
	zoompopup.document.close();
}

/*
 *	This object wraps AjaxAnywhere to provide 1) semaphore operation of
 *	ajax and 2) handling in the event that the ajax request doesn't
 *	complete.
 *	ajaxTimeout - interval (in msec) starting when the browser initiates an ajax
 *	request. At the end of the interval, the browser is redirected to the
 *	ajax problem page
*/
function AcesAjaxAnywhere() {
	this.ajaxWorking = 0;
	this.ajaxTimeout = 10000; 
}

AcesAjaxAnywhere.prototype = new AjaxAnywhere();

AcesAjaxAnywhere.prototype.submit = function() {
	if(this.ajaxWorking == 0) {
		this.ajaxWorking = 1;
		return true;
	} else {
		return false;
	}
}

AcesAjaxAnywhere.prototype.submitAcesAJAX = function(additionalPostData, submitButton) {
	if(this.ajaxWorking == 0) {
		this.ajaxWorking = 1;
		this.timerId = setTimeout("ajaxTimeoutHandler()", this.ajaxTimeout);
		this.submitAJAX(additionalPostData, submitButton);
	}
}

AcesAjaxAnywhere.prototype.hideLoadingMessage = function() {
    var div = document.getElementById("AA_" + this.id + "_loading_div");
    if (div != null) {
        div.style.display = "none";
    }
    this.ajaxWorking = 0;
	clearTimeout(this.timerId);
}


acesAjaxAnywhere = new AcesAjaxAnywhere();

function ajaxTimeoutHandler() {
	acesAjaxAnywhere.ajaxWorking = 0;
	window.document.location.href=ajaxProblemUrl;
	//alert("AJAX timedout. Redirect:" + ajaxProblemUrl);
}
	
/*
 * disable enter key
 */

function disableEnter(e) {
	e = __normalizeEvent(e);
	var pK = e.which ? e.which : e.keyCode;
	//alert("Key pressed:" + pK);
	return pK != 13;
}

document.onkeypress = disableEnter;
