var focusInput = "";

var pageLoadFunction = function() { };

String.prototype.trim = function() {
   return this.replace(/^\s+|\s+$/g, "");
};

function createCookie(name, value, days) {
   var expires = "";
   var days_default = 10000;
   if (days) {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      expires = "; expires = " + date.toGMTString();
   }
   else {
      days = days_default;
   }
   document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
   var re = new RegExp("[;^]\\s*" + name + "=([^;]*?)(?:;|$)");
   var m = re.exec(document.cookie);
   return (m == null ? "" : m[1]);
}

function eraseCookie(name) {
   createCookie(name, "", -1);
}

function showHelp() {
	if (this.helpAnchor == undefined) {
	   return;
	}
	var url = "/ACI_ECommerce/help.html#" + helpAnchor;
	var helpWindow = window.open(url, 'applicationHelp', 'height=400,width=500,scrollbars=1,resize=1');
	if (window.focus) {
	   helpWindow.focus();
	}
}


function handleTabKey(e, shift) {
   var initialInput = (IE && !MAC) ? (document.activeElement) : (e.target);
   
   var foundInput = false;
   var foundNextField = false;
   var firstInput;
   if (shift) {
      for (var i = hatsForm.elements.length - 1; i >= 0; --i) {
         var input = hatsForm.elements[i];
//         if ((isHInput(input.type, input.name) || (input.name != null && input.name.substring(0, 7) == "hatsgv_")) &&
//         (input.disabled != "true") && input.type != "hidden") {
         if (input.disabled != "true" && input.type != "hidden") {
            if (firstInput == undefined) {
               firstInput = input;
            }
            if (foundInput) {
               input.focus();
               foundNextField = true;
               break;
            }
            if (input == initialInput) {
               foundInput = true;
	        }
         }
      }
   }
   else {
      for (var i = 0; i < hatsForm.elements.length; ++i) {
         var input = hatsForm.elements[i];
//         if ((isHInput(input.type, input.name) || (input.name != null && input.name.substring(0, 7) == "hatsgv_")) &&
//         (input.disabled != "true") && input.type != "hidden") {
         if (input.disabled != "true" && input.type != "hidden") {
            if (firstInput == undefined) {
               firstInput = input;
            }
            if (foundInput) {
		        window.status = input.name;
               input.focus();
               foundNextField = true;
               break;
            }
            if (input == initialInput) {
               foundInput = true;
	        }
         }
      }
   }
   if (firstInput == null) {
      return true;
   }
   if (!foundNextField) {
      firstInput.focus();
   } 
}

function showDisclaimer() {
	if (this.hatsForm == undefined) {
		var disclaimer = window.open("legalDisclaimer.jsp");
	}
	else {
    	hatsForm.hatsgv_showingDisclaimer.value = "true";
    	ms("refresh", "hatsportletid");
	}
}

function fixCalendarInputs() {
   for (var i in hatsForm.elements) {
      try {
         var e = hatsForm.elements[i];
         if (e.type == "text" && e.nextSibling != null && e.nextSibling.nextSibling != null) {
            var testObj = e.nextSibling.nextSibling;
            if (testObj.src != null && testObj.src.match(/([^\/]*)$/)) {
               if (RegExp.$1 == "calendar.gif") {
                  if (e.value.match(/^(\d{4})\d\d(\d\d)$/)) {
                     e.value = RegExp.$1 + RegExp.$2;
                  }
                  else {
                     e.value = e.value.replace(/\s+/g, "");
                     if (e.value.match(/^(\d{6})\d/)) {
                        e.value = RegExp.$1;
                     }
                  }
               }
            }
         }
      } catch (ex) {
      }
   }
}

function setHiddenValue(name, value) {
   var inputName = name;
   if (!inputName.match(/^in_/)) {
      inputName = "hatsgv_" + inputName;
   }
   var input = document.getElementById(inputName);
   if (input == null || input == undefined) {
      input = createHiddenInput(name);
   }
   input.value = value;
}

function createHiddenInput(name, value) {
   var hiddenDiv = document.getElementById("hiddenDiv");
   if (hiddenDiv == null || hiddenDiv == undefined) {
      hiddenDiv = document.createElement("div");
      hiddenDiv.style.visibility = "hidden";
      hiddenDiv.id = "hiddenDiv";
      hatsForm.appendChild(hiddenDiv);
   }
   var input = document.createElement("input");
   input.setAttribute("type", "hidden");
   if (!name.match(/^in_/)) {
      name = "hatsgv_" + name;
   }
   input.name = name;
   input.id = input.name;
   input.value = (value == undefined ? "" : value);
   hiddenDiv.appendChild(input);
   return input;
}

function removeEmptyRows(obj) {
   var i = 0;
   while (i < obj.childNodes.length) {
      var e = obj.childNodes[i];
      var removedRow = false;
      if (e.nodeName == "TABLE") {
// remove empty rows from start of table
         while (/<tt>/.test(e.rows[0].innerHTML)) {
            var x = e.rows[0].innerHTML;
            x = x.replace(/(&nbsp;|\s|\n)+/mg, "");
            x = x.replace(/(<tt>|<.tt>)/img, "");
            x = x.replace(/(<td>|<.td>)/img, "");
            if (x == "") {
               e.tBodies[0].removeChild(e.rows[0]);
               removedRow = true;
            }
         }
// remove empty rows from end of table
         while (/<tt>/i.test(e.rows[e.rows.length - 1].innerHTML)) {
            var x = e.rows[e.rows.length - 1].innerHTML;
            x = x.replace(/(&nbsp;|\s|\n)+/mg, "");
            x = x.replace(/(<tt>|<.tt>)/img, "");
            x = x.replace(/(<td>|<.td>)/img, "");
            if (x == "") {
               e.tBodies[0].removeChild(e.rows[e.rows.length - 1]);
               removedRow = true;
            }
         }
      }
      if (!removedRow) {
         removeEmptyRows(e);
      }
      ++i;
   }
}

function checkForFocusOverride() {
	if (focusInput != "") {
   		document.getElementById(focusInput).focus();
	}
}

/*
Add multiple links to a navigation table cell at once
linkList format: <link_text>|<link_url>||[...]
*/
function addLinksToNavCell(cellID, linkList, linkStyle) {
   var pairs = linkList.split(/\|\|/);
   for (var i = 0; i < pairs.length; i++) {
      /^(.*?)\|(.*)/.test(pairs[i]);
      addLinkToNavCell(cellID, RegExp.$1, RegExp.$2, linkStyle);
   }
}

function addLinkToNavCell(cellID, linkText, linkURL, linkStyle) {
   var cell = document.getElementById(cellID);
   if (cell.innerHTML != "") {
      cell.innerHTML += " | ";
   }
   if (linkURL == "") {
      cell.innerHTML += linkText;
   }
   else {
	   var link = document.createElement("a");
	   link.href = linkURL;
	   if (linkStyle != undefined) {
	      link.className = linkStyle;
	   }
	   link.appendChild(document.createTextNode(linkText));
       cell.appendChild(link);
   }
}


function returnToAdmin() {
	setHiddenValue('goodBranchCustomer', 'N');
	setHiddenValue('getOrderData', 'Y');
	setHiddenValue('adminSelected', 'Y');
	ms('macrorun_returnToAdmin');
}

function promptField(row, col) {
	var pos = (row - 1) * 80 + col;
	setCursorPosition(pos);
	ms('?  [enter]]');
}

function addToEvent(obj, event_name, text) {
	if (typeof obj == "string") {
		obj = document.getElementById(obj);
		if (obj == null) {
			return;
		}
	}
	text = String(text);
	var e = obj[event_name];
	var x = (e == null ? "" : e.toString());
	if (x != "") {
		x = x.substring(x.indexOf('{') + 1, x.lastIndexOf('}') - 1);
	}
	x = (x ? x + ";" : "") + text + ";";
	obj[event_name] = new Function("event", x);
}

function getHatsForm() {
	if (hatsForm) {
		return hatsForm;
	}
	return document.HATSForm;
}

function useWaitCursor() {
	waitDiv = document.createElement("div");
	waitDiv.style.position = "absolute";
	waitDiv.style.left = 0;
	waitDiv.style.top = 0;
	waitDiv.style.width = document.body.clientWidth;
	waitDiv.style.height = document.body.clientHeight;
	waitDiv.style.zIndex = 1000;
	waitDiv.style.cursor = "wait";
	document.body.appendChild(waitDiv);
	var selects = document.getElementsByTagName("select");
	if (selects.length > 0) {
		for (var i = 0; i < selects.length; i++) {
			selects[i].style.cursor = "wait";
		}
	}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
