﻿// Showplate Platebuilder specific functions
function setSupply() {
    if (document.aspnetForm.supplyPlates.value == "F") {
        document.aspnetForm.FrontRear.value = "Front";
        document.aspnetForm.fSize.disabled = false;
        document.aspnetForm.rSize.disabled = true;
        setSizeFront();
    } else {
        if (document.aspnetForm.supplyPlates.value == "R") {
            document.aspnetForm.FrontRear.value = "Rear";
            document.aspnetForm.fSize.disabled = true;
            document.aspnetForm.rSize.disabled = false;
            setSizeRear();
        } else {
            document.aspnetForm.FrontRear.value = "Both";
            document.aspnetForm.fSize.disabled = false;
            document.aspnetForm.rSize.disabled = false;
            setSizeFront();
            setSizeRear();
        }
    }
}

// price calculation
function updatePrice() {
  var iPlateQuantity;
  var dBasePrice;
  var dFrontPrice;
  var dRearPrice;
  var dFontPrice;
  var dBadgeLPrice;
  var dBorderPrice;
  var dSloganPrice;
  var dTotalPrice;
  dBasePrice = dBaseSetPrice;
  if (document.aspnetForm.FrontRear.value=="Both") {
    iPlateQuantity = 2;
  } else {
    iPlateQuantity = 1;
  }

  if ((document.aspnetForm.FrontRear.value == "Front") || (document.aspnetForm.FrontRear.value == "Both")) {
    if (document.aspnetForm.fSize.options[document.aspnetForm.fSize.selectedIndex].value!="N") {
      if ((document.aspnetForm.fSize.options[document.aspnetForm.fSize.selectedIndex].text!="FRONT SIZE STD") && (document.aspnetForm.fSize.options[document.aspnetForm.fSize.selectedIndex].text!='STD 20.5" x 4.5"')) {
        dFrontPrice=dFrontSetPrice;
      } else {
        dFrontPrice=0.0;
      }
    } else {
      dFrontPrice=0.0;
    }
  } else {
    dFrontPrice=0.0;
  }

  if ((document.aspnetForm.FrontRear.value == "Rear") || (document.aspnetForm.FrontRear.value == "Both")) {
    if (document.aspnetForm.rSize.options[document.aspnetForm.rSize.selectedIndex].value!="N") {
      if ((document.aspnetForm.rSize.options[document.aspnetForm.rSize.selectedIndex].text!="REAR SIZE STD") && (document.aspnetForm.rSize.options[document.aspnetForm.rSize.selectedIndex].text!='STD 20.5" x 4.5"')) {
        dRearPrice=dRearSetPrice;
      } else {
        dRearPrice=0.0;
      }
    } else {
      dRearPrice=0.0;
    }
  } else {
    dRearPrice = 0.0;
  }

  if (document.aspnetForm.FontType.selectedIndex==0) {
    dFontPrice = 0.0;
  } else {
    dFontPrice = dFontSetPrice;
  }

  if (document.aspnetForm.BadgeL.value=="nobadge.gif") {
    dBadgeLPrice = 0;
  } else {
    dBadgeLPrice = dBadgeLSetPrice;
  }

  if ((document.aspnetForm.Border.value=="NO BORDER")) {
    dBorderPrice = 0.0;
  } else {
    dBorderPrice = dBorderSetPrice;
  }

  if (document.aspnetForm.FText.value=="") {
    dFooterPrice = 0.0;
  } else {
    dFooterPrice =dFooterSetPrice;
  }

  dTotalPrice = (iPlateQuantity * (dBasePrice + dFontPrice + dBorderPrice + dBadgeLPrice + dFooterPrice)) + dFrontPrice + dRearPrice; 
  dTotalPrice = (Math.round(dTotalPrice * 100) / 100).toFixed(2);
  document.aspnetForm.totalPrice.value = '£' + dTotalPrice;
}

// check the border colour - popup if blue
function checkBorder(borderName){
  borderName = borderName.toUpperCase();
  if (borderName.indexOf("BLUE")!=-1){
    alert("Please be aware that a blue border will appear much\ndarker (almost black) when applied to a yellow plate.");
  }
  if (borderName.indexOf("PINK")!=-1){
    alert("Please be aware that a pink border will appear much\ndarker (almost red) when applied to a yellow plate.");
  }
}

// set the flash plate display
function updatePlates(){
  supply = document.aspnetForm.FrontRear.value;
  RegText1 = document.aspnetForm.RegText1.value;
  if ((supply == 'Both')) {
    fshape = document.aspnetForm.fSize.options[document.aspnetForm.fSize.selectedIndex].value;
    fstring = fshape + RegText1;
    rshape = document.aspnetForm.rSize.options[document.aspnetForm.rSize.selectedIndex].value;
    rstring = rshape + RegText1;
  } else {
    if (supply == 'Front') {
      fshape = document.aspnetForm.fSize.options[document.aspnetForm.fSize.selectedIndex].value;
      fstring = fshape + RegText1;
      rstring = 'N';
    } else {
      fstring = 'N';
      rshape = document.aspnetForm.rSize.options[document.aspnetForm.rSize.selectedIndex].value;
      rstring = rshape + RegText1;
    }
  }
  ftext = document.aspnetForm.FText.value;

  var fontvars = { fontfolder: '', fonttype: '', TTFFileName: '', TTFSize: '', TTFVertOffset: '', TTFHorzOffset: '' };
  setFontVariables(document.aspnetForm.FontType.value, fontvars);

  if (document.aspnetForm.BadgeL.value == "nobadge.gif"){
    badgel = 'N';
  }else{
    badgel = 'Y' + document.aspnetForm.BadgeL.value;
  }

  brdrcol=document.aspnetForm.BorderColour.value;
  if (brdrcol==""){
    brdr='N';
  } else if (document.aspnetForm.BorderThick.checked){
    brdr='W';
  } else {
    brdr='T';
  }

  varStr = 'image.aspx?FR=' + fstring;
  varStr = varStr + '&RR=' + rstring;
  varStr = varStr + '&SL=' + ftext;  
  varStr = varStr + '&BD=' + brdr + brdrcol;
  varStr = varStr + '&FT=' + fontvars.TTFFileName + '&FS=' + fontvars.TTFSize + '&FH=';
  varStr = varStr + fontvars.TTFHorzOffset + '&FV=' + fontvars.TTFVertOffset;
  varStr = varStr + '&BL=' + badgel;
  varStr = varStr + '&TP=J';
  document.images["plates"].src = varStr;
}

// set cookie vars
pathname = location.pathname;
myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';

// write cookie
function writeCookie(cName, duration){
  var largeExpDate = new Date ();
  largeExpDate.setTime(largeExpDate.getTime() + duration);
  cStr = document.aspnetForm.supplyPlates.selectedIndex + "|";
  cStr = cStr + document.aspnetForm.FrontSize.value + "|" + document.aspnetForm.RearSize.value + "|";
  cStr = cStr + document.aspnetForm.fSize.selectedIndex + "|" + document.aspnetForm.rSize.selectedIndex + "|";
  cStr = cStr + document.aspnetForm.RegText1.value + "|";
  cStr = cStr + document.aspnetForm.FText.value + "|";
  cStr = cStr + document.aspnetForm.FontType.value + "|";
  cStr = cStr + document.aspnetForm.BadgeL.value + "|" + document.aspnetForm.BadgeLName.value + "|";
  cStr = cStr + document.aspnetForm.BadgeLBtn.value + "|";
  cStr = cStr + document.aspnetForm.Border.value + "|" + document.aspnetForm.BorderColour.value + "|";
  cStr = cStr + document.aspnetForm.BorderThick.checked + "|" + currSelectedBorder + "|";
  SetCookie(cName, cStr, largeExpDate, myDomain); 
}

// read saved plate designs and set the control panel options / flash elements accordingly
function setFormOptions(opts){
  if (opts.length) {
    document.aspnetForm.supplyPlates.selectedIndex = opts[0];
    setSupply();
    document.aspnetForm.FrontSize.value = opts[1];  
    document.aspnetForm.RearSize.value=opts[2]; 
    document.aspnetForm.fSize.selectedIndex=opts[3]; 
    document.aspnetForm.rSize.selectedIndex=opts[4]; 
    document.aspnetForm.RegText1.value=opts[5]; 
    if (opts[6] == "null") {
      document.aspnetForm.FText.value = "";
    } else {
      document.aspnetForm.FText.value = opts[6];
    }
    if (opts[7] == "null") {
      document.aspnetForm.FontType.options[0].selected = true;
    } else {
      document.aspnetForm.FontType.value = opts[7];
    }
    if (opts[8] == "null") {
      document.aspnetForm.BadgeL.value = "nobadge.gif";
      document.aspnetForm.BadgeLName.value = "NO BADGE";
      document.aspnetForm.badgelbtn.value = "SELECT A BADGE";
    } else {
      document.aspnetForm.BadgeL.value = opts[8];
      document.aspnetForm.BadgeLName.value = opts[9];
      document.aspnetForm.BadgeLBtn.value = opts[10];
    }
    if (opts[11] == "null") {
      document.aspnetForm.Border.value = "NO BORDER";
    } else {
      document.aspnetForm.Border.value = opts[11];
    }
    if (opts[12] == "null") {
      document.aspnetForm.BorderColour.value = "";
    } else {
      document.aspnetForm.BorderColour.value = opts[12];
    }
    if (opts[13] == "true") {
      document.aspnetForm.BorderThick.checked = true;
    } else {
      document.aspnetForm.BorderThick.checked = false;
    }
    if (opts[14] == "null") {
      thisborder = null;
    } else {
      thisborder = opts[14];
    }
    setBorder(document.aspnetForm.Border.value, document.aspnetForm.BorderColour.value, thisborder);  }
}

// validate registration entry
function CheckReg() {
    var bValid = true;
    if (bValid == true) {
        bValid = CheckReg2(document.aspnetForm.RegText1);
    }
    return bValid;
}

// validate registration field element
function CheckReg2(RegElm) {
    RegNumb = RegElm.value;
    RegSize = 0;
    NumbOfSpaces = 0;
    RegNumbCharNumb = RegNumb.length
    var charact = /[0-9]/
    var splitchar = /[\+]/
    var space = /[\ ]/

    for (i = 0; i < RegNumbCharNumb; i++) {
        if (!RegNumb.charAt(i).match(charact) && !RegNumb.charAt(i).match(space) && !RegNumb.charAt(i).match(splitchar)) {
            alert("The registration text can only contain numbers, please correct.");
            RegElm.select();
            RegElm.focus();
            return false;
        }
        if (!RegNumb.charAt(i).match(splitchar)) { RegSize = RegSize + 1; }
        if (RegSize > 6) {
            alert("No more than 6 digits are allowed.");
            RegElm.select();
            RegElm.focus();
            return false;
        }
    }
    return true;
}

// validate footer
function CheckFooter() {
  Footer = document.aspnetForm.FText.value;
  FooterCharNumb = Footer.length;
  var charact = /[0-z]/
  var comma= /[\,]/
  var dot= /[\.]/
  var dash= /[\-]/
  var space = /[\ ]/
  var ampas = /[\&]/
  var doubleq = /[\"]/
  var singleq = /[\']/
  for (i= 0; i<FooterCharNumb; i++) {
        if ((!Footer.charAt(i).match(doubleq)) & (!Footer.charAt(i).match(singleq)) & (!Footer.charAt(i).match(charact)) & (!Footer.charAt(i).match(space)) & (!Footer.charAt(i).match(comma)) & (!Footer.charAt(i).match(dot)) & (!Footer.charAt(i).match(ampas)) & (!Footer.charAt(i).match(dash))){
          setOptions(2);
          alert("Illegal Character(s) in Footer, please correct.")
          document.aspnetForm.Footer.select();
          document.aspnetForm.Footer.focus();
          return false;
        }
  }
  return true;
}

// initialise the control panel
function initForm() {
    var optStr = '0|STD 20.5" x 4.5"|STD 20.5" x 4.5"|0|0|YOUR REG||STANDARD TEXT|'
               + 'nobadge.gif|NO BADGE|SELECT A BADGE|'
               + 'null|NO BORDER|N|null';
    setFormOptions(optStr.split("|"));
    writeCookie('jersey', 300000);
}

// called by the left badge popup to apply badges
function setBadgeL(FileName, BadgeName){
    document.aspnetForm.BadgeL.value = FileName; 
    document.aspnetForm.BadgeLName.value = BadgeName; 
    if (BadgeName.length > 20){
      BadgeName = Trim(BadgeName.substring(0,18)) + "...";
    }
    document.aspnetForm.BadgeLBtn.value = BadgeName; 
    updatePlates(); 
    updatePrice(); 
    writeCookie('jersey2', 300000);
}

function setImage() {
    updatePlates();
    updatePrice();
    writeCookie('jersey2', 300000);
}

function updateReg() {
    if (CheckReg()) {
        updatePlates();
        writeCookie('jersey2', 300000);
    }
}

// set the front plate size
function setSizeFront() {
    document.aspnetForm.FrontSize.value = document.aspnetForm.fSize.options[document.aspnetForm.fSize.selectedIndex].text;
    updatePlates();
    updatePrice();
    writeCookie('jersey2', 300000);
}

// set the rear plate size
function setSizeRear() {
    document.aspnetForm.RearSize.value = document.aspnetForm.rSize.options[document.aspnetForm.rSize.selectedIndex].text;
    updatePlates();
    updatePrice();
    writeCookie('jersey2', 300000);
}

function updatePlatesShapes() {
    setSupply();
    updatePlates();
    updatePrice();
    writeCookie('jersey2', 300000);
}

// set the plate border
function setBorder(name, brdrcol, newlySelected) {
    if (name != null) {
        name = name.toUpperCase();
        if (currSelectedBorder != null) {
            MM_findObj(currSelectedBorder).className = 'cellOff';
        }
        if (newlySelected != null) {
            MM_findObj(newlySelected).className = 'cellOn';
        }
        currSelectedBorder = newlySelected;
    }
    if (name == "NO BORDER") {
        document.aspnetForm.Border.value = "NO BORDER";
        document.aspnetForm.BorderColour.value = "";
        document.aspnetForm.BorderThick.checked = false;
    } else {
        if (name != null) {
            document.aspnetForm.Border.value = name;
            document.aspnetForm.BorderColour.value = brdrcol;
        }
    }
}

function updateFooter() {
    if (CheckFooter()) {
        updatePlates();
        updatePrice();
        writeCookie('jersey2', 300000);
    } 
}



