// JavaScript Document
function init(){
	calculateTotal();
}



function validate_payment(  ){
	var frm = document.getElementById('paymentfrm');

	var level = frm.levellimit.value;
	if( level > 0 ){
		var checked = '';
		for( i=0 ; i<frm.level.length ; i++){
			if(frm.level[i].checked)
				checked = frm.level[i].value;
		}
		if(level != checked){
			var name = frm.levelname.value;
			alert('This coupon is only good for the '+name+' plan. \nPlease select that plan and re-submit');
			return false;
		}
	}

	if( calculateTotal() > 0){
		var msg = '';

		var name = frm.name.value;
		var cc = frm.cc.value;
		var m = frm.ccexpmo.selectedIndex;
		var y = frm.ccexpyr.selectedIndex;
		var address = frm.address.value;
		var zip = frm.zip.value;
		var goodcode = frm.goodcode.value;
		var code = frm.code.value;	
		
		if( name == '' )
			msg += 'Please enter the Card Holder Name\n';
		if( m == 0 || y == 0 )
			msg += 'Please enter a valid expiration date\n';
		if( address == '' || zip == '' )
			msg += 'Please enter a valid address and zip code\n';
		if( checkCC( cc ) === false )
			msg += 'Please enter a valid credit card number\n';
		if( code != "" && goodcode == 0 )
			msg += 'The gift code you entered is not valid';
			
		if( msg.length > 0 ){
			alert( msg );
			return false;
		}
		else
			frm.submit();
	}
	else
		frm.submit();
}

function validategift( frm ){
		var msg = '';
	
		var name = frm.name.value;
		var cc = frm.cc.value;
		var m = frm.ccexpmo.selectedIndex;
		var y = frm.ccexpyr.selectedIndex;
		var address = frm.address.value;
		var zip = frm.zip.value;
		
		if( name == '' )
			msg += 'Please enter the Card Holder Name\n';
		if( m == 0 || y == 0 )
			msg += 'Please enter a valid expiration date\n';
		if( address == '' || zip == '' )
			msg += 'Please enter a valid address and zip code\n';
		if( checkCC( cc ) === false )
			msg += 'Please enter a valid credit card number\n';
			
		if( msg.length > 0 ){
			alert( msg );
			return false;
		}
		else
			return true;
}


function checkCode(  ){
	var code = document.getElementById( 'code' ).value;
	
	if( code != ""){
	
		var pf = new PassFail2();
		pf.setFailID( 'badcode' );
		pf.setPassID( 'goodcode');
	
		var async = new Async();
		async.setURI( 'actions/check_code.php?code='+code );
		async.setProcessor( pf );
		async.loadResponse();
	

	}
	else
		document.getElementById('codechecked').value = 1;
}

function pass(text){
	document.getElementById('passmsg').innerHTML = text.substring(1);
	var level = text.substring(0,1);
	document.getElementById('levellimit').value = level;
	
	var dollar = text.indexOf("$");
	var price = text.substring( dollar +1);
	
	var name = text.substring(1,dollar);
	document.getElementById('levelname').value = name;

	

	document.getElementById('giftvalue').value = price;
	calculateTotal();
	document.getElementById('goodcode').value = 1;
	document.getElementById('codechecked').value = 1;
}

function fail( text ){
	document.getElementById('codechecked').value = 1;
	return false;
}

function calculateTotal(  ){
	
	var price = 0;
	var levels = document.getElementsByName('level');
	var choice = '';
	for( i=0 ; i<levels.length ; i++)
		if( levels[i].checked == true )
			choice = levels[i].value;
	var price = document.getElementById( 'price1'+choice );	
	var gift = document.getElementById( 'giftvalue' ).value;
	var total = (price.value) - gift;
	document.getElementById('totalcharge').innerHTML = '$' + CurrencyFormatted( total );

	return total;
}


function toggleCoupon( chk ){
	if(chk.checked == true){
		document.getElementById('purchasetype').value='coupon';
		document.getElementById('giftvalue').value=0;
		DIV.close('normal');
		DIV.open('couponpurchase');
		DIV.hide('couponbox1');
		DIV.hide('couponbox2');
	}
	else{
		document.getElementById('purchasetype').value='individual';
		DIV.close('couponpurchase');
		DIV.open('normal');
		DIV.show('couponbox1');
		DIV.show('couponbox2');
		if( document.getElementById('code').value != '')
			checkCode();
	}
	calculateTotal();
}
function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
// end of function CurrencyFormatted()

function checkCC(s) {

  var i, n, c, r, t;

  // First, reverse the string and remove any non-numeric characters.

  r = "";
  for (i = 0; i < s.length; i++) {
    c = parseInt(s.charAt(i), 10);
    if (c >= 0 && c <= 9)
      r = c + r;
  }

  // Check for a bad string.

  if (r.length <= 1)
    return false;

  // Now run through each single digit to create a new string. Even digits
  // are multiplied by two, odd digits are left alone.

  t = "";
  for (i = 0; i < r.length; i++) {
    c = parseInt(r.charAt(i), 10);
    if (i % 2 != 0)
      c *= 2;
    t = t + c;
  }

  // Finally, add up all the single digits in this string.

  n = 0;
  for (i = 0; i < t.length; i++) {
    c = parseInt(t.charAt(i), 10);
    n = n + c;
  }

  // If the resulting sum is an even multiple of ten (but not zero), the
  // card number is good.

  if (n != 0 && n % 10 == 0)
    return true;
  else
    return false;
}
