//define global variable args
var args = 'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=600,height=600' ;
var args2 = 'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,' ;
var args3 = 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,' ;
//Set up a help window with attributes 
function OpenHelp( page ){
Help_Win = window.open("/Help/HelpIndex.htm?"+page,"Help_Win","status=1,scrollbars=1,resizable=1,toolbar=0,width=300,height=360") ;
Help_Win.focus() ;
}

//Open a window for BS Searching & Display
function SearchHint( page, px_wide, px_high ){
Hint_Win = window.open("/doc/SearchHints/Index.htm?"+page,"Hint_Win",args2+'width='+px_wide+',height='+px_high) ;
Hint_Win.focus() ;
}

//Open a window for BS Searching & Display
function BS_Search( page ){
BS_Win = window.open("/doc/BS/Index.htm?"+page,"BS_Win",args) ;
BS_Win.focus() ;
}

//Open a window for Process Information
function Process_Info( page ){
Process_Win = window.open("/doc/processes/index.htm?"+page,"Process_Win",args) ;
Process_Win.focus() ;
}

//Pop up window for site seal - code supplied by BT Trustwise
function popUp( url ){
sealWin = window.open(url,"win",args) ;
self.name = "mainWin" ; 
}

//Pop up window - general use
function popUpWin(url,wide,high) {
    popWin=window.open(url,"win",args2+'width='+wide+',height='+high) ;
}

//Pop up window - general use
function popUpVideo(url,wide,high) {
    popWin=window.open(url,"win",args3+'width='+wide+',height='+high) ;
}

//Pop up a processing Video Window
function popUpProcessingVideo(url, w, h)
{
  // center window
  wleft = (screen.width - w) / 2 ;
  wtop = (screen.height - h) / 2 ;
  // IE5 and other old browsers might allow a window that is
  // partially offscreen or wider than the screen. Fix that.
  // (Newer browsers fix this for us, but let's be thorough.)
  if (wleft < 0) {
    w = screen.width ;
    wleft = 0 ;
  }
  if (wtop < 0) {
    h = screen.height ;
    wtop = 0 ;
  }
  // open the window
  var win = window.open(url,
    'video',
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=no, resizable=no') ;
  // Just in case width and height are ignored
  win.resizeTo(w, h) ;
  // Just in case left and top are ignored
  win.moveTo(wleft, wtop) ;
  win.focus() ;
}

// limit form submission to one click - then disable button
var doSubmit = "yes" ;

function checkSubmit(formName) {
    if( doSubmit == "yes" ){
        document.forms[formName].submit() ;
        doSubmit = "no" ;
    }   
}
// call in form opening tag: onSubmit="checkSubmit('formName'); return false"


// Check field input is a number
function checkNumeric(formName, fieldName, fieldDesc) {
  var testField = document.forms[formName].elements[fieldName].value ;
    if( isNaN(testField) ) {
        alert(fieldDesc+"\nmust be a numeric value.") ;
        document.forms[formName].elements[fieldName].value = "" ;
        document.forms[formName].elements[fieldName].focus() ;
    }
}
//call with onBlur="checkNumeric('formName', 'fieldName', 'User Field Description')"


function noImage( ImgRef ){
    document.images[ImgRef].src = "/img/missingImg.gif" ;
}

// functions to validate part number input 

var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var otr = ' &-_(){}[]#@£!$%+=';
 
function isValid(parm,val) {
  if (parm == "") return true;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1){
            return false;
        } 
  }
  return true;
}
 
function isNum(parm) {return isValid(parm,numb);}
function isLower(parm) {return isValid(parm,lwr);}
function isUpper(parm) {return isValid(parm,upr);}
function isAlpha(parm) {return isValid(parm,lwr+upr);}
function isOtrChar(parm) {return isValid(parm,otr);}
function isAlphanum(parm) {return isValid(parm,lwr+upr+numb);}
function isAlpNumChr(parm) {return isValid(parm,lwr+upr+numb+otr);} 


function checkAlpNumChr(formName, fieldName, fieldDesc) {
  var testField = document.forms[formName].elements[fieldName].value ;
    // test for validity    
    if( !isValid(testField,lwr+upr+numb+otr) || testField.length < 3 ) {
        alert(fieldDesc+"\nmust be at least 3 characters long,\ncontain only alphanumeric characters \nand \&\-\_\(\)\{\}\[\]\#\@\£\!\$\%\+\=") ;
        document.forms[formName].elements[fieldName].value = "" ;
        //document.forms[formName].elements[fieldName].focus() ;
    }
}
//call with onBlur="checkAlpNumChr('formName', 'fieldName', 'User Field Description')"

// Cache images for the mouseovers //
search_On = new Image() ;  
search_On.src = "/img/head/search_disc_on.gif" ;
search_Off = new Image() ;  
search_Off.src = "/img/head/search_disc.gif" ;
order_On = new Image() ;  
order_On.src = "/img/head/order_disc_on.gif" ;
order_Off = new Image() ;  
order_Off.src = "/img/head/order_disc.gif" ;
help_On = new Image() ;  
help_On.src = "/img/head/help_disc_on.gif" ;
help_Off = new Image() ;  
help_Off.src = "/img/head/help_disc.gif" ;
FastTrack_On = new Image() ;  
FastTrack_On.src = "/img/head/fasttrack_disc_on.gif" ;
FastTrack_Off = new Image() ;  
FastTrack_Off.src = "/img/head/fasttrack_disc.gif" ;

my_account_On = new Image() ;  
my_account_On.src = "/img/submenu/account/my_account_over.jpg";
my_account_Off = new Image() ;  
my_account_Off.src = "/img/submenu/account/my_account.jpg";
setup_account_On = new Image() ;  
setup_account_On.src = "/img/submenu/account/setup_account_over.jpg";
setup_account_Off = new Image() ;  
setup_account_Off.src = "/img/submenu/account/setup_account.jpg";
invoices_On = new Image() ;  
invoices_On.src = "/img/submenu/account/invoices_over.jpg";
invoices_Off = new Image() ;  
invoices_Off.src = "/img/submenu/account/invoices.jpg";
orders_On = new Image() ;  
orders_On.src = "/img/submenu/account/orders_over.jpg";
orders_Off = new Image() ;  
orders_Off.src = "/img/submenu/account/orders.jpg";
statements_On = new Image() ;  
statements_On.src = "/img/submenu/account/statements_over.jpg";
statements_Off = new Image() ;  
statements_Off.src = "/img/submenu/account/statements.jpg";
users_On = new Image() ;  
users_On.src = "/img/submenu/account/users_over.jpg";
users_Off = new Image() ;  
users_Off.src = "/img/submenu/account/users.jpg";
invoicing_On = new Image() ;  
invoicing_On.src = "/img/submenu/account/invoicing_over.jpg";
invoicing_Off = new Image() ;  
invoicing_Off.src = "/img/submenu/account/invoicing.jpg";
delivery_On = new Image() ;  
delivery_On.src = "/img/submenu/account/delivery_over.jpg";
delivery_Off = new Image() ;  
delivery_Off.src = "/img/submenu/account/delivery.jpg";
queries_On = new Image() ;  
queries_On.src = "/img/submenu/account/queries_over.jpg";
queries_Off = new Image() ;  
queries_Off.src = "/img/submenu/account/queries.jpg";
tracking_On = new Image() ; 
tracking_On.src = "/img/submenu/account/tracking_over.jpg"; 
tracking_Off = new Image() ;
tracking_Off.src = "/img/submenu/account/tracking.jpg";

function getObject( name ){
    if( document.getElementById ){ 
        return document.getElementById( name ) ; 
    }
    else if(document.all) { 
        return document.all[name]; 
    }
    else if(document.layers) { 
        return document.layers[name].document ; 
    }
}


function hilite( imgName ) {
    // Switch Image
    if (document.getElementById(imgName)) {
        document.getElementById(imgName).src = eval(imgName+'_On').src;
    } else {
        document.images[imgName].src = eval(imgName+'_On').src;
    }
}

function lolite( imgName ) {
    // Switch Image
    if (document.getElementById(imgName)) {
        document.getElementById(imgName).src = eval(imgName+'_Off').src;
    } else {
        document.images[imgName].src = eval(imgName+'_Off').src ;
    }
}

function display(obj) {
var el = document.getElementById(obj);
		el.style.display = '';
}

function hide(obj) {
	var el = document.getElementById(obj);
		el.style.display = 'none';
}

activateMenu = function(nav) {
    /* currentStyle restricts the Javascript to IE only */
    if (document.all && document.getElementById(nav).currentStyle) {
       var navroot = document.getElementById(nav);

        /* Get all the list items within the menu */
        var lis=navroot.getElementsByTagName("LI");
        for (i=0; i<lis.length; i++) {

           /* If the LI has another menu level */
            if(lis[i].lastChild.tagName=="UL"){

                /* assign the function to the LI */
                lis[i].onmouseover=function() {

                   /* display the inner menu */
                   this.lastChild.style.display="block";
                }
                lis[i].onmouseout=function() {
                   this.lastChild.style.display="none";
                }
            }
        }
    }
}

/*
window.onload= function(){
   // pass the function the id of the top level UL
   if (document.getElementById('vertnav')) {
       activateMenu('vertnav');
   }
}
*/

function CommitAcUserChanges(f) {
    if ( confirm('You are about to alter account access status.\nAre you certain you wish to continue?') ) {
        f.submit();
    }
}

function info(del_ref,inv_ref,add_1,add_2,add_3,town,county,Pcode_1,Pcode_2) {
    this.del_ref = del_ref;
    this.inv_ref = inv_ref;
    this.add_1 = add_1;
    this.add_2 = add_2;
    this.add_3 = add_3;
    this.town = town;
    this.county = county;
    this.Pcode_1 = Pcode_1;
    this.Pcode_2 = Pcode_2;
}

function populate(f) {
    // get field value to search against 
    var ref = f.Delivery_Address.options[f.Delivery_Address.selectedIndex].value;
    if( ref != "0" ) {
        f.Del_Ref.value = AddressDisplay[ref].del_ref;
        f.Inv_Ref.value = AddressDisplay[ref].inv_ref; 
        f.Disp_Add_Line_1.value = AddressDisplay[ref].add_1; 
        f.Disp_Add_Line_2.value = AddressDisplay[ref].add_2; 
        f.Disp_Add_Line_3.value = AddressDisplay[ref].add_3; 
        f.Disp_Add_Town.value = AddressDisplay[ref].town; 
        f.Disp_Add_County.value = AddressDisplay[ref].county; 
        f.Disp_Add_PCode_1.value = AddressDisplay[ref].Pcode_1; 
        f.Disp_Add_PCode_2.value = AddressDisplay[ref].Pcode_2; 
    }
}

// Checks/Unchecks all checkboxs on form.
function checkAll(f,State) {
	for (a=0; a<f.elements.length; a++) {
		var str = f.elements[a].name;
		if (str.indexOf("Email") == 0) {
			f.elements[a].checked = State ;
		}
	}
}

// submit search.mac/UserStock form.
function submitUserStockForm (f,fnc) {
	test.Function.value = fnc;
	f.submit();
	
} 

// Limit characters entered in text area.
function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    }   
}

// Date input validation within delivery options.

// Day validation.
function dateTestDay( checkField, fieldDesc ){
var testValue = document.forms['SelectDelivery'].elements[checkField].value ;
    if( testValue != "" ) {
        if( isNaN(testValue) ) {
            alert( fieldDesc + " must be a numeric value." ) ;
            document.forms['SelectDelivery'].elements[checkField].value = "" ;        
            document.forms['SelectDelivery'].elements[checkField].focus() ;
        } 
  
        if ( testValue < 1 || testValue > 31 ) {
            alert( fieldDesc + " is not part of a valid date." ) ;
            document.forms['SelectDelivery'].elements[checkField].value = "" ;        
            document.forms['SelectDelivery'].elements[checkField].focus() ;   
        } 
    }
}

// Month validation.
function dateTestMonth( checkField, fieldDesc ){
var testValue = document.forms['SelectDelivery'].elements[checkField].value ;
    if( testValue != "" ) {
        if( isNaN(testValue) ) {
            alert( fieldDesc + " must be a numeric value." ) ;
            document.forms['SelectDelivery'].elements[checkField].value = "" ;        
            document.forms['SelectDelivery'].elements[checkField].focus() ;   
        } 
  
        if ( testValue < 1 || testValue > 12 ) {
            alert( fieldDesc + " is not part of a valid date." ) ;
            document.forms['SelectDelivery'].elements[checkField].value = "" ;        
            document.forms['SelectDelivery'].elements[checkField].focus() ;   
        }
    }    
}

// Year validation. 
function dateTestYear( checkField, scope, fieldDesc ){
var testValue = document.forms['SelectDelivery'].elements[checkField].value ;
    if( testValue != "" ) {
        if( isNaN(testValue) ) {
            alert( fieldDesc + " must be a numeric value." ) ;
            document.forms['SelectDelivery'].elements[checkField].value = "" ;        
            document.forms['SelectDelivery'].elements[checkField].focus() ;   
        }
          
        if ( testValue < scope || (testValue - 1) > scope ) {
            alert( fieldDesc + " is not part of a valid date.\nYear should be in the format CCYY." ) ;
            document.forms['SelectDelivery'].elements[checkField].value = "" ;
            document.forms['SelectDelivery'].elements[checkField].focus() ;
        }
    }
}    


// Open and position material grade selection window.
function openGradeSelect(url) {
    gradeSelect = window.open(url, 'gradeSelect', 'width=300,height=135');
    gradeSelect.moveTo(500,400);
}

// Navigate to product page from material grade selection window. 
function productNavigation(url) {
    window.opener.location.href = url;
    self.close(); 
}

// Matrix Helper
var showMatrix=false;

function ToggleMatrixCrossHair(){
    if (!showMatrix){
        showMatrix=true;
        document.getElementById("toggleMatrix").value="Disable Matrix Helper";
    } else if (showMatrix){
        showMatrix=false;
        document.getElementById("toggleMatrix").value="Enable Matrix Helper";
       
    }
}

function HighlightMatrixCrossHair(x,y,s){
    if(showMatrix){
        var tab=document.getElementById("matrixTable");
        for (var yItem = 0; yItem < tab.rows.length; yItem++){
            for (var xItem = 0; xItem < tab.rows[yItem].cells.length; xItem++){
                if(xItem==x){
                    tab.rows[yItem].cells[xItem].style.backgroundColor=s;
                }else{
                    tab.rows[y].cells[xItem].style.backgroundColor=s;
                }
            }
        }
    }
}

function InitMatrixTable(){
    var tab=document.getElementById("matrixTable");
    for (var yItem = 0; yItem < tab.rows.length; yItem++){
        for (var xItem = 0; xItem < tab.rows[yItem].cells.length; xItem++){
            tab.rows[yItem].cells[xItem].onmouseover=new Function("HighlightMatrixCrossHair("+xItem+","+yItem+",'#FFFF77');");
            tab.rows[yItem].cells[xItem].onmouseout=new Function("HighlightMatrixCrossHair("+xItem+","+yItem+",'');");
        }
    }
}
