﻿ // check positive numeric must be integer
 function isPosNumWOD(str) {
	for (var j=0; j<str.length; j++) {
		var temp = str.charAt(j);
        if (!((temp=='0') || (temp=='1') || (temp=='2') || (temp=='3') || (temp=='4') || (temp=='5') || (temp=='6') || (temp=='7') || (temp=='8') || (temp=='9') )){
			return false;
            break;
        }
        // if j = last term, it means true - all char are digits!! Great
        if (j == str.length-1)
			return true;
	}
 }

 //display error message ('field name', 'err msg')
 function error(elem, text) {
	if (errfound) return;
	window.alert (text);
	elem.focus();
	errfound=true;
 }

 function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');

	if(isNaN(num))
		num = "0";

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();

	if(cents<10)
		cents = "0" + cents;

	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));

	return (((sign)?'':'-') + num + '.' + cents);
}

NoOfProductID = 10;

var ProductID = new Array();
ProductID[-1] = -1;
ProductID[0] = 0;
ProductID[1] = 1;
ProductID[2] = 2;
ProductID[3] = 3;
ProductID[4] = 4;
ProductID[5] = 5;
ProductID[6] = 6;
ProductID[7] = 7;
ProductID[8] = 8;
ProductID[9] = 9;

var ProductName = new Array();
ProductName[-1] = '';
ProductName[0] = 'Bar & Lounge';
ProductName[1] = 'Chinese Restaurant';
ProductName[2] = 'Discotheque';
ProductName[3] = 'Hairdressing & Beauty Salon';
ProductName[4] = 'Hotel & Guesthouse';
ProductName[5] = 'Karaoke';
ProductName[6] = 'Karaoke (Secondary Entertainment)';
ProductName[7] = 'Night Club, Ballroom & Dancing Hall';
ProductName[8] = 'Restaurant (Except Chinese Restaurant)';
ProductName[9] = 'Retail Store';

var MinPrice = new Array();
MinPrice[-1] = 0;
MinPrice[0] = 4500;
MinPrice[1] = 3000;
MinPrice[2] = 0;
MinPrice[3] = 1200;
MinPrice[4] = 9000;
MinPrice[5] = 0;
MinPrice[6] = 0;
MinPrice[7] = 1700;
MinPrice[8] = 1500;
MinPrice[9] = 980;

var SeatPrice = new Array();
SeatPrice[-1] = 0;
SeatPrice[0] = 160;
SeatPrice[1] = 99999;
SeatPrice[2] = 99999;
SeatPrice[3] = 120;
SeatPrice[4] = 0;
SeatPrice[5] = 81;
SeatPrice[6] = 0;
SeatPrice[7] = 90;
SeatPrice[8] = 90;
SeatPrice[9] = 0;

var SquarePrice = new Array();
SquarePrice[-1] = 0;
SquarePrice[0] = 0;
SquarePrice[1] = 0;
SquarePrice[2] = 0;
SquarePrice[3] = 0;
SquarePrice[4] = 0;
SquarePrice[5] = 0;
SquarePrice[6] = 0;
SquarePrice[7] = 0;
SquarePrice[8] = 0;
SquarePrice[9] = 2;

function appendOptionLast(inElement, inText, inValue, inSelect)
{
    var elOptNew = document.createElement('option');
    elOptNew.text = inText;
    elOptNew.value = inValue;
    elOptNew.selected = inSelect;
    var elSel = document.getElementById(inElement);

    try {
        elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
    }
    catch(ex) {
        elSel.add(elOptNew); // IE only
    }
}

function LoadProduct()
{
	appendOptionLast('product', '-- Select One --', -1, true);

    for (i=0; i<NoOfProductID; i++) {
        appendOptionLast('product', ProductName[i], ProductID[i], false);
    }
}

function FirstLoad()
{
	LoadTariff();
	LoadProduct();
	Clear();
}

function LoadTariff()
{
	$('#BT0').text(ProductName[0]);
	$('#BT1').text(ProductName[1]);
	$('#BT2').text(ProductName[2]);
	$('#BT3').text(ProductName[3]);
	$('#BT4').text(ProductName[4]);
	$('#BT5').text(ProductName[5]);
	$('#BT6').text(ProductName[6]);
	$('#BT7').text(ProductName[7]);
	$('#BT8').text(ProductName[8]);
	$('#BT9').text(ProductName[9]);
}

function Clear()
{
	// Clear All the result
	$('#license-fee').text('---');
	document.aspnetForm.area.value='';
	document.aspnetForm.screen.value='';
	document.aspnetForm.person.value='';
	document.aspnetForm.room.value='';
}

function ClientSelected()
{
	Clear();

	selectProductID = document.aspnetForm.product.value;

	if (SquarePrice[selectProductID]==0) {
		document.getElementById('showArea').style.display = 'none';
	} else {
		document.getElementById('showArea').style.display = 'block';
	}

	if (SeatPrice[selectProductID]==0) {
		document.getElementById('showPerson').style.display = 'none';
	} else {
		document.getElementById('showPerson').style.display = 'block';
	}
	
	if (selectProductID==5 || selectProductID==6) {
		document.getElementById('showScreen').style.display = 'block';
	} else {
		document.getElementById('showScreen').style.display = 'none';
	}
	
	if (selectProductID==4) {
		document.getElementById('showRoom').style.display = 'block';
	} else {
		document.getElementById('showRoom').style.display = 'none';
	}
	
	setFocusWithTimer();
}

function setFocusWithTimer()
{
	setTimeout('setFocus()',1);
}

function setFocus()
{
	showButton = false;
	
	if (document.getElementById('showArea').style.display == 'block' && !showButton) {
		document.aspnetForm.area.focus();
		showButton = true;
	}
	
	if (document.getElementById('showScreen').style.display == 'block' && !showButton) {
		document.aspnetForm.screen.focus();
		showButton = true;
	}
	
	if (document.getElementById('showRoom').style.display == 'block' && !showButton) {
		document.aspnetForm.room.focus();
		showButton = true;
	}
	
	if (document.getElementById('showPerson').style.display == 'block' && !showButton) {
		if (!showButton) {
			document.aspnetForm.person.focus();
			showButton = true;
		}
	}
	
	if (showButton) {
		document.getElementById('submit').style.display = 'block';
	} else {
		document.getElementById('submit').style.display = 'none';
	}
	
}

function Calulate()
{
	if (Validate()) {
		selectProductID = document.aspnetForm.product.value;
		minProductPrice = MinPrice[selectProductID];
		
		if (document.aspnetForm.area.value.length==0) {
			AreaVal = 0;
		} else {
			AreaVal = parseInt(document.aspnetForm.area.value);
		}
		
		if (document.aspnetForm.person.value.length==0) {
			PersonVal = 0;
		} else {
			PersonVal = parseInt(document.aspnetForm.person.value);
		}
		
		if (document.aspnetForm.room.value.length==0) {
			RoomVal = 0;
		} else {
			RoomVal = parseInt(document.aspnetForm.room.value);
		}
		
		if (document.aspnetForm.screen.value.length==0) {
			ScreenVal = 0;
		} else {
			ScreenVal = parseInt(document.aspnetForm.screen.value);
		}

		ttlVal = 0;
		ttlVal = ttlVal + (SquarePrice[selectProductID] * AreaVal);

		if (selectProductID==1) {
			PersonPrice_1_50 = 60;
			PersonPrice_51_100 = 48;
			PersonPrice_101_200 = 36;
			PersonPrice_200up = 12;

			Person50 = PersonPrice_1_50 * 50;
			Person100 = Person50 + (PersonPrice_51_100 * 50);
			Person200 = Person100 + (PersonPrice_101_200 * 100);

			if (PersonVal>200) {
				ttlVal = ttlVal + (PersonPrice_200up * (PersonVal-200)) + Person200;
			} else if (PersonVal>100) {
				ttlVal = ttlVal + (PersonPrice_101_200 * (PersonVal-100)) + Person100;
			} else if (PersonVal>50) {
				ttlVal = ttlVal + (PersonPrice_51_100 * (PersonVal-50)) + Person50;
			} else {
				ttlVal = ttlVal + (PersonPrice_1_50 * PersonVal);
			}
		} else if (selectProductID==2) {
			PersonPrice_1_50 = 68000;
			PersonPrice_51_100 = 99000;
			PersonPrice_101_200 = 130000;
			PersonPrice_200up = 160000;
			
			if (PersonVal>200) {
				ttlVal = PersonPrice_200up;
			} else if (PersonVal>100) {
				ttlVal = PersonPrice_101_200;
			} else if (PersonVal>50) {
				ttlVal = PersonPrice_51_100;
			} else {
				ttlVal = PersonPrice_1_50;
			}
		} else if (selectProductID==4) {
			RoomPrice_1_500 = 90;
			RoomPrice_500up = 50;
			
			Room500 = (RoomPrice_1_500 * 500);
			
			if (RoomVal>500) {
				ttlVal = (RoomPrice_500up * (RoomVal-500)) + Room500;
			} else {
				ttlVal = RoomPrice_1_500 * RoomVal;
			}
		} else if (selectProductID==5) {
			ScreenPrice_1_10 = 3285;
			ScreenPrice_11_20 = 648;
			ScreenPrice_20up = 387;
			
			Screen10 = (ScreenPrice_1_10 * 10);
			Screen20 = (ScreenPrice_11_20 * 10) + Screen10;
			
			if (ScreenVal>20) {
				ttlVal = ((ScreenVal-20) * ScreenPrice_20up) + Screen20;
			} else if (ScreenVal>10) {
				ttlVal = ((ScreenVal-10) * ScreenPrice_11_20) + Screen10;
			} else {
				ttlVal = ScreenPrice_1_10 * ScreenVal;
			}
			
			ttlVal = ttlVal + (SeatPrice[selectProductID] * PersonVal);
		} else if (selectProductID==6) {
			ScreenPrice_1_10 = 3285;
			ScreenPrice_11_20 = 648;
			ScreenPrice_20up = 387;
			
			Screen10 = (ScreenPrice_1_10 * 10);
			Screen20 = (ScreenPrice_11_20 * 10) + Screen10;
			
			if (ScreenVal>20) {
				ttlVal = ((ScreenVal-20) * ScreenPrice_20up) + Screen20;
			} else if (ScreenVal>10) {
				ttlVal = ((ScreenVal-10) * ScreenPrice_11_20) + Screen10;
			} else {
				ttlVal = ScreenPrice_1_10 * ScreenVal;
			}
		} else {
			ttlVal = ttlVal + (SeatPrice[selectProductID] * PersonVal);
		}
		
		if (selectProductID==9) {
			ScreenPrice_1_10 = 3285;
			ScreenPrice_11_20 = 648;
			ScreenPrice_20up = 387;
			
			Screen10 = ScreenPrice_1_10 * 10;
			Screen20 = Screen10 + (ScreenPrice_11_20 * 10);
			
			if (ScreenVal>20) {
				ttlVal = ttlVal + (ScreenPrice_20up * (ScreenVal-20)) + Screen20;
			} else if (ScreenVal>10) {
				ttlVal = ttlVal + (ScreenPrice_11_20 * (ScreenVal-10)) + Screen10;
			} else {
				ttlVal = ttlVal + (ScreenPrice_1_10 * ScreenVal);
			}
		}

		if (ttlVal<minProductPrice)
			ttlVal = minProductPrice;

		$('#license-fee').text(formatCurrency(ttlVal));
	}
	
	setFocusWithTimer();
}

function Validate()
{
	errfound= false;
	
	if (document.getElementById('showArea').style.display == 'block')
		if (document.aspnetForm.area.value.length==0) {
			error(document.aspnetForm.area,"Please input the value");
		} else if (!isPosNumWOD(document.aspnetForm.area.value)) {
			error(document.aspnetForm.area,"No. of Area must be a positive integer");
		}
		
	if (document.getElementById('showScreen').style.display == 'block')
		if (document.aspnetForm.screen.value.length==0) {
			error(document.aspnetForm.screen,"Please input the value");
		} else if (!isPosNumWOD(document.aspnetForm.screen.value)) {
			error(document.aspnetForm.screen,"No. of Screen must be a positive integer");
		}

	if (document.getElementById('showRoom').style.display == 'block')
		if (document.aspnetForm.room.value.length==0) {
			error(document.aspnetForm.room,"Please input the value");
		} else if (!isPosNumWOD(document.aspnetForm.room.value)) {
			error(document.aspnetForm.room,"No. of Room must be a positive integer");
		}
		
	if (document.getElementById('showPerson').style.display == 'block')
		if (document.aspnetForm.person.value.length==0) {
			error(document.aspnetForm.person,"Please input the value");
		} else if (!isPosNumWOD(document.aspnetForm.person.value)) {
			error(document.aspnetForm.person,"No. of Person must be a positive integer");
		}
	
	return !errfound;
}

function KeyDown(event,actionType)
{
	if (event.keyCode == 13)
    {
		calStatus = false;
		
		if (actionType==0) {
			if (document.getElementById('showPerson').style.display == 'block') {
				document.aspnetForm.person.focus();
			} else {
				Calulate();
				document.aspnetForm.area.focus();
			}
		} else if (actionType==1) {
			Calulate();
			document.aspnetForm.person.focus();
		} else if (actionType==2) {
			if (document.getElementById('showPerson').style.display == 'block') {
				document.aspnetForm.person.focus();
			} else {
				Calulate();
				document.aspnetForm.screen.focus();
			}
		} else if (actionType==3) {
			Calulate();
			document.aspnetForm.room.focus();
		}
	}
}

function selectBusinessType(actionType)
{
	var selObj = document.getElementById('product');
	selObj.selectedIndex = actionType+1;
	
	ClientSelected();
}

FirstLoad();