
function checkIE() {
  isIE = 0;
  var ieOK = null;
  var str = navigator.appName;

  var IE4 = (document.all && !document.getElementById) ? true : false;
  var NS4 = (document.layers) ? true : false;
  var IE5 = (document.all && document.getElementById) ? true : false;
  var N6 = (document.getElementById && !document.all) ? true : false;



  if(str == "Microsoft Internet Explorer" || IE4 || IE5) {
    isIE = "IE";
  }
  else if(NS4) {
    isIE = "NS4";
  }
  else {
    isIE = "NS6";
  }
  //window.status = isIE;
  return(isIE);
}

// GLOBALS
var MAXLINE = 22;
var MAXTOTAL = 66;

//var MAXLINE = 25;
//var MAXTOTAL = 75;

function update_forms() {
  var theitem;
  update_chars(theitem, 9);
  update_chars(theitem, 11);
  update_chars(theitem, 13);
  set_time();
}

function set_time() {
   isIE = checkIE();
   if(isIE == "NS4") {
     setTimeout ("update_forms();", 60);
   }
}

// This function updates the number of characters contained within a template.
function update_chars(theitem, id) {
  var number_so_far = 0;
  var max_items = 0;
  var current_total;
  if(theitem) {
    max_items = theitem.personalise_numberboxes.value;
    number_so_far = theitem.personalise.value.length;
  }
  else {
    lab = "product"+id;
    theitem = document.forms[lab];
    if(theitem) {
      number_so_far = theitem.personalise.value.length;
      max_items = theitem.personalise_numberboxes.value;
    }
  }

  // right. now calculate the number so far
  number_so_far = 0;
  for(i=1; i <= max_items; i++) {
    var mystring = "theitem.personalise"+i;
    var theval = eval(mystring);
    var myvalue = theval.value.length;
    if(myvalue > MAXLINE) {
       //alert("reset for "+mystring+" and "+myvalue);
       theval.value = theval.value.substring(0,MAXLINE);
       myvalue = MAXLINE;
    }
    if(myvalue) {
      number_so_far += myvalue;
    }
  }


   if (number_so_far > 66) {
    theitem.personalise.value = theitem.personalise.value.substring(0,66);
    free = 0;
  } else {
    free = 66 - number_so_far;
  }


  if(theitem) {
    theitem.num.value = free;
  }
}


// This function updates the number of characters contained within a template.
function get_field(theitem, id) {
  var so_far;
  var max_items;
  var current_total;
  var personalisefield;
  if(theitem) {
    max_items = theitem.personalise_numberboxes.value;
    so_far = theitem.personalise.value.length;
    personalisefield = theitem.personalise;
  }
  else {
    lab = "product"+id;
    theitem = document.forms[lab];
    so_far = theitem.personalise.value.length;
    max_items = theitem.personalise_numberboxes.value;
    personalisefield = theitem.personalise;
  }

  // right. now calculate the number so far
  so_far = "";
  for(i=1; i <= max_items; i++) {
    var mystring = "theitem.personalise"+i;
    var theval = eval(mystring);
    var myvalue = theval.value;


    if(myvalue) {
      so_far = so_far + myvalue + escape("\n");
      //alert("we have "+so_far);
    }
  }

  //alert("value is "+so_far);
  personalisefield.value = so_far;



}

function order_process(theform, url, id) {
  var theval;
  isIE = checkIE();
  if(isIE == "NS4") {
          divobj=getObject("scrollData");
          label = "product"+id;
          y = document.forms;
          x = divobj.document.forms.length;
          str = "document.forms."+label;
          aform = divobj.document.forms[label];
  }
  else {
          aform = theform;
  }
  //alert("here");
  if(aform.elements["personalise"]) {

    theval = aform.elements["personalise"].value;

    if(theval == "") {
      var myurl = 'yesno.html?id='+id;
      var theht = 110;
      var newwin = window.open(myurl,"label","resize=yes,width=290,height="+theht);
      newwin.focus();
		newwin.theform = theform;
    }
    else {
      // open the personalise label window.
      var myurl = 'pers/previewid'+id+'.html';
      //alert("open the window with '"+theval+"'");
      windowOpen(theform, myurl, id);
    }
  }
  else {
    // no personalise label, so just submit the form

alert("no label");

    aform.action = "/shop/basket.html?/permit/basket/additem";
    aform.submit();
  }
}

var labeltext;
function windowOpen(theform,url,id) {
        isIE = checkIE();
        if(isIE == "NS4") {
          ns4_windowOpen(theform,url, id);
        }
        else {
          labeltext = theform.elements['personalise'].value;
          var stripped = "";
          var i;
          for(i=0;i<labeltext.length;i++) {
                if(labeltext.charCodeAt(i) != 10) stripped = stripped + labeltext.charAt(i);
                }
          labeltext = stripped;
          labeltext = stripped;
          var theht = 320;
          var match1 = /labelid(\d+)\.html/;
          var theres = url.match(match1);

          if(theres) {
            if(theres[1] == '22') {
              theht = 390;
            }
          }
          url = url+"?thelabel="+labeltext;

          var newwin = window.open(url,"label","resize=yes,width=390,height="+theht);
          newwin.focus();
        }
      }


function ns4_windowOpen(theform,url,id) {
        var labeltext = "";
        if(theform) {
	  labeltext = theform.elements['personalise'].value;
        }
        else {
          divobj=getObject("scrollData");
          label = "product"+id;
          y = document.forms;
          x = divobj.document.forms.length;
          str = "document.forms."+label;
          aform = divobj.document.forms[label];

          labeltext = aform.elements['personalise'].value;

        }
	var stripped = "";
	var i;
	for(i=0;i<labeltext.length;i++) {
		if(labeltext.charCodeAt(i) != 10) stripped = stripped + labeltext.charAt(i);
		}
	labeltext = stripped;
        var theht = 320;
        var match1 = /id(\d+)\.html/;
        var theres = url.match(match1);

        if(theres) {
          if(theres[1] == '22') {
            theht = 400;
          }
        }
        url = url+"?thelabel="+labeltext;
	var newwin = window.open(url,"label","resize=yes,width=390,height="+theht);
	newwin.focus();
	}