// JavaScript Document
//  Copy right 2003 , Bosco ITS, Yellagiri Hills - 635 855 South India. 
//  	made by Loganathan N MCA* <logudotcom@yahoo.com> on 29, September 2003 2:30 PM 

// product 1

function product1(txtCtrl) {
	Remove_Spaces(txtCtrl);
	NumberValidate(txtCtrl);

	var txtValue = parseInt(txtCtrl.value);
	
	var total = (txtValue * (9.00 + 1.00));
	
	document.frmshop.tot1.value = formatFloat(total);

	CalculateTotal();
}

// product 2

function product2(txtCtrl) {
	Remove_Spaces(txtCtrl);
	NumberValidate(txtCtrl);

	var txtValue = parseInt(txtCtrl.value);
	
	var total = (txtValue * (14.00 + 1.00));
	
	document.frmshop.tot2.value = formatFloat(total);

	CalculateTotal();
}

// product 3
function product3(txtCtrl) {
	Remove_Spaces(txtCtrl);
	NumberValidate(txtCtrl);

	var txtValue = parseInt(txtCtrl.value);
	
	var total = (txtValue * (17.00 + 1.00));
	
	document.frmshop.tot3.value = formatFloat(total);

	CalculateTotal();
}


// product 4 
function product4(txtCtrl) {
	Remove_Spaces(txtCtrl);
	NumberValidate(txtCtrl);

	var txtValue = parseInt(txtCtrl.value);
	
	var total = (txtValue * (11.00 + 1.00));
	
	document.frmshop.tot4.value = formatFloat(total);

	CalculateTotal();
}

// product 5
function product5(txtCtrl) {
	Remove_Spaces(txtCtrl);
	NumberValidate(txtCtrl);

	var txtValue = parseInt(txtCtrl.value);
	
	var total = (txtValue * (3.50 + 0.50));
	
	document.frmshop.tot5.value = formatFloat(total);

	CalculateTotal();
}

// ******************************COMMON FUNCTIONS********************************************************

// validate text
function TextValidate(txtCtrl,val) {	
	var ctrName = val;				
	if (txtCtrl.value == "") {		
		alert("Please enter " + ctrName + ".");		
		txtCtrl.focus();   	
		return false;		
	}
	
	if(txtCtrl.value.indexOf("'") > -1){		
		alert("Please Don't enter ' in " + ctrName + ".");		
		txtCtrl.focus(); 
		txtCtrl.select();  	
		return false;		
	}			
}

// validate text area 
function TextAreaValidate(txtCtrl, val) {
	var ctrName	= val;
	var str = txtCtrl.value;	
	if(str.length < 10){
		alert("Please enter " + ctrName + " (minimum 10 chars).");
		txtCtrl.focus();	   	
		txtCtrl.select();
		return false;
	}
}

// validate number
function NumberValidate(txtCtrl) {
	var txtCtrlchk = txtCtrl.value.length;	
	Remove_Spaces(txtCtrl);
	if (txtCtrlchk == 0) {
 		return false;
	}
	if (txtCtrl.value == "" || isNaN(txtCtrl.value)) {
		alert("Please enter number");
		txtCtrl.focus();
		txtCtrl.select();	   	
		return false;
	}
	
}

// Not req, but check format - use onblur
function EmailValidateCheck(txtCtrl) {
	var xlogus = txtCtrl.value.length;
	if (xlogus == 0) {
 		return false;
	}
	if (txtCtrl.value.indexOf("@") < 2 ||txtCtrl.value.indexOf(".") < 0) {
		alert("Please enter a valid email id.\t\n your-name@domain-name"); 	
		txtCtrl.focus();
		txtCtrl.select();
		return false;
	}		

}

// to remove spaces
function Remove_Spaces(txtCtrl){
  txtCtrl.value = txtCtrl.value.replace(/\r/g, " ");

  txtCtrl.value = txtCtrl.value.replace(/[^ A-Za-z0-9`~!@#\$%\^&\*\(\)-_=\+\\\|\]\[\}\{'";:\?\/\.>,<]/g, "");

  txtCtrl.value = txtCtrl.value.replace(/'/g, "");

  txtCtrl.value = txtCtrl.value.replace(/ +/g, " ");

  txtCtrl.value = txtCtrl.value.replace(/^\s/g, "");

  txtCtrl.value = txtCtrl.value.replace(/\s$/g, "");
  
  if (txtCtrl.value == ' '){	
	 txtCtrl.value = '';
   } 
 } 
 
// Open popup window
function popupWindow(url) {
	window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=650,height=500,screenX=150,screenY=150,top=150,left=150')
}

function LGShowDialog(pagePath, args, width, height) {	
	return showModalDialog(pagePath, args, "dialogWidth:" + width + "px;dialogHeight:" + height + "px;help:no;scroll:no;status:no");
}

//  float calculation e-g 12.0 to 12.00
function CalculateTotal() {
	var subtot1 = 0;
	var subtot2 = 0;	
	var subtot3 = 0;
	var subtot4 = 0;	
	var subtot5 = 0;
	var grandTotal = 0;
	
	subtot1 = document.frmshop.tot1.value;	
	subtot2 = document.frmshop.tot2.value;
	subtot3 = document.frmshop.tot3.value;	
	subtot4 = document.frmshop.tot4.value;
	subtot5 = document.frmshop.tot5.value;	
	
	
	if (subtot1 != "") {
		subtot1 = subtot1;
	} else {
		subtot1 = 0.00;
	}

	if (subtot2 != "") {
		subtot2 = subtot2;
	} else {
		subtot2 = 0.00;
	}
	
	if (subtot3 != "") {
		subtot3 = subtot3;
	} else {
		subtot3 = 0.00;
	}

	if (subtot4 != "") {
		subtot4 = subtot4;
	} else {
		subtot4 = 0.00;
	}
	
	if (subtot5 != "") {
		subtot5 = subtot5;
	} else {
		subtot5 = 0.00;
	}	
	grandTotal = parseFloat(subtot1) + parseFloat(subtot2) + parseFloat(subtot3) + parseFloat(subtot4) + parseFloat(subtot5);	
	document.frmshop.totend.value = formatFloat(grandTotal);
}

// Format Float value (e-g 23, 0 => 23, 00)
function formatFloat(txtVal) {	
	var getValue = txtVal;
	var formatedValue;
	var valCeil = Math.floor(getValue);
	var valDecimal = Math.round((getValue - valCeil) * 100);
	if (valDecimal < 10 && valDecimal != 0) {
		valDecimal = valDecimal * 10;
	}
	if (valDecimal == '0') {
		valDecimal = valDecimal+"0";
	}
	formatedValue = valCeil + "." + valDecimal;
	return formatedValue;
}
// Comments & Thanks to LOGANATHAN N ************************************************************************