/* pricing.js */

//global variables
print_package='';
print_quantity='';
print_mailing='';
print_pdf=false;

email_recipients='';

articles_employees='';
articles_categories=Array();
articles_payment='';

function print_set_package() {
	print_package=document.getElementById('printpackage').value; //set the global var
	print_update_pricing(); //update it in the browser
}
function print_set_quantity() {
	var fieldvalue=document.getElementById('printquantity').value;
	print_quantity=document.getElementById('printquantity').value=(isNaN(parseInt(fieldvalue)) ? '' : parseInt(fieldvalue)); //set the global var
	print_update_pricing(); //update it in the browser
}
function print_set_mailing() {
	print_mailing=document.getElementById('printmailing').value; //set the global var
	print_update_pricing(); //update it in the browser
}
function print_set_pdf() {
	print_pdf=document.getElementById('printpdf').checked; //set the global var
	print_update_pricing(); //update it in the browser
}
function print_update_pricing() {
	//fill the selection fields
	document.getElementById('print_package_selection').innerHTML=print_package;	
	
	if (print_pdf==true) {
		if (print_package=='Premium') {
			var pdf=75;
		} else {
			var pdf=50;
		}
		document.getElementById('print_pdf_selection').innerHTML=formatCurrency(pdf);
		
	} else {
		var pdf=0;
		document.getElementById('print_pdf_selection').innerHTML='';
	}
	
	var priceeach=0,mailpriceeach=0;
	//determine the total
	if (!isNaN(print_quantity) && print_quantity>0) {
		//make sure we have the minimum order
		if (print_package=='Basic' && print_quantity<50) {
			alert("The minimum order for this package is 50 copies.");
			document.getElementById('printquantity').focus();
			document.getElementById('print_total').innerHTML='';
			document.getElementById('print_quantity_selection').innerHTML='';
			return;
		} else if ((print_package=='Premium' || print_package=='Select') 
				&& print_quantity<100) {
			alert("The minimum order for this package is 100 copies.");
			document.getElementById('printquantity').focus();
			document.getElementById('print_total').innerHTML='';
			document.getElementById('print_quantity_selection').innerHTML='';			
			return;
		} else if ((print_mailing=='1st Class' || print_mailing=='Standard') 
			&& print_quantity<200) {
			alert("The minimum order eligible for mailing services is 200 copies.");
			
			document.getElementById('print_total').innerHTML='';
			document.getElementById('print_quantity_selection').innerHTML='';			
			document.getElementById('printquantity').focus();
			return;
		}

		
		for (var i=0; i<printpricecount; i++) {
			if (print_quantity>=printqtyminlist[i] && print_quantity<=printqtymaxlist[i]) {
				//bingo
				//which level?
				if (print_package=='Select')
					priceeach=printpricelistselect[i];
				else if (print_package=='Basic')
					priceeach=printpricelistbasic[i];
				else if (print_package=='Premium')
					priceeach=printpricelistpremium[i];
				else
					priceeach=0;
				break;
			}
		}
		
		//now figure the mailfees
		for (i=0; i<printmailfeespricecount; i++) {		
			if (print_quantity>=printmailfeesqtyminlist[i] && print_quantity<=printmailfeesqtymaxlist[i]) {
				//bingo
				
				//which level?
				if (print_mailing=='1st Class')
					mailpriceeach=printmailfeespricelistfirstclass[i];
				else if (print_mailing=='Standard')
					mailpriceeach=printmailfeespriceliststandard[i];
				else
					mailpriceeach=0;
//				alert(mailpriceeach);
				break;
			}
		}
		
		
	}
//alert(mailpriceeach);
	//show the ea price
	if (priceeach>0) {
		document.getElementById('print_quantity_selection').innerHTML=formatCurrency(priceeach)+'/ea';
	} else {
		document.getElementById('print_quantity_selection').innerHTML='';
	}
	if (mailpriceeach>0) {
		document.getElementById('print_mailing_selection').innerHTML=formatCurrency(mailpriceeach)+'/ea';
	} else {
		document.getElementById('print_mailing_selection').innerHTML='';
	}
	
	//calculate the price and set the div
	var price=(priceeach*print_quantity)+(mailpriceeach*print_quantity)+pdf;
	
	document.getElementById('print_total').innerHTML=formatCurrency(price);
}



function email_set_recipients() {
	email_recipients=document.getElementById('emailrecipients').value;
	email_update_pricing();	
}
function email_update_pricing() {	
	var price=0;
	//determine the total
	if (!isNaN(email_recipients) && email_recipients>0) {
		for (var i=0; i<emailpricecount; i++) {
			if (email_recipients>=emailqtyminlist[i] && email_recipients<=emailqtymaxlist[i]) {
				//bingo
				price=emailpricelist[i];
			}
		}
		
		//show the ea price
		document.getElementById('email_recipients_selection').innerHTML=formatCurrency((price/email_recipients))+" per recipient";
	} else {
		document.getElementById('email_recipients_selection').innerHTML='';	
	}
	
	
	document.getElementById('email_total').innerHTML=formatCurrency(price)+' per mailing';
	
}


function articles_set_employees() {

	articles_employees=document.getElementById('articlesemployees').value;
	/*switch(articles_employees) {
	case "0":
		articles_employees_text = "1-3";
		break;
	case "1":
		articles_employees_text = "4-10";
		break;
	case "2":
		articles_employees_text = "11-20";
		break;
	case "3":
		articles_employees_text = "20+";
		break;
	default:
		articles_employees_text = "";
	}*/
	articles_update_pricing(); 
}
function articles_set_categories() {
	articles_update_pricing();
}
function articles_set_payment() {
	articles_payment=document.getElementById('articlespayment').value; 
	articles_update_pricing(); 
}
function articles_update_pricing() {
	
	//document.getElementById('articles_employees_selection').innerHTML=articles_employees;
	document.getElementById('articles_payment_selection').innerHTML=articles_payment;	

	//handle visibility
	if (articles_payment=='Unlimited')
		showcatrow();
	else
		hidecatrow();
	
	var price=0,checkcount=0,priceeach=0;
	if (articles_payment=='Unlimited') {// && !isNaN(articles_employees)) {

		
		//see how many categories where selected
		for (var x=1; x<5; x++) {
			if (document.getElementById('categories['+x+']').checked==true) {
				checkcount++;
			}
		}
		
		if (checkcount>0) {
			//find a pricing bracket
			var bracket=parseInt(document.getElementById('articlesemployees').value);
			//alert(articlepricelist[bracket][checkcount]);
			
			price=articlepricelist[checkcount][bracket];
			priceeach=price/checkcount;
		}
			
		document.getElementById('articles_categories_selection').innerHTML=checkcount+' categories selected<br/>'+formatCurrency(priceeach)+"/yr each";
		document.getElementById('articles_total').innerHTML=formatCurrency(price)+'/year';
		
	} else if (articles_payment=='Pay as You Go') {
		//find price per article based on employees
		
		var bracket=parseInt(document.getElementById('articlesemployees').value);
		price=articlewordlist[bracket];
		
		document.getElementById('articles_total').innerHTML=formatCurrency(price)+"/article";
		return;
	} else {
		//invalid options combo, clear
			
		document.getElementById('articles_categories_selection').innerHTML='';
		document.getElementById('articles_total').innerHTML='';
	}
	
	
}



function showcatrow() {
	document.getElementById('catrow1').style.visibility='';
	document.getElementById('catrow2').style.visibility='';
}
function hidecatrow() {
	document.getElementById('catrow1').style.visibility='hidden';
	document.getElementById('catrow2').style.visibility='hidden';
}


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 isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

