/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#
#   heals.js
#   Lance Keay
#   www.e-InBusiness.co.uk 01494 614 530
#
#   javascript module for heal's
#
#
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */

/* ***************************
Domino Systems
****************************
Navigation functions
*************************** */

// find out the browser version
var isDOM = (document.getElementById ? true : false);
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);

/* function getRef()
  get DOM reference */
function getRef(id){
  if (isDOM) return document.getElementById(id);
  if (isIE4) return document.all[id];
  if (isNS4) return document.layers[id];
}

/* function getSty()
  get stle if needed */
function getSty(id){
  return (isNS4 ? getRef(id) : getRef(id).style);
}

var popTimer = 0;
var litNow = new Array();   // higlighted menu array

//  function popOver()
function popOver(menuNum, itemNum){
  clearTimeout(popTimer);
  hideAllBut(menuNum);
  litNow = getTree(menuNum, itemNum);
  changeCol(litNow, true);
  targetNum = menu[menuNum][itemNum].target;
  if (targetNum > 0){
    thisX = parseInt(menu[menuNum][0].ref.left) + parseInt(menu[menuNum][itemNum].ref.left);
    thisY = parseInt(menu[menuNum][0].ref.top) + parseInt(menu[menuNum][itemNum].ref.top);

    with (menu[targetNum][0].ref){
      left = parseInt(thisX + menu[targetNum][0].x);
      top = parseInt(thisY + menu[targetNum][0].y);
      visibility = 'visible';
    }
  }
}

//  function popOut()
function popOut(menuNum, itemNum) {
  if ((menuNum == 0) && !menu[menuNum][itemNum].target)
    hideAllBut(0)
  else
    popTimer = setTimeout('hideAllBut(0)', 500);
}

//  function getTree()
function getTree(menuNum, itemNum) {

  itemArray = new Array(menu.length);   // menu number

  while(1) {
    itemArray[menuNum] = itemNum;
    // If we've reached the top of the hierarchy, return.
    if (menuNum == 0) return itemArray;
      itemNum = menu[menuNum][0].parentItem;
      menuNum = menu[menuNum][0].parentMenu;
    }
}

//  function changeCol()
// Pass an array and a boolean to specify colour change
function changeCol(changeArray, isOver) {
  for (menuCount = 0; menuCount < changeArray.length; menuCount++) {
    if (changeArray[menuCount]) {
      newCol = isOver ? menu[menuCount][0].overCol : menu[menuCount][0].backCol;
      // Change the colours of the div/layer background.
      with (menu[menuCount][changeArray[menuCount]].ref) {
        if (isNS4) bgColor = newCol;
        else backgroundColor = newCol;
      }
    }
  }
}

//  function hideAllBut()
function hideAllBut(menuNum) {
  var keepMenus = getTree(menuNum, 1);
  for (count = 0; count < menu.length; count++)
    if (!keepMenus[count])
      menu[count][0].ref.visibility = 'hidden';
      changeCol(litNow, false);
}

//  function Menu()
// sets the parameters for the menu
function Menu(isVert, popInd, x, y, width, overCol, backCol, borderClass, textClass) {

  this.isVert = isVert; // vertical menu
  this.popInd = popInd // popput indicator
  this.x = x;  // screen x position
  this.y = y;  // screen y position
  this.width = width;   // width
  this.overCol = overCol;  // mouseover colour
  this.backCol = backCol;  // background colour
  this.borderClass = borderClass;  // border class
  this.textClass = textClass;     // text class
  this.parentMenu = null;
  this.parentItem = null;
  this.ref = null;
}

//  function Item()
// sets the parameters for the object
function Item(text, href, frame, length, spacing, target, style) {
  this.text = text;
  this.href = href;
  this.frame = frame;
  this.length = length;
  this.spacing = spacing;
  this.target = target;
  this.ref = null;
  this.style = style;
}

function writeMenus() {
if (!isDOM && !isIE4 && !isNS4) return;

for (currMenu = 0; currMenu < menu.length; currMenu++) with (menu[currMenu][0]) {
  // Variable for holding HTML for items and positions of next item.
  var str = '', itemX = 0, itemY = 0;

  for (currItem = 1; currItem < menu[currMenu].length; currItem++) with (menu[currMenu][currItem]) {
    var itemID = 'menu' + currMenu + 'item' + currItem;

    // The width and height of the menu item - dependent on orientation!
    var w = (isVert ? width : length);
    var h = (isVert ? length : width);

    // div  for ie
    if (isDOM || isIE4) {
      str += '<div id="' + itemID + '" style="border: '+ frame +'px solid #DFE0EA;position: absolute; left: ' + itemX + '; top: ' + itemY + '; width: ' + w + '; height: ' + h + '; visibility: inherit; ';
      if (backCol) str += 'background: ' + backCol + '; ';
        str += '" ';
    }
    // layer for ns4
    if (isNS4) {
      str += '<layer id="' + itemID + '" left="' + itemX + '" top="' + itemY + '" width="' +  w + '" height="' + h + '" visibility="inherit" ';
      if (backCol) str += 'bgcolor="' + backCol + '" ';
    }
    if (borderClass) str += 'class="' + borderClass + '" ';

    // Add mouseover handlers and finish div/layer.
    str += 'onMouseOver="popOver(' + currMenu + ',' + currItem + ')" onMouseOut="popOut(' + currMenu + ',' + currItem + ')">';

    // Add contents of item (default: table with link inside).
    // In IE/NS6+, add padding if there's a border to emulate NS4's layer padding.
    // If a target frame is specified, also add that to the <a> tag.

    str += '<table width="' + (w - 8) + '" border="0" cellspacing="0" cellpadding="' + (!isNS4 && borderClass ? 3 : 0) + '"><tr onclick="document.location.href = \'' + href + '\'" style=\'cursor:pointer; cursor:hand;\'><td width="1"></td><td align="left" height="' + (h - 7) + '" style=\'cursor:pointer; cursor:hand;\'>' + '<a class="' + style + '" href="' + href + '">' + text + '</a></td>';
    //str += '<table width="' + (w - 8) + '" border="0" cellspacing="0" cellpadding="' + (!isNS4 && borderClass ? 3 : 0) + '"><tr><td width="1"><img src=#StoreImagePath/images/blank.gif width=1 height=1 border=0></td><td align="left" height="' + (h - 7) + '">' + '<a class="' + style + '" href="' + href + '">' + text + '</a></td>';
    if (target > 0) {
      // Set target's parents to this menu item.
      menu[target][0].parentMenu = currMenu;
      menu[target][0].parentItem = currItem;

      // Add a popout indicator.
      if (popInd) str += '<td class="' + textClass + '" align="right">' + popInd + '</td>';
    }
    str += '</tr></table>' + (isNS4 ? '</layer>' : '</div>');
    if (isVert) itemY += length + spacing;
    else itemX += length + spacing;
  }

// ie
if (isDOM) {
  var newDiv = document.createElement('div');
  document.getElementsByTagName('body').item(0).appendChild(newDiv);
  newDiv.innerHTML = str;
  ref = newDiv.style;
  ref.position = 'absolute';
  ref.visibility = 'hidden';
}

// Insert a div tag to the end of the BODY with menu HTML in place for IE4.
if (isIE4) {
  document.body.insertAdjacentHTML('beforeEnd', '<div id="menu' + currMenu + 'div" ' + 'style="position: absolute; border: 1px solid #DFE0EA; visibility: hidden">' + str + '</div>');
  ref = getSty('menu' + currMenu + 'div');
}

// In NS4, create a reference to a new layer and write the items to it.
if (isNS4) {
  ref = new Layer(0);
  ref.document.write(str);
  ref.document.close();
}

for (currItem = 1; currItem < menu[currMenu].length; currItem++) {
itemName = 'menu' + currMenu + 'item' + currItem;
if (isDOM || isIE4) menu[currMenu][currItem].ref = getSty(itemName);
if (isNS4) menu[currMenu][currItem].ref = ref.document[itemName];
   }
}
with(menu[0][0]) {
ref.left = x;
ref.top = y;
ref.visibility = 'visible';
   }
}

var menu = new Array(); // create new menu array

// set the default values
var defOver = '#CECDD3', defBack = '#ffffff';
var defLength = 22;



// resize fix for ns4
var popOldWidth = window.innerWidth;
nsResizeHandler = new Function('if (popOldWidth != window.innerWidth) location.reload()');

// capture ns4 mouse clicks
if (isNS4) document.captureEvents(Event.CLICK);
document.onclick = clickHandle;

//  function clickHandle()
function clickHandle(evt){
 if (isNS4) document.routeEvent(evt);
 hideAllBut(0);
}

//  function moveRoot()
// moving command for the example.
function moveRoot(){
 with(menu[0][0].ref) left = ((parseInt(left) < 100) ? 100 : 5);
}


/****************************
 end Navigation.js
****************************/









/****************************
 generic functions
****************************/


function doSubmit(){
    if (document.emailForm.email.value == "" || document.emailForm.email.value == "\n" ||  document.emailForm.email.value == ' ' || document.emailForm.email.value == 'your email')
        {
          document.emailForm.email.value ='your email';
         }
      else
        {
           document.emailForm.action="http://#URL_UserTemplate/81";
           document.emailForm.submit();
           var vtemp=self.location.href;
           self.location.href=vtemp;
        }

} //end doSubmit


/* this one is for the promo pages, or wherever you want the submit to go to the homepage */

function doSubmit2(){
    //alert(vStore);
    if (document.emailForm.email.value == "" || document.emailForm.email.value == "\n" ||  document.emailForm.email.value == ' ' || document.emailForm.email.value == 'your email')
        {
          document.emailForm.email.value ='your email';
         }
      else
        {
           document.emailForm.action="http://" + vStore + "/UserTemplate/131";
           document.emailForm.submit();
           self.location.href="http://" + vStore + "/Home";
        }

} //end doSubmit




//----------------------------------------------------------------------------------------------
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    Sitestat is used to measure page hits.  sitestat_referrer is used on entry pages only.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

/*
function sitestat(ns_l){ns_l+="&ns__t="+(new Date()).getTime();ns_pixelUrl=ns_l;
if(document.images){ns_1=new Image();ns_1.src=ns_l;}else
document.write("<img src="+ns_l+" width=1 height=1>");}

"Normal measurement without referrers"

*/

//Normal measurement with referrers
function sitestat(ns_l)
{

ns_l+="&ns__t="+(new Date()).getTime();
ns_pixelUrl=ns_l;

ns_0=document.referrer;
ns_0=(ns_0.lastIndexOf("/")==ns_0.length-1)?ns_0.substring(ns_0.lastIndexOf("/"),0):ns_0;

if(ns_0.length>0)ns_l+="&ns_referrer="+escape(ns_0);
if(document.images){ns_1=new Image();ns_1.src=ns_l;}else
document.write("<img src="+ns_l+" width=1 height=1>");

}







//----------------------------------------------------------------------------------------------

function jump(catalog){
     window.location.href="http://#URL_CachedCatalog/"+catalog;
}


function openWindow(URL,wndName,wndAttributes) {
    var eURL    = URL;
    var eName   = escape(wndName);
    var eAtt    = wndAttributes;
    //alert(eURL +"  "+ eName +"  "+ eAtt);

    window.open(eURL,eName,eAtt);
}




//Functions from Registration page
//moved 4Oct - BS

function makelower(){
//not using the login field anymore...
//document.forms.RegForm.login.value = document.forms.RegForm.login.value.toLowerCase();
document.forms.RegForm.passwd.value = document.forms.RegForm.passwd.value.toLowerCase();
document.forms.RegForm.passwdc.value = document.forms.RegForm.passwdc.value.toLowerCase();
}

function unjoin(){
fname = document.forms.RegForm.name1.value;
endpoint = 0;
current = "";
for (i=0; i < document.forms.RegForm.title.options.length;i++){
current = document.forms.RegForm.title.options[i].text;
if ((fname.indexOf(current) != -1) && fname.length>0){
endpoint = document.forms.RegForm.title.options[i].text.length+1;
fname =  fname.substring(endpoint,fname.length);
document.forms.RegForm.title.selectedIndex = i;
}
}
document.forms.RegForm.name1.value = fname;
}

function jointitle(){
document.forms.RegForm.name1.value = document.forms.RegForm.title.options[document.forms.RegForm.title.selectedIndex].text + " " + document.forms.RegForm.name1.value;
makelower();
}


// used on the registration form
function submitThis()
{
    //document.forms.RegForm.login.value = document.forms.RegForm.email.value;
    //alert(document.forms.RegForm.login.value +' - '+ document.forms.RegForm.email.value);

// replace all ampersands with the word 'and'
    var answer = '';
    for (var i = 0; i<document.RegForm.elements.length; i++) {
        if ((document.RegForm.elements[i].type == 'text')) {
            document.RegForm.elements[i].value = document.RegForm.elements[i].value.replace(/\&/g, ' and ');
        }
    }


// check for duplicate password
if(document.forms.RegForm.passwd.value != document.forms.RegForm.passwdc.value || (!document.forms.RegForm.passwd.value)){alert('please verify that your password entries match.');document.forms.RegForm.passwd.focus();return;}

// check for blank fields
if(!document.forms.RegForm.email.value){alert('please enter an email address.');document.forms.RegForm.email.focus();return;}
if(!document.forms.RegForm.name1.value){alert('please enter a name.');document.forms.RegForm.name1.focus();return;}
if(!document.forms.RegForm.phone.value){alert('please enter a phone number.');document.forms.RegForm.phone.focus();return;}
if(!document.forms.RegForm.name2.value){alert('please enter an address.');document.forms.RegForm.name2.focus();return;}
if(!document.forms.RegForm.city.value){alert('please enter a city or town.');document.forms.RegForm.city.focus();return;}
if(!document.forms.RegForm.zipcode.value){alert('please enter a postal code.');document.forms.RegForm.zipcode.focus();return;}

// check for blank shipping fields

if(!document.forms.RegForm.sh_phone.value && !document.forms.RegForm.sh_name1.value){document.forms.RegForm.sh_phone.value = document.forms.RegForm.phone.value;}
if(!document.forms.RegForm.sh_name2.value && !document.forms.RegForm.sh_name1.value){document.forms.RegForm.sh_name2.value = document.forms.RegForm.name2.value;}
if(!document.forms.RegForm.sh_name3.value && !document.forms.RegForm.sh_name1.value){document.forms.RegForm.sh_name3.value = document.forms.RegForm.name3.value;}
if(!document.forms.RegForm.sh_city.value && !document.forms.RegForm.sh_name1.value){document.forms.RegForm.sh_city.value = document.forms.RegForm.city.value;}
if(!document.forms.RegForm.sh_state.value && !document.forms.RegForm.sh_name1.value){document.forms.RegForm.sh_state.value = document.forms.RegForm.state.value;}
if(!document.forms.RegForm.sh_zipcode.value && !document.forms.RegForm.sh_name1.value){document.forms.RegForm.sh_zipcode.value = document.forms.RegForm.zipcode.value;}
if(!document.forms.RegForm.sh_name1.value){document.forms.RegForm.sh_name1.value = document.forms.RegForm.name1.value;}


document.forms.RegForm.submit();
}


// used on the change billing address form
function submitBillingAddressChange()
{

// replace all ampersands with the word 'and'
    var answer = '';
    for (var i = 0; i<document.RegForm.elements.length; i++) {
        if ((document.RegForm.elements[i].type == 'text')) {
            document.RegForm.elements[i].value = document.RegForm.elements[i].value.replace(/\&/g, ' and ');
        }
    }
    document.forms.RegForm.submit();
}



function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function showShipping()
{
MM_showHideLayers('Layer1','','show','Layer2','','show','Layer3','','show','Layer4','','show','Layer5','','show','Layer6','','show','Layer7','','show','Layer8','','show','Layer9','','show','Layer10','','show');
//document.all.sn1.style.visibility = 'show';
//alert(document.styleSheets[1].name);
}






//Functions from the Credit Card Entry Page

function creditSubmit(vForm, vURL)
{
    var vDate = new Date();
    var vYr = vDate.getFullYear();
    var vMo = vDate.getMonth()+1;

   // alert(vMo);
    //return false;

    // check type of card
    if( vForm.cardtype.selectedIndex == 0)
    {
        alert("Please enter a valid card type.");
        vForm.cardtype.focus();
        return(false);
    }

    // check issue date, but only if they put it in.
    if(vForm.start_year.selectedIndex != 0 || vForm.start_month.selectedIndex != 0){
        if(vForm.start_year.value >= vYr)
        {
            if(vForm.start_year.value == vYr && vForm.start_month.value > vMo)
            {
                alert("Please enter a valid expiry date.");
                vForm.exp_year.focus();
                return(false);
            }
            if(vForm.start_year.value > vYr)
            {
                alert("Please enter a valid expiry date.");
                vForm.exp_year.focus();
                return(false);
            }
        }
    }

    // check expiry date
    if(vForm.exp_year.selectedIndex == 0){
        alert("Please enter a valid expiry date.");
        vForm.exp_year.focus();
        return(false);
    }
    if( vForm.exp_month.selectedIndex == 0){
        alert("Please enter a valid expiry date.");
        vForm.exp_month.focus();
        return(false);
    }
    if(vForm.exp_year.value <= vYr)
    {
        if(vForm.exp_year.value == vYr && vForm.exp_month.value < vMo)
        {
            alert("Please enter a valid expiry date .");
            vForm.exp_year.focus();
            return(false);
        }
        if(vForm.exp_year.value < vYr)
        {
            alert("Please enter a valid expiry date .");
            vForm.exp_year.focus();
            return(false);
        }
    }


    // check credit card number
    vForm.num.value = vForm.num.value.replace(/[^\d]/g, '');  //clean cc number
    if( !LuhnCheck(vForm.num) || !vForm.num.value.match(/\d{9}/g)){
        alert("Please check your credit card number and try again.");
        vForm.num.focus();
        return false;
    }

    // check CSC
    vForm.cv2.value = vForm.cv2.value.replace(/[^\d]/g, '');  //clean csc number
    if( !vForm.cv2.value || !vForm.cv2.value.match(/\d/g) ){
        alert("Please check your credit card security code number and try again.");
        vForm.cv2.focus();
        return false;
    }

    // check issue number for switch
    vForm.issue.value = vForm.issue.value.replace(/[^\d]/g, '');  //clean issue number
    if( vForm.cardtype.selectedIndex == 3 && vForm.issue.value && !vForm.issue.value.match(/\d/g) ){
        alert("Please check your issue number and try again.");
        vForm.issue.focus();
        return false;
    }


    //alert("end creditSubmit");
    return true;
}

function addStar(vForm)
{
    if( vForm.cardtype.selectedIndex == 3){ document.all.issueStar.style.visibility = 'visible' }
    else{ document.all.issueStar.style.visibility = 'hidden' }
}


/* Luhn check - only for version 4 browsers */

function LuhnCheck(obj) {
//
// LuhnCheck
// Copyright 1998 DataCash Ltd (http://www.datacash.com)
// Calculates the Luhn check didigit of the input.
//
var cardnumber = obj.value;
var re = /[^0-9]/g;
cardnumber = cardnumber.replace(re, '');

if (cardnumber.length == 0) return true;

var store_card = cardnumber;
var total = 0;
var d = 0;
while (cardnumber.length != 0) {

total += parseInt(cardnumber.substring(cardnumber.length - 1, cardnumber.length));
cardnumber = cardnumber.substr(0, cardnumber.length - 1);
d = parseInt(cardnumber.substring(cardnumber.length - 1, cardnumber.length));
cardnumber = cardnumber.substr(0, cardnumber.length - 1);
d = isNaN(d) ? 0 : d;
if (d < 9)
total += ((d * 2) % 9);
else
total += 9;
}
if (total % 10 == 0) {
return true;
} else {
return false;
}
}

/* Called when a date field is changed (field points to our field, otherfield to the other field in this month/year pair). If it's null, set the other field to null. If it's non-null, make sure the other is as well and reset the issue number field. */

function change_date (field, otherfield) {
if (field.selectedIndex==0) {
otherfield.selectedIndex=0;
} else {
//field.form.issue.selectedIndex=0;
}
}

/* Called when the issue number is changed If it's now non-null, resets the start date fields. */

function change_issue (field) {
if (field.selectedIndex!=0) {
//field.form.start_month.selectedIndex=0;
//field.form.start_year.selectedIndex=0;
}
}

//open window function for the verisign thingie
   function open_win(url)
   {
           if (navigator.appName == "Netscape")
           {
                var siteseal_win = window.open(url, "win", "toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=0,resizable=0,width=645,height=540");
                   siteseal_win.focus();
           }

           if (navigator.appName == "Microsoft Internet Explorer")
           {
                   counter++;
                   newWin = "win" + counter;
                   var siteseal_win = window.open(url, newWin, "toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=0,resizable=0,width=645,height=540");
           }
   }


/* Called when we click on the submit button - complains if this was
   the second time */
var clicked=false;
function donotdoubleclick ()
{
    if (clicked) {
        alert("You have already sent your payment. Clicking the submit button again could lead to you being charged twice. It will take 10 to 15 seconds for your transaction to complete, please be patient.");
        return false;
    } else {
        clicked=true;
        return true;
    }
}


function openWin()
{
    window.open('http://#URL_UserTemplate/87','','width=400,height=430,scrollbars=no');
}

function checkMe(passedString)
{
    var s = new String(passedString);
    if(s.search(/\w+/)){return true;}
    else {return false;}
}

