/*AutoMend Java File...
*/

if(!Array.indexOf){
  Array.prototype.indexOf = function(obj){
   for(var i=0; i<this.length; i++){
    if(this[i]==obj){
     return i;
    }
   }
   return -1;
  }
}

function Ajax(url)
{
	var xmlHttp=null;
	
	try
	{
		// Firefox, Opera 8.0+, Safari, IE7
		xmlHttp=new XMLHttpRequest();
	}
	catch(e)
	{
		// Old IE
		try
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			alert ("Your browser does not support XMLHTTP!");
			return;
			// Do what in this scenario then? Their browser does not support Ajax
		}
	}
	url=url+"&sid="+Math.random();
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);

	return xmlHttp.responseText;
}

function UnTick(frm,cbname)
{
	// unticks all checkboxes matching pattern
	tick = 0;

    // find ALL form fields called 'cbname' and tick them!
    for (var i=0; i < frm.elements.length; i++)
    {
		form_field = frm.elements[i];
		field_name = form_field.name;
		if (field_name.substring(0,cbname.length) == cbname)
		{
			form_field.checked = tick;
		}
    }
}

function trim(str, chars) 
{
    return ltrim(rtrim(str, chars), chars);
}

function se(ele) 
{
   try { document.getElementById(ele).style.display = ''; } catch(err) { }
}

function he(ele) 
{
   try { document.getElementById(ele).style.display = 'none'} catch(err) { }
}

function tve(ele) 
{
   ele = document.getElementById(ele);
   if (ele)
   {
	   try 
	   {
		   if (ele.style.display == 'block' || ele.style.display == '')
			  ele.style.display = 'none';
		   else
			  ele.style.display = 'block';
	   }
	   catch (err)
	   {
	   }
   }
}
function ltrim(str, chars)
{
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) 
{
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function AddSlctOption(obj,value,text)
{
	var option = document.createElement("option");
	option.text = text;
	option.value = value;
		
	if (document.all && !window.opera)
	{
		obj.add(option);
	}
	else
	{
		obj.add(option, null);
	}
}
function UpdateTotal(frm) {
  order_total = 0;
  for (var i=0; i < frm.elements.length; i++)
  {
    form_field = frm.elements[i];
    field_name = form_field.name;
    if (field_name.substring(0,5) == 'Price')
    {
      item_value = parseFloat(form_field.value);
      if (item_value >= 0) {
        order_total += item_value;
      }
    }
  }
  frm.total.value = round_decimals(order_total, 2);
}

function UpdateTotals(frm) {
  order_total = 0;
  for (var i=0; i < frm.elements.length; i++)
  {
    form_field = frm.elements[i];
    field_name = form_field.name;
    if (field_name.substring(0,5) == 'Price')
    {
      item_value = parseFloat(form_field.value);
      if (item_value >= 0) {
        order_total += item_value;
      }
    }
  }
  frm.vat.value        = pad_with_zeros(((Math.floor(order_total * 0.15 * 100))/100),2); 
  frm.total.value      = pad_with_zeros(((Math.floor(order_total * 100))/100), 2);
  frm.grandtotal.value = pad_with_zeros(((Math.floor(order_total * 1.15 * 100))/100), 2);
}

function round_decimals(original_number, decimals) {
  var result1 = original_number * Math.pow(10, decimals)
  var result2 = Math.round(result1)
  var result3 = result2 / Math.pow(10, decimals)
  return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {
  var value_string = rounded_value.toString()
  var decimal_location = value_string.indexOf(".")
  if (decimal_location == -1) {
     decimal_part_length = 0
     value_string += decimal_places > 0 ? "." : ""
  }
  else {
     decimal_part_length = value_string.length - decimal_location - 1
  }
  var pad_total = decimal_places - decimal_part_length
  if (pad_total > 0) {
  for (var counter = 1; counter <= pad_total; counter++)
        value_string += "0"
  }
	if (pad_total < 0)
	{
		// Possibly too many decimal places
		value_string = substr(value_string, 0, decimal_location + 2);
	}
    return value_string
}

function checkmsg(textarea, maxlength, remainingbox) {
  var len = textarea.value.length;
  var cr;

  if (len > maxlength) {
    textarea.value = textarea.value.substring(0, maxlength);
    cr = 0;
  }
  else {
    cr = maxlength - len;
  }

  // Now set the document display of chars remaining...
  remainingbox.value = cr;
}

function detailsPop(url, id) {
  popup = window.open(url, "puw", "location=no,scrollbars=yes,resizable=yes,width=600,height=500");
  popup.focus();
}

function bigpop(url) {
  bigpopwin = window.open(url, "bpw", "location=no,scrollbars=yes,resizable=yes,top=0,left=0,width=800,height=600");
  bigpopwin.focus();
}

function biggerpop(url) {
  bigpopwin = window.open(url, "bgpw", "location=no,scrollbars=yes,resizable=yes,top=0,left=0,width=850,height=700");
  bigpopwin.focus();
}


function bigpopc(url,wide,high) {
  bigpopwin = window.open(url, "bpw", "location=no,scrollbars=yes,resizable=yes,top=0,left=0,width="+wide+",height="+high);
  bigpopwin.focus();
}

function detailsPop2(url, id) {
  if (parseInt(document.SearchForm.stockid.value) > 0)
  {
    var theurl = url + "?showdetails=1&stockid=" + document.SearchForm.stockid.value;
    window.open(theurl, id, "location=no,scrollbars=yes,resizable=yes,width=600,height=400");
  }
  else
  {
    alert('You must first enter a valid Stock ID number');
  }
}

function KeyIsEnter() {
  // if the key IS enter then launch the popup with the correct value for stock ID
  // rather than submitting the form as before.
  if (window.event.keyCode == 13)
  {
    detailsPop2('/cgi-bin/repairables/previewdetails.cgi', 'details');
    return false;
  }
  else
  {
    return true;
  }
}

function cPop(url, wide, high) {
  customWindow = window.open(url,"custompopup",'width=' + wide + ',height=' + high);
}

function cPopId(url, wide, high, id) {
  customWindow = window.open(url, id, 'width=' + wide + ', height=' + high);
}

function Pop(url){
  cuteLittleWindow = window.open(url, "littleWindow", "location=no,scrollbars=yes,width=530,height=380");
}

function dPop(url){
  dLongWindow = window.open(url, "longWindow", "location=no,scrollbars=yes,width=530,height=520");
}

function printWindow(){
   bV = parseInt(navigator.appVersion)
   if (bV >= 4) window.print()
}

function MM_findObj(n, d) { //v3.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); 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 MM_changeProp(objName,x,theProp,theValue) { //v3.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)) eval("obj."+theProp+"='"+theValue+"'");
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function ConfirmClick(messagetext, imagename) {
  if (confirm(messagetext))
  {
    MM_swapImage(imagename,'','/html/images/processing.gif',1);
    return true;
  }
  else
  {
    return false;
  }
}

function ConfirmClickM(messagetext, imagename) {
  if (confirm(messagetext))
  {
    MM_swapImage(imagename,'','/dmhtml/images/btn_ndmm_processing.gif',1);
    return true;
  }
  else
  {
    return false;
  }
}

function ConfirmClickS(messagetext, imagename) {
  if (confirm(messagetext))
  {
    MM_swapImage(imagename,'','/dmhtml/images/btn_ndms_processing.gif',1);
    return true;
  }
  else
  {
    return false;
  }
}

function ConfirmClickMG(messagetext, imagename) {
  if (confirm(messagetext))
  {
    MM_swapImage(imagename,'','/mgimages/btn-processing.gif',1);
    return true;
  }
  else
  {
    return false;
  }
}

function ConfirmClickL(messagetext, imagename) {
  if (confirm(messagetext))
  {
    MM_swapImage(imagename,'','/dmhtml/images/btn_ndml_processing.gif',1);
    return true;
  }
  else
  {
    return false;
  }
}

function ConfirmClickNI(messagetext) {
  if (confirm(messagetext))
  {
    return true;
  }
  else
  {
    return false;
  }
}

function write_greeting()
{
  var today = new Date();
  var greeting = '';
  var hrs = today.getHours();

  if (hrs < 5)
    greeting = 'Good morning';
  else if (hrs < 12)
    greeting = 'Good morning';
  else if (hrs <= 18)
    greeting = 'Good afternoon';
  else if (hrs < 24)
    greeting = 'Good evening';

  document.write(greeting);
}

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
  var cookie = " " + document.cookie;
  var search = " " + name + "=";
  var setStr = null;
  var offset = 0;
  var end = 0;
  if (cookie.length > 0) {
    offset = cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      end = cookie.indexOf(";", offset)
      if (end == -1) {
        end = cookie.length;
      }
      setStr = unescape(cookie.substring(offset, end));
    }
  }
  return(setStr);
}

var blnFormSubmitted;
blnFormSubmitted = false;
function checkFormSubmitStatus ()
{
  if (!blnFormSubmitted)
  {
    blnFormSubmitted = true;
    return true;
  }
  else
  {
    alert('Your submission is currently being processed.\nIt will only take a few more seconds,\nsorry for any inconvenience.');
    return false;
  }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
