function getStyleObject(objectId) {
    if(document.getElementById && document.getElementById(objectId)) {
        return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
        return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
        return document.layers[objectId];
    } else {
        return false;
    }
}

function getElement(id){
    if(document.getElementById && document.getElementById(id)){
        return document.getElementById(id);
    } else if (document.all && document.all(id)) {
        return document.all(id);
    } else if (document.layers && document.layers[id]) {
        return document.layers[id];
    } else {
        return false;
    }
}

function alterContent(value,id){
    if (document.all && document.all(id)){
        document.all(id).innerHTML=value;
    }
    else if (document.getElementById){
        rng = document.createRange();
        el = document.getElementById(id);
        rng.setStartBefore(el);
        htmlFrag = rng.createContextualFragment(value);
        while (el.hasChildNodes())
                el.removeChild(el.lastChild);
        el.appendChild(htmlFrag);
    }
}

function getRadioValue(group,eform){
var el = document.forms[eform].elements;
 for(var i = 0 ; i < el.length ; ++i) {
  if(el[i].name == group) {
   var radiogroup = el[el[i].name];
   for(var j = 0 ; j < radiogroup.length ; ++j) {
    if(radiogroup[j].checked) {
         return radiogroup[j].value;
    }
   }
  }
 }
}

function getFrmElement(group,eform){
var el = document.forms[eform].elements;
 for(var i = 0 ; i < el.length ; ++i) {
  if(el[i].name == group) {
     return el[el[i].name];

   }
  }
}


var groups = new Object();
var total_cost=0.00;
var product_price=0.00;
var total_amount=1;
var currencySymbol = '';
var currencyRate = 0;

function price_format(n,dd) {
  if (dd == 0) return n;

  var dp = String(n).indexOf('.');
  if (dp < 0) {
    return price_format(n + ".0", dd);
  } else {
    if (String(n).length - dp - 1 < dd) {
      return price_format(n + "0", dd);
    } else {
      return n;
    }
  }
}


function setBasePrice(price){
        product_price=price;
        total_cost = price;
}

function setCurrency(str,val){
    currencySymbol = str;
    currencyRate = val;
}

function addGroup(groupid,type,products,price){
        groups[groupid] = new Object();
        groups[groupid].type=type;
        groups[groupid].products=products;
        groups[groupid].price=0;
}


function setMaxChecks(id,max){
    if(groups[id].type=='C' && max>0){
        groups[id].max = max;
    }
}

function roundTwo(num){
        return price_format(Math.round(num*100)/100,2);
}

function renderRadio(groupid){
    var val = getRadioValue('groups['+groupid+']','orderform');
    var price = groups[groupid].products[val];
    total_cost-=groups[groupid].price;
    total_cost+=price;
    groups[groupid].price = price;
    total_cost = total_cost;
    extra = '';

    if(currencyRate>0 && currencySymbol != ""){
        extra = ' ('+currencySymbol+' '+roundTwo(total_cost*total_amount*currencyRate)+')';
    }
    alterContent('$'+roundTwo(total_cost*total_amount)+extra,'total_cost');
    for(pid in groups[groupid].products){
            var prod = groups[groupid].products[pid];
            var str;
            if(prod<price){
                str = 'Subtract: $'+roundTwo(price-prod);
                if(currencyRate>0 && currencySymbol != ""){
                    str+= ' ('+currencySymbol+''+roundTwo((price-prod)*currencyRate)+')';
                }
            }else if(prod==price){
                        str = '&nbsp';
            }else{ 
                str = 'Add: $'+roundTwo(prod-price);
                if(currencyRate>0 && currencySymbol != ""){
                    str+= ' ('+currencySymbol+''+roundTwo((prod-price)*currencyRate)+')';
                }
            }

            alterContent(str,'price_'+groupid+'_'+pid);
    }
}

function renderChecks(groupid){
        var price=0.00;
        total_cost-=groups[groupid].price;
       // var max = (groups[id].max==null) ? -1 : (groups[id].max);
				var max = (groups[groupid].max==null) ? -1 : (groups[groupid].max);
        var msg = false;
        for(pid in groups[groupid].products){
            var prod = groups[groupid].products[pid];
            var str;
            var ele = getElement('groups['+groupid+']['+pid+']');
            if(!ele){
                    ele = getFrmElement('groups['+groupid+']['+pid+']','orderform');
            }
            if(ele.checked){
                    if(max==0){
                        ele.checked = false;
                        msg = true;
                        str="Add: $"+prod;
                        if(currencyRate>0 && currencySymbol != ""){
                            str+= ' ('+currencySymbol+''+roundTwo(prod*currencyRate)+')';
                        }
                    }else{
                        max--;
                        price+=prod;
                        str="Subtract: $"+prod;
                        if(currencyRate>0 && currencySymbol != ""){
                            str+= ' ('+currencySymbol+''+roundTwo(prod*currencyRate)+')';
                        }
                    }
            }else{
                str="Add: $"+prod;
                if(currencyRate>0 && currencySymbol != ""){
                    str+= ' ('+currencySymbol+''+roundTwo(prod*currencyRate)+')';
                }
            }
            alterContent(str,'price_'+groupid+'_'+pid);
    }
    groups[groupid].price=price;
    total_cost+=price;
    total_cost = price_format((Math.round(total_cost*100)/100),2);
    extra = '';
    if(currencyRate>0 && currencySymbol != ""){
        extra = ' ('+currencySymbol+''+roundTwo(total_cost*total_amount*currencyRate)+')';
    }
    alterContent('$'+roundTwo(total_cost*total_amount)+extra,'total_cost');
    if(msg){
        alert('Please review your selections and select the '+groups[groupid].max+' items in this group that best fits your needs.  Thank you.');
    }
}

function renderGroup(groupid){
        if(groups[groupid].type == 'R'){
                renderRadio(groupid);
        }else if(groups[groupid].type == 'C'){
                renderChecks(groupid);
        }
}

function renderGroups(){
        total_cost=product_price;
        for (id in groups){
            renderGroup(id);
        }
}

function calculatePrice(){
        amount=getElement("amount").value;
        if(!amount){
                amount = getFrmElement('amount','orderform').value;
        }
         if (!(parseInt(amount)==amount && amount.toString().indexOf('.')==-1)){
                getElement('amount').value=1;
                amount=1;
        }
    extra = '';
    if(currencyRate>0 && currencySymbol != ""){
        extra = ' ('+currencySymbol+' '+roundTwo(total_cost*total_amount*currencyRate)+')';
    }
    alert(extra);
        total_amount=parseInt(amount);
    alterContent('$'+roundTwo(total_cost*total_amount)+extra,'total_cost');
}
