
	function fn_gethop(hop,aff){
		var a = "affiliate";
		var h = "vendor";
		if(!aff) aff = a;
		if(!hop) hop = h;


		var v = 'http://'+aff+'.'+hop+'.hop.clickbank.net';

		return v;
		// return 'http://hop.clickbank.net/?'+aff+'/'+hop;
	}
	function GenHopLink(){
		var calc = $('hoplinkform');
		calc.hoplink.value = fn_gethop(calc.hop.value, calc.aff.value);
		calc.hoplink.select();
	}
	function trim(str){
		return str.replace(/^\s*|\s*$/g,"");
	}
	function GenSalesLink(){
		var calc = $('saleslinkform');

		var link = 'http://www.clickbank.net/sell.cgi?';
		var vend = calc.id.value;
		// var prod = calc.name.value;
		var num  = calc.number.value;
		vend = trim(vend); 
		// prod = trim(prod); prod = prod.replace(' ','_');
		num  = trim(num); 
		if(!num)  num  = "1";
		if(!vend) vend = "vendorID";

		var link = 'http://';
		link += num+'.';
		link += vend+'.';
		link += 'pay.clickbank.net';


		// http://ITEM.VENDOR.pay.clickbank.net 

		calc.link.value = link;
		calc.link.select();
	}

	function linktitle(){
		var calc = $('saleslinkform');
		var vend = calc.id.value;
		// var prod = calc.name.value;
		// prod = trim(prod); 
		vend = trim(vend); 
		// if(!prod) calc.name.value = vend;
	}

	function roundNumber(num, dec) {
		var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
		return result;
	}
	function calcRefund(){

		var calc = $('refundform');
		var st = [];
		var a = [];
		a.price = calc.price.value;
		a.pct = calc.pct.value;
		a.pac = calc.pac.value;

		if(a.price > 0){
			st.fees = 1 + (.075*a.price);
			st.netprice = a.price - st.fees;
			st.mc = st.netprice;
		}
		if(a.pct > 0 && st.netprice > 0){
			st.ac  = (((st.netprice*(a.pct/100)))*100)/100;
			st.mc  = ((st.netprice-st.ac)*100)/100;
		}

		if(a.pac > 0){
			st.refund = '';
			if(a.price > 0){
				st.refund = 0;
			}
			if(st.netprice > 0 && st.ac > 0){
				st.remaining = st.ac - a.pac;
				if(a.pac > 0){
					st.refund = ((st.remaining / st.ac) *100);
				}
			}
			if(st.refund < 0){
				st.refund = '';
			}
		}

		st.refund = roundNumber(st.refund,2);
		st.ac = roundNumber(st.ac,2);
		st.mc = roundNumber(st.mc,2);
		st.fees = roundNumber(st.fees,2);
		st.netprice = roundNumber(st.netprice,2);

		calc.refund.value = st.refund;
		calc.fees.value = st.fees;
		calc.ac.value = st.ac;
	}




	function cbcalculate(){
		var calc = $('payform');
		var com = calc.com.value/100;
		var saleprice = calc.saleprice.value;
		var clickBankCut = (saleprice*0.075)+1;
		var salepriceRem = saleprice - clickBankCut;
		var affComm = toDollarsAndCents(salepriceRem * com, true);
		var mercComm = salepriceRem - affComm;
		if(isNaN(calc.saleprice.value)){
			alert('Product Price is required');
			calc.saleprice.focus();
		}else if(isNaN(calc.com.value)){
			alert('Commission Rate is required');
			calc.com.focus();
		}else{
			calc.aff.value = affComm;
			calc.merc.value = toDollarsAndCents(mercComm,false);
		}
	}




	function cbreverse() {
		var calc = $('revpayform');
		var ac = calc.ac.value;
		var rt = calc.rt.value;
		if(isNaN(calc.ac.value)){
			alert('Affiliate Earned is required');
			calc.ac.focus();
		}else if(isNaN(calc.rt.value)){
			alert('Commission Rate is required');
			calc.rt.focus();
		}else{
			var mc = ac * (1 / (rt/100));
			var sp = (mc + 1)* 1.081098616;
			sp = Math.round(sp*Math.pow(10,2))/Math.pow(10,2);
			calc.price.value = sp;
		}
	}
	function toDollarsAndCents(n, round) {
		var s="" +n;
		if(round){
			s = "" + Math.round(n * 100) / 100;
		}
		var i = s.indexOf('.');
		if (i < 0) return s + ".00";
		var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3);
		if (i + 2 == s.length) t += "0";
		return t;
	}

