var DBFDEBUG        = false;
var Records         = new Array();
var Customer        = new Array();
var Methods         = new Array();
var TotalRecords;
var BillingInfoPage = "billinginfo.htm";
var CheckOutPage    = "checkout.htm";
var FinalizePage    = "process.htm";
var StorePage       = "";
var ViewCartPage    = "viewcart.htm";
var TaxRate         = 0.0625;
var TotalTaxes      = 0.0;
var TotalShipping   = 0.0;
var TotalAmount     = 0.0;
var DogWatchName    = "";
var DogImageName    = "";
var CartCookie      = "DBFCart";
var CustCookie      = "DBFCust";
var BDayCookie      = "DBFBDay";
var LastDogIndex;

function AddItem(Description,ItemID,ImageFile,Price)
{
    var i     = 0;
    var found = false;

    for(i = 0; i < Records.length; i++)
     {
        if(Records[i].Description == Description)
         {
            found = true;
            break
         }
     }
    if(found == false)
     {
        i = TotalRecords;
        Records[i] = new CreateRecord(Description,ItemID,1,ImageFile,Price);
        SaveRecordsToCookie();
     }
    location = ViewCartPage;
}

function CreateCustomer(BillFirst,BillLast,BillCompany,BillAddress1,BillAddress2,BillCity,BillState,BillZip,ShipFirst,ShipLast,ShipCompany,ShipAddress1,ShipAddress2,ShipCity,ShipState,ShipZip,Phone,Cardtype,CardNumber,CardLast4,ExpireMM,ExpireYY,Email)
{
    this.BillFirst    = BillFirst;
    this.BillLast     = BillLast;
    this.BillCompany  = BillCompany;
    this.BillAddress1 = BillAddress1;
    this.BillAddress2 = BillAddress2;
    this.BillCity     = BillCity;
    this.BillState    = BillState;
    this.BillZip      = BillZip;
    this.ShipFirst    = ShipFirst;
    this.ShipLast     = ShipLast;
    this.ShipCompany  = ShipCompany;
    this.ShipAddress1 = ShipAddress1;
    this.ShipAddress2 = ShipAddress2;
    this.ShipCity     = ShipCity;
    this.ShipState    = ShipState;
    this.ShipZip      = ShipZip;
    this.Phone        = Phone;
    this.Cardtype     = Cardtype;
    this.CardNumber   = CardNumber;
    this.CardLast4    = CardLast4;
    this.ExpireMM     = ExpireMM;
    this.ExpireYY     = ExpireYY;
    this.Email        = Email;
}

function CreateMethod(One,Two)
{
    this.One = One;
    this.Two = Two;
}

function CreateRecord(Description,ItemID,Quantity,ImageFile,Price)
{
    if(Quantity > 0)
     {
        this.Description = Description;
        this.ItemID      = ItemID;
        this.Quantity    = Quantity;
        this.ImageFile   = ImageFile;
        this.Price       = Price;
        TotalRecords++;
     }
}

function DefineMethods()
{
    var tempstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~0123456789 `!@#$%^&*()_-+={[}]\\:;\"'<,>.?/";
    var tempfld = "Vg0;Lp@\"Fa3$HY#'Wi7+R Ik4[Aqz:Ejy=NZ5>Qe~.Mb9]Xcx)Bds*Cfw(Dh1_Gl2-Jm6{K<n8}Oo`\\Pr!St%,Tu^?Uv&/";
    var i       = 0;

    if(Methods.length < 1)
     {
        for(i = 0; i < 94; i++)
         {
            Methods[i] = new CreateMethod(tempstr.substr(i,1),tempfld.substr(i,1));
         }
     }
}

function DeleteCookie(CookieKey)
{
    SaveCookie(CookieKey,"",SetCookieExpire(0,0,0,0,0,0,-2));
}

function DeleteRecordsCookie()
{
    var i        = 0;
    var contents = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    for(i = 0; i < 10000; i++)
     {
        contents = ReadCookieContents(CartCookie + '_' + i);
        if(contents != '' && contents != null)
         {
            DeleteCookie(CartCookie + '_' + i);
         }
        else
         {
            break;
         }
     }
}

function DeleteCustomerCookie()
{
    var contents = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    contents = ReadCookieContents(CartCookie + '_0');
    if(contents != '' && contents != null)
     {
        DeleteCookie(CustCookie);
     }
}

function DisplayRecords()
{
   var tempstr = "";
   var i       = 0;

   for(i = 0; i < Records.length; i++)
    {
       tempstr += Records[i] + "\n";
    }
   alert(tempstr);
}

function EmptyCart()
{
    var i = 0;

    for(i = 0; i < Records.length; i++)
     {
        UpdateQuantity(i,0);
     }
    SaveRecordsToCookie();
    location.reload();
}

function GetCookie(CookieKey)
{
    var tempcookie = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    if(document.cookie && document.cookie.indexOf(CookieKey) != -1)
     {
        tempcookie = document.cookie;
        tempcookie = tempcookie.substring(tempcookie.indexOf(CookieKey),tempcookie.length);
        if(tempcookie.indexOf(';') != -1)
         {
            tempcookie = tempcookie.substring(0,tempcookie.indexOf(';'));
         }
        return tempcookie;
     }
}

function GetMethod(FieldValue)
{
    var i       = 0;
    var j       = 0;
    var ch      = "";
    var tempstr = "~";

    DefineMethods();
    if(FieldValue.charAt(0) != "~")
     {
        for(i = 0; i < FieldValue.length; i++)
         {
            ch = FieldValue.charAt(i);
            for(j = 0; j < 94; j++)
             {
                if(ch == Methods[j].One)
                    break;
             }
            tempstr += Methods[j].Two;
         }
        return(tempstr);
     }
    else
     {
        return(FieldValue);
     }
}

function isalpha(CharValue)
{
    var tempstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";

    if(tempstr.indexOf(CharValue) == -1)
     {
        return false;
     }
    return true;
}

function isdigit(CharValue)
{
    var tempstr = "-0123456789"

    if(tempstr.indexOf(CharValue) == -1)
     {
        return false;
     }
    return true;
}

function isempty(StrValue)
{
    if(StrValue == "" || StrValue == null)
     {
        return true;
     }
    return false;
}

function isfloat(StrValue)
{
    var i  = 0;
    var ch = "";

    if(StrValue.indexOf(".") == -1)
     {
        return false;
     }
    for(i = 0; i < StrValue.length; i++)
     {
        ch = StrValue.charAt(i);
        if(!ispennies(ch))
         {
            return false;
         }
     }
    return true;
}

function ispennies(CharValue)
{
    var tempstr = "-.0123456789";

    if(tempstr.indexOf(CharValue) == -1)
     {
        return false;
     }
    return true;
}

function PutMethod(FieldValue)
{
    var i       = 0;
    var j       = 0;
    var ch      = "";
    var tempstr = "";

    DefineMethods();
    if(FieldValue.charAt(0) == "~")
     {
        for(i = 1; i < FieldValue.length; i++)
         {
            ch = FieldValue.charAt(i);
            for(j = 0; j < 94; j++)
             {
                if(ch == Methods[j].Two)
                    break;
             }
            tempstr += Methods[j].One;
         }
        return(tempstr);
     }
    else
     {
        return(FieldValue);
     }
}

function ReadCookieContents(CookieKey)
{
    var tempcookie = "";
    var tempvalue  = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    tempcookie = GetCookie(CookieKey);
    if(tempcookie != '' && tempcookie != null)
     {
        tempvalue = tempcookie.substring(tempcookie.indexOf(CookieKey) + CookieKey.length + 1,tempcookie.length);
        if(tempvalue.indexOf('|') != -1)
         {
            tempvalue = tempvalue.split('|');
         }
        return tempvalue;
     }
    else
     {
        return "";
     }
}

function ReadCookieToCustomer()
{
    var contents = "";
    var fields   = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    contents = ReadCookieContents(CustCookie);
    if(contents != '' && contents != null)
     {
        fields = ReadCookieContents(CustCookie);
        UpdateCustomer(fields[0],fields[1],fields[2],fields[3],fields[4],fields[5],fields[6],fields[7],fields[8],fields[9],fields[10],fields[11],fields[12],fields[13],fields[14],fields[15],fields[16],fields[17],fields[18],fields[19],fields[20],fields[21],fields[22],fields[23]);
     }
    else
     {
        UpdateCustomer("","","","","","","","","","","","","","","","","","","","","","","","");
        if(DBFDEBUG == true)
         {
            alert('WARNING: ReadCookieToCustomer() was called, but customer cookie is either not set, or NULL');
         }
     }
}

function ReadCookieToRecords()
{
    var i        = 0;
    var contents = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    TotalRecords = 0;
    for(i = 0; i < 100; i++)
     {
        contents = ReadCookieContents(CartCookie + '_' + i);
        if(contents != '' && contents != null)
         {
            itemlist   = ReadCookieContents(CartCookie + '_' + i);
            Records[i] = new CreateRecord(itemlist[0],itemlist[1],itemlist[2],itemlist[3],itemlist[4]);
         }
        else
         {
            if(DBFDEBUG == true)
             {
                alert('WARNING: ReadCookieToRecords() was called, but Records[] cookie is either not set, or NULL');
             }
            break;
         }
     }
}

function RecalcOrder()
{
    var i     = 0;
    var qty   = 0;
    var index = 0;

    SetDealerMode();
    for(i = 0; i < document.OrderForm.length; i++)
     {
        if(document.OrderForm[i].type == "text")
         {
            if(document.OrderForm[i].name != "DealerCode")
             {
                qty = parseInt(document.OrderForm[i].value);
                if(qty < 1)
                 {
                    alert("Please enter a quantity greater than 0.");
                    document.OrderForm[i].focus();
                    return;
                 }
                index = document.OrderForm[i].record_index;
                UpdateQuantity(index,qty);
             }
         }
     }
    SaveRecordsToCookie();
    location.reload();
}

function RecalcQuantity()
{
    var qty = 0;

    for(i = 0; i < document.OrderForm.length; i++)
     {
        if(document.OrderForm[i].type == "text")
         {
            if(document.OrderForm[i].name != "DealerCode")
             {
                qty += parseInt(document.OrderForm[i].value);
             }   
         }   
     }
    return(qty); 
}

function RemoveItem(RecIndex)
{
    UpdateQuantity(RecIndex,0);
    SaveRecordsToCookie();
    location.reload();
}

function RoundFloat(NumberValue,Places)
{
    var result1 = NumberValue * Math.pow(10,Places);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10,Places);
    return ZeroPad(result3,Places);
}

function SaveCookie(CookieKey,CookieValue,CookieExpire)
{
    var tempcookie = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    tempcookie = CookieKey + '=' + CookieValue;
    if(CookieExpire != "" && CookieExpire != null)
     {
        tempcookie += ';expires=' + CookieExpire.toGMTString();
     }
    document.cookie = tempcookie;
}

function SaveCustomerToCookie()
{
    var contents = "";
    var tempstr  = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    contents = ReadCookieContents(CartCookie + '_0');
    if(contents != '' && contents != null)
     {
        DeleteCookie(CustCookie);
     }
    if(Customer.length > 0)
     {
        tempstr  = "";
        tempstr += Customer[0].BillFirst    + '|';
        tempstr += Customer[0].BillLast     + '|';
        tempstr += Customer[0].BillCompany  + '|';
        tempstr += Customer[0].BillAddress1 + '|';
        tempstr += Customer[0].BillAddress2 + '|';
        tempstr += Customer[0].BillCity     + '|';
        tempstr += Customer[0].BillState    + '|';
        tempstr += Customer[0].BillZip      + '|';
        tempstr += Customer[0].ShipFirst    + '|';
        tempstr += Customer[0].ShipLast     + '|';
        tempstr += Customer[0].ShipCompany  + '|';
        tempstr += Customer[0].ShipAddress1 + '|';
        tempstr += Customer[0].ShipAddress2 + '|';
        tempstr += Customer[0].ShipCity     + '|';
        tempstr += Customer[0].ShipState    + '|';
        tempstr += Customer[0].ShipZip      + '|';
        tempstr += Customer[0].Phone        + '|';
        tempstr += Customer[0].Cardtype     + '|';
        tempstr += Customer[0].CardNumber   + '|';
        tempstr += Customer[0].CardLast4    + '|';
        tempstr += Customer[0].ExpireMM     + '|';
        tempstr += Customer[0].ExpireYY     + '|';
        tempstr += Customer[0].Email        + '|';
        SaveCookie(CustCookie,tempstr,eval(SetCookieExpire(0,0,4,0,0,0,0)));
     }
}

function SaveRecordsToCookie()
{
    var i        = 0;
    var contents = "";
    var tempstr  = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    for(i = 0; i < 10000; i++)
     {
        contents = ReadCookieContents(CartCookie + '_' + i);
        if(contents != '' && contents != null)
         {
            DeleteCookie(CartCookie + '_' + i);
         }
        else
         {
            break;
         }
     }
    if(Records.length > 0)
     {
        for(i = 0; i < Records.length; i++)
         {
            if(Records[i].ItemID != "undefined")
             {
                tempstr  = "";
                tempstr += Records[i].Description + '|';
                tempstr += Records[i].ItemID      + '|';
                tempstr += Records[i].Quantity    + '|';
                tempstr += Records[i].ImageFile   + '|';
                if(Records[i].ItemID != "Dog Watch")
                 {
                    tempstr += Records[i].Price + '|';
                 }    
                else
                 { 
                    if(GetGloVar("DCOK") == 1)
                     {
                        tempstr += 7.95 + '|';
                     }
                    else
                     {
                        tempstr += 14.95 + '|';
                     }    
                 }   
                SaveCookie(CartCookie + '_' + i,tempstr,eval(SetCookieExpire(0,0,1,0,0,0,0)));
             }
         }
     }
}

function SetDealerMode()
{
    var dc  = 0;
    var qty = 0;
    
    if(ValidDealerCode() == false)
     {
        PutGloVar("DCOK",0);
        return;
     }
    dc  = parseInt(GetGloVar("DC"));
    qty = RecalcQuantity();
    if((qty >= dc) && (dc > 0))
     {
        PutGloVar("DCOK",1);
     }  
    else
     { 
        PutGloVar("DCOK",0);
     }   
}

function StringStrip(StrValue,SearchValues)
{
    var tempstr = "";
    var ch      = "";
    var i       = 0;

    for(i = 0; i < StrValue.length; i++)
     {
        ch = StrValue.charAt(i);
        if(SearchValues.indexOf(ch) == -1)
         {
            tempstr += ch;
         }
     }
    return tempstr;
}

function UpdateCustomer(BillFirst,BillLast,BillCompany,BillAddress1,BillAddress2,BillCity,BillState,BillZip,ShipFirst,ShipLast,ShipCompany,ShipAddress1,ShipAddress2,ShipCity,ShipState,ShipZip,Phone,Cardtype,CardNumber,CardLast4,ExpireMM,ExpireYY,Email)
{
    if(Customer.length == 0)
     {
        Customer[0] = new CreateCustomer(BillFirst,BillLast,BillCompany,BillAddress1,BillAddress2,BillCity,BillState,BillZip,ShipFirst,ShipLast,ShipCompany,ShipAddress1,ShipAddress2,ShipCity,ShipState,ShipZip,Phone,Cardtype,CardNumber,CardLast4,ExpireMM,ExpireYY,Email);
     }
    Customer[0].BillFirst    =  BillFirst;
    Customer[0].BillLast     =  BillLast;
    Customer[0].BillCompany  =  BillCompany;
    Customer[0].BillAddress1 =  BillAddress1;
    Customer[0].BillAddress2 =  BillAddress2;
    Customer[0].BillCity     =  BillCity;
    Customer[0].BillState    =  BillState;
    Customer[0].BillZip      =  BillZip;
    Customer[0].ShipFirst    =  ShipFirst;
    Customer[0].ShipLast     =  ShipLast;
    Customer[0].ShipCompany  =  ShipCompany;
    Customer[0].ShipAddress1 =  ShipAddress1;
    Customer[0].ShipAddress2 =  ShipAddress2;
    Customer[0].ShipCity     =  ShipCity;
    Customer[0].ShipState    =  ShipState;
    Customer[0].ShipZip      =  ShipZip;
    Customer[0].Phone        =  Phone;
    Customer[0].Cardtype     =  Cardtype;
    Customer[0].CardNumber   =  CardNumber;
    Customer[0].CardLast4    =  CardLast4;
    Customer[0].ExpireMM     =  ExpireMM;
    Customer[0].ExpireYY     =  ExpireYY;
    Customer[0].Email        =  Email;
    SaveCustomerToCookie();
}

function UpdateQuantity(RecIndex,Quantity)
{
    Records[RecIndex].Quantity = Quantity;
    if(Quantity == 0)
     {
        TotalRecords--;
     }
}

function ValidCreditCard(CardNumber,Cardtype)
{
    var vmask = "4###############";
    var mmask = "5###############";
    var cmask = "";

    if(Cardtype.toUpperCase() == "VISA")
     {
        cmask = vmask;
     }
    else if(Cardtype.toUpperCase() == "MASTERCARD")
     {
        cmask = mmask;
     }
    if(!ValidMask(CardNumber,cmask,false))
     {
        return false;
     }
    return true;
}

function ValidDealerCode()
{
    var i       = 0;
    var validdc = 0;

    for(i = 0; i < document.OrderForm.length; i++)
     {
        if(document.OrderForm[i].type == "text")
         {
            if(document.OrderForm[i].name == "DealerCode")
             {
                if(document.OrderForm[i].value == "DC15" || document.OrderForm[i].value == "dc15")
                 {
                    validdc = 1;
                    PutGloVar("DC",15);
                 }
                if(document.OrderForm[i].value == "DC10" || document.OrderForm[i].value == "dc10")
                 {
                    validdc = 1;
                    PutGloVar("DC",10);
                 }
                if(validdc > 0)
                 {
                    i = 1 + document.OrderForm[i].length;
                 }
             }   
         }
     }
    if(validdc == 0)
     {
        return(false);
     }
    else
     {
        return(true);
     } 
}

function ValidEmail(EmailAddress)
{
    var atpos  = 0;
    var dorpos = 0;

    if(EmailAddress.length < 5)
     {
        return false;
     }
    atpos  = EmailAddress.indexOf("@");
    dotpos = EmailAddress.lastIndexOf(".");
    if(atpos == -1 || dotpos == -1 || atpos > dotpos)
     {
        return false;
     }
    if(atpos == 0)
     {
        return false;
     }
    if(dotpos - atpos < 2)
     {
        return false;
     }
    if(EmailAddress.length - dotpos < 2)
     {
        return false;
     }
    return true;
}

function ValidField(FieldName,FieldValue,Fieldtype,FieldOption)
{
    // Fieldtype's
    //
    // D  = Credit Card Date (see FieldOption's below)
    // E  = Email
    // N  = Credit Card Number
    // S  = String
    // T  = Telephone
    // Z  = Zip Code (Must be NNNNN[-NNNN])

    // FieldOption's
    //
    // '' = Ignore
    // Fieldtype == D      FieldValue == Month, FieldOption == Year
    // Fieldtype == N      Credit Card Number
    //

    var today   = new Date();
    var failed  = false;
    var tempstr = "";
    var message = "";
    var ct      = "";
    var cn      = "";
    var month   = 0;
    var year    = 0;
    var i       = 0;

    message = 'The information you supplied for the "' + FieldName + '" field is invalid.\nThis must be corrected before you can submit this form.';
    if(Fieldtype == "D")
     {
        month = today.getMonth();
        year  = today.getYear();

        month++;
        if(FieldOption < year)
         {
            failed = true;
         }
        else
         {
            if(FieldOption == year)
             {
                if(FieldValue < month)
                 {
                    failed = true;
                 }
             }
         }
     }
    if(Fieldtype == "E")
     {
        tempstr = StringStrip(FieldValue," ");
        if(ValidEmail(tempstr) == false)
         {
            failed = true;
         }
     }
    if(Fieldtype == "N")
     {
        if(FieldValue == "")
         {
            failed = true;
         }
        if(FieldOption == "")
         {
            failed == true;
         }
        if(failed == false)
         {
            if(ValidCreditCard(FieldValue,FieldOption) == false)
             {
                i       = 0;
                cn      = FieldValue.charAt(0);
                ct      = FieldOption.charAt(0);
                tempstr = 'You have selected a ' + FieldOption + ' as your credit card, but the number appears to belong to a ';
                if(ct == "V" && cn == "5")
                 {
                    i = 1;
                    tempstr = tempstr + 'MasterCard.\n\nThe correct format for a Visa is 4###-####-####-####.';
                 }
                if(ct == "M" && cn == "4")
                 {
                    i = 2;
                    tempstr = tempstr + 'Visa.\n\nThe correct format for a MasterCard is 55##-####-####-###4.';
                 }
                if(i > 0)
                 {
                    tempstr = tempstr + '\n\nThis must be corrected before you can submit this form.';
                    message = tempstr;
                 }
                failed = true;
             }
         }
        if(failed == false)
         {
            CardLast4 = FieldValue.substring(FieldValue.length - 4);
         }
     }
    if(Fieldtype == "P")
     {
        tempstr = StringStrip(FieldValue," ");
        if(tempstr != FieldOption)
         {
            failed = true;
         }
        if(tempstr == "")
         {
            failed = true;
         }
     }
    if(Fieldtype == "S")
     {
        tempstr = StringStrip(FieldValue," ");
        if(tempstr == "")
         {
            failed = true;
         }
     }
    if(Fieldtype == "T")
     {
        if(FieldValue.length == 10)
         {
            for(i = 0; i < FieldValue.length; i++)
             {
                if(!isdigit(FieldValue.charAt(i)))
                 {
                    failed = true;
                 }
             }
         }
        else
         {
            failed = true;
         }
     }
    if(Fieldtype == "Z")
     {
        tempstr = StringStrip(FieldValue," ");
        if(tempstr.length == 5 || tempstr.length == 10)
         {
            for(i = 0; i < tempstr.length; i++)
             {
                if(!isdigit(tempstr.charAt(i)))
                 {
                    failed = true;
                 }
             }
         }
        else
         {
            failed = true;
         }
     }
    if(failed == true)
     {
        alert(message);
        return false;
     }
    return true;
}

function ValidMask(FieldValue,MaskValue,Sensitive)
{
    var mask = "";
    var ch   = "";
    var i    = 0;

    if(FieldValue.length != MaskValue.length)
     {
        return false;
     }
    for(i = 0; i < MaskValue.length; i++)
     {
        mask = MaskValue.charAt(i);
        ch   = FieldValue.charAt(i);
        if(mask == "@")
         {
            if(!isalpha(ch))
             {
                return false;
             }
         }
        else if(mask == "#")
         {
            if(!isdigit(ch))
             {
                return false;
             }
         }
        else
         {
            if(!Sensitive)
             {
                mask = mask.toUpperCase();
                ch   = ch.toUpperCase();
             }
            if(mask != ch)
             {
                return false;
             }
         }
     }
    return true;
}

function ZeroPad(RoundValue,Places)
{
    var tempstr   = "";
    var decpos    = 0;
    var declength = 0;
    var padlength = 0;
    var i         = 0;

    tempstr = RoundValue.toString();
    decpos  = tempstr.indexOf(".");
    if(decpos == -1)
     {
        declength = 0;
        tempstr += Places > 0 ? "." : "";
     }
    else
     {
        declength = tempstr.length - decpos - 1;
     }
    padlength = Places - declength;
    if(padlength > 0)
     {
        for(i = 1; i <= padlength; i++)
            tempstr += "0";
     }
    return tempstr;
}
                                                