
// --- ROLLOVER SECTION ---------------------------------------

var imgpath=""; // default base path for rollover images

var rollovers=new Object;
var pageloaded=false;

function rollover(name) {
  var oTemp = eval('rollovers.'+name);
  if (oTemp == null) {
    oTemp = eval('rollovers.'+name+'=new Object');
    oTemp.over = new Array;
  }
  oTemp.index=0;
  for (var i=1; i<rollover.arguments.length; ++i)
    oTemp.over[i]=imgpath+rollover.arguments[i];
  if (pageloaded == true) {
    for (var i=1; i<oTemp.over.length; ++i) {
      var oTemp2 = new Image;
      oTemp2.src=oTemp.over[i];
      oTemp.over[i]=oTemp2;
    }
    oTemp.out=document[name].src;
    document[name].onload=null;
  }
}

function over(name,i) {
  if (!i) i=1;
  var oTemp = document[name]; 
  if (oTemp.onload==null) {
	  oTemp.index=i;
    oTemp.src=eval('rollovers.'+name).over[i].src;
  }
}

function out(name) {
  var oTemp = document[name];
  if (oTemp.onload==null) {
	  oTemp.index=0;
    oTemp.src=eval('rollovers.'+name).out;
  }
}

function preload() {
  for (var j in rollovers) {
    obj=eval("rollovers."+j);
    for (var i=1; i<obj.over.length; ++i) {
      var temp = new Image;
      temp.src=obj.over[i];
      obj.over[i]=temp;
    }
    obj.out=document[j].src;
    document[j].onload=null;
  }
  pageloaded=true;
}

window.onload = new Function("setTimeout('preload()',500)"); // delay, to compensate for some NS problems

// --- POPUP SECTION ---------------------------------------

var oPopupImage = null;
var oPopupPage = null;
var iLoadingPopupX = 140;
var iLoadingPopupY = 100;  
var bPopupComplete = false;
var iPopupMarginX = 0;
var iPopupMarginY = 0;
var iPopupMinX = 0;
var iPopupMinY = 0;

function aiScreenPosXY(iPopupX, iPopupY) {
  var iScreenPosX = (screen.availWidth>iPopupX) ? (screen.availWidth-iPopupX)/2 : 0;
  var iScreenPosY = (screen.availHeight>iPopupY) ? (screen.availHeight-iPopupY)/2 : 0;
  return [iScreenPosX, iScreenPosY];
}

function aiPageXY() {
  var iX, iY;
  if (window.innerWidth) {
    iX = window.innerWidth;
    iY = window.innerHeight;
  }
  else if (document.body.clientWidth) {
    iX = document.body.clientWidth;
    iY = document.body.clientHeight;
  }
  return [iX,iY];
}

function aiBrowserChrome() {
  top.resizeTo(200,200);
  var aiPageSize = aiPageXY();
  aiPageSize[0] = 200 - aiPageSize[0];
  if (aiPageSize[0] < 0)
    aiPageSize[0] = 0; 
  aiPageSize[1] = 200 - aiPageSize[1];
  if (aiPageSize[1] < 0)
    aiPageSize[1] = 0; 
  return aiPageSize;  
}

function showError() {
  if (bPopupComplete == true)
    return;
  bPopupComplete = true;
  LayerPosHide("loading", 0, 0);
  LayerPosShow("error", 0, 0);
}

function showImage(bResize) {
  if (bPopupComplete == true)
    return;
  bPopupComplete = true;
  
  var oPic = (document.layers) ? document.layers.image.document.pic : document.pic;
  
  iPosLoadingX = oPic.width + 2*iPopupMarginX;
  if (iPosLoadingX < iPopupMinX)
    iPosLoadingX = iPopupMinX;

  iPosLoadingY = oPic.height + 2*iPopupMarginY + iPopupButtonY;
  if (iPosLoadingY < iPopupMinY)
    iPosLoadingY = iPopupMinY;
	
  LayerPosHide("loading",iPosLoadingX-140,iPosLoadingY-100); // position in lower right corner, used for oversize
  
  if (bResize == true) {
    var aiChrome = aiBrowserChrome();
    var iPopupX = oPic.width + aiChrome[0] + 2*iPopupMarginX;
    var iPopupY = oPic.height + aiChrome[1] + 2*iPopupMarginY + iPopupButtonY;
    if (iPopupX < iPopupMinX)
      iPopupX = iPopupMinX;
    if (iPopupY < iPopupMinY)
      iPopupY = iPopupMinY;
	  
    if (self == top && (iPopupX > screen.availWidth-50 || iPopupY > screen.availHeight-50)) { // larger than screen
      location.href += "&type=oversize&xsize="+iPosLoadingX+"&ysize="+iPosLoadingY;
      return;   	
    }	
    if (iPopupX > screen.availWidth - 50)
      iPopupX = screen.availWidth - 50;
    if (iPopupY > screen.availHeight - 50)
      iPopupY = screen.availHeight - 50;

    var aiScreenPos = aiScreenPosXY(iPopupX, iPopupY);	
    top.moveTo(aiScreenPos[0], aiScreenPos[1]);
    top.resizeTo(iPopupX,iPopupY);
  }

  var aiSize = aiPageXY();
  var iImagePosX = (aiSize[0] - oPic.width) / 2;
  var iImagePosY = (aiSize[1] - oPic.height - iPopupButtonY) / 2;

  if (iImagePosX < 0) 
    iImagePosX = iPopupMarginX;
  if (iImagePosY < 0) 
    iImagePosY = iPopupMarginY;
	
  if (document.layers) 
    setTimeout('LayerPosShow("image",'+iImagePosX+','+iImagePosY+')',200);
  else
    LayerPosShow("image",iImagePosX,iImagePosY);
}

function popupImage(sImageURL, sTitle) {
  if (sTitle == null || sTitle == "")
    sTitle = "Image Popup";
  if (oPopupImage != null) {
    oPopupImage.close();
	oPopupImage = null;
  }
  aiScreenPos = aiScreenPosXY(iLoadingPopupX,iLoadingPopupY);
  oPopupImage = window.open("../content/popup.asp?image="+escape(sImageURL)+"&title="+escape(sTitle),"","scrollbars=no,resizable,width="+iLoadingPopupX+",height="+iLoadingPopupY+",screenX="+aiScreenPos[0]+",screenY="+aiScreenPos[1]+",left="+aiScreenPos[0]+",top="+aiScreenPos[1]);
  oPopupImage.focus();
}

function popupPage(sPageURL, iXsize, iYsize, bSeparate, bNoScrollbars) {
  if (iXsize == null)
    iXsize=500;
  if (iYsize == null)
    iYsize=350;
  var sScroll = "scrollbars,";
  if (bNoScrollbars == true)
    sScroll = "";
  var iXpos = (screen.width-26>iXsize) ? (screen.width-iXsize)/2 : 0;
  var iYpos = (screen.height-26>iYsize) ? (screen.height-iYsize)/2 : 0;
  if (bSeparate == true)
    window.open(sPageURL,"",sScroll+"resizable,width="+iXsize+",height="+iYsize+",screenX="+iXpos+",screenY="+iYpos+",left="+iXpos+",top="+iYpos);
  else {
    if (oPopupPage != null && oPopupPage.closed == false)
      oPopupPage.close();
    oPopupPage = window.open(sPageURL,"PopupPage",sScroll+"resizable,width="+iXsize+",height="+iYsize+",screenX="+iXpos+",screenY="+iYpos+",left="+iXpos+",top="+iYpos);
    oPopupPage.focus();
  }
}

function closePopups() {
  if (oPopupImage != null)
    oPopupImage.close();
  if (oPopupPage != null && oPopupPage.closed == false)
    oPopupPage.close();
}

window.onunload=closePopups;

// --- LISTBOX SECTION ---------------------------------------

function decodeURL(oSelect) {
  var sEncodedURL = oSelect.options[oSelect.selectedIndex].value;
  oSelect.selectedIndex = -1;
  if (sEncodedURL == "")
    return;
  var asURL = sEncodedURL.split(":");
  if (asURL[0] == "anchor") {
    if (top.frame0)
      top.frame0.location.hash = asURL[1];
    else
      top.location.hash = asURL[1];
  }	   
  else if (asURL[0] == "mailto")
    self.location.href="mailto:"+asURL[1];
  else if (asURL[0] == "_blank")
    window.open(asURL[1]+"?name="+asURL[3]);
  else if (asURL[0] == "_top" && window.cmsframeset != asURL[1])
    top.location.href = asURL[1]+"?name="+asURL[3];
  else if (asURL[3] != "") {
    if (top.frame0)
      top.frame0.location.href = asURL[2]+"?name="+asURL[3];
    else
      top.location.href = asURL[2]+"?name="+asURL[3];
  }
}

// --- FORM SECTION ---------------------------------------

function showSearchPage(sForm, iPage) {
  var oForm = document.forms[sForm];
  if (oForm != null) {
    oForm.searchpage.value = iPage;
	oForm.submit();
  }
}

function showCalendarPage(sForm, iPage) {
  var oForm = document.forms[sForm];
  if (oForm != null) {
    oForm.calendarpage.value = iPage;
	oForm.submit();
  }
}


function sRetrieveValue(oElement) {
  var sElementValue = "";
  if (oElement.type == "text" || oElement.type == "textarea" || oElement.type == "password" || oElement.type == "file") {
    return String(oElement.value);
  }
  else if (oElement.type == "checkbox") {
    if (oElement.checked)
      return String(oElement.value);
  }
  else if (oElement.type == "radio") {
    if (oElement != oElement.form[oElement.name][0])
		  return null;
    var oRadioElement = oElement.form[oElement.name];
    for (var j=0; j<oRadioElement.length; ++j)
      if (oRadioElement[j].checked)
 	      return String(oRadioElement[j].value);
  }
  else if (oElement.type == "select-one" || oElement.type == "select-multiple") {
	  if (oElement.selectedIndex >= 0)
      return String(oElement.options[oElement.selectedIndex].value);
  }
	return null;
}

function bValidateForm(oForm) {
  var sResult = "";
  var oFirstElement = null;
  for (i=0; i<oForm.elements.length; ++i) {
    var oElement = oForm.elements[i];
    var sElementName = oElement.name;
	  var sElementValue = sRetrieveValue(oElement);
		if (sElementValue == null)
		  continue;
		// if non-editable form item, and value starts with *, make that field mandatory instead
		if (sElementValue != null && sElementValue.charAt(0) == "*" && (oElement.type == "radio" || oElement.type == "checkbox" || oElement.type == "select-one" || oElement.type == "select-multiple")) {
      var oTemp = oElement.form[sElementValue.substr(1)];
			if (oTemp == null)
			  continue;
		  sElementName = sElementValue;
      oElement = oTemp;
		  sElementValue = sRetrieveValue(oElement);
		}
    if (sElementName.charAt(0) == "*") { // leading * in name indicates required field
      var sElementName = sElementName.substr(1); // trim leading * from name
      if (sElementName.toLowerCase().indexOf("email") != -1) { // do extended email checking
        if (sElementValue == "")
   	      sResult += "Please enter an email address for "+sElementName+"\n\n";
        else {	  
          var recheck = /\s/;	    
          if (recheck.test(sElementValue) == true)
  	      sResult += "Email addresses cannot contain spaces\n\n";
          if (sElementValue.indexOf("@") == -1)
            sResult += "Email addresses must contain an @ symbol\n\n";
          recheck = /[\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/
          if (recheck.test(sElementValue) == false)
            sResult += "There appears to be a mistake in the\nemail address you have typed.\nPlease check the address and try again.\n\n"; 
        }
      }
	    else if (sElementValue == "") {
        sResult += "Please enter a value for "+sElementName+"\n\n";
      }
      if (oFirstElement == null && sResult != "")
        oFirstElement = oElement;
    }
  }
  if (sResult != "") {
    alert(sResult);
  	if (oFirstElement != null && oFirstElement.focus != null)
	    oFirstElement.focus()
    return false;
  }
  return true;
}

// --- FLYUP SECTION ---------------------------------------

var iFlyupTimeout;
var sFlyupName;

function oFindLayer(sLayer) {
  if (document.getElementById)
    return document.getElementById(sLayer);
  if (document.all)
    return eval("document.all."+sLayer);
  return eval("document."+sLayer);
}

function getXYpos(oImage) {
  var iX = 0;
  var iY = 0;
  if (document.layers) {
    iX = oImage.x;
    iY = oImage.y;
  }
  else {
    while (oImage.offsetParent != null) {
      iX += oImage.offsetLeft;
      iY += oImage.offsetTop;
      oImage = oImage.offsetParent;
    }
//    iX += parseInt(document.body.leftMargin); // Mac IE5
//    iY += parseInt(document.body.topMargin); // Mac IE5
  }
  return [iX,iY];
}

function getXY(sName) {
  var iX = 0;
  var iY = 0;
  var iXwidth;
  var iYheight;
  
  var oTemp = document[sName];  
  if (oTemp != null) {
    iXwidth = oTemp.width;
    iYheight = oTemp.height;
    var aiXY = getXYpos(oTemp);
  }
  else {  
    oTemp = document[sName+"_p2"];  
	var aiXY = getXYpos(oTemp); 
	iXwidth = aiXY[0];
  	iYheight = aiXY[1];
    oTemp = document[sName+"_p1"];  
    var aiXY = getXYpos(oTemp);
	iXwidth -= aiXY[0];
	iYheight -= aiXY[1];
  }	
  return [aiXY[0],aiXY[1],iXwidth,iYheight];
}

function LayerPosShow(layername, iPosX, iPosY) {
  oLayer = oFindLayer(layername);
  if (document.layers) {
    oLayer.left = iPosX;
    oLayer.top = iPosY;
    oLayer.visibility = "show";
  }
  else {
    oLayer.style.left = iPosX;
    oLayer.style.top = iPosY;
    oLayer.style.visibility = "visible";
  }
}

function LayerPosHide(layername, iPosX, iPosY) {
  oLayer = oFindLayer(layername);
  if (document.layers) {
    oLayer.left = iPosX;
    oLayer.top = iPosY;
    oLayer.visibility = "hide";
  }
  else {
    oLayer.style.left = iPosX;
    oLayer.style.top = iPosY;
    oLayer.style.visibility = "hidden";
  }
}

function FlyupShowPos(sLayer, iPosX, iPosY) {
  if (sFlyupName != null) {
    if (sLayer == sFlyupName) { // re-showing nav about to be hidden, cancel
      if (iFlyupTimeout != null) {
        clearTimeout(iFlyupTimeout);
        iFlyupTimeout = null;
      }
    }
    else {		     
      LayerPosHide(sFlyupName,-500,-500); // hide early
    }
  }
  LayerPosShow(sLayer,iPosX,iPosY);
  sFlyupName = sLayer;
}


function FlyupShowHZ(sLayer) {
  if (sFlyupName != null) {
    if (sLayer == sFlyupName) { // re-showing nav about to be hidden, cancel
      if (iFlyupTimeout != null) {
        clearTimeout(iFlyupTimeout);
        iFlyupTimeout = null;
      }
    }
    else {		     
      LayerPosHide(sFlyupName,-500,-500); // hide early
    }
  }
  var aiCoord = getXY("b"+sLayer.substr(1));
  LayerPosShow(sLayer,aiCoord[0],aiCoord[1]+aiCoord[3]);
  sFlyupName = sLayer;
}

function FlyupShowVT(sLayer) {

  if (sFlyupName != null) {
    if (sLayer == sFlyupName) { // re-showing nav about to be hidden, cancel
      if (iFlyupTimeout != null) {
        clearTimeout(iFlyupTimeout);
        iFlyupTimeout = null;
      }
    }
    else {		     
      LayerPosHide(sFlyupName,-500,-500); // hide early
    }
  }
  var aiCoord = getXY("b"+sLayer.substr(1));
  LayerPosShow(sLayer,aiCoord[0]+aiCoord[2]-5,aiCoord[1]+aiCoord[3]-5);
  sFlyupName = sLayer;
}

function FlyupShow(sLayer) {
  if (sFlyupName != null) {
    if (sLayer == sFlyupName) { // re-showing nav about to be hidden, cancel
      if (iFlyupTimeout != null) {
        clearTimeout(iFlyupTimeout);
        iFlyupTimeout = null;
      }
    }
  }
  sFlyupName = sLayer;
}

function FlyupHide(sLayer) {
  if (iFlyupTimeout != null)
    clearTimeout(iFlyupTimeout);
  iFlyupTimeout = setTimeout("FlyupDelayedHide('"+sLayer+"')",200);
}

function FlyupDelayedHide(sLayer) {
  LayerPosHide(sLayer,-500,-500);
  if (sLayer == sFlyupName)
    sFlyupName = null; 
}


// --- CART SECTION ---------------------------------------

function CartRemove(sFormName, iProductID) {
  document.forms[sFormName].cartaction.value = "remove";
  document.forms[sFormName].cartactionid.value = iProductID;
  document.forms[sFormName].submit();
}

function CartReset(sFormName) {
  document.forms[sFormName].cartaction.value = "reset";
  document.forms[sFormName].submit();
}

function CartUpdate(sFormName) {
  document.forms[sFormName].submit();
}

