// test to see that this is within the frameset
// if( top.frames.length == 0 ){
// top.window.location="/"
// }


//Mouse Over action for help and basket buttons 
//Cache Required Images
Help_On = new Image() ;  
Help_On.src = "/img/buttons/Help_Button_On.gif" ;
Help_Off = new Image() ;  
Help_Off.src = "/img/buttons/Help_Button.gif" ;
Basket_On = new Image() ;  
Basket_On.src = "/img/buttons/Basket_Button_On.gif" ;
Basket_Off = new Image() ;  
Basket_Off.src = "/img/buttons/Basket_Button.gif" ;
Menuoff = new Image() ;
Menuoff.src = "/img/buttons/FP_left-menu-off.gif" ; 
Menuon = new Image() ; 
Menuon.src = "/img/buttons/FP_left-menu-on.gif" ;
//function.

function SwapImage(ImgName,NewImg) {
    document.images[ImgName].src= eval(NewImg+'.src')
}

/* Replace a missing image with a blank transparent image using onError in img tag*/ 
function failedImage(ImageRef) { 
    document.images[ImageRef].src='/img/transparent.gif' 
} 

/* Replace a missing image with a "No image available" image using onError in img tag*/ 
function failedImage2(ImageRef) { 
    document.images[ImageRef].src='/img/noimage.jpg' 
} 

//Set up a help window with attributes 
function OpenHelp(Context) {
    var newWindow = window.open("/Help/HelpIndex.htm?"+Context,"HelpWindow","status,scrollbars,resizable=1,toolbar=0,width=300,height=360");
    newWindow.focus()
}

//Open a window to show tables for Flanges in Valves and Fittings 
function showReturnForm() {
    var ReturnForm = window.open("/Macros/Information/Returns.mac/ReturnsForm","ReturnForm","status,scrollbars,menubar=yes,toolbar=yes,width=520,height=520");
    ReturnForm.focus()
}


//Open a window to show tables for Flanges in Valves and Fittings 
function showFlangeTables() {
    var FlangeWindow = window.open("/Help/FlangeTables.htm","FlangeWindow","status,scrollbars,toolbar=no,width=575,height=500");
    FlangeWindow.focus()
}


//Open a window to show intro for Kee Klamps
function showKeeKlamps() {
    var KeeKlampsWindow = window.open("/Help/KeeKlamps.htm","KeeKlampsWindow","status,scrollbars=auto,toolbar=no,width=440,height=480");
    KeeKlampsWindow.focus()
}

//Pop up window for site seal - code supplied by BT Trustwise
function popUp(url) {
    sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0, status=1,menubar=1,scrollbars=1,resizable=1,width=650,height=450');
    self.name = "mainWin"; }

//Pop up window - general use
function popUpWin(url,wide,high) {
    popWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width='+wide+',height='+high) ;
}

//Pop up window - fixed size, no options, general use
function popUpFixedWin(url,wide,high) {
    popWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width='+wide+',height='+high+',top=175') ;
}


// 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')"


/* Quick search processing  */ 
function quickSearch(vID, sts) {
	  var searchStr = document.getElementById("searchBox").value;
	  searchStr = searchStr.replace(/-/g, "");
	  if(isNaN(searchStr)) {
		    location.replace('/macros/Search.mac/ProductResults?VisitorID=' + vID + '&Status=' + sts + '&SearchType=prod&SearchString=' + searchStr);            
	  } else {
		    // location.replace('/macros/Search.mac/Results?VisitorID=' + vID + '&Status=' + sts + '&SearchType=stock&SearchString=' + searchStr);    	
		    location.replace('/StockResults.php?str=' + searchStr);
	  }  
}


// 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')"

function imgSwap(replaceImg){
  document.images['featureImg'].src = '../../../doc/PDFLIT/Images/' + replaceImg ;
}

function setBackgroundImage (id, imageURL) {
  if (document.layers)
    document[id].background.src = imageURL == 'none' ? null : imageURL;
  else if (document.all)
    document.all[id].style.backgroundImage = imageURL == 'none' ? 'none' 
: 'url(' + imageURL + ')';
  else if (document.getElementById)
    document.getElementById(id).style.backgroundImage = imageURL == 
'none' ? 'none' : 'url(' + imageURL + ')';
}

/* Script: CountDown Timer */
// Update display (strContent, countdownId)
function CD_UD(s, id) {
    CD_OBJS[id].innerHTML = s;
};

// Tick (countdownId, eventDate)
function CD_T(id, e) {
    var n   = new Date();
    var p   = 1E3 - n.getMilliseconds();
    setTimeout("CD_T('" + id + "', " + e + ")", p);
    CD_D(+n, id, e);
};

// Calculate time and invoke draw routine (dateNow, countdownId, eventDate)
function CD_D(n, id, e) {
    var ms = e - n;
    if (ms <= 0) ms *= -1;
    var d = Math.floor(ms / 864E5);
    ms -= d * 864E5;
    var h = Math.floor(ms / 36E5);
    ms -= h * 36E5;
    var m = Math.floor(ms / 6E4);
    ms -= m * 6E4;
    var s = Math.floor(ms / 1E3);
    CD_UD(d + " day" + (d == 1 ? " " : "s ") + CD_ZP(h) + "h " + CD_ZP(m) + "m " + CD_ZP(s) + "s", id);
};

// Prefix single integers with a zero
function CD_ZP(i) {
    return (i<10 ? "0" + i : i);
};

// Initialisation
function CD_Init() {
    var pref = "countdown";
    var objH = 1; // temp boolean true value
    if (document.getElementById || document.all) {
        for (var i=1; objH; ++i) {
            var id  = pref + i;
            objH    = document.getElementById ? document.getElementById(id) : document.all[id];

            if (objH && (typeof objH.innerHTML) != 'undefined') {
                var s   = objH.innerHTML;
                var dt  = new Date(s);
                if (!isNaN(dt)) {
                    CD_OBJS[id] = objH; // Store global reference to countdown element object
                    CD_T(id, dt.valueOf());
                    if (objH.style) {
                        objH.style.visibility = "visible";
                    }
                }
                else {
                    objH.innerHTML = s + "<a href=\"http://andrewu.co.uk/clj/countdown/\" title=\"Countdown Error: Invalid date format used, check documentation (see link)\">*</a>";
                }
            }
        }
    }
};

var CD_OBJS = new Object();

// Try not to commandeer the default onload handler if possible
if (window.attachEvent) {
    window.attachEvent('onload', CD_Init);
}
else if (window.addEventListener) {
    window.addEventListener("load", CD_Init, false);
}
else {
    window.onload = CD_Init;
}
/* End of Countdown Timer */

// Cache images for mouseovers.
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";

function hilite( imgName ) {
    // Switch Image
    if (document.getElementById(imgName)) {
    	  //alert("1" + imgName);
        document.getElementById(imgName).src = eval(imgName+'_On').src;
    } else {
    	  //alert("2" + imgName);
        document.images[imgName].src = eval(imgName+'_On').src;
    }
}

function lolite( imgName ) {
    // Switch Image
    if (document.getElementById(imgName)) {
    	  //alert("3" + imgName);
        document.getElementById(imgName).src = eval(imgName+'_Off').src;
    } else {
    	  //alert("4" + imgName);
        document.images[imgName].src = eval(imgName+'_Off').src ;
    }
}

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 ;
		}
	}
}

// Handle product page paging controls.
function productPaging(num,newURL) { 
 
    // Check for entered quantities. 
    var qntyFound = "N";
	  for (i=1; i<=num; i++ ) {
	  	  var qnty = document.getElementById(i).value;
	  	  if( qnty != "" ) {
            qntyFound = "Y";
        }	 
	  }
	  
	  //Present a warning message if any quantities found, otherwise handle the paging.  
	  if( qntyFound == "N" ) {
	      document.location.href = newURL;
	  } else {
	  	  alert("Selected product lines must be added to the\n basket before browsing other product lines.");
	  }	
}

// submit search.mac/Results form.
function submitResultsForm(f,fnc) {
	f.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);
    }   
}

// Open PDF certificates. 
function openCertificate ( inString ) {
	  var fileName = inString.split(',');
	  for (i = 0; i < fileName.length - 1; i++) {
   	    window.open("/doc/Certificates/" + fileName[i] + "#view=FitH","_blank","status=yes,toolbar=no,menubar=no,location=no");
	  }
}	

// Display object.
function display(obj) {
var el = document.getElementById(obj);
		el.style.display = '';
}

// Hide object. 
function hide(obj) {
	var el = document.getElementById(obj);
		el.style.display = 'none';
}