function blinkFormats(obj,t){
	t--;
	if ((t%2)==1){
		$(obj).className='prod_notice_format';
	}else{
		$(obj).className='prod_format';
	}
	if (t>0){
	window.setTimeout("blinkFormats('"+obj+"',"+t+")",250);}
}

function createAccount(){
	var errs='';
	var f = document.forms['account'];

	errs= validateFormCore(f);

	if (f.password.value!=f.cpassword.value){
		errs=errs+'-Password not properly confirmed!\n';
	}

	if (errs==''){
		//return true;
		f.submit();
	}else{
		alert('Before submitting, please complete the following fields:\n\n'+errs);
		//return false;
	}
}

function cwt_baddresschangeboxcheck(v){

	var fldArray = new Array('email','fname','lname','company','address1','address2','city','state','postal','phone','fax');

	for (n=0;n<fldArray.length;n++){
		eval("document.forms['checkout'].b"+fldArray[n]+".disabled=false;");
	}
	document.forms['checkout'].baddress.value=1;

	if (v==0){

		for (n=0;n<fldArray.length;n++){
			eval("document.forms['checkout'].b"+fldArray[n]+".value = document.forms['checkout'].s"+fldArray[n]+".value;");
		}

		for (n=0;n<fldArray.length;n++){
			eval("document.forms['checkout'].b"+fldArray[n]+".disabled = true;");
		}

		document.forms['checkout'].baddress.value=0;
	}


}

function cartInformation(){
	var errs='';
	var f = document.forms['checkout'];

	errs= validateFormCore(f);	

	if (vbaddress==1){
		if (f.bfname.value==0){errs=errs+'Billing: First name cannot be empty\n';}
		if (f.blname.value==0){errs=errs+'Billing: Last name cannot be empty\n';}
		if (f.bemail.value==0){errs=errs+'Billing: Email cannot be empty\n';}
		if (f.bcity.value==0){errs=errs+'Billing: City cannot be empty\n';}
		if (f.bstate.value==0){errs=errs+'Billing: State cannot be empty\n';}
		if (f.bpostal.value==0){errs=errs+'Billing: Postal/ZIP cannot be empty\n';}
		if (f.bphone.value==0){errs=errs+'Billing: Phone number cannot be empty\n';}
	}

	if (errs==''){
		//return true;
		f.submit();
	}else{
		alert('Before submitting, please complete the following fields:\n\n'+errs);
		//return false;
	}

}

function validateRadioButton(theFld) {
	found = false;
	var thCounter=0;
	if (!theFld) return -1;
	if (!theFld.length) return -1;
	for (thCounter=0; thCounter<theFld.length && !found; thCounter++)
		if (theFld[thCounter].checked)
			found=true;
	if (found)
		res = thCounter;
	else
		res = -1;
	return res;
}

function cartPayment(m){
	var errs='';
	var f = document.forms['payment'];

	errs= validateFormCore(f);
	if (f['payment'].value!='manual') {
		if (validateRadioButton(f['shippingType'])<0) {
			errs = 'Specify shipping type' + "\n";
		}
	}
	
	switch(f['payment'].value) {
	case 'automatic':
		if (!f['CC_name'].value || !f['CC_number'].value || 
			!f['CC_expMonth'].value || !f['CC_expYear'].value || !f['CC_verifNum'].value) {
				errs += 'Please provide full credit card information' + "\n";
		}
		break;
	}
	//switch(m){
	//	
	//}

	if (errs==''){
		//return true;
		f.submit();
	}else{
		alert('Before submitting, please complete the following fields:\n\n'+errs);
		return false;
	}

}

function recalculateOrder(el) {
	var theForm = document.forms['payment'];
	//if (!theForm) theForm = el.form;
	if (!theForm) return false;
	var subTotal = theForm['subTotal'].value*1;
	var sShipping = 'N/A'; var sTax1 = 'N/A'; var sTax2 = 'N/A'; var sTotal = 'N/A';
	var vDiscount = 0; var vShipping = 0; var vTax1 = 0; var vTax2 = 0; var vTotal = 0;
	var shipRadio = validateRadioButton(theForm['shippingType']);
	if (shipRadio>0) {
		shipRadio--;
		vShipping = theForm['shippingType'][shipRadio].getAttribute('shipPrice')*1;
		if (vShipping===0)
			sShipping = 'FREE';
		else
			sShipping = '$' + vShipping;
	}
	if (vShipping>=0) {
		vDiscount = theForm['discount'].value*1;
		vTotal = subTotal + vDiscount + vShipping;
		if (theForm['tax1prcnt']) {
			if ((theForm['tax1prcnt'].value*1)>0) {
				vTax1 = vTotal * (theForm['tax1prcnt'].value*1) / 100;
				vTax1 = MakeCurrency(vTax1)*1;
				sTax1 = '$' + MakeCurrency(vTax1);
				if (theForm['tax2prcnt']) {
					if ((theForm['tax2prcnt'].value*1)) {
						if (theForm['taxado'].value*1) {
							vTax2 = (vTotal + vTax1) * (theForm['tax2prcnt'].value*1) / 100;
						} else {
							vTax2 = vTotal * (theForm['tax2prcnt'].value*1) / 100;
						}
						vTax2 = MakeCurrency(vTax2)*1;
						sTax2 = '$' + MakeCurrency(vTax2);
					}
				}
			}
		}
		vTotal += vTax1; vTotal += vTax2;
		vTotal = MakeCurrency(vTotal);
		sTotal = '$' + vTotal;
		theForm['shipping'].value = vShipping;
		if (theForm['tax1']) theForm['tax1'].value = vTax1;
		if (theForm['tax2']) theForm['tax2'].value = vTax2;
		theForm['total'].value = vTotal;
	}
	if ($('sShipping')) $('sShipping').update(sShipping);
	if ($('sTax1')) $('sTax1').update(sTax1);
	if ($('sTax2')) $('sTax2').update(sTax2);
	if ($('sTotal')) $('sTotal').update(sTotal);
}


function MakeCurrency( strValue ) {
	/************************************************
	DESCRIPTION: Formats a number as currency.
	
	PARAMETERS:
	  strValue - Source string to be formatted
	
	REMARKS: Assumes number passed is a valid
	  numeric value in the rounded to 2 decimal
	  places.  If not, returns original value.
	*************************************************/
  var f = parseFloat(strValue);
  if (isNaN(f)) return strValue;
  strValue = parseFloat(strValue).toFixed(2);  // if it is taxes - they might have precisions up to 4... need to have 2
  var i = Math.floor(f*100);

var f = parseFloat(strValue).toFixed(4); // need to do the jump, because JS loses precisions in calculations
var  f2= (parseFloat(100)*f).toFixed(4);

  var cents = Math.abs(f2%100);
  if (cents<10) cents='0'+cents;
	strValue =  Math.floor(i/100)+'.'+cents;
//  alert(strValue);
  var objRegExp = /-?[0-9]+\.[0-9]{2}$/;

    if( objRegExp.test(strValue)) {
      objRegExp.compile('^-');
      strValue = addCommas(strValue);
      if (objRegExp.test(strValue)){
        strValue = '(' + strValue.replace(objRegExp,'') + ')';
      }
	  strValue = strValue.replace(',', '');
      return strValue;
//      return '$' + strValue;
    }
    else {
	  strValue = strValue.replace(',', '');
      return strValue;
	 }
}

function addCommas( strValue ) {
/************************************************
DESCRIPTION: Inserts commas into numeric string.

PARAMETERS:
  strValue - source string containing commas.

RETURNS: String modified with comma grouping if
  source was all numeric, otherwise source is
  returned.

REMARKS: Used with integers or numbers with
  2 or less decimal places.
*************************************************/
  var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');

    //check for match to search criteria
    while(objRegExp.test(strValue)) {
       //replace original string with first group match,
       //a comma, then second group match
       strValue = strValue.replace(objRegExp, '$1,$2');
    }
  return strValue;
}
function toggleHidden(hiddenID) {
	if (!document.getElementById) return;
	el = document.getElementById(hiddenID);
	if (el)
		el.style.display = (el.style.display=='none') ?  'block' : 'none';
}


