﻿function UpdateTotalPrice(anArray,CtrlArray)//txtQuantityComboID)
{
	AccArray = anArray;
  var TotPrice = 0;
  
	for(  i = 0; i < AccArray.length; i++)
	{
		if (AccArray[i].indexOf('chkLine') > 0 )
		{
			re = new RegExp(AccArray[i]);
			for(j = 0; j < document.forms[0].elements.length; j++)
			{
				elm = document.forms[0].elements[j];

				if (elm.type == 'checkbox')
				{
					if (re.test(elm.name))
					{
						if (elm.checked)
						{
						if (elm.innerText.parentNode && typeof elm.innerText.parentNode.textContent !="undefined")		    
						{//Firefox						
							if (elm.parentNode.textContent.indexOf('$') >0)//parentElement.innerHTML
								{
									TotPrice =  TotPrice + Number( elm.parentNode.textContent.slice(elm.parentNode.textContent.indexOf('$') + 1));
								}
							}
						else
							{
							if (elm.parentElement.innerText.indexOf('$') >0)
								{
									TotPrice =  TotPrice + Number( elm.parentElement.innerText.slice(elm.parentElement.innerText.indexOf('$') + 1));
								}
							
							}
						}
					}
				}
			}		
		}
		else if (AccArray[i].indexOf('radLine') > 0 )
		{
		  var cbi = document.getElementsByName(AccArray[i]);
		  for(var k=0;k < cbi.length; k++)
		  {
		    if (cbi[k].checked )
		    {
					if (cbi[k].parentNode && typeof cbi[k].parentNode.textContent !="undefined")		    
					{//Firefox
						if (cbi[k].parentNode.textContent.indexOf('$') >0) //parentElement.innerHTML.indexOf('$') >0)
							{
								TotPrice =  TotPrice + Number(cbi[k].parentNode.textContent.slice(cbi[k].parentNode.textContent.indexOf('$') + 1));//parentElement.innerHTML
							}		      
					}
					else
					{
						if (cbi[k].parentElement.innerHTML.indexOf('$') >0)
							{
								TotPrice =  TotPrice + Number(cbi[k].parentElement.innerText.slice(cbi[k].parentElement.innerText.indexOf('$') + 1));
							}		      					
					}
		    }
		  }
		}
		else
		{
			var xs =	document.getElementsByName(AccArray[i]);
			if 	(xs[0].type ==	'select-one')
			{
			  var htxt = xs[0].options[xs[0].selectedIndex].text;
        if (htxt.indexOf('$') >0)
        {
          TotPrice =  TotPrice + Number(htxt.slice(htxt.indexOf('$') + 1));
        }				
			}
		}
	}	
  var tq = document.getElementById(CtrlArray[0]);//txtQuantityComboID;// document.getElementById(txtQuantityCombo);
  if (isNumeric(tq.value))
  {
    var CPriceHold = document.getElementById(CtrlArray[1]);//hidComboPrice);
    if (CPriceHold.value !== '') {TotPrice =  TotPrice + Number(CPriceHold.value);}
    //var ProdPrice = document.getElementById('inPrice');
    //TotPrice =  TotPrice + Number(ProdPrice.innerText);
    var STot = document.getElementById(CtrlArray[2]);//'txtTotPrice');
    var Tot1 = TotPrice * tq.value;
   // STot.value = addCommas(Tot1.toFixed(2))
   STot.innerHTML = addCommas(Tot1.toFixed(2))
  }
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function SetUniqueRadioButton(nameregex, current, ComboIdCtl,anArray,CtrlArray)//txtQuantityCombo)
{
	re = new RegExp(nameregex);
	for(i = 0; i < document.forms[0].elements.length; i++)
	{
		elm = document.forms[0].elements[i]

		if (elm.type == 'radio')
		{
			if (re.test(elm.name))
			{
				elm.checked = false;
			}
		}
	}
	current.checked = true;
//	var buynow = document.getElementById(buynowbutid)
//	buynow.disabled=false
	var CID = document.getElementById(ComboIdCtl)
	var SID = document.getElementById(CtrlArray[3]);//hidSelectedComboID)
	SID.value = CID.value
	
	var CPriceHold = document.getElementById(CtrlArray[1]);//hidComboPrice)
	CPriceHold.value= document.getElementById(current.id.replace(/radCombo/,"lblPrice")).innerHTML	
	UpdateTotalPrice(anArray,CtrlArray); //txtQuantityCombo)
}

function isNumeric(x) {
// if (isNumeric(myVar)) { }
// regular expression that validates a value is numeric
var RegExp = /^(-)?(\d*)(\.?)(\d*)$/; // Note: this WILL allow a number that ends in a decimal: -452.
// compare the argument to the RegEx
// the 'match' function returns 0 if the value didn't match
var result = x.match(RegExp);
if (result==null) result=false;
return result;
}
