﻿// Guernsey 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, dRearPrice;
    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.BadgeL.value == "nobadge.gif") {
        dBadgePrice = 0;
    } else {
        dBadgePrice = dBadgeLSetPrice;
    }

    dTotalPrice = (iPlateQuantity * (dBasePrice + dBadgePrice)) + dFrontPrice + dRearPrice;
    dTotalPrice = (Math.round(dTotalPrice * 100) / 100).toFixed(2);
    document.aspnetForm.totalPrice.value = '£' + dTotalPrice;
}

// form validation
function TestValid() {
    var bValid = true;
    if ((document.aspnetForm.RegText1.value == '') || (document.aspnetForm.RegText1.value == 'YOUR REG')) {
        bValid = false;
        alert("Please enter the registration number.");
        document.aspnetForm.RegText1.select();
        document.aspnetForm.RegText1.focus();
    }
    if (bValid == true) {
        if (!CheckReg()) {
            bValid = false;
        }
    }
    return bValid;
}

// 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;
        }
    }

    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;
    }

    varStr = 'image.aspx?FR=' + fstring;
    varStr = varStr + '&RR=' + rstring;
    varStr = varStr + '&FT=' + fontvars.TTFFileName + '&FS=' + fontvars.TTFSize;
    varStr = varStr + '&FH=' + fontvars.TTFHorzOffset + '&FV=' + fontvars.TTFVertOffset;
    varStr = varStr + '&BL=' + badgel;
    varStr = varStr + '&TP=G';
    document.images["plates"].src = varStr;
}

// save the plate design to cookie
function saveDesign() {
    if (checkCookies()) {
        if (CheckReg('both')) {
            if (CheckSlogan()) {
                var messageStr = "Saving a design will overwrite any previously saved design.\n\nAre you sure you wish to proceed?";
                if (confirm(messageStr)) {
                    writeCookie('saveguernsey', 31536000000);
                    alert("Your design has been saved.");
                }
            }
        }
    }
}

// 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.value + "|";
    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.FontType.value + "|";
    cStr = cStr + document.aspnetForm.BadgeL.value + "|" + document.aspnetForm.BadgeLName.value + "|" + document.aspnetForm.badgelopts.selectedIndex;
    SetCookie(cName, cStr, largeExpDate, myDomain);
}

// load saved plate design from cookie
function loadDesign() {
    if (checkCookies()) {
        var messageStr = "Loading a saved design will overwrite the current design.\n\nAre you sure you wish to proceed?";
        if (confirm(messageStr)) {
            var cSave = GetCookie('saveguernsey');
            if (cSave == null) {
                alert("Loading failed: No saved design was found.");
            } else {
                setFormOptions(cSave.split("|"));
                writeCookie('guernsey', 300000);
                updatePlates();
                updatePrice();
                alert("Saved design loaded.");
            }
        }
    }
}

// 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];
        document.aspnetForm.FrontSize.value = opts[1];
        document.aspnetForm.RearSize.value = opts[2];
        document.aspnetForm.fSize.selectedIndex = opts[3];
        document.aspnetForm.rSize.selectedIndex = opts[4];
        setSupply();
        document.aspnetForm.RegText1.value = opts[5];
        if (opts[6] == "null") {
            document.aspnetForm.FontType.options[0].selected = true;
        } else {
            document.aspnetForm.FontType.value = opts[6];
        }
        if (opts[7] == "null") {
            document.aspnetForm.BadgeL.value = "nobadge.gif";
            document.aspnetForm.BadgeLName.value = "NO IMAGE";
            document.aspnetForm.badgelopts.selectedIndex = 0;
        } else {
            document.aspnetForm.BadgeL.value = opts[7];
            document.aspnetForm.BadgeLName.value = opts[8];
            document.aspnetForm.badgelopts.selectedIndex = opts[9];
        }
    }
}

// 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]/
    for (i = 0; i < RegNumbCharNumb; i++) {
        if (!RegNumb.charAt(i).match(charact)) {
            alert("Illegal Character(s) in Registration Number, please correct.");
            RegElm.select();
            RegElm.focus();
            return false;
        }
        RegSize = RegSize + 1;
        if (RegSize > 6) {
            alert("No more than 6 digits are allowed.");
            RegElm.select();
            RegElm.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 IMAGE|0';
    setFormOptions(optStr.split("|"));
    writeCookie('guernsey', 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)) + "...";
    }
    updatePlates();
    updatePrice();
    writeCookie('guernsey', 300000);
}

function updateReg() {
    if (CheckReg()) {
        updatePlates();
        writeCookie('guernsey', 300000);
    }
}

// set the front plate size
function setSizeFront() {
    document.aspnetForm.FrontSize.value = document.aspnetForm.fSize.options[document.aspnetForm.fSize.selectedIndex].text;
    updatePlates();
    updatePrice();
    writeCookie('guernsey', 300000);
}

// set the rear plate size
function setSizeRear() {
    document.aspnetForm.RearSize.value = document.aspnetForm.rSize.options[document.aspnetForm.rSize.selectedIndex].text;
    updatePlates();
    updatePrice();
    writeCookie('guernsey', 300000);
}

function updatePlatesShapes() {
    setSupply();
    updatePlates();
    updatePrice();
    writeCookie('guernsey', 300000);
}


