ctype = 'none';
camount = 0;
slicprice = '';
vprice = 0;

function $(o) {
	return document.getElementById(o);
}

var base_url = "http://dlp.desuade.com/";

function disableGroup(formName, groupName, booleanDisabled) {
  for (var i=0; i<formName.elements.length; i++) {
	if (formName.elements[i].name == groupName) {
	  formName.elements[i].disabled = booleanDisabled;
	}
  }
}

function setslic(pr){
	slicprice = Number(pr);
	amvalue();
}

function removecp(){
	ctype = 'none';
	camount = 0;
	div = $('coupstatus');
	div.innerHTML = "<strong>Coupon removed<\/strong>";
	amvalue();
	$('os1').value = "";
}

function setlam(){
	if($('custom').value == 'V'){
		$('os0').value = $('vamt').value;
	} else {
		$('os0').value = 1;
	}
}

function ajap(url) {
	div = $('coupstatus');
	div.innerHTML = "<strong>Applying coupon...<\/strong>";
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = base_url + "core/testcoupon.php?pid=" + pid + "&coupon=" + $('os1').value + '&ltype=' + $('custom').value;
	document.getElementsByTagName("head")[0].appendChild (jsel);		
}
function amvalue(){
	var vt = Number(slicprice);
	if($('vamt') != null){
		var va = (Number($('vamt').value) >= 1 && $('custom').value == 'V') ? Number($('vamt').value) : 1;
	} else {
		var va = 1;
	}
	var num = 0;
	vt = vt + (Number(vprice) * (va-1));
	switch(ctype){
		case 'none':
			num = vt;
		break;
		case 'percent':
			num = vt-(vt*(Number(camount)/100));
		break;
		case 'dollar':
			num = vt-Number(camount);
		break;
	}
	if(num <= 0) num = Number(0.00);
	num = (parseFloat(num)).toFixed(2);
	$('amount').value = String(num);
	$('amt').innerHTML = String(num);
}
function tog(ro){
	if(ro){
		$('os1').setAttribute('readOnly', 'readonly');
		disableGroup($('form1'), 'licensegroup', true);
		$('applybtn').value = "Remove";
		$('applybtn').setAttribute('onclick', "removecp(); tog(false);");
	}else{
		$('os1').removeAttribute("readOnly");
		disableGroup($('form1'), 'licensegroup', false);
		$('applybtn').value = "Apply";
		$('applybtn').setAttribute('onclick', "ajap();");
	}
}

function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value==""||value=="0.00")
  {alert(alerttxt);return false}
else {return true}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(custom,"Please select a license by clicking the radio button to the left of the license title.")==false || validate_required(amount,"Please select a license by clicking the radio button to the left of the license title.")==false)
  {licensegroup.focus();return false}
}
}