function WinCenter(wfile, wname, ww, wh) {
var maxwidth, maxheight, xpos, ypos;
maxwidth 	= screen.availWidth;
maxheight	= screen.availHeight;
xpos = maxwidth/2 - ww/2;
ypos = maxheight/2 - wh/2 -20;
return open(wfile, wname,"left="+xpos +",top="+ypos +",width="+ww +",height="+wh +",resizable=1,scrollbars=1,menubar=no, location=no,toolbar=no,directories=no,status=no,copyhistory=no'");
}
function PopupStory(wfile, wname, ww, wh) {
var maxwidth, maxheight, xpos, ypos;
maxwidth 	= screen.availWidth;
maxheight	= screen.availHeight;
xpos = maxwidth/2 - ww/2;
ypos = maxheight/2 - wh/2 -20;
return open(wfile, wname,"left="+xpos +",top="+ypos +",width="+ww +",height="+wh +",resizable=1,scrollbars=1,menubar=no, location=no,toolbar=no,directories=no,status=yes,copyhistory=no'");
}
function HelpFile(filename){WinCenter(filename,"",768,630);}
function PrintFrame(frameName){ frames[frameName].focus(); frames[frameName].print(); }
function TermofUse(filename){ WinCenter(filename,"",300,450); }
function ForgotPwd(filename){ WinCenter(filename,"",380,270); }

function CalPrice(packagename,user,catCount){
	var divprice = document.getElementById('divPricePerUser');
	if (document.getElementById('rdoMonth_0').checked==true){
		var Month = document.getElementById('rdoMonth_0').value  ;
	}else if (document.getElementById('rdoMonth_1').checked==true){
		var Month = document.getElementById('rdoMonth_1').value  ;
	}else if (document.getElementById('rdoMonth_2').checked==true){
		var Month = document.getElementById('rdoMonth_2').value  ;
	}else if (document.getElementById('rdoMonth_3').checked==true){
		var Month = document.getElementById('rdoMonth_3').value  ;
	}
	var x=document.getElementById('TablePrice').cells
	var strUser ='' ; var strPrice ='' ; var strPriceTotal='' ; var strCategoryCount='' ; var strMonth='' ;
	var fPrice = GetFirstPersonPrice(packagename, Month);
	var oPrice = GetSecondPersonPrice(packagename, Month);
	
	var isEng ;
	if (location.href.indexOf("/en/") >= 0){ isEng = true; }else{ isEng = false; }
	
	var price ;
	
	for (i = 1; i <= user; i++){
		if (i==1){
			price = fPrice ;
			if (isEng == true){
				strUser = strUser + "&nbsp;"+ i +"<sup>st</sup>&nbsp;user<br>" ;
			}else{
				strUser = strUser + "&nbsp;ผู้ใช้งานท่านแรก<sup>&nbsp;</sup><br>" ;
			}
        }else{
			price = oPrice ;
			if (isEng == true){
				if ((i % 100 == 11) || (i % 100 == 12) || (i % 100 == 13)){//?11, ?12, ?13
					strOrdinal = "th";
				}else if (i % 10 == 1){//First
					strOrdinal = "st";
				}else if (i % 10 == 2){//Second
					strOrdinal = "nd";
				}else if (i % 10 == 3){//Third
					strOrdinal = "rd";
				}else{//other..
					strOrdinal = "th";
				}
                strUser = strUser + "&nbsp;" + i + "<sup>" + strOrdinal + "</sup>&nbsp;user<br>"

			}else{
				strUser = strUser + "&nbsp;ผู้ใช้งานท่านที่ " + i + "<sup>&nbsp;</sup><br>";
			}
        }
		strPrice = strPrice + FormatCurrency(price) + "<sup>&nbsp;</sup><br>";
		strPriceTotal = strPriceTotal + FormatCurrency((price * catCount * Month)) + "<sup>&nbsp;</sup>&nbsp;<br>";
		strCategoryCount = strCategoryCount + catCount + "<sup>&nbsp;</sup><br>";
		strMonth = strMonth + Month + "<sup>&nbsp;</sup><br>";
	}
	
	x[5].innerHTML = strUser;
	x[6].innerHTML = strCategoryCount;
	x[7].innerHTML = strPrice;
	x[8].innerHTML = strMonth;
	x[9].innerHTML = strPriceTotal;	
	
	var Discount = GetDiscount(packagename, Month);
	var dPriceAll  = (fPrice * catCount * Month) + (oPrice * catCount * Month * (user - 1));
	var dDiscount  = (dPriceAll * Discount) / 100;
	var dTotal = dPriceAll - dDiscount;
	var dVat  = (dTotal * 7) / 100;
	var dNet  = dTotal + dVat;

	document.getElementById('lblPriceAll').innerText = FormatCurrency(dPriceAll);
	document.getElementById('lblDiscount').innerText = FormatCurrency(dDiscount);
	document.getElementById('lblTotal').innerText = FormatCurrency(dTotal);
	document.getElementById('lblVat').innerText = FormatCurrency(dVat);
	document.getElementById('lblNet').innerText = FormatCurrency(dNet);
}
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);
}
function UpdateCategory(obj,i,pages){
	var iSelected = document.getElementById('rptCategory__ctl'+i+'_lblTopicSelected').innerText;
	if (obj.checked){
		iSelected++;
		document.getElementById('rptCategory__ctl'+i+'_lblTopicSelected').innerText = iSelected;
	}else{
		iSelected--;
		document.getElementById('rptCategory__ctl'+i+'_lblTopicSelected').innerText = iSelected;
	}
	if (pages != 0) { DisableBtnPrice(); }
}
function uncheckAll(i,count){
	document.getElementById('rptCategory__ctl'+i+'_lblTopicSelected').innerText = 0;
	for (i = 0; i < count; i++)
		document.getElementById('rptTopics__ctl'+i+'_chkTopic').checked = false ;
		//document.getElementById('rptTopics:_ctl'+i+':chkTopic').checked = false ;
}
function ReCheck(pages){
	RequestGetResultFirstCat(pages);
	if (pages != 'category') { DisableBtnPrice(); }
}
function DisableBtnPrice(){		
	var cat = document.getElementById('hdnCatCount').value ;
	var iChecked = 0 ;
	var iCat;
	for (var ii = 0; ii < cat; ii++){
		iCat = document.getElementById('rptCategory__ctl'+ii+'_lblTopicSelected').innerText ;
		iChecked += parseInt(iCat);
	}
	if (iChecked > 0) {
		document.getElementById('btnCalPrice').disabled=false;
	}else{
		document.getElementById('btnCalPrice').disabled=true;
	}
} 
function setCookie(c_name,value,days)
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = c_name+"="+ value + expires +"; path=/";
} 
