// identify from the current URL the root context of the web app
   function identifyContext() {
   	 var context = location.pathname.replace(/^(\/[^\/]+).*$/,"$1");
   	 return context;
   }
var _webappContext = identifyContext();

/* This script  sends the focus to the next form element (should one exist) after reaching the specified number of characters in the current field */
/*  Old style 9before portia) */
function autoTab_old(input,len, e) {
    var isNN = (navigator.appName.indexOf("Netscape")!=-1);
    var keyCode = (isNN) ? e.which : e.keyCode;
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if(input.value.length >= len && !containsElement(filter,keyCode)) {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
    }
    function containsElement(arr, ele) {
        var found = false, index = 0;
        while(!found && index < arr.length){
            if(arr[index] == ele){
                found = true;
            } else {
                index++;
            }
        }
        return found;
    }
    function getIndex(input) {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1){
            if (input.form[i] == input){
               index = i;
            }else{
               i++;
            }
        }
        return index;
    }
    return true;
}
/* This script  sends the focus to the next form element (should one exist) after reaching the specified number of characters in the current field */
function autoTab(input,len, e) {
    var isNN = (navigator.appName.indexOf("Netscape")!=-1);
    var keyCode = (isNN) ? e.which : e.keyCode;
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if(input.value.length >= len && !containsElement(filter,keyCode)) {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
    }
    function containsElement(arr, ele) {
        var found = false, index = 0;
        while(!found && index < arr.length){
            if(arr[index] == ele){
                found = true;
            } else {
                index++;
            }
        }
        return found;
    }
    function getIndex(input) {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1){
            if (input.form[i] == input){
               index = i;
            }else{
               i++;
            }
        }
        return index;
    }
    return true;
}//end autoTab()


function setSelectedIndexByValue(selectObject, value)
{
  for (var i = 0; i < selectObject.length; ++i)
  {
    if (selectObject.options[i].value == value)
    {
      selectObject.selectedIndex = i;

      break;
    }
  }
}

function appendParameterAndSubmit(form, actionParameter) {
	var action = form.action;
	if(action.indexOf("?") != -1){
	    action = action + "&" + actionParameter;
	}else{
	    action = action + "?" + actionParameter;
	}
	form.action = action;
    form.submit();
}

function tellAFriend()
{

    var url = _webappContext+'/tellAFriend01.do?url="' + self.location.href + '"';
    window.open(url, 'myWin', 'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=yes, width=770, height=680');
}

 function termsAndConditions()
 {
    var url = _webappContext+'/myaccountReferAFriendTerms.do';
    window.open(url, 'myWin', 'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=yes, width=650, height=550');
 }

function pleaseWaitSubmit(hideDivName, pleaseWaitDivName, obj, action)
{
  // hide the related div
  document.getElementById(hideDivName).style.display = 'none';
  //show please wait div
  document.getElementById(pleaseWaitDivName).style.display = 'block';
  if(obj != null && obj != ""){
      if(action == null || action ==""){
        obj.form.submit();
      }else{
        obj.form.action = action;
        obj.form.submit();
      }
  }//end if
}

function checkRadioButton(form, name, value) {
    var myForm;
    var allForms = document.forms;
    for(var i=0; i < allForms.length; i++){
        if(allForms[i].name==form){
            myForm = allForms[i];
            break;
        }
    }

    for(var j=0;j < myForm.length; j++) {
        if(myForm[j].name==name && myForm[j].type=="radio" && myForm[j].value==value){
           myForm[j].checked = true;
        }
    }

}

function checkNumberAndDot(field, event) {
     return checkData(field, event, "1234567890.");
}

function checkNumber(field, event) {
     return checkData(field, event, "1234567890");
}

function checkNumberAndSeparation(field, event) {
     return checkData(field, event, "1234567890, ");
}

function checkNumberSpaceAndSlash(field, event) {
     return checkData(field, event, "1234567890/ ");
}

function checkDouble(field, event) {
     return checkNumberAndDot(field, event)
}

// limit keys to certain combinations only
function checkData(field, event, strCheck) {
	var key = '';
	var whichCode;
	var keyCode;
	var shift = false;
	if (window.Event) {
		// Netscape code
		whichCode = event.which;
		shift = event.shiftKey;
		keyCode = event.keyCode;
		if (keyCode == 8 ||
			(whichCode == 0 &&
				(keyCode== 9 || keyCode == 46 || keyCode == 37 || keyCode == 39)) ) {
			// accept tab, delete and backspace keys
			return true;
		}
	} else {
		// Microsoft IE code
		whichCode = event.keyCode;
	}
	if (whichCode == 13 || whichCode == 118 || whichCode == 99 || whichCode == 0) {
		// ignore the Enter key
		return true;
	}
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	return true;
}


/*  js for the benji dropdown menus below  */



startList = function() {

if (document.all&&document.getElementById) {

navRoot = document.getElementById("nav");

for (i=0; i<navRoot.childNodes.length; i++) {

node = navRoot.childNodes[i];

if (node.nodeName=="LI") {

node.onmouseover=function() {

this.className+=" over";

  }

  node.onmouseout=function() {

  this.className=this.className.replace(" over", "");

   }

   }

  }

 }

}

