﻿/* ******************************************** */
/*                                              */
/* Function1.js                                  */
/*                                              */
/* ******************************************** */

function StripSpaces(x)
{
    while (x.substring(0,1) == ' ')
        x = x.substring(1);
    
    return x;
}

function Empty(x)
{
    if (x.length > 0)
        return false;
    else
        return true;
}

function isWeekend(d)
{
    myDate = new Date(d);

    day = myDate.getDay();
    
    if ( (day == 0) || (day == 6) )
        return true;
    else
        return false;

    return true;
}

function isHoliday(d)
{
    var myDate = new Date(d);

    var month = myDate.getMonth() + 1;
    var day = myDate.getDate();
    var year = myDate.getFullYear();
    
    date = (month + "/" + day + "/" + year);
    
    // alert(date);

    if (date == "5/31/2010") {
        alert("In observance of Memorial Day, The Lunch Bunch is closed on May 31, 2010. Please choose a different date.");
        return true;
    }
    else if (date == "7/5/2010") {
        alert("The Lunch Bunch is closed on July 5, 2010. Please choose a different date.");
        return true;
    }
    else if (date == "9/6/2010") {
        alert("The Lunch Bunch is closed on September 6, 2010. Please choose a different date.");
        return true;
    }
    else if ((date == "11/22/2010") || (date == "11/23/2010") || (date == "11/24/2010") || (date == "11/25/2010") || (date == "11/26/2010")) {
        alert("The Lunch Bunch is closed between November 22, 2010 and November 26, 2010 for Thanksgiving. Please choose a different date.");
        return true;
    }
    else if ((date == "12/24/2010") || (date == "12/27/2010") || (date == "12/28/2010") || (date == "12/29/2010") || (date == "12/30/2010") || (date == "12/31/2010")) {
        alert("The Lunch Bunch is closed Christmas Eve and between December 27, 2010 and December 31, 2010 for the holidays. Please choose a different date.");
        return true;
    }
    else if (date == "1/12/2011") {
        alert("Unfortunately The Lunch Bunch is closed tomorrow because of poor weather conditions, please choose another day. We apologize for the inconvenience.");
        return true;
    }
    else if (date == "1/27/2011") {
        alert("Unfortunately The Lunch Bunch is closed on Thursday 27th January 2011 because of poor weather conditions, please choose another day. We apologize for the inconvenience.");
        return true;
    }
    else if (date == "1/28/2011") {
        alert("Unfortunately The Lunch Bunch is closed on Friday 28th January 2011, please choose another day. We apologize for the inconvenience.");
        return true;
    }
    else if (date == "2/2/2011") {
        alert("Unfortunately The Lunch Bunch is closed on Wednesday 2nd February 2011 because of poor weather conditions, please choose another day. We apologize for the inconvenience.");
        return true;
    }
    else if ((date == "2/21/2011") || (date == "2/22/2011") || (date == "2/23/2011") || (date == "2/24/2011") || (date == "2/25/2011")) {
        alert("The Lunch Bunch is closed between February 21, 2011 and February 25, 2011. Please choose a different date.");
        return true;
    }
    else if (date == "5/30/2011") {
        alert("Unfortunately The Lunch Bunch is closed on Monday May 30th for the Memorial Day holiday, please choose another day. We apologize for the inconvenience.");
        return true;
    }
    else if ((date == "6/22/2011") ||
             (date == "6/23/2011") ||
             (date == "6/23/2011") ||
             (date == "6/24/2011") ||
             (date == "6/25/2011") ||
             (date == "6/26/2011") ||
             (date == "6/27/2011") ||
             (date == "6/28/2011") ||
             (date == "6/29/2011") ||
             (date == "6/30/2011") ||
             (date == "7/01/2011") ||
             (date == "7/02/2011") ||
             (date == "7/03/2011") ||
             (date == "7/04/2011") ||
             (date == "7/05/2011") ||
             (date == "7/06/2011") ||
             (date == "7/07/2011") ||
             (date == "7/08/2011") ||
             (date == "7/09/2011") ||
             (date == "7/10/2011") ||
             (date == "7/11/2011") ||
             (date == "7/12/2011")) {
        alert("The Lunch Bunch is currently closed and will be open again starting Wednesday July 13th. We look forward to serving you again on our return.");
        return true;
    }
    else if ((date == "8/5/2011") ||
             (date == "8/8/2011") ||
             (date == "8/9/2011")) {
        alert("The Lunch Bunch is currently closed and will be open again starting Wednesday August 10th. We look forward to serving you again on our return.");
        return true;
    }
    else if ((date == "9/5/2011")) {
        alert("The Lunch Bunch is currently closed and will be open again starting Tuesday September 6th, 2011. We look forward to serving you again on our return.");
        return true;
    }
    else if ((date == "10/10/2011")) {
        alert("The Lunch Bunch is not open on Columbus Day but will be open again starting Tuesday October 11th. We look forward to serving you again on our return.");
        return true;
    }
    else if ((date == "11/11/2011")) {
        alert("In observance of Veterans Day, The Lunch Bunch is closed on Friday, November 11, 2011. Please choose a different date.");
        return true;
    }
    else if ((date == "11/23/2011") || (date == "11/24/2011") || (date == "11/25/2011")) {
        alert("The Lunch Bunch is closed between November 23, 2011 and November 25, 2011 for Thanksgiving. Please choose a different date.");
        return true;
    }
    else if (new Date(date) >= new Date("12/12/2011")) {
        alert("The Lunch Bunch has closed");
        return true;
    }
    else
        return false;

    return true;
}

function CheckMenuItem(dd_id, of_id, qty_id, hid_id)
{
    var dd = document.getElementById(dd_id);
    var of = document.getElementById(of_id);
    var qty = document.getElementById(qty_id);
    var hid = document.getElementById(hid_id);

    var res = true;
    
    if (res == true)
    {
        if (Empty(StripSpaces(dd.value)))
        {
            alert('You did not enter a delivery date');
            res = false;
        }
    }
    
    if (res == true)
    {
        if (isDate(dd.value) == false)
        {
            res = false;
        }
    }
    
    if (res == true)
    {
        if (isWeekend(dd.value) == true)
        {
            alert('We only deliver between Monday and Friday');
            res = false;
        }
    }
    
    if (res == true)
    {
        if (isHoliday(dd.value) == true)
            res = false;
    }
    
    if (res == true)
    {
        var tempDate = new Date();
        var userDate = new Date(dd.value);
        
        var currentDate = new Date(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate(), 0, 0, 0);
        
        var currentTime = currentDate.getTime();
        var userTime = userDate.getTime();
        
        if (currentTime >= userTime)
        {
            alert('Delivery date must be tomorrow or later');
            res = false;
        }
    }
    
    if (res == true)
    {
        if (Empty(StripSpaces(of.value)))
        {
            alert('You did not specify who this order is for');
            res = false;
        }
    }
    
    if (res == true)
    {
        if (Empty(StripSpaces(qty.value)))
        {
            alert('You did not enter a quantity');
            res = false;
        }
    }
    
    if (res == true)
    {
        if (isInteger(qty.value) == false)
        {
            alert('You did not enter a valid quantity');
            res = false;
        }
    }
    
    if (res == true)
    {
        if ( (parseInt(qty.value) < 1) || (parseInt(qty.value) > 99) )
        {
            alert('Quantity must be between 1 and 99');
            res = false;
        }
    }
    
    if (res)
    {
        hid.value = 'Add';
        __doPostBack()
    }
}

function CalculateGiftCertificates(qty5_id, value5_id, qty10_id, value10_id, qty25_id, value25_id, total_id) {
    var qty5 = document.getElementById(qty5_id);
    var value5 = document.getElementById(value5_id);
    var qty10 = document.getElementById(qty10_id);
    var value10 = document.getElementById(value10_id);
    var qty25 = document.getElementById(qty25_id);
    var value25 = document.getElementById(value25_id);

    var total = document.getElementById(total_id);

    var val5 = 0;
    var val10 = 0;
    var val25 = 0;

    if (isInteger(qty5.value)) {
        val5 = parseInt(qty5.value * 5);
        value5.innerText = "$" + val5.toString() + ".00";
    }

    if (isInteger(qty10.value)) {
        val10 = parseInt(qty10.value * 10);
        value10.innerText = "$" + val10.toString() + ".00";
    }

    if (isInteger(qty25.value)) {
        val25 = parseInt(qty25.value * 25);
        value25.innerText = "$" + val25.toString() + ".00";
    }

    total.innerText = "$" + (val5 + val10 + val25).toString() + ".00";
}

function CheckGiftCertificate(qty5_id, qty10_id, qty25_id, dd_id, hid_id) {
    var dd = document.getElementById(dd_id);

    var qty5 = document.getElementById(qty5_id);
    var qty10 = document.getElementById(qty10_id);
    var qty25 = document.getElementById(qty25_id);
    
    var hid = document.getElementById(hid_id);

    var res = true;

    if (res == true) {
        if (Empty(StripSpaces(dd.value))) {
            alert('You did not enter a delivery date');
            res = false;
        }
    }

    if (res == true) {
        if (isDate(dd.value) == false) {
            res = false;
        }
    }

    if (res == true) {
        if (isWeekend(dd.value) == true) {
            alert('We only deliver between Monday and Friday');
            res = false;
        }
    }

    if (res == true) {
        if (isHoliday(dd.value) == true)
            res = false;
    }

    if (res == true) {
        var tempDate = new Date();
        var userDate = new Date(dd.value);

        var currentDate = new Date(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate(), 0, 0, 0);

        var currentTime = currentDate.getTime();
        var userTime = userDate.getTime();

        if (currentTime >= userTime) {
            alert('Delivery date must be tomorrow or later');
            res = false;
        }
    }
    
    var gotOneFlag = false;

    if (res == true) {
        if (Empty(StripSpaces(qty5.value)) == false)
            gotOneFlag = true;
    }

    if (res == true) {
        if (isInteger(qty5.value) == false) {
            alert('You did not enter a valid quantity');
            qty5.focus();
            res = false;
        }
    }

    if (res == true) {
        if ((parseInt(qty5.value) < 1) || (parseInt(qty5.value) > 99)) {
            alert('Quantity must be between 1 and 99');
            qty5.focus();
            res = false;
        }
    }

    if (res == true) {
        if (Empty(StripSpaces(qty10.value)) == false)
            gotOneFlag = true;
    }

    if (res == true) {
        if (isInteger(qty10.value) == false) {
            alert('You did not enter a valid quantity');
            qty10.focus();
            res = false;
        }
    }

    if (res == true) {
        if ((parseInt(qty10.value) < 1) || (parseInt(qty10.value) > 99)) {
            alert('Quantity must be between 1 and 99');
            qty10.focus();
            res = false;
        }
    }



    if (res == true) {
        if (Empty(StripSpaces(qty25.value)) == false)
            gotOneFlag = true;
    }

    if (res == true) {
        if (isInteger(qty25.value) == false) {
            alert('You did not enter a valid quantity');
            qty25.focus();
            res = false;
        }
    }

    if (res == true) {
        if ((parseInt(qty25.value) < 1) || (parseInt(qty25.value) > 99)) {
            alert('Quantity must be between 1 and 99');
            qty25.focus();
            res = false;
        }
    }

    if (gotOneFlag) {
        if (res) {
            hid.value = 'Add';
            __doPostBack()
        }
    }
    else
        alert('You did not enter a quantity for any of the gift certificates');
}

function cursor_clear()
{
    document.body.style.cursor = 'default';
}

function noenter()
{
    return !(window.event && window.event.keyCode == 13);
}

/* ******************************************** */
/*                                              */
/* File functions                            */
/*                                              */
/* ******************************************** */

function roundNumber(rnum, rlength)  // Arguments: number to round, number of decimal places
{ 
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  
  return newnumber; // Output the result to the form field (change for your purposes)
}

function InitTextBox(id)
{
    var ctrl = document.getElementById(id);
    
    ctrl.value = "";
}

function SetTextBox(id1, id2, id3)
{
    var ctr1 = document.getElementById(id1);
    var ctr2 = document.getElementById(id2);
    var ctr3 = document.getElementById(id3);

    ctr2.value = ctr1.value;
    ctr3.value = ctr1.value;
}

function InitCheckBox(id)
{
    var ctrl = document.getElementById(id);
    
    ctrl.checked = false;
}

function CalcGoodsTotal(idEC1, idEC2, idEC3, idEC4, idEC5,
                         idTF1, idTF2, idTF3, idTF4, idTF5,
                          idGTA, idGT)
{
    var ctrlEC1 = document.getElementById(idEC1);
    var ctrlEC2 = document.getElementById(idEC2);
    var ctrlEC3 = document.getElementById(idEC3);
    var ctrlEC4 = document.getElementById(idEC4);
    var ctrlEC5 = document.getElementById(idEC5);

    var ctrlTF1 = document.getElementById(idTF1);
    var ctrlTF2 = document.getElementById(idTF2);
    var ctrlTF3 = document.getElementById(idTF3);
    var ctrlTF4 = document.getElementById(idTF4);
    var ctrlTF5 = document.getElementById(idTF5);

    var ctrlGTA = document.getElementById(idGTA);
    var ctrlGT = document.getElementById(idGT);

    var gta = 0;
    var gt = 0;
    
    if (ctrlEC1.value != "")
    {
        gt = gt + parseFloat(ctrlEC1.value);
        
        if (ctrlTF1.checked == true)
            gta = gta + parseFloat(ctrlEC1.value);
    }
        
    if (ctrlEC2.value != "")
    {
        gt = gt + parseFloat(ctrlEC2.value);
        
        if (ctrlTF2.checked == true)
            gta = gta + parseFloat(ctrlEC2.value);
    }
        
    if (ctrlEC3.value != "")
    {
        gt = gt + parseFloat(ctrlEC3.value);
        
        if (ctrlTF3.checked == true)
            gta = gta + parseFloat(ctrlEC3.value);
    }
        
    if (ctrlEC4.value != "")
    {
        gt = gt + parseFloat(ctrlEC4.value);
        
        if (ctrlTF4.checked == true)
            gta = gta + parseFloat(ctrlEC4.value);
    }
        
    if (ctrlEC5.value != "")
    {
        gt = gt + parseFloat(ctrlEC5.value);
        
        if (ctrlTF5.checked == true)
            gta = gta + parseFloat(ctrlEC5.value);
    }

    ctrlGT.value = gt.toFixed(2);
    ctrlGTA.value = gta.toFixed(2);
}

function ParseCost(idQty, idCost, idExtCost, idTaxFlag)
{
    var ctrlQty = document.getElementById(idQty);
    var ctrlCost = document.getElementById(idCost);
    var ctrlExtCost = document.getElementById(idExtCost);
 
    if (ctrlCost.value == "")
        ctrlCost.value = "0.00";
    
    var cost = parseFloat(ctrlCost.value);
    ctrlCost.value = cost.toFixed(2);
    
    var extCost = roundNumber(parseFloat(ctrlQty.value) * parseFloat(ctrlCost.value), 2);
    
    ctrlExtCost.value = extCost.toFixed(2);
}

function ParseQuantity(idQty, idCost, idExtCost, idTaxFlag)
{
    var ctrlQty = document.getElementById(idQty);
    var ctrlCost = document.getElementById(idCost);
    var ctrlExtCost = document.getElementById(idExtCost);
 
    if (ctrlQty.value != "")
    {
        var qty = parseInt(ctrlQty.value);
        ctrlQty.value = qty.toFixed(0);
    
        var extCost = roundNumber(parseFloat(ctrlQty.value) * parseFloat(ctrlCost.value), 2);
    
        ctrlExtCost.value = extCost.toFixed(2);
    }
}

function ParseDiscountPercentage(idDP, idDA, idGT)
{
    var ctrlDP = document.getElementById(idDP);
    var ctrlDA = document.getElementById(idDA);
    var ctrlGT = document.getElementById(idGT);
 
    if (ctrlDP.value != "")
    {
        var dp = parseFloat(ctrlDP.value);
        ctrlDP.value = dp.toFixed(2);
    
        var da = roundNumber((dp * parseFloat(ctrlGT.value) / 100), 2);
    
        if (da > 0)
            da = da * -1;
    
        ctrlDA.value = da.toFixed(2);
    }
}

function ParseDiscountAmount(idDP, idDA, idGT)
{
    var ctrlDP = document.getElementById(idDP);
    var ctrlDA = document.getElementById(idDA);
    var ctrlGT = document.getElementById(idGT);
 
    if (ctrlDA.value == "")
        ctrlDA.value = "0";
    
    var da = parseFloat(ctrlDA.value);
    
    if (da > 0)
        da = da * -1;
        
    ctrlDA.value = da.toFixed(2);
    
    ctrlDP.value = "";
}

function ParseFreightPercentage(idFP, idFA, idGT)
{
    var ctrlFP = document.getElementById(idFP);
    var ctrlFA = document.getElementById(idFA);
    var ctrlGT = document.getElementById(idGT);
 
    if (ctrlFP.value != "")
    {
        var fp = parseFloat(ctrlFP.value);
        ctrlFP.value = fp.toFixed(2);
    
        var fa = roundNumber((fp * parseFloat(ctrlGT.value) / 100), 2);
    
        ctrlFA.value = fa.toFixed(2);
    }
}

function ParseFreightAmount(idFP, idFA, idGT)
{
    var ctrlFP = document.getElementById(idFP);
    var ctrlFA = document.getElementById(idFA);
    var ctrlGT = document.getElementById(idGT);
 
    if (ctrlFA.value == "")
        ctrlFA.value = "0";
    
    var fa = parseFloat(ctrlFA.value);
    ctrlFA.value = fa.toFixed(2);
    
    ctrlFP.value = "";
}

function ParseTaxPercentage(idTP, idTA, idGTA)
{
    var ctrlTP = document.getElementById(idTP);
    var ctrlTA = document.getElementById(idTA);
    var ctrlGTA = document.getElementById(idGTA);
 
    if (ctrlTP.value != "")
    {
        var tp = parseFloat(ctrlTP.value);
        ctrlTP.value = tp.toFixed(2);
    
        var ta = roundNumber((tp * parseFloat(ctrlGTA.value) / 100), 2);
    
        ctrlTA.value = ta.toFixed(2);
    }
}

function ParseTaxAmount(idTP, idTA, idGTA)
{
    var ctrlTP = document.getElementById(idTP);
    var ctrlTA = document.getElementById(idTA);
    var ctrlGTA = document.getElementById(idGTA);
 
    if (ctrlTA.value == "")
        ctrlTA.value = "0";
    
    var ta = parseFloat(ctrlTA.value);
    ctrlTA.value = ta.toFixed(2);
    
    ctrlTP.value = "";
}

function CalcPercentages(idDP, idDA, idFP, idFA, idTP, idTA, idGT, idGTA)
{
    ParseDiscountPercentage(idDP, idDA, idGT);
    ParseFreightPercentage(idFP, idFA, idGT);
    ParseTaxPercentage(idTP, idTA, idGTA);
}

function CalcTotal(idGoodsTotal, idGoodsTaxableTotal, idDiscount, idFreight, idTax, idGrandTotal, idBalance)
{
    var ctrlGoodsTotal = document.getElementById(idGoodsTotal);
    var ctrlGoodsTaxableTotal = document.getElementById(idGoodsTaxableTotal);
    var ctrlDiscount = document.getElementById(idDiscount);
    var ctrlFreight = document.getElementById(idFreight);
    var ctrlTax = document.getElementById(idTax);
    var ctrlGrandTotal = document.getElementById(idGrandTotal);
    var ctrlBalance = document.getElementById(idBalance);
 
    var cur1 = 0;

    if (ctrlGoodsTotal.value == "")
        ctrlGoodsTotal.value = "0.00";
    
    if (ctrlGoodsTaxableTotal.value == "")
        ctrlGoodsTaxableTotal.value = "0.00";
    
    if (ctrlDiscount.value == "")
        ctrlDiscount.value = "0.00";
    
    cur1 = parseFloat(ctrlDiscount.value);
    ctrlDiscount.value = cur1.toFixed(2);
    
    if (ctrlFreight.value == "")
        ctrlFreight.value = "0.00";
    
    cur1 = parseFloat(ctrlFreight.value);
    ctrlFreight.value = cur1.toFixed(2);

    if (ctrlTax.value == "")
        ctrlTax.value = "0.00";
    
    cur1 = parseFloat(ctrlTax.value);
    ctrlTax.value = cur1.toFixed(2);

    if (ctrlGrandTotal.value == "")
        ctrlGrandTotal.value = "0.00";
    
    if (ctrlBalance.value == "")
        ctrlBalance.value = "0.00";
    
    cur1 = parseFloat(ctrlGoodsTotal.value) +
           parseFloat(ctrlDiscount.value) +
           parseFloat(ctrlFreight.value) +
           parseFloat(ctrlTax.value);

    ctrlGrandTotal.value = cur1.toFixed(2);
    
    cur1 = cur1 * -1;
    
    ctrlBalance.value = cur1.toFixed(2);
}

function SetFullName(idFirst, idLast, idFull)
{
    var ctrlFirst = document.getElementById(idFirst);
    var ctrlLast = document.getElementById(idLast);
    var ctrlFull = document.getElementById(idFull);
    
    var first = ctrlFirst.value;
    var last = ctrlLast.value;
    var full = ctrlFull.value;
    
    if (full == "")
    {
        full = trim(first + " " + last);
    }
    else if (full == first)
    {
        if (last != "")
            full = first + " " + last;
    }
    else if (full == last)
    {
        if (first != "")
            full = first + " " + last;
    }
    
    ctrlFull.value = full;
}

function SetField(idFrom, idTo, ifBlankFlag)
{
    var ctrlFrom = document.getElementById(idFrom);
    var ctrlTo = document.getElementById(idTo);
    
    if (ifBlankFlag == true)
    {
        if (ctrlTo.value == "")
            ctrlTo.value = ctrlFrom.value;
    }
    else
        ctrlTo.value = ctrlFrom.value;
}

function Redirect(url)
{
    window.location.replace(url)
}

function getObject(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

function toCount(entrance,exit,text,characters) {
  var entranceObj=getObject(entrance);
  var exitObj=getObject(exit);
  var length=characters - entranceObj.value.length;
  if(length <= 0) {
    length=0;
    text='<span class="disable"> '+text+' </span>';
    entranceObj.value=entranceObj.value.substr(0,characters);
  }
  
  if (length == 0)
    exitObj.innerHTML = text.replace("{CHAR} characters", "No characters");
  else if (length == 1)
    exitObj.innerHTML = text.replace("{CHAR} characters", "1 character");
  else
    exitObj.innerHTML = text.replace("{CHAR}",length);
}

function SetControl(id, val)
{
    //alert(document.getElementById(id).innerHTML);
    
    document.getElementById(id).innerHTML = val;
}

function ToggleControl(id)
{
    var ctrl = document.getElementById(id);

    if (ctrl.value == "true")
        ctrl.value = "false";
    else
        ctrl.value = "true";
}

function DoPostBackNoValidate()
{
    __doPostBack("", "NOVALIDATE");
}

function SetCheckBox(ctrl, sufHid)
{
    var nameThis=ctrl.name;
    
    var nameHid=nameThis.substring(0, nameThis.length - 20) + sufHid;

    var idHid=document.getElementById(nameHid);

    if (idHid.value == "true")
        idHid.value = "false";
    else
        idHid.value = "true";
}

function GenericSetCheckBox(hidID, hidValue1, hidValue2, postBackFlag)
{
    //alert("1: " + hidID + "(" + hidValue1 + ":" + hidValue2 + ")");
    
    var ctrl = document.getElementById(hidID);

    //alert("Cur: " + ctrl.value);
    
    if (ctrl.value == hidValue1)
        ctrl.value = hidValue2;
    else
        ctrl.value = hidValue1;

    //alert("New: " + ctrl.value);

    if (postBackFlag)
        __doPostBack()
}

function GenericSetHidden(hidID, hidValue, postBackFlag)
{
    //alert("1: " + hidID + "(" + hidValue + ")");
    
    var ctrl = document.getElementById(hidID);

    ctrl.value = hidValue;

    if (postBackFlag)
        __doPostBack()
}

function GenericConfirmDelete(hidID, value, recordType)
{
    var agree = confirm("Delete " + recordType + " - Are you sure?");

    if (agree)
    {
        var ctrl = document.getElementById(hidID);

        ctrl.value = value;

        __doPostBack()
    }
}

function ConfirmCancel(hid_ID)
{
    var agree = confirm("Delete this order - Are you sure?");

    if (agree)
    {
        var ctrl = document.getElementById(hidID);

        ctrl.value = value;

        __doPostBack()
    }
}

function GenericConfirmDelete2(et, ei, url)
{
    var agree = confirm("Delete " + et + " - Are you sure?");

    if (agree)
    {
        window.location.href = url;
    }
}

function GenericConfirmDelete3(hidID, value, msg)
{
    var agree = confirm(msg);

    if (agree)
    {
        var ctrl = document.getElementById(hidID);

        ctrl.value = value;

        __doPostBack()
    }
}

/* ******************************************** */

function CellImageButtonClick(hidID, hidValue)
{
    var ctrl = document.getElementById(hidID);

    ctrl.value = hidValue;

    __doPostBack()
}

function CellTextButtonClick(hidID, hidValue)
{
    var ctrl = document.getElementById(hidID);

    ctrl.value = hidValue;

    __doPostBack()
}

function CellLinkButtonTextClick(hidID, value)
{
    var ctrl = document.getElementById(hidID);

    ctrl.value = value;

    __doPostBack()
}

function CellLinkButtonClick(ctrl, sufHid, value)
{
    var nameThis = ctrl.id;
    var nameHid = nameThis.substring(0, nameThis.length - 22) + sufHid;
    var idHid = document.getElementById(nameHid);

    idHid.value = value;

    __doPostBack()
}

function SetHiddenFromRadio(ctrl, sufHid, content_ID)
{
    var nameThis=ctrl.name;
    
    var nameHid=nameThis.substring(0, nameThis.length - 9) + sufHid;

    var idHid=document.getElementById(nameHid);

    idHid.value = content_ID;
}

function CellActionButtonClick(nameHid, value)
{
    var idHid = document.getElementById(nameHid);

    idHid.value = value;

    __doPostBack()
}

function ChangeCurrentUrl(url)
{
    window.location.href = url;
}

function SetUniqueRadioButton(nameregex, current)
{
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i]
      if (elm.type == 'radio')
      {
         if (re.test(elm.name))
         {
            elm.checked = false;
         }
      }
   }

   current.checked = true;
}

function FileAdd(hid, val)
{
    var hid_Ctl=document.getElementById(hid)

    hid_Ctl.value = val;

    __doPostBack()
}

function ConfirmDelete(hid, val, recordType)
{
    var agree = confirm("Delete " + recordType + " - Are you sure?");

    if (agree)
        FileDelete(hid, val);
}

function FileDelete(hid, val)
{
    var hid_Ctl=document.getElementById(hid)

    hid_Ctl.value = val;

    __doPostBack()
}

function UploadMaximum(max, fileType)
{
    if (max == 1)
        alert("You are only allowed 1 " + fileType);
    else
        alert("You are only allowed " + max + " " + fileType + "s");
}

function SelectMaximum(max, fileType)
{
    if (max == 1)
        alert("You are only allowed 1 " + fileType);
    else
        alert("You are only allowed " + max + " " + fileType + "s");
}

function AutoHide(div)
{
    var div_Ctl=document.getElementById(div)

    div_Ctl.style.display = "none";
}

/* ******************************************** */
/*                                              */
/* Comment functions                            */
/*                                              */
/* ******************************************** */

function CommentAddToggle(div, hid, btnAdd, btnCancel)
{
    var div_Ctl=document.getElementById(div)
    var hid_Ctl=document.getElementById(hid)
    var btnAdd_Ctl=document.getElementById(btnAdd)
    var btnCancel_Ctl=document.getElementById(btnCancel)

    if (hid_Ctl.value == "False")
    {
        div_Ctl.style.display = "block";
        hid_Ctl.value = "True";
        btnAdd_Ctl.style.display = "none";
        btnCancel_Ctl.style.display = "block";
    }
    else
    {
        div_Ctl.style.display = "none";
        hid_Ctl.value = "False";
        btnAdd_Ctl.style.display = "block";
        btnCancel_Ctl.style.display = "none";
    }
}

function DivToggle(div, flag)
{
    var div_Ctl=document.getElementById(div)

    if (div_Ctl.style.display == "none")
        div_Ctl.style.display = "block";
    else
        div_Ctl.style.display = "none";

//    if (flag)
//        div_Ctl.style.display = "block";
//    else
//        div_Ctl.style.display = "none";
}

function DivShow(div)
{
    var div_Ctl=document.getElementById(div)

    div_Ctl.style.display = "block";
}

function DivHide(div)
{
    var div_Ctl=document.getElementById(div)

    div_Ctl.style.display = "none";
}

function ElementToggle(div, flag)
{
    var div_Ctl=document.getElementById(div)

    if (div_Ctl.style.display == "none")
        div_Ctl.style.display = "block";
    else
        div_Ctl.style.display = "none";

    if (flag)
        div_Ctl.style.display = "block";
    else
        div_Ctl.style.display = "none";
}

function DivToggle2(div, hide, show)
{
    var div_Ctl=document.getElementById(div)
    var hide_Ctl=document.getElementById(hide)
    var show_Ctl=document.getElementById(show)

    if (div_Ctl.style.display == "none")
    {
        div_Ctl.style.display = "inline";
        hide_Ctl.style.display = "inline";
        show_Ctl.style.display = "none";
    }
    else
    {
        div_Ctl.style.display = "none";
        hide_Ctl.style.display = "none";
        show_Ctl.style.display = "inline";
    }
}

function SetFocus(ctrl)
{
    document.getElementById(ctrl).focus();
}

function Toggle(div)
{
    var div_Ctl=document.getElementById(div)

    if (div_Ctl.style.display == "none")
        div_Ctl.style.display = "inline";
    else
        div_Ctl.style.display = "none";
}

function ToggleSpan(span)
{
    var span_Ctl=document.getElementById(span)

    if (span_Ctl.style.display == "none")
        span_Ctl.style.display = "inline";
    else
        span_Ctl.style.display = "none";
}

function ToggleDiv(div)
{
    var div_Ctl=document.getElementById(div)

    if (div_Ctl.style.display == "none")
        div_Ctl.style.display = "block";
    else
        div_Ctl.style.display = "none";
}

function DoDivToggle(div)
{
    var div_Ctl=document.getElementById(div)

    if (div_Ctl.style.display == "none")
        div_Ctl.style.display = "block";
    else
        div_Ctl.style.display = "none";
}

function trim(str)
{
	return str.replace(/^\s+|\s+$/g,"");
}

function ltrim(str)
{
	return str.replace(/^\s+/,"");
}

function rtrim()
{
	return str.replace(/\s+$/,"");
}

/* ******************************************** */
/*                                              */
/* Generic functions                            */
/*                                              */
/* ******************************************** */

function Toggle1(divName, modeName)
{
    var modeCtl=document.getElementById(modeName)
    var divCtl=document.getElementById(divName)

    if (modeCtl.value == "False")
    {
        divCtl.style.display = "block";
        modeCtl.value = "True";
    }
    else
    {
        divCtl.style.display = "none";
        modeCtl.value = "False";
    }
}

function Toggle2(divName1, divName2, modeName)
{
    var modeCtl=document.getElementById(modeName)
    var divCtl1=document.getElementById(divName1)
    var divCtl2=document.getElementById(divName2)

    if (modeCtl.value == "False")
    {
        divCtl1.style.display = "none";
        divCtl2.style.display = "block";
        modeCtl.value = "True";
    }
    else
    {
        divCtl1.style.display = "block";
        divCtl2.style.display = "none";
        modeCtl.value = "False";
    }
}

function Toggle3(divName)
{
    var divCtl=document.getElementById(divName)

    if (divCtl.style.display == "none")
        divCtl.style.display = "block";
    else
        divCtl.style.display = "none";
}

function ToggleDisplay(ctrlName, action)
{
    var ctl=document.getElementById(ctrlName)

    if (action == "Show")
        ctl.style.display = "block";
    else
        ctl.style.display = "none";
}

function DoStuff(submitFlag)
{
    if ((!opener.closed) && (submitFlag))
    {
        window.opener.__doPostBack("", "NOVALIDATE");
    }

    self.close();
}

function PostBack()
{
    __doPostBack();
}

function DoStuff5()
{
    window.returnValue = "TTTTT";
    window.close();
}

function DoStuffOld2()
{ 
    window.opener.document.forms(0).submit();

    self.close();
}

function openChild(file,window)
{
    childWindow=open(file,window,'resizable=no,width=200,height=400');
    
    if (childWindow.opener == null)
        childWindow.opener = self;
}

function updateParent(rf, tf)
{
    var txtValue = document.getElementById(tf).value;
    
    window.opener.form1.txtUsername.value = txtValue; 
    window.close();

}

function ShowSettings(btn, ctlMain, ctlSettings)
{
    var ctl1=document.getElementById(ctlMain)
    var ctl2=document.getElementById(ctlSettings)

    ctl1.style.display = "none";
    ctl2.style.display = "block";

    btn.style.visibility = "hidden";
}

function HideSettings(btn, ctlMain, ctlSettings)
{
    var ctl1=document.getElementById(ctlMain)
    var ctl2=document.getElementById(ctlSettings)
    var ctl3=document.getElementById(btn)

    ctl1.style.display = "block";
    ctl2.style.display = "none";

    ctl3.style.visibility = "visible";
}

function SetUrl(strID, strVal)
{
    var ctrl=document.getElementById(strID)

    ctrl.value = strVal;
}

/* ******************************************** */
/*                                              */
/* Panel control functions                      */
/*                                              */
/* ******************************************** */

function SetPanelButtons(divPanel, divBody, btnCol, btnExp, btnMin, btnMax, minCol, maxCol)
{
    var ctrl_divPanel=document.getElementById(divPanel);
    var ctrl_divBody=document.getElementById(divBody);

    var ctrl_btnCol=document.getElementById(btnCol);
    var ctrl_btnExp=document.getElementById(btnExp);
    var ctrl_btnMin=document.getElementById(btnMin);
    var ctrl_btnMax=document.getElementById(btnMax);

    ctrl_btnCol.style.display = "block";
	ctrl_btnExp.style.display = "block";

    if (ctrl_divPanel.style.width == minCol)
		ctrl_btnCol.style.display = "none";
    else
    {
        if (ctrl_divPanel.style.width == maxCol)
		    ctrl_btnExp.style.display = "none";
    }

	if (ctrl_divBody.style.display == "none")
        ctrl_btnMin.style.display = "none";
    else
        ctrl_btnMax.style.display = "none";
}

function CollapseMe(divPanel, divBody, btnCol, btnExp, btnMin, btnMax, minCol, maxCol)
{
    var ctrl_divPanel=document.getElementById(divPanel);

    switch(ctrl_divPanel.style.width)
    {
        case "40%":
		ctrl_divPanel.style.width = "20%";
        break

        case "60%":
		ctrl_divPanel.style.width = "40%";
        break

        case "80%":
		ctrl_divPanel.style.width = "60%";
        break

        case "100%":
		ctrl_divPanel.style.width = "80%";
        break

        default:
        alert("Bad width");
    }
    
    SetPanelButtons(divPanel, divBody, btnCol, btnExp, btnMin, btnMax, minCol, maxCol);
}

function ExpandMe(divPanel, divBody, btnCol, btnExp, btnMin, btnMax, minCol, maxCol)
{
    var ctrl_divPanel=document.getElementById(divPanel);

    switch(ctrl_divPanel.style.width)
    {
        case "20%":
		ctrl_divPanel.style.width = "40%";
        break

        case "40%":
		ctrl_divPanel.style.width = "60%";
        break

        case "60%":
		ctrl_divPanel.style.width = "80%";
        break

        case "80%":
		ctrl_divPanel.style.width = "100%";
        break

        default:
        alert("Bad width");
    }
    
    SetPanelButtons(divPanel, divBody, btnCol, btnExp, btnMin, btnMax, minCol, maxCol);
}

function MinimizeMe(divPanel, divBody, btnCol, btnExp, btnMin, btnMax, minCol, maxCol)
{
    var ctrl_divBody=document.getElementById(divBody);
    var ctrl_btnMin=document.getElementById(btnMin);
    var ctrl_btnMax=document.getElementById(btnMax);

	ctrl_btnMin.style.display = "none";
	ctrl_btnMax.style.display = "block";

	ctrl_divBody.style.display = "none";
}

function MaximizeMe(divPanel, divBody, btnCol, btnExp, btnMin, btnMax, minCol, maxCol)
{
    var ctrl_divBody=document.getElementById(divBody);
    var ctrl_divPanel=document.getElementById(divPanel);
    var ctrl_btnMin=document.getElementById(btnMin);
    var ctrl_btnMax=document.getElementById(btnMax);

	ctrl_btnMin.style.display = "block";
	ctrl_btnMax.style.display = "none";

	ctrl_divBody.style.display = "block";

    // SetPanelButtons(divPanel, divBody, btnCol, btnExp, btnMin, btnMax, minCol, maxCol);
}

function RemoveMe(divPanel, divBody, btnCol, btnExp, btnMin, btnMax, minCol, maxCol, browser)
{
    var ctrl_divPanel=document.getElementById(divPanel);

    ctrl_divPanel.removeChild(ctrl_divPanel.lastChild);
    ctrl_divPanel.removeChild(ctrl_divPanel.lastChild);
    
    if (browser == "Firefox")
    {
        ctrl_divPanel.removeChild(ctrl_divPanel.lastChild);
        ctrl_divPanel.removeChild(ctrl_divPanel.lastChild);
    }
}

/* ******************************************** */
/*                                              */
/* Other functions                              */
/*                                              */
/* ******************************************** */

function DoStuffOld()
{
    window.returnValue = "TTTTT";
    window.close();
}

function PanelChange(strID, strVal)
{
    var ctrl=document.getElementById(strID)

    ctrl.value = strVal;
}

/*

function openCustomWindow(strUrl, strOptions)
{
    window.open(strUrl, null, strOptions);
}

function openNewWindowFull(strUrl)
{
    window.open(strUrl, null, "width=800,status=yes,toolbar=yes,menubar=yes,scrollbars=yes,location=yes,resizable=yes");
}

function openNewWindow10(strUrl)
{
alert("kk");
//    window.open(strUrl, null, "height=200, width=400,status=no, toolbar=no,menubar=no,location=no");
    window.open(strUrl, null, "width=800,status=yes, toolbar=yes,menubar=yes,location=yes");
}

function openNewWindow2(strUrl)
{
    window.open(strUrl, null, "width=800,status=yes, toolbar=yes,menubar=yes,scrollbars=yes,location=yes");
}

function openNewWindow3OLD(strUrl)
{
//    window.open(strUrl, null, "top=200, left=200, width=800, height=500, status=no, toolbar=no, menubar=no, scrollbars=yes, location=no");

//    window.open(strUrl, null, "top=200, left=200, width=800, height=520, status=no, toolbar=yes, menubar=yes, scrollbars=yes, location=no");
    window.open(strUrl, null, "top=200, left=200, width=800, height=520");
}

function openNewWindow3(strUrl)
{
    window.open(strUrl, null, "location=no, top=200, left=200, width=800, height=520, status=no, toolbar=no, menubar=no, scrollbars=yes, titlebar=no");
}

function openNewWindow4(strUrl)
{
    window.open(strUrl, null, "location=no, top=200, left=200, width=835, height=820, status=no, toolbar=no, menubar=no, scrollbars=yes, titlebar=no");
}

function openNewWindow5(strUrl)
{
    window.open(strUrl, null, "location=no, top=200, left=200, width=1025, height=820, status=no, toolbar=no, menubar=no, scrollbars=yes, titlebar=no");
}

function openNewWindow6(strUrl)
{
    window.open(strUrl, null, "location=no, top=50, left=250, height=570, width=770, status=no, toolbar=no, menubar=no, scrollbars=yes, titlebar=no");
}

function openNewWindow7(strUrl)
{
    window.open(strUrl, null, "top=50, left=250, height=570, width=770");
}

function openHelpWindow(strUrl)
{
    window.open(strUrl, null, "top=100,left=100,width=600,height=540,status=no, toolbar=no,menubar=no,scrollbars=yes,location=no");
}

function openNewWindowReturn(strUrl, returnID)
{
    var ctrl=document.getElementById(returnID)

    window.open(strUrl, null, "width=800,status=yes, toolbar=yes,menubar=yes,location=yes");
}

function openModalWindow(strUrl, returnID)
{
//    var ctrl=document.getElementById(returnID)

//    ctrl.value = window.showModalDialog(strUrl, "width=800, status=yes, toolbar=yes, menubar=yes, location=yes");

//    ctrl.value = window.showModalDialog(strUrl, "width=800, status=yes, toolbar=yes, menubar=yes, location=yes");

    window.open(strUrl, null, "width=700, height=400, status=yes, toolbar=no,menubar=no,location=no, scrollbars=yes");
}

function openPopupWindow(strUrl, width, height)
{
    //window.open(strUrl, null, "width=" + width + ", height=" + height + ", status=yes,toolbar=no,menubar=no,location=no, scrollbars=yes");

//    window.open(strUrl, "myWindow", "width=" + width + ",top=200, left=200 ,height=" + height + ",scrollbars=no,toolbars=no;void(0)");
    window.open(strUrl, "myWindow", "width=720,top=200,left=200,height=500,scrollbars=no,toolbars=no;void(0)");

    //window.open(strUrl, "myWindow", "width=" + width + ",height=" + height + ",scrollbars=no,toolbars=no,top='+((screen.availHeight/2)-(50/2))+',left='+((screen.availWidth/2)-(50/2))+'');void(0)");
}

function openPopupWindowOLD(strUrl, width, height)
{
    window.open(strUrl, "myWindow", "status=1,toolbar=0,menubar=0,location=0,scrollbars=1,width=" + width + ",height=" + height);
}

function openModelessWindow(strUrl, returnID)
{
    var ctrl=document.getElementById(returnID)

//    ctrl.value = window.showModalDialog(strUrl, "width=800, status=yes, toolbar=yes, menubar=yes, location=yes");
    ctrl.value = window.openModelessDialog(strUrl, "width=800, status=yes, toolbar=yes, menubar=yes, location=yes");
}

function openModelessWindowOLD(strUrl, returnID)
{
    var ctrl=document.getElementById(returnID)

//    ctrl.value = window.showModalDialog(strUrl, "width=800, status=yes, toolbar=yes, menubar=yes, location=yes");
    ctrl.value = window.open(strUrl, null, "width=800, status=yes, toolbar=yes, menubar=yes, location=yes");
}

*/

function openHelpWindow(strUrl)
{
    window.open(strUrl, null, "top=100,left=100,width=680,height=540,status=no, toolbar=no,menubar=no,scrollbars=yes,location=no");
}


function openPopupWindowSize(strUrl, width, height)
{
    window.open(strUrl, null, "top=100,left=100,width=" + width + ",height=" + height + ",status=no,toolbar=no,menubar=no,scrollbars=yes,location=no,resizable=yes,urlBar=false");
}

function openPopupWindow(strUrl)
{
    window.open(strUrl, null, "top=100,left=100,width=700,height=490,status=no,toolbar=no,menubar=no,scrollbars=yes,location=no,resizable=yes,urlBar=false");
}

function openNewWindow(strUrl)
{
    window.open(strUrl, null, "width=800,status=yes, toolbar=yes,scrollbars=yes,menubar=yes,location=yes,resizable=yes");
}

/* function ColumnAlign(obj)
{
    var pnl = obj.parentNode.parentNode.parentNode;
    var tmp = pnl.lastChild
    var str1 = tmp.id;
    var str2;

    str2 = document.getElementById(str1).style.styleFloat;

    if (confirm("Right Align?"))
    {
  	 tmp.style.float = "right";
    }

    document.getElementById(str1).style.styleFloat="right";
}

*/

function RemoveFromList(item, hid_ID, lbl_ID)
{
    var hid = document.getElementById(hid_ID)
    var lbl = document.getElementById(lbl_ID)

    hid.value = hid.value.replace(" " + item + ";", "");
    hid.value = hid.value.replace(item + ";", "");
    
    BuildSelectedList(hid_ID, lbl_ID);
}

function AddToList(item, hid_ID, lbl_ID)
{
    var hid = document.getElementById(hid_ID)

    if (hid.value != "")
    {
        if (hid.value.endsWith(";", true) == false)
            hid.value = hid.value + " ;";
    }
        
    hid.value = hid.value + " " + item + ";";
    hid.value = trim(hid.value);
    
    BuildSelectedList(hid_ID, lbl_ID);
}

function BuildSelectedList(hid_ID, lbl_ID)
{
    var h = document.getElementById(hid_ID)
    var l = document.getElementById(lbl_ID)
    
    var hid = h.value;

    var img = "../../image/icon/icon_sm_delete.gif";

    var vars = new Array();
    
    vars = hid.split(";");

    var str = "";

    if ( (vars.length > 0) && (hid != "") )
    {
        var i = 0;

        for (i = 0; i < vars.length; i++)
        {
            var item = trim(vars[i]);
            
            if (item != "")
            {
//                alert(item);
                var lbl = item;
                lbl = lbl.replace(/</g, "&lt;")
                lbl = lbl.replace(/>/g, "&gt;")
                lbl = lbl.replace(/~/g, "'")
               
//                alert(lbl);

                var lnk = "javascript: RemoveFromList('" + item + "','" + hid_ID + "','" + lbl_ID + "');";

                str = str + "<a href=\"" + lnk + "\"><img alt=\"remove\" class=\"va-m\" src=\"" + img + "\"></a>";

                if (item.substring(0, 1) == "[")
                    str = str + "&nbsp;" + "<a class=\"c-bl fw-b\" href=\"" + lnk + "\">" + lbl + "</a>" + "<br />";
                else
                    str = str + "&nbsp;" + "<a class=\"va-m\" href=\"" + lnk + "\">" + lbl + "</a>" + "<br />";
                    
//                alert(str);
            }
        }
    }
    
    l.innerHTML = str;
}

function SendList(hid_ID, txt_ID, lbl_ID)
{
    var txt = document.getElementById(txt_ID)
    var hid = document.getElementById(hid_ID)

    var work = hid.value;
    work = work.replace(/~/g, "'")

    txt.value = work;
}

function GetList(hid_ID, txt_ID, lbl_ID)
{
    var txt = document.getElementById(txt_ID)
    var hid = document.getElementById(hid_ID)

    var work = txt.value;
    work = work.replace(/'/g, "~")

    hid.value = work;
    
    BuildSelectedList(hid_ID, lbl_ID);
}

function endsWith(eString)
{
    var reg = new RegExp(eString + "$");
     
    return reg.test(this);
}

function startsWith(eString)
{
    var reg = new RegExp("^" + eString);
    
    return reg;
}

String.prototype.endsWith = function(txt,ignoreCase)
{
  var rgx;

  if(ignoreCase)
  {
    rgx = new RegExp(txt+"$","i");
  }
  else
  {
    rgx = new RegExp(txt+"$");
  }

  return this.match(rgx)!=null; 
}


