// ------------------------------------------------------------------------
// AUTHOR: Darren Shultz
// LAST MODIFIED: 03/01/06
// ------------------------------------------------------------------------

var flagDEBUG = 1;
var flagSetTestingValues = 1;

var bDisplayQuestionCombos = 1;

var titleWidth = 0;

var priceWidth = 0;

var bClear = 0;
var bFilled = 0;

// ------------------------------------------------------------------------
// SPECIAL COMBO ITEMS TO BE DISPLAYED AS SUB-ITEMS:

var items_included_special = new String();

// ------------------------------------------------------------------------
// SPECIFIC ITEMS TO BE DISPLAYED UNDER SPECIFIED QUESTION/ANSWER COMBINATIONS:

// EX. choices_1o2_2o2 -->  QUESTION #1: ANSWER #2
//                          QUESTION #2: ANSWER #2

/*  QUESTION/ANSWERS:

    QUESTION #1: # TVs
        1.  1
        2.  2
        3.  3
        4.  4
        
    QUESTION $2: Equipment
    
        1.  Standard Receiver
        2.  DVR
        3.  HD
        4.  Combo: HD & DVR
    
*/

// -----------------------------------------------------

var choices_1o1_2o1     = new String();
var choices_1o1_2o2     = new String();
var choices_1o1_2o3     = new String();
var choices_1o1_2o4     = new String();

var choices_1o2_2o1     = new String();
var choices_1o2_2o2     = new String();
var choices_1o2_2o3     = new String();
var choices_1o2_2o4     = new String();

var choices_1o3_2o1     = new String();
var choices_1o3_2o2     = new String();
var choices_1o3_2o3     = new String();
var choices_1o3_2o4     = new String();

var choices_1o4_2o1     = new String();
var choices_1o4_2o2     = new String();
var choices_1o4_2o3     = new String();
var choices_1o4_2o4     = new String();

// -----------------------------------------------------
// EXCEPTIONS: ITEMS TO REMOVE IF OTHER ITEMS PRESENT
//     FORMAT: "i1.i2.i3_r1.r2" (IF (i1 & i2 & i3) PRESENT THEN REMOVE (r1 & r2))
//    EXAMPLE: "pp10_m7" [pp10 = Total Choice Premier & m7 = DVR Service Fee]
//             IF pp10 IS PRESENT THEN REMOVE m7
//     LAYMAN: "DVR Service Fee waived with Total Choice Premier"

var exceptions = new Array  ();

// ------------------------------------------------------------------------
// CURRENT QUESTION/ANSWER SELECTIONS:

var curOption1 = 1;                 // QUESTION #1 SELECTED OPTION
var curOption2 = 1;                 // QUESTION #2 SELECTED OPTION
var curOption3 = new String();      // QUESTION #3 SELECTED OPTION
var curOption4 = new String();      // QUESTION #4 SELECTED OPTION (PROGRAMMING PACKAGE)

var lastOption4Set1 = new String(); // LAST (SET 1) PROGRAMMING PACKAGE SELECTED
var lastOption4Set2 = new String(); // LAST (SET 2) PROGRAMMING PACKAGE SELECTED

// ------------------------------------------------------------------------
// ITEM SECTION COUNTS:

var qCount;      // # OF QUESTION #3 CHOICES
var eCount;      // # OF EQUIPMENT ITEMS:        cost_e#
var aCount;      // # OF ACTIVATION ITEMS:       cost_a#
var mCount;      // # OF MONTHLY ITEMS:          cost_m#
var ppCount;     // # OF CHANNEL PACKAGE ITEMS:  cost_pp#
var pcCount;     // # OF PREMIUM CHANNEL ITMES:  cost_pc#
var spCount;     // # OF SPECIAL COMBOS:         cost_sp#

var ppSetDivide;

// ------------------------------------------------------------------------
// ITEM IDs:

var standardIDs = new Array();

var requiredFields = new Array();

var dependCheck = new Array();

// ------------------------------------------------------------------------
// SECURITY ACCESS

function grantAccess(bAccess,urlCode,accessPage)
{

    if (bAccess == 1)
    {
        if (accessPage.indexOf("?") == -1)
        {
            window.location.href = accessPage + "?accessCode=" + urlCode;
        }
        else
        {
            window.location.href = accessPage + "&accessCode=" + urlCode;
        }
    }
    else
    {
        window.location.href = "accessDenied.php?urlPage=" + accessPage;
    }
    
}

function giveAccess(bAccess,urlCode,accessPage)
{

    if (bAccess != 1)
    {
        window.location.href = "accessDenied.php?urlPage=" + accessPage;
    }
    
}

// ------------------------------------------------------------------------
// ITEM LOADING AND INITIALIZATION:

function init()
{
    
    setValidation();
    
    initConfiguration();
    initCounts();
    initIDArrays();
    
    if (bDisplayQuestionCombos == 1)
    {
    	initQ3Choices();
    }

    loadItems();
    
    hideAll();
    
    if (bDisplayQuestionCombos == 1)
    {
    	hideOptions();      
    }
      
    setDefaults();

    performTesting();
    
    eval(frmShop.initFunction.value);
    
    calculateTotals();
/*    
    ccNameSelect(frmShop.order_payment_credit_card_name.value);
*/
    validateForm(false);
    
}

function initConfiguration()
{

    titleWidth = parseInt(frmShop.titleWidth.value);
    priceWidth = parseInt(frmShop.priceWidth.value);

    flagDEBUG = frmShop.flagDEBUG.value;
    flagSetTestingValues = frmShop.flagSetTestingValues.value;
    bDisplayQuestionCombos = frmShop.bDisplayQuestionCombos.value;
    
}

function setValidation()
{
    
    setValidationFields();

}

function setValidationFields()
{

/*
    var sCCNum = "";
    var obj = frmShop.order_payment_credit_card_name; 
    
    if (obj.value == "American Express")
    {
        sCCNum = "!Payment Information: Credit Card Number.order_payment_credit_card_number+cc_1_num_0|cc_1_num_1|cc_1_num_2#4,6,5";
    }
    else
    {
    	sCCNum = "!Payment Information: Credit Card Number.order_payment_credit_card_number+cc_0_num_0|cc_0_num_1|cc_0_num_2|cc_0_num_3#4,4,4,4";
    }
*/    
    requiredFields = new Array (
    
        "Contact Details: First Name.order_contact_first_name",
        "Contact Details: Last Name.order_contact_last_name",
        "!Contact Details: Phone (Installation Address).order_contact_phone_installation+phone_0_0|phone_0_1|phone_0_2#3,3,4",
        "!Contact Details: Phone (Secondary).order_contact_phone_secondary+phone_1_0|phone_1_1|phone_1_2#3,3,4",
        "@Contact Details: Email.order_contact_email",

        "Installation Address: Street.order_install_address_street",
        "Installation Address: City.order_install_address_city",
        "Installation Address: State.order_install_address_state",
        "#Installation Address: Zip Code.order_install_address_zip",
        "Installation Address: Residence Type.order_install_address_type",
        "Installation Address: Ownership.order_residence",
        "~Terms and Conditions: Agree.agreement0"
		
/*		
		,

        "Billing Address: Street.order_billing_address_street",
        "Billing Address: City.order_billing_address_city",
        "Billing Address: State.order_billing_address_state",
        "#Billing Address: Zip Code.order_billing_address_zip",

        "Payment Information: Credit Card Type.order_payment_credit_card_type",
        "Payment Information: Credit Card Name.order_payment_credit_card_name",
        
        "Payment Information: Name on Credit Card.order_payment_name_on_card",
        
        sCCNum,
        
        "!Payment Information: Credit Card Expiration Month.order_payment_expiration_date+order_payment_expiration_month#2",
        "!Payment Information: Credit Card Expiration Year.order_payment_expiration_date+order_payment_expiration_year#4",

        "$Payment Information: Merchant Code.order_payment_verification_code",
        
        "*Advertisement Phone Number.order_ad_phone+ad_phone_0|ad_phone_1|ad_phone_2#3,3,4",

        "!Social Security Number.order_ssn+ssn_0|ssn_1|ssn_2#3,2,4"
*/
        );
        
    /*
   	if (frmShop.serviceID.value == 1)
   	{
   		requiredFields.push("^Equipment: Active Land-Line Phone Connection.active_phone");
   	}
   	*/
    
}

function initCounts()
{

    qCount = frmShop.qCount.value;      // # OF QUESTION #3 CHOICES
    eCount = frmShop.eCount.value;      // # OF EQUIPMENT ITEMS:        cost_e#
    aCount = frmShop.aCount.value;      // # OF ACTIVATION ITEMS:       cost_a#
    mCount = frmShop.mCount.value;      // # OF MONTHLY ITEMS:          cost_m#
    ppCount = frmShop.ppCount.value;    // # OF CHANNEL PACKAGE ITEMS:  cost_pp#
    pcCount = frmShop.pcCount.value;    // # OF PREMIUM CHANNEL ITMES:  cost_pc#
    spCount = frmShop.spCount.value;    // # OF SPECIAL COMBOS:         cost_sp#
    
    ppSetDivide = frmShop.ppSetDivide.value;
    
}

function initIDArrays()
{

    standardIDs = frmShop.standardItems.value.split(".");

    qIDs  = frmShop.qIDs.value.split(".");      
    eIDs  = frmShop.eIDs.value.split(".");
    aIDs  = frmShop.aIDs.value.split(".");
    mIDs  = frmShop.mIDs.value.split(".");
    ppIDs = frmShop.ppIDs.value.split(".");
    pcIDs = frmShop.pcIDs.value.split(".");
    spIDs = frmShop.spIDs.value.split(".");
    dependCheck  = frmShop.dependItems.value.split("_");

}

function initQ3Choices()
{

    var iQ1 = 1;
    var iQ2 = 1;
    var cName = new String();
    
    for(iQ1 = 1; iQ1 <= 4; iQ1++)
    {
        for(iQ2 = 1; iQ2 <= 4; iQ2++)
        {
            cName = "choices_1o" + (iQ1) + "_2o" + (iQ2);
            eval (cName + " = frmShop." + cName + ".value");
        }
    }

}

function setDefaults()
{
    
    curOption1 = 1;
    curOption2 = 1;
    curOption3 = qIDs[0];
    curOption4 = ppIDs[0];
    
    lastOption4Set1 = ppIDs[0];
    lastOption4Set2 = ppSetDivide;
    
    frmShop.q1_op[curOption1 - 1].checked = true;
    
    if (bDisplayQuestionCombos == 1)
    {
    
    	frmShop.q2_op[curOption2 - 1].checked = true;

	    hideOptions();
	    showOptions();
    
    }
    
    //frmShop.install_time0.checked = true;

    eval("frmShop.q4_op_" + curOption4 + ".checked = true");
    show(q4_set1);
    if (ppSetDivide != "")
    {
        hide(q4_set2);    
    }
    
    update();
    
}

function performTesting()
{

    if (flagSetTestingValues == 1)
    {
        setTestingValues();
    }
    
}

function setTestingValues()
{

    frmShop.order_residence.value = 'Own';
    frmShop.order_install_address_type.value = 'House';
    
    frmShop.install_time3.checked = true;
    frmShop.install_time13.checked = true;
    frmShop.install_time14.checked = true;

    frmShop.order_installation_instructions.value = "Yo Ryan!\nThis is just a buncha test data that's gett'n crammed in for now...\n\nWe'll remove it later, obviously!";

    frmShop.order_contact_first_name.value = "Herman";
    frmShop.order_contact_last_name.value = "Munster";
    
    //frmShop.order_contact_phone_installation.value = "512-656-8786";
    
    frmShop.phone_0_0.value = "313";
    frmShop.phone_0_1.value = "131";
    frmShop.phone_0_2.value = "1313";
    
    //frmShop.order_contact_phone_secondary.value = "512-555-5555";
    
    frmShop.phone_1_0.value = "313";
    frmShop.phone_1_1.value = "131";
    frmShop.phone_1_2.value = "1313";
    
    frmShop.order_contact_email.value = "hmunster@yahoo.com";

    frmShop.order_install_address_street.value = "1313 Mockingbird Ln";
    frmShop.order_install_address_city.value = "Mockingbird Heights";
    frmShop.order_install_address_state.value = "IN";
    frmShop.order_install_address_zip.value = "13131";

    frmShop.agreement0.checked = true;
    
    update();
    
    validateForm(false);
      
}

// ------------------------------------------------------------------------
// QUESTION/ANSWER SELECTION HANDLERS:

function q1_select(op)
{

    curOption1 = op;
    
    update();
    
}

function q2_select(op)
{

    curOption2 = op;
    
    if (ppSetDivide != "")
    {
        if (op == 1 || op == 2)
        {
            hide(q4_set2);
            show(q4_set1);
            lastOption4Set2 = curOption4;
            curOption4 = lastOption4Set1;
        }
        else
        {
            hide(q4_set1);
            show(q4_set2);
            lastOption4Set1 = curOption4;
            curOption4 = lastOption4Set2;
        }
    }
    
    eval("frmShop.q4_op_" + curOption4 + ".checked = true");
    
    update();
    
}

function q3_select(op)
{

    curOption3 = op;
    
    update();
    
}

function q4_select(op)
{
    
    if (curOption4 == "pp0")
  	{
      	document.getElementById("q5_op_pc1").checked = document.getElementById("q5_op_pc1").title;
      	document.getElementById("q5_op_pc2").checked = document.getElementById("q5_op_pc2").title;
      	document.getElementById("q5_op_pc3").checked = document.getElementById("q5_op_pc3").title;
      	document.getElementById("q5_op_pc4").checked = document.getElementById("q5_op_pc4").title;
  	}
  	
  	if (curOption4 == "pp30")
  	{
  	    document.getElementById("q5_op_pc3").checked = document.getElementById("q5_op_pc3").title;
    }
    
    if (curOption4 == "pp31")
  	{
  	    document.getElementById("q5_op_pc4").checked = document.getElementById("q5_op_pc4").title;
    }
    
    if (curOption4 == "pp40")
  	{
  	    document.getElementById("q5_op_pc3").checked = document.getElementById("q5_op_pc3").title;
  	    document.getElementById("q5_op_pc4").checked = document.getElementById("q5_op_pc4").title;
    }
    
    curOption4 = op;
    
    if (curOption4 == "pp0")
  	{
      	document.getElementById("q5_op_pc1").checked = "checked";
      	document.getElementById("q5_op_pc2").checked = "checked";
      	document.getElementById("q5_op_pc3").checked = "checked";
      	document.getElementById("q5_op_pc4").checked = "checked";
  	}
  	
  	if (curOption4 == "pp30")
  	{
  	    document.getElementById("q5_op_pc3").checked = "checked";
    }
    
    if (curOption4 == "pp31")
  	{
  	    document.getElementById("q5_op_pc4").checked = "checked";
    }
    
    if (curOption4 == "pp40")
  	{
  	    document.getElementById("q5_op_pc3").checked = "checked";
  	    document.getElementById("q5_op_pc4").checked = "checked";
    }

    update();
    
}

function q5_select(op)
{

	if (document.getElementById("serviceID").value == 2)
	{
	
		if (curOption4 == "pp0" && op != "pc5")
		{
		
		  document.getElementById("q5_op_" + op).checked = "checked";
      
		}
		else
		{
			document.getElementById("q5_op_" + op).title = document.getElementById("q5_op_" + op).checked;
		}
		
		if (curOption4 == "pp30" && op == "pc3")
		{
			document.getElementById("q5_op_" + op).checked = "checked";
		}
		else
		{
			document.getElementById("q5_op_" + op).title = document.getElementById("q5_op_" + op).checked;
		}
		
		if (curOption4 == "pp31" && op == "pc4")
		{
			document.getElementById("q5_op_" + op).checked = "checked";
		}
		else
		{
			document.getElementById("q5_op_" + op).title = document.getElementById("q5_op_" + op).checked;
		}
		
		if (curOption4 == "pp40" && (op == "pc3" || op == "pc4") )
		{
			document.getElementById("q5_op_" + op).checked = "checked";
		}
		else
		{
			document.getElementById("q5_op_" + op).title = document.getElementById("q5_op_" + op).checked;
		}
		
	}

  update();
    
}

function time_select(num)
{
    // NOTHING
}


// ------------------------------------------------------------------------
// QUESTION/ANSWER AND TOTALS: UPDATING

function update()
{

	if (bDisplayQuestionCombos == 1)
    {
    	hideOptions();
    	showOptions();
    }
    
    if (document.getElementById("serviceID").value == 2)
	{
		if (curOption4 == "pp0")
	    {
	    	document.getElementById("q5_op_pc1").checked = "checked";
	    	document.getElementById("q5_op_pc2").checked = "checked";
	    	document.getElementById("q5_op_pc3").checked = "checked";
	    	document.getElementById("q5_op_pc4").checked = "checked";
	    }
	}
	
    displayItems();
    
    calculateTotals();
    
    //validateForm(false);
    
}

// ------------------------------------------------------------------------
// ITEM DISPLAY FUNCTIONS: SHOW

function showOptions()
{

    var i = 0;
    var qNum = new String();
    var qFirst = new String();
    var sChoice = eval("choices_1o" + parseInt(curOption1) + "_2o" + parseInt(curOption2));
    var aChoice = sChoice.split(".");
    
    var bOption = 0;
    
    for (i in aChoice)
    {
        show(eval("q3_" + aChoice[i]));
        qNum = aChoice[i];
        if (qFirst == "")
        {
            qFirst = qNum;
        }
        if (curOption3 == qNum)
        {
            bOption = 1;
        }
    }
    
    if (bOption == 0)
    {
        curOption3 = qFirst; 
    }

    eval("frmShop.q3_op_" + curOption3 + ".checked = true");
    
}

function displayItems()
{

    hideAll();
    
    displayStandardItems();
    
    eval(frmShop.equipDisplayFunction.value);
    
    displayProgrammingPackages();
    displayPremiumChannels();
    
    displayDependItems();
    
    displaySpecialCombos();
    
    //ccNameSelect(frmShop.order_payment_credit_card_name.value,"frmShop");
    
}

function displaySectionedFields()
{

    // TODO: viewOrder.php
    // called from viewOrder.php
    // split field by period(.)
    
    // display correct cc fields (based on cc name)
    
    
    var sPhone0 = new String();
    var sPhone1 = new String();
    
    sPhone0 = frmShop.order_contact_phone_installation.value;
    sPhone1 = frmShop.order_contact_phone_secondary.value;
    
    var aPhone0 = new Array();
    var aPhone1 = new Array();
   
    
    aPhone0 = new Array(sPhone0.substr(0,3),sPhone0.substr(3,3),sPhone0.substr(6,4));
    //phone_0_0;phone_0_1;phone_0_2;
    frmShop.phone_0_0.value = aPhone0[0];
    frmShop.phone_0_1.value = aPhone0[1];
    frmShop.phone_0_2.value = aPhone0[2];
    
    aPhone1 = new Array(sPhone1.substr(0,3),sPhone1.substr(3,3),sPhone1.substr(6,4));
    //phone_1_0;phone_1_1;phone_1_2;
    frmShop.phone_1_0.value = aPhone1[0];
    frmShop.phone_1_1.value = aPhone1[1];
    frmShop.phone_1_2.value = aPhone1[2];

}

function displayStandardItems()
{

    // STANDARD ITEMS: FOR ALL QUESTION/ANSWER COMBINATIONS:

    for (i in standardIDs)
    {
        show(eval("cost_" + standardIDs[i]));
    }
    
}

function displayEquipmentItems()
{

    // EQUIPMENT ITEMS: FOR EACH QUESTION/ANSWER COMBINATION:
    //  INCLUDE ACTUAL EQUIPMENT & MONTHLY RENTAL FEES

    var sItems = new String();
    sItems = eval("frmShop." + curOption3 + ".value");
    var iItems = sItems.split(".");
    for (i in iItems)
    {
        show(eval("cost_" + iItems[i]));
    }
    
}

function displayProgrammingPackages()
{

    // CHANNEL PACKAGES: 

    if (curOption4 != "")
    {
        show(eval("cost_" + curOption4));
    }
    
}

function displayPremiumChannels()
{

    // PREMIUM CHANNELS: IF LISTED AS FREE (0.0), THEN IT IS PROMOTIONAL: AUTOMATICALLY INCLUDE IT
    
    var iPremiumCount = 0;
    var i = 0;

    for (i in pcIDs)
    {
        if ( eval("frmShop.q5_op_" + pcIDs[i] + ".checked") || eval("parseFloat(frmShop."  + pcIDs[i] +  ".value) == 0.0"))
        {
            eval("frmShop.q5_op_"  + pcIDs[i] +  ".checked = true;");
            iPremiumCount++;
            show(eval("cost_"  + pcIDs[i]));
        }
    }
    
    if (iPremiumCount > 0)
    {
        show(eval("cost_ptotal"));
    }

}

function displaySpecialCombos()
{

    // SPECIAL COMBINATION PACKAGES:

    var ci = 0;

    var incDiv = "";
    var sIncluded = new String("");
    
    for( ci in spIDs)
    {

        var i = 0;
        var j = 0;
        var bApply = 0;
        var oItem;
        var iCount = 0;
        var aCount = 0;

        var aItemsIncluded = new Array();
        
        var iItemCount = eval("frmShop." + spIDs[ci] + "_items.value");
        var sCombosString = eval("frmShop." + spIDs[ci] + "_items.title");
        
        var aCombos = new Array();
        aCombos = sCombosString.split("_");
        
        var aItems = new Array();
        var aItem;

        i = 0;
        aCount = aCombos.length;
        bApply = 0;
        while (bApply == 0 && i < aCount)
        {
            aItems = aCombos[i].split(".");
            j = 0;
            iCount = aItems.length;
            bApply = 1;
            while (bApply == 1 && j < iCount)
            {
                aItem = aItems[j];
                if (aItem.charAt(0) == '*')
                {
                    aItem = aItem.substr(1,aItem.length-1);                    
                }
                oItem = eval("cost_" + aItem);
                if (oItem.style.display == 'none')
                {
                    bApply = 0;
                }
                j++;
            }
            i++;
        }
        
        if (bApply == 0)
        {
            // NO SET OF ITEM COMBINATIONS WERE SATISFIED
            hide(eval("cost_" + spIDs[ci]));
        }
        else
        {
            // COMBINATION ITEMS WERE FOUND: aItems
            // REMOVE THEM AND DISPLAY THE COMBO PACKAGE
            i = 1;
            var sItem = new String();
            for(iItem in aItems)
            {
                sItem = aItems[iItem];
                
                if (sItem.charAt(0) != '*')
                {
                    sIncluded = sIncluded + incDiv + sItem;
                    incDiv = ".";
                    oItem = eval("cost_" + sItem);
                    hide(oItem);
                    oItem = eval(spIDs[ci] + "_item" + parseInt(i));
                    setDIVText(oItem,eval("frmShop." + sItem + ".title"));
                    i++;
                }
            }
            show(eval("cost_" + spIDs[ci]));
        }
    }
    
    var serviceID = document.getElementById("serviceID").value;
    if ((serviceID == 1 || serviceID ==2) && curOption4 == "pp0")
    {
    	sIncluded = sIncluded + incDiv + "pc1.pc2.pc3.pc4";
    }

    items_included_special = sIncluded;
    
    // CHECK FOR ANY PREMIUM CHANNELS NOT IN A SPECIAL COMBO
    // REMOVE "PREMIUM CHANNEL MOVIES" TOTAL IF NONE PRESENT
    
    i = 0;
    var item = new String();
    var obj = new String();
    var bShow = 0;
    
    for(i in pcIDs)
    {
        obj = "cost_" + pcIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            bShow = 1;
        }               
    }
    
    if (bShow == 0)
    {
        hide(cost_ptotal);
    }
    
}

function displayDependItems()
{

	var di = 0;
	
	for( di in dependCheck)
    {
    
		var str = dependCheck[di];
		
		if (str.indexOf("-") != -1 && str.indexOf("+") != -1)
		{
		
			var split1 = str.split("-");		
			var split2 = split1[1].split("+");
	
			var left  = split1[0];
			var mid   = split2[0];
			var right = split2[1];
			
			var cond = left.split(".");
			var remove = mid.split(".");
			var insert = right.split(".");
			
			var bCond = 1;
			var i = 0;
			
			for( i in cond)
			{
				obj = "cost_" + cond[i];
		        obj = eval(obj + ".style.display");
		        if (obj != "inline")
		        {
		            bCond = 0;
		        }               
			}		
			
			if (bCond == 1)
			{
				for( i in remove)
				{
					if (remove[i] != "")
		        	{
						obj = "cost_" + remove[i];
			        	obj = eval(obj);
		        		hide(obj);
		        	}
				}
				for( i in insert)
				{
					if (insert[i] != "")
		        	{
						obj = "cost_" + insert[i];
			        	obj = eval(obj);
		        		show(obj);
		        	}
				}
			}
			
		}

    }

    
    for(i in pcIDs)
    {
        obj = "cost_" + pcIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            bShow = 1;
        }               
    }

    
}

// ------------------------------------------------------------------------
// ITEM DISPLAY FUNCTIONS: HIDE

function hideAll()
{

    // HIDE ALL ITEMS IN THE TOTALS DISPLAY:

    hideEquipment();
    hideActivation();
    hideMonthly();
    hideProgrammingPackages();
    hidePremiumChannels();
    hideSpecialCombos();
    hideCustom();
    
}

function hideEquipment()
{

    var i = 0;
    
    for(i in eIDs)
    {
        hide(eval("cost_" + eIDs[i]));
    }
    
}

function hideActivation()
{

    var i = 0;
    
    for(i in aIDs)
    {
        hide(eval("cost_" + aIDs[i]));
    }
    
}

function hideMonthly()
{

    var i = 0;
    
    for(i in mIDs)
    {
        hide(eval("cost_" + mIDs[i]));
    }
    
}

function hideProgrammingPackages()
{

    var i = 0;
    
    for(i in ppIDs)
    {
        hide(eval("cost_" + ppIDs[i]));
    }
    
}

function hidePremiumChannels()
{

    var i = 0;
    
    for(i in pcIDs)
    {
        hide(eval("cost_" + pcIDs[i]));
    }
    
    hide(cost_ptotal);
    
}

function hideSpecialCombos()
{

    var i = 0;
    
    for(i in spIDs)
    {
        hide(eval("cost_" + spIDs[i]));
    }
    
}

function hideOptions()
{

    // HIDE ALL QUESTION #3 ANSWER SETS:
    
    var i = 0;
    
    for(i in qIDs)
    {
        hide(eval("q3_" + qIDs[i]));
    }
    
}

// ------------------------------------------------------------------------
// LOAD ITEM NAMES/PRICES:

function loadItems()
{

	if (bDisplayQuestionCombos == 1)
    {
    	loadQuestion3Options();
    }

    loadEquipmentItems();
    loadActivationItems();
    loadMonthlyItems();
    
    loadPremiumChannelItems();
    loadProgrammingPackageItems();
    
    loadSpecialComboItems();
    
}

function loadQuestion3Options()
{

    var i = 0;
    
    for(i in qIDs)
    {
        setDIVText(eval("q3_" + qIDs[i] + "_text"),eval("frmShop." + qIDs[i] + ".title"));
    }
    
}

function loadEquipmentItems()
{

    // EQUIPMENT ITEMS:
    
    var i = 0;
    var txt = new String();
    var pr = 0.0;
    for(i in eIDs)
    {
        txt = eval("frmShop." + eIDs[i] + ".title");
        setDIVText(eval("item_" + eIDs[i]),txt);
        pr = eval("parseFloat(frmShop." + eIDs[i] + ".value)");
        setDIVText(eval("price_" + eIDs[i]),fixPrice(pr));
    }
    
}

function loadActivationItems()
{

    // ACTIVATION ITEMS:

    var i = 0;
    var txt = new String();
    var pr = 0.0;
    for(i in aIDs)
    {
        txt = eval("frmShop." + aIDs[i] + ".title");
        setDIVText(eval("item_" + aIDs[i]),txt);
        pr = eval("parseFloat(frmShop." + aIDs[i] + ".value)");
        setDIVText(eval("price_" + aIDs[i]),fixPrice(pr));
    }
    
}

function loadMonthlyItems()
{

    // MONTHLY ITEMS:

    var i = 0;
    var txt = new String();
    var pr = 0.0;
    for(i in mIDs)
    {
        txt = eval("frmShop." + mIDs[i] + ".title");
        setDIVText(eval("item_" + mIDs[i]),txt);
        pr = eval("parseFloat(frmShop." + mIDs[i] + ".value)");
        setDIVText(eval("price_" + mIDs[i]),fixPrice(pr));
    }
    
}

function loadPremiumChannelItems()
{

    // PREMIUM CHANNEL ITEMS:

    var i = 0;
    var txt = new String();
    var pr = 0.0;
    for(i in pcIDs)
    {
        txt = eval("frmShop." + pcIDs[i] + ".title");
        setDIVText(eval("item_" + pcIDs[i]),txt);
        setDIVText(eval("item_sel_" + pcIDs[i]),txt);
        pr = eval("parseFloat(frmShop." + pcIDs[i] + ".value)");
        setDIVText(eval("price_" + pcIDs[i]),fixPrice(pr));
    }
    
}

function loadProgrammingPackageItems()
{

    // CHANNEL PACKAGE ITEMS:

    var i = 0;
    var txt = new String();
    var strim = new String();
    var pr = 0.0;
    var arr = new Array();
    for(i in ppIDs)
    {
        txt = eval("frmShop." + ppIDs[i] + ".title");
        arr = txt.split("*");
        strim = arr[0]; 
        txt = arr[0] + arr[1];
        setDIVText(eval("item_" + ppIDs[i]),txt);
        setDIVText(eval("item_sel_" + ppIDs[i]),strim);
        pr = eval("parseFloat(frmShop." + ppIDs[i] + ".value)");
        setDIVText(eval("price_" + ppIDs[i]),fixPrice(pr));
    }
    
}

function loadSpecialComboItems()
{

    // SEPCIAL COMBO ITEMS:

    var i = 0;
    var txt = new String();
    var pr = 0.0;
    for(i in spIDs)
    {
        txt = eval("frmShop." + spIDs[i] + ".title");
        setDIVText(eval("item_" + spIDs[i]),txt);
        pr = eval("parseFloat(frmShop." + spIDs[i] + ".value)");
        setDIVText(eval("price_" + spIDs[i]),fixPrice(pr));
    }
    
}

// ------------------------------------------------------------------------
// TOTALS CALCULATIONS:

function calculateTotals()
{

    calculateEquipment();
    calculateActivation();
    calculateMonthly();
    
}

function calculateEquipment()
{

    var i = 0;
    var tot = 0.0;
    var item = new String();
    var obj = new String();
    
    for(i in eIDs)
    {
        obj = "cost_" + eIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            eval("tot = tot + parseFloat(frmShop." + eIDs[i] + ".value);");            
        }
    }

    setDIVText(price_etotal,fixPrice(tot));

}

function calculateActivation()
{

    var i = 0;
    var tot = 0.0;
    var item = new String();
    var obj = new String();
    
    for(i in aIDs)
    {
        obj = "cost_" + aIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            eval("tot = tot + parseFloat(frmShop." + aIDs[i] + ".value);");            
        }
    }
    
    setDIVText(price_atotal,fixPrice(tot));

}

function calculateMonthly()
{

    var i = 0;
    var tot = 0.0;
    var obj = new String();
    var tot_pc = 0.0;
    var tot_pp = 0.0;
    var tot_sp = 0.0;
    
    tot_pc = calculatePremiumChannels();
    tot_pp = calculateProgrammingPackages();
    tot_sp = calculateSpecialCombos();
    
    tot = tot_pc + tot_pp + tot_sp;
    
    for(i in mIDs)
    {
        obj = "cost_" + mIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            eval("tot = tot + parseFloat(frmShop." + mIDs[i] + ".value);");            
        }        
    }
    
    setDIVText(price_mtotal,fixPrice(tot));

}

function calculatePremiumChannels()
{

    var i = 0;
    var tot = 0.0;
    var item = new String();
    var obj = new String();
    
    for(i in pcIDs)
    {
        obj = "cost_" + pcIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            eval("tot = tot + parseFloat(frmShop." + pcIDs[i] + ".value);");            
        }               
    }
    
    setDIVText(price_ptotal,fixPrice(tot));
    
    return tot;

}

function calculateProgrammingPackages()
{

    var i = 0;
    var tot = 0.0;
    var item = new String();
    var obj = new String();
    
    for(i in ppIDs)
    {
        obj = "cost_" + ppIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            eval("tot = tot + parseFloat(frmShop." + ppIDs[i] + ".value);");            
        }               
    }
    
    return tot;

}

function calculateSpecialCombos()
{

    var i = 0;
    var tot = 0.0;
    var item = new String();
    var obj = new String();
    
    for(i in spIDs)
    {
        obj = "cost_" + spIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            eval("tot = tot + parseFloat(frmShop." + spIDs[i] + ".value);");            
        }               
    }
    
    return tot;

}

// ------------------------------------------------------------------------
// ORDER FORM SUBMISSION: UPDATE

function doArchive(aCode)
{
    window.location.href="viewOrder.php?accessCode=" + aCode + "&orderID=" + frmShop.orderID.value + "&archiveOrder=1";
}

function doActivate(aCode)
{
    window.location.href="viewOrder.php?accessCode=" + aCode + "&orderID=" + frmShop.orderID.value + "&activateOrder=1";
}

function doUpdate(aCode, bValidate)
{

    frmShop.method = "POST";
    
    frmShop.order_contact_phone_installation.value = frmShop.phone_0_0.value + "." + frmShop.phone_0_1.value + "." + frmShop.phone_0_2.value;
    frmShop.order_contact_phone_secondary.value = frmShop.phone_1_0.value + "." + frmShop.phone_1_1.value + "." + frmShop.phone_1_2.value;
    
    
    if (frmShop.orderCurrentStatus.value == frmShop.order_header_status.value)
    {
        frmShop.action = "viewOrder.php?accessCode=" + aCode + "&updateOrder=1&orderID=" + frmShop.orderID.value;
    }
    else
    {
        frmShop.action = "viewOrder.php?accessCode=" + aCode + "&updateOrder=1&orderID=" + frmShop.orderID.value + "&statusChange=" + frmShop.order_header_status.value + "&contactEmail=" + frmShop.order_contact_email.value;
    }
    
    if (bValidate)
    {
        if(validateForm(true))
        {
            frmShop.submit();
        }
    }
    else
    {
        frmShop.submit();
    }
    
}

function doStatusUpdate(aCode)
{
    window.location.href="viewOrder.php?accessCode=" + aCode + "&orderID=" + frmShop.orderID.value + "&statusChange=" + frmShop.order_header_status.value + "&contactEmail=" + frmShop.order_contact_email.value;
}

// ------------------------------------------------------------------------
// ORDER FORM SUBMISSION: INSERT

function doSubmit()
{

    // FILL IN THE HIDDEN FIELDS WITH DATA FROM USER-INPUT
    // TO BE INSERTED INTO THE DATABASE
    
    if (validateForm(true))
    {
        
        fillSectionedFields();
        
        fillCostDisplay();
        
        fillSummary();
        
        fillInstallationTimes();
        
        // alert("Order Submission: Under Development.");

        frmShop.method = "POST";
        frmShop.action = "orderSubmit.php";
        frmShop.submit();
        
    }

}

function setClear(val)
{
	
    bClear = val;
    
    if(val == 1)
    {
    	hideUserInputs();
    	//validateForm();
    }
    
}

function hideUserInputs()
{
	var obj;
	var s = new String();
	for(i in frmShop.elements)
	{
		obj = frmShop.elements[i];
		if(obj && obj.type == "text" && obj.name.indexOf("_DB") != -1 && obj.name.indexOf("expiration") == -1)
		{
			s = obj.name.substr(0,obj.name.length - 3);
			hide(eval(s + "_USER"));
			hide(eval(s + "_INPUT"));
		}
	}
	hide(eval("order_payment_expiration_date_USER"));
	hide(eval("order_payment_expiration_date_INPUT"));
}

function validateForm(bAlert)
{

    setValidation();

    var bSubmit = 1;  
    var sFailed = new String();
    var sDiv = "";
    var i = 0;
    var split = new Array();
    var displayText = new String();
    var fieldID = new String();
    var obj;
    var iText = new String();
    
    var bSection = 0;
    var bRequired = 1;
    var bFilled = 0;
    var sSplit = new String();
    
    var sFields = new Array();
    var sRight = new Array();
    var sCounts = new Array();
    
    // Validate preferred installation times (MUST CHOOSE ONE OR MORE)
    
    if (frmShop.formValid.value == 1)
    {
    	
    	//TODO: orderForm.php INSTALLATION TIMES VALIDATION (0-14)
    	
    	var bValid = 0;
    	var obj;
    	var i = 0;
    	
    	for (i = 0;i <= 14;i++)
    	{
    		obj = eval("frmShop.install_time" + i);
    		if (obj.checked)
    		{
    				bValid = 1;
    		}
    		
    	}
    	if (bValid == 0)
    	{
    		bSubmit = 0;
            sFailed = sFailed + sDiv + "Preferred Installation Times";
            sDiv = "\n";
    	}
    	
    }
    
    for(i in requiredFields)
    {
    
        split = requiredFields[i].split(".");
        sSplit = split[0];
        
        bRequired = 1;

        if  ( sSplit.charAt(0) == "#" )
        {
            
            // ZIP CODE: len = 5 or 9 (all numeric)
            // #####
            // ##### ####
            // #####-####
            // #########
            
            sSplit = sSplit.substr(1);
            sZipField = split[1];
            
            obj = eval("frmShop." + sZipField);
            iText = trimZip(obj.value);
            setInputStyle(obj,"#FFFFFF");
            if (iText == "" || ( !checkNumericField(iText,5) && !checkNumericField(iText,9) ))
            {
                setInputStyle(obj,"#FFFFe2");
                if (bClear == 1)
                { 
                    obj.value = ""; 
                    show(eval(obj.name + "_USER"));
                    show(eval(obj.name + "_INPUT"));
                }                            
                bSubmit = 0;
                sFailed = sFailed + sDiv + sSplit;
                sDiv = "\n";
            }
            else
            {
                setInputStyle(obj,"#FFFFFF");
            }

        }
        else if  ( sSplit.charAt(0) == "~" )
        {
        
            sSplit = sSplit.substr(1);
            // Checkbox
            sCheckboxField = split[1];
            
            obj = eval("frmShop." + sCheckboxField);
            iText = trimAll(obj.value);
            setInputStyle(obj,"#FFFFFF");

			if (!obj.checked)
            {
                setInputStyle(obj,"#FFFFe2");
                if (bClear == 1)
                { 
                    obj.value = ""; 
                    show(eval(obj.name + "_USER"));
                    show(eval(obj.name + "_INPUT"));
                }                            
                bSubmit = 0;
                bSection = 0;
                j = -1;
                sFailed = sFailed + sDiv + sSplit;
                sDiv = "\n";
            }
            else
            {
                setInputStyle(obj,"#FFFFFF");
            }
            
        }
        else if  ( sSplit.charAt(0) == "@" )
        {
        
            // EMAIL ADDRESS:
            // SSSS@SSSS
            
            var atLocation = -1;
            
            sSplit = sSplit.substr(1);
            sEmailField = split[1];
            
            obj = eval("frmShop." + sEmailField);
            iText = trimAll(obj.value);
            setInputStyle(obj,"#FFFFFF");
            atLocation = iText.indexOf("@");
            //  @ must be present   @ can't be first   @ can't be at the end
            if (atLocation == -1 || atLocation == 0 || atLocation == iText.length - 1)
            {
                setInputStyle(obj,"#FFFFe2");
                if (bClear == 1)
                { 
                    obj.value = ""; 
                    show(eval(obj.name + "_USER"));
                    show(eval(obj.name + "_INPUT"));
                }                            
                bSubmit = 0;
                bSection = 0;
                j = -1;
                sFailed = sFailed + sDiv + sSplit;
                sDiv = "\n";
            }
            else
            {
                setInputStyle(obj,"#FFFFFF");
            }
            
        }
        else if (sSplit.charAt(0) == "*")
        {
        
        	sDBSplit = split[1].split("+");
            
            sDB = sDBSplit[0];

            sRight = sDBSplit[1].split("#");
           
            sFields = sRight[0].split("|");
            sCounts = sRight[1].split(",");
            
            sSplit = sSplit.substr(1);
            
            var bFail = 0;
            var bBlank = 0;
            var bFill = 0;
            
            for (j = 0; j < sFields.length; j++)
            {

                obj = eval("frmShop." + sFields[j]);
                iText = trimAll(obj.value);
                setInputStyle(obj,"#FFFFFF");
                
                if(iText == "")
                {
                	bBlank = 1;
                }
                else
                {
                	bFill = 1;
                }


                if ((bFail == 0 && bBlank == 1 && bFill == 1) || (bFail == 0 && iText != "" && !checkNumericField(iText,sCounts[j])))
                {
                    setInputStyle(obj,"#FFFFe2");
                    if (bClear == 1)
                    { 
                        obj.value = ""; 
                    }                            
                    bSubmit = 0;
                    bSection = 0;
                    j = -1;
                    sFailed = sFailed + sDiv + sSplit;
                    sDiv = "\n";
                    bFail = 1;
                }
                else if (bFail == 1)
                {
                	setInputStyle(obj,"#FFFFe2");
                	//obj.value = "";
                }
                else
                {
                    setInputStyle(obj,"#FFFFFF");
                }
                
            }
            
            if (bSection == 0 && bClear == 1)
            {
            	show(eval(sDB + "_USER"));
                show(eval(sDB + "_INPUT"));
            }
            
        }
        else if (sSplit.charAt(0) == "!")
        {
        
            // SPECIAL VALIDATION (NUMERIC AND LENGTH)
            
            sDBSplit = split[1].split("+");
            
            sDB = sDBSplit[0];

            sRight = sDBSplit[1].split("#");
           
            sFields = sRight[0].split("|");
            sCounts = sRight[1].split(",");
            
            sSplit = sSplit.substr(1);
            
            if (bRequired == 1)
            {
            
                bSection = 1
                
                for (j = 0; j < sFields.length; j++)
                {
                
                    obj = eval("frmShop." + sFields[j]);
                    iText = trimAll(obj.value);

                    if (bSection == 1)
                    {

                        if (iText == "" || !checkNumericField(iText,sCounts[j]) )
                        {
                            setInputStyle(obj,"#FFFFe2");
                            if (bClear == 1)
                            { 
                                obj.value = ""; 
                            }                            
                            bSubmit = 0;
                            bSection = 0;
                            j = -1;
                            sFailed = sFailed + sDiv + sSplit;
                            sDiv = "\n";
                        }
                        else
                        {
                            setInputStyle(obj,"#FFFFFF");
                        }

                    }
                    else
                    {
                        setInputStyle(obj,"#FFFFe2");
                        if (bClear == 1)
                        { 
                            obj.value = ""; 
                        }  
                    }
                    
                }
                if (bSection == 0 && bClear == 1)
                {
                	show(eval(sDB + "_USER"));
                    show(eval(sDB + "_INPUT"));
                }
            }
        }
        else if (sSplit.charAt(0) == "%")
        {
        
            // SPECIAL VALIDATION (SOCIAL SECURITY CODE)
            // VALID: "### ## ####" OR "####" IN LAST BOX
            
            sDBSplit = split[1].split("+");
            
            sDB = sDBSplit[0];

            sRight = sDBSplit[1].split("#");
           
            sFields = sRight[0].split("|");
            sCounts = sRight[1].split(",");
            
            sSplit = sSplit.substr(1);
            
            if (bRequired == 1)
            {
            
                bSection = 1
                
                var bSSN = 1;
                
                // CHECK FOR LAST BOX ONLY: --- -- ####
                
                //alert("SSN: " + frmShop.ssn_0.value + " - " + frmShop.ssn_1.value + " - " + frmShop.ssn_2.value);
                //alert("order_ssn: " + frmShop.order_ssn.value);
                
                if (bClear == 1 && checkNumericField(trimAll(frmShop.order_ssn.value),4))
                {
                	frmShop.ssn_0.value = "";
                	frmShop.ssn_1.value = "";
                	frmShop.ssn_2.value = frmShop.order_ssn.value;
                	setInputStyle(frmShop.ssn_0,"#FFFFFF");
                	setInputStyle(frmShop.ssn_1,"#FFFFFF");
                	setInputStyle(frmShop.ssn_2,"#FFFFFF");
                	bSSN = 0;
                }
                
                if (bSSN == 1 && frmShop.ssn_0.value == "" && frmShop.ssn_1.value == "" && checkNumericField(frmShop.ssn_2.value,4))
                {
                	setInputStyle(frmShop.ssn_0,"#FFFFFF");
                	setInputStyle(frmShop.ssn_1,"#FFFFFF");
                	setInputStyle(frmShop.ssn_2,"#FFFFFF");
                	bSSN = 0;
                }
                
                if (bSSN == 1)
                {
	                for (j = 0; j < sFields.length; j++)
	                {
	                
	                    obj = eval("frmShop." + sFields[j]);
	                    iText = trimAll(obj.value);
	
	                    if (bSection == 1)
	                    {
	
	                        if (iText == "" || !checkNumericField(iText,sCounts[j]) )
	                        {
	                            setInputStyle(obj,"#FFFFe2");
	                            if (bClear == 1)
	                            { 
	                                obj.value = ""; 
	                            }                            
	                            bSubmit = 0;
	                            bSection = 0;
	                            j = -1;
	                            sFailed = sFailed + sDiv + sSplit;
	                            sDiv = "\n";
	                        }
	                        else
	                        {
	                            setInputStyle(obj,"#FFFFFF");
	                        }
	
	                    }
	                    else
	                    {
	                        setInputStyle(obj,"#FFFFe2");
	                        if (bClear == 1)
	                        { 
	                            obj.value = ""; 
	                        }  
	                    }
	                    
	                }
	                if (bSection == 0 && bClear == 1)
	                {
	                	show(eval(sDB + "_USER"));
	                    show(eval(sDB + "_INPUT"));
	                }
	            }
	            
            }
        }
        else if (sSplit.charAt(0) == "$")
        {
        
        	// SPECIAL VALIDATION: MERCHANT CODE
        	//	AMEX: 4 DIGITS
        	//  OTHERS: 3 DIGITS

            obj = eval("frmShop." + split[1]);
            iText = trimAll(obj.value);
            
            sSplit = sSplit.substr(1);
            
            var bMCode = 1;

			if(frmShop.order_payment_credit_card_name.value == "American Express")
        	{
        		if(checkNumericField(iText,4))
        		{
        			bMCode = 0;
        		}
        	}
        	else
        	{
        		if(checkNumericField(iText,3))
        		{
        			bMCode = 0;
        		}
        	}
        	
        	if(checkNumericField(iText,4))
    		{
    			bMCode = 0;
    		}
    		if(checkNumericField(iText,3))
    		{
    			bMCode = 0;
    		}
        	
            if (bMCode == 1)
            {
            	setInputStyle(obj,"#FFFFe2");
                if (bClear == 1)
                { 
                    obj.value = ""; 
                    show(eval(obj.name + "_USER"));
                    show(eval(obj.name + "_INPUT"));
                }  
                bSubmit = 0;
                sFailed = sFailed + sDiv + sSplit;
                sDiv = "\n";
            }
            else
            {
                setInputStyle(obj,"#FFFFFF");
            }

        }
        else if (sSplit.charAt(0) == "^")
        {
        
        	// SPECIAL VALIDATION: ACTIVE LAND-LINE PHONE
        	
        	if (q2phone.style.display == "inline")
        	{
        
	            obj = eval("frmShop." + split[1]);
	            iText = trimAll(obj.value);
	            
	            sSplit = sSplit.substr(1);
	
	            if (iText == "")
	            {
	                setInputStyle(obj,"#FFFFe2");
	                if (bClear == 1)
	                { 
	                    obj.value = ""; 
	                    show(eval(obj.name + "_USER"));
	                    show(eval(obj.name + "_INPUT"));
	                }  
	                bSubmit = 0;
	                sFailed = sFailed + sDiv + sSplit;
	                sDiv = "\n";
	            }
	            else
	            {
	                setInputStyle(obj,"#FFFFFF");
	            }
	            
	      	}
            
        }
        else
        {
        
            obj = eval("frmShop." + split[1]);
            iText = trimAll(obj.value);

            if (iText == "")
            {
                setInputStyle(obj,"#FFFFe2");
                if (bClear == 1)
                { 
                    obj.value = ""; 
                    show(eval(obj.name + "_USER"));
                    show(eval(obj.name + "_INPUT"));
                }  
                bSubmit = 0;
                sFailed = sFailed + sDiv + split[0];
                sDiv = "\n";
            }
            else
            {
                setInputStyle(obj,"#FFFFFF");
            }
            
        }

    }

    if (bSubmit == 1)
    {    
        return true;
    }
    
    if (bAlert)
    {
        sFailed = "The following fields must be successfully completed:\n" + sFailed;
        alert(sFailed);
    }
    
    return false;
    
}

function fillSectionedFields()
{

    frmShop.order_contact_phone_installation.value = frmShop.phone_0_0.value + "." + frmShop.phone_0_1.value + "." + frmShop.phone_0_2.value;
    frmShop.order_contact_phone_secondary.value = frmShop.phone_1_0.value + "." + frmShop.phone_1_1.value + "." + frmShop.phone_1_2.value;
    
    
}

function fillCostDisplay()
{

    // FULL COST/TOTALS TABLE-FORMATTED DISPLAY:

    var sTableHeader = new String("<table bgcolor=#ffffff cellpadding=2>\n");
    
    var sEquipmentHeader = new String("<tr><td colspan=2 bgcolor=#e6e6e6><b>Satellite Equipment and Installation:</b></td></tr>\n<td>\n");
    var sEquipmentBody = fillEquipment();
    var sEquipmentFooter = new String("<table bgcolor=#bbbbbb height=5 cellpadding=0 cellspacing=0><tr height=4><td bgcolor=#ffffff></td></tr><tr height=1><td width=360></td></tr></table>\n<table><tr><td width=" + titleWidth + "><b>Equipment Total</b></td><td width=" + priceWidth + "><div style=\"font-weight:bold;\">" + getDIVText(price_etotal) + "</div></td></tr></table>\n<br />\n</td></tr>\n");
    
    var sActivationHeader = new String("<tr><td colspan=2 bgcolor=#e6e6e6><b>Activation Fees:</b></td></tr>\n<td>\n");
    var sActivationBody = fillActivation();
    var sActivationFooter = new String("<table bgcolor=#bbbbbb height=5 cellpadding=0 cellspacing=0><tr height=4><td bgcolor=#ffffff></td></tr><tr height=1><td width=360></td></tr></table>\n<table><tr><td width=" + titleWidth + "><b>Activation Total</b></td><td width=" + priceWidth + "><div style=\"font-weight:bold;\">" + getDIVText(price_atotal) + "</div></td></tr></table>\n<br />\n</td></tr>\n");
    
    var sMonthlyHeader = new String("<tr><td colspan=2 bgcolor=#e6e6e6><b>Monthly Charges Estimate:</b></td></tr>\n<td>\n");
    var sMonthlyBody = fillMonthly();
    var sMonthlyFooter = new String("<table bgcolor=#bbbbbb height=5 cellpadding=0 cellspacing=0><tr height=4><td bgcolor=#ffffff></td></tr><tr height=1><td width=360></td></tr></table>\n<table><tr><td width=" + titleWidth + "><b>Total Monthly Cost</b></td><td width=" + priceWidth + "><div style=\"font-weight:bold;\">" + getDIVText(price_mtotal) + "</div></td></tr></table>\n<br />\n</td></tr>\n");
    
    var sTableFooter = new String("</table>\n");

    // COMBINE SECTIONS INTO COMPLETE TABLE: sCostDisplay
    
    var sCostDisplay = sTableHeader + 
                            sEquipmentHeader +
                            sEquipmentBody +
                            sEquipmentFooter +
                            sActivationHeader +
                            sActivationBody +
                            sActivationFooter +
                            sMonthlyHeader +
                            sMonthlyBody +
                            sMonthlyFooter +
                       sTableFooter;
    
    frmShop.order_cost_display.value = sCostDisplay;
    
}

function fillSummary()
{
    
    var i = 1;
    var j = 0;
    var sItem = new String();
    var aIncludes = items_included_special.split(".");
    var disp = new String();
    var obj = new String();

    // EQUIPMENT:
    
    var sEquipmentSummary = new String("<b>Equipment Selections:</b>\n<br>");
    var sEquipmentEmail = new String();
    for(i in eIDs)
    {
        obj = "cost_" + eIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            sEquipmentSummary += getDIVHTML(eval("item_" + eIDs[i])) + "\n<br>";
            sEquipmentEmail += getDIVText(eval("item_" + eIDs[i])) + "\n";
        }
    }
    for(j in aIncludes)
    {
        sItem = aIncludes[j];
        if(sItem[0] == 'e')
        {
            sEquipmentSummary += getDIVHTML(eval("item_" + sItem)) + "\n<br>";
            sEquipmentEmail += getDIVText(eval("item_" + sItem)) + "\n";
        }
    }
    sEquipmentSummary += "\n<br>";
    frmShop.order_summary_equipment.value = sEquipmentSummary;
    frmShop.order_email_equipment.value = stripHTML(sEquipmentSummary);
    
    // PROGRAMMING PACKAGES:
    
    var bNone = 1;
    var sProgrammingSummary = new String("<b>Programming Selection:</b>\n<br>");
    var sProgrammingEmail = new String();
    for(i in ppIDs)
    {
        obj = "cost_" + ppIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            bNone = 0;
            sProgrammingSummary += getDIVHTML(eval("item_" + ppIDs[i])) + "\n<br>";
            sProgrammingEmail += getDIVText(eval("item_" + ppIDs[i])) + "\n";
        }
    }
    for(j in aIncludes)
    {
        sItem = aIncludes[j];
        if(sItem[0] == 'p' && sItem[1] == 'p')
        {
            sProgrammingSummary += getDIVHTML(eval("item_" + sItem)) + "\n<br>";
            sProgrammingEmail += getDIVText(eval("item_" + sItem)) + "\n";
            bNone = 0;
        }
    }
    if (bNone == 1)
    {
        sProgrammingSummary += "None\n<br>";
        sProgrammingEmail += "None\n";
    }
    sProgrammingSummary += "\n<br>";
    frmShop.order_summary_programming.value = sProgrammingSummary;
    frmShop.order_email_programming.value = stripHTML(sProgrammingSummary);
    
    // PREMIUM CHANNELS:

    bNone = 1;
    var sChannelsSummary = new String("<b>Premium Channel Selections:</b>\n<br>");
    var sChannelsEmail = new String();
    for(i in pcIDs)
    {
        obj = "cost_" + pcIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            bNone = 0;
            sChannelsSummary += getDIVHTML(eval("item_" + pcIDs[i])) + "\n<br>";
            sChannelsEmail += getDIVText(eval("item_" + pcIDs[i])) + "\n";
        }
    }
    for(j in aIncludes)
    {
        sItem = aIncludes[j];
        if(sItem[0] == 'p' && sItem[1] == 'c')
        {
            sChannelsSummary += getDIVHTML(eval("item_" + sItem)) + "\n<br>";
            sChannelsEmail += getDIVText(eval("item_" + sItem)) + "\n";
            bNone = 0;
        }
    }
    if (bNone == 1)
    {
        sChannelsSummary += "None\n<br>";
        sChannelsEmail += "None\n";
    }
    sChannelsSummary += "\n<br>";
    frmShop.order_summary_channels.value = sChannelsSummary;
    frmShop.order_email_channels.value = stripHTML(sChannelsSummary);

}

function fillEquipment()
{

    var sEquipment = new String();
    
    var i = 0;
    var obj = new String();
    
    for(i in eIDs)
    {
        obj = "cost_" + eIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            sEquipment += "<table><tr><td width=" + titleWidth + ">" + getDIVText(eval("item_" + eIDs[i])) + "</td><td width=" + priceWidth + ">" + getDIVText(eval("price_" + eIDs[i])) + "</td></tr></table>\n";    
        }    
    }
   
    frmShop.order_equipment.value = sEquipment;
    frmShop.order_equipment_total.value = getDIVText(price_etotal);

    return sEquipment;
    
}

function fillActivation()
{

    var sActivation = new String();
    
    var i = 0;
    var obj = new String();
    
    for(i in aIDs)
    {
        obj = "cost_" + aIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            sActivation += "<table><tr><td width=" + titleWidth + ">" + getDIVText(eval("item_" + aIDs[i])) + "</td><td width=" + priceWidth + ">" + getDIVText(eval("price_" + aIDs[i])) + "</td></tr></table>\n";
        }
    }
    
    frmShop.order_activation.value = sActivation;
    frmShop.order_activation_total.value = getDIVText(price_atotal);
    
    return sActivation;
    
}

function fillMonthly()
{

    var sMonthly = new String();
    
    var i = 0;
    var obj = new String();
    
    for(i in mIDs)
    {
        obj = "cost_" + mIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            sMonthly += "<table><tr valign=\"top\"><td width=" + titleWidth + ">" + getDIVText(eval("item_" + mIDs[i])) + "</td><td width=" + priceWidth + ">" +  getDIVText(eval("price_" + mIDs[i])) + "</td></tr></table>\n";
        }
    }
    
    sMonthly += fillProgrammingPackages();
    sMonthly += fillPremiumChannels();
    sMonthly += fillSpecialCombos();

    frmShop.order_monthly.value = sMonthly;
    frmShop.order_monthly_total.value = getDIVText(price_mtotal);
    
    return sMonthly;
    
}

function fillProgrammingPackages()
{

    var sProgrammingPackages = new String();
    
    var i = 0;
    var obj = new String();
    
    for(i in ppIDs)
    {
        obj = "cost_" + ppIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            sProgrammingPackages += "<table><tr valign=\"top\"><td width=" + titleWidth + ">" + getDIVText(eval("item_" + ppIDs[i])) + "</td><td width=" + priceWidth + ">" + getDIVText(eval("price_" + ppIDs[i])) + "</td></tr></table>\n";
        }
    }

    return sProgrammingPackages;
    
}

function fillPremiumChannels()
{

    var sPremiumChannels = new String();
    var i = 0;
    var obj = new String();
    
    if(cost_ptotal.style.display == "inline")
    {

        sPremiumChannels = "<table><tr><td width=" + titleWidth + " colspan=2><b>Premium Movie Channels</b></td><td width=" + priceWidth + "><b>" + getDIVText(eval("price_ptotal")) + "</b></td></tr></table>\n";
        
        for(i in pcIDs)
        {
            obj = "cost_" + pcIDs[i];
            obj = eval(obj + ".style.display");
            if (obj == "inline")
            {
                sPremiumChannels += "<table><tr><td width=\"20\"></td><td width=" + (titleWidth - 24) + ">" + getDIVText(eval("item_" + pcIDs[i])) + "</td><td width=" + priceWidth + ">" + getDIVText(eval("price_" + pcIDs[i])) + "</td></tr></table>\n";
            }
        }
        
    }

    return sPremiumChannels;
    
}

function fillSpecialCombos()
{

    var sSpecialCombos = new String();
    
    sp_pc_included = new String();
    
    var i = 0;
    var j = 0;
    var tot = 0.0;
    var obj = new String();
    var si = 0;
    
    for(i in spIDs)
    {
        obj = "cost_" + spIDs[i];
        obj = eval(obj + ".style.display");
        if (obj == "inline")
        {
            
            sSpecialCombos += "<table>\n";
            sSpecialCombos += "<tr><td width=" + titleWidth + " colspan=\"2\"><b>" + getDIVText(eval("item_" + spIDs[i])) + "</b></td><td width=" + priceWidth + "><b>" + getDIVText(eval("price_" + spIDs[i])) + "</b></td></tr>\n";
            si = eval("frmShop." + spIDs[i] + "_items").value;
            j = 1;
            for(j = 1; j <= si; j++)
            {
                sSpecialCombos += "<tr><td width=\"20\"></td><td width=" + (titleWidth - 24) + ">" + getDIVText(eval(spIDs[i] + "_item" + parseInt(j))) + "</td><td width=" + priceWidth + "></td></tr>\n"
            }
            sSpecialCombos += "</table>\n";
            
        }
    }

    return sSpecialCombos;
    
}

function fillInstallationTimes()
{

    var sTimes = new String();
    var sSummaryTimes = new String();
    var i = 0;
    
    for(i = 0;i <= 14;i++)
    {
        if (eval("frmShop.install_time" + parseInt(i) + ".checked"))
        {
            sTimes = sTimes + eval("frmShop.install_time" + parseInt(i) + ".value") + "\n";
            sSummaryTimes = sSummaryTimes + eval("frmShop.install_time" + parseInt(i) + ".value") + "<br>\n";
        }
    }
    
    frmShop.order_installation_times.value = sTimes;
    frmShop.order_summary_installation_times.value = sSummaryTimes;
    
    return sTimes;
    
}

// ------------------------------------------------------------------------
// UTILITY FUNCTIONS:

function doStandardComment(bWhere)
{

	// bWhere:
	//	0: pre-pend (before all)
	//  1: in-text  (selection/cursor location)
	//  2: append 	(after all)
		
	//SELECT: 	frmShop.comment_select
	//TEXTAREA: frmShop.order_header_comments
	
	var str = new String(frmShop.order_header_comments.value);
	var newLine = new String("\n");
	
	if (str.indexOf("\n") == -1)
	{
		newLine = "";		
	}
	
	frmShop.order_header_comments.value = frmShop.order_header_comments.value + newLine + eval("frmShop.comment" + parseInt(frmShop.comment_select.value)).value;

}

function hide(obj)
{
    obj.style.display = "none";
}

function show(obj)
{
    obj.style.display = "inline";
}

function isVisible(obj)
{
	return (obj.style.display == "inline");
}

function setDIVText(obj,txt)
{

	/*
    
    */
    
    obj.textContent = txt;
    obj.innerText = txt;
    obj.innerHTML = txt;
    
}

function getDIVText(obj)
{

	/*
	
    if (obj.textContent != null)
    {
        return obj.textContent;
    }
    
    */
    
    return obj.innerText;
    
}


function getDIVHTML(obj)
{

	/*
	
    if (obj.textContent != null)
    {
        return obj.textContent;
    }
    
    */
    
    return obj.innerHTML;
    
}

function copyAddress()
{

    // FILL BILLING ADDRESS WITH INSTALLATION ADDRESS VALUES
    
    frmShop.order_billing_address_street.value = frmShop.order_install_address_street.value;
    frmShop.order_billing_address_city.value = frmShop.order_install_address_city.value;
    frmShop.order_billing_address_state.value = frmShop.order_install_address_state.value;
    frmShop.order_billing_address_zip.value = frmShop.order_install_address_zip.value;
    
    validateForm(false,"frmShop");
    
}

function setInputStyle(obj,bgColor)
{

    obj.style.backgroundColor = bgColor;
    obj.style.fontFamily = "Verdana,Tahoma,Arial"; 
    obj.style.fontWeight = "normal";
    obj.style.color = "black"; 
    obj.style.fontSize = "10px"; 
    obj.style.border = "solid 1px #bbbbbb";
    obj.style.height = "20px";
    obj.style.padding = "2px";
    
}

function hideCCFields()
{

    cc_0.style.display = "none";
    cc_1.style.display = "none";
    
}

function ccNameSelect(val)
{

    hideCCFields();
    
    var obj;
    
    if (val == "American Express")
    {
        cc_1.style.display = "inline";
        obj = eval("frmShop.cc_1_num_0");
        setInputStyle(obj,"#FFFFFF");
        obj = eval("frmShop.cc_1_num_1");
        setInputStyle(obj,"#FFFFFF");
        obj = eval("frmShop.cc_1_num_2");
        setInputStyle(obj,"#FFFFFF");
    }
    else
    {
        cc_0.style.display = "inline";
        obj = eval("frmShop.cc_0_num_0");
        setInputStyle(obj,"#FFFFFF");
        obj = eval("frmShop.cc_0_num_1");
        setInputStyle(obj,"#FFFFFF");
        obj = eval("frmShop.cc_0_num_2");
        setInputStyle(obj,"#FFFFFF");
        obj = eval("frmShop.cc_0_num_3");
        setInputStyle(obj,"#FFFFFF");
    }
    
    validateForm(false);
    
}

function trimAll(sString)
{

    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    
    return sString;
    
}

function trimZip(sString)
{

    var ret = new String();
    var i = 0;  
      
    for(i = 0;i < sString.length;i++)
    {
        if ( sString.charAt(i) >= '0' && sString.charAt(i) <= '9')
        {
            ret += sString.charAt(i);
        }
    }

    return ret;
    
}

function fixPrice(tot)
{
    if (tot != 0.0)
    {
        return ("$" + tot.toFixed(2));
    }
    return new String("FREE");
}

function checkNumericField(field, count)
{

    var sField = new String(field);
    
    if (sField.length != count)
    {
        return false
    }
    
    var c;
    var i = 0;
    
    for(i = 0;i < sField.length;i++)
    {
        c = sField.charAt(i);
        if (c < '0' || c > '9')
        {
            return false;
        }
    }
    
    return true;
    
}

function displayMerchantCode()
{
    window.open("images/vcode.jpg","mcode","width=350,height=300,dependent,resizable=no,toolbar=no");
}

function displayTerms()
{
    window.open("terms.html","Terms","width=500,height=185,dependent,resizable=no,toolbar=no");

}

function stripHTML(str)
{

	var re = /<\S[^>]*>/g; 
	return str.replace(re,""); 

}

function stripHTML3(oldString) 
{

   var newString = "";

   var inTag = false;
	var setInTag = false;
   for(var i = 0; i < oldString.length; i++) 
   {
  
        if(oldString.charAt(i) == '<') 
        {
        	inTag = true;
        }

        if(oldString.charAt(i) == '>') 
        {
            inTag = false;
            setEndTag = false;
        }

        if(setInTag) 
        {
        	newString += oldString.charAt(i);
        	setEndTag = true;
        }
        
        
        
        

   }

   return newString;

}

// ------------------------------------------------------------------------
