// GLOBAL VARIABLES
var ns4 = document.layers
var ns6 = document.getElementById&&!document.all
var ie4 = document.all
var child;
var child2;

// SUBMIT FORM
function submitForm() 
{
    document.form.submit();
}

function ClosePopup() 
{
	window.opener.document.form.submit();
	window.close();
}

function checkHFldVals(){
	str = 'Action01=' + document.getElementById('Action01').value;
	str = str + ':H05=' + document.getElementById('H05').value;
	
	alert(str);
}

// GET DOCUMENT ELEMENT
function GetObject(id)
{
	// This function checks for DOM strategy, then 
	// returns an object reference.
	if (document.all)
	{
		return document.all[id];
	}
	else if(document.layers)
	{
		return document.layers[id];
	}
	else
	{
		return document.getElementById(id);
	}
}

function go_anchor(divname,objname)
{
	document.getElementById(divname).scrollTop = document.getElementById(objname).offsetTop;
}

function strTooLong(fldToTest,maxLength)
{
	if(fldToTest.value.length > maxLength)
	{
		fldToTest.value = fldToTest.value.substr(0,maxLength);
	}
}

function SetField(field, val)
{
	document.getElementById(field).value = val;
}

function SetInnerHtml(field, val)
{
	GetObject(field).innerHTML = val;
}

function selectPrdRow(id) {
	obj = document.getElementById(id);
	div = document.getElementById('orditem');
	y = getY(obj);
	obj.className = 'mopt_HLight02';
	div.scrollTop = y;

}

function getY(obj){
	return obj.offsetTop;
}

function sumComRevenue(){
	cash =  parseFloat(document.getElementById('cashrev').value);
	chq =  parseFloat(document.getElementById('chqrev').value);
	credit =  parseFloat(document.getElementById('credrev').value);
	totobj = document.getElementById('totrev');
	
	if (isNaN(cash)) {
		cash = 0;
	}
	if (isNaN(chq)) {
		chq = 0;
	}
	if (isNaN(credit)) {
		credit = 0;
	}
	tot = round_decimals((cash + chq + credit),2);
	totobj.value = tot;
}

function sumBookComm(){
	taken =   parseFloat(document.getElementById('bctaken').value);
	spent =   parseFloat(document.getElementById('bkordtot').value);
	totobj = document.getElementById('bkComm');

	totobj.value = round_decimals((taken + spent),2);
}

// SHOW/HIDE DIV & SPAN TAGS
function DisplayToggle(id) {

	var daObj = GetObject(id);
	if (daObj)
	{
		if (daObj.style.display == "none")
		{
			// display the object
			daObj.style.display = "";
			daObj.style.visibility = "visible";
		}
		else
		{
			// disable display
			daObj.style.display = "none";
			//daObj.style.visibility = "hidden";
			if (ns6)
			{
				daObj.style.visibility = "collapse";
			}
		}
	}
}


// SHOW/HIDE DIV & SPAN TAGS
function showHideContent(id) {

	var daObj = GetObject(id);
	if (daObj)
	{
		if (daObj.style.display == "none")
		{
			// display the object
			daObj.style.display = "";
			daObj.style.visibility = "visible";
		}
		else
		{
			// disable display
			daObj.style.display = "none";
			//daObj.style.visibility = "hidden";
			if (ns6)
			{
				daObj.style.visibility = "collapse";
			}
		}
	}
}

function changePrice(price,fldid)
{
	TotalAmount_Field = document.getElementById("total");
	TotalAmount = TotalAmount_Field.innerHTML;
	Qty = parseInt(document.getElementById("qty" + fldid).innerHTML);
	ItemTotal_Field = document.getElementById("tot" + fldid);
	ItemTotal = ItemTotal_Field.innerHTML;
	newTotal = 0.0;

	TotalAmount = TotalAmount - ItemTotal;

	if (Qty > 0) {
		newTotal = Qty * price;
	
		TotalAmount = TotalAmount + newTotal;
		TotalAmount_Field.innerHTML = round_decimals(TotalAmount,2);
		ItemTotal_Field.innerHTML = round_decimals(newTotal,2);
	} else {
		TotalAmount_Field.innerHTML = round_decimals(TotalAmount,2);
		ItemTotal_Field.innerHTML = 0.00;
	}
	
}

function addROItem(price,add,fldid) {

	TotalQty_Field = document.getElementById("qty");
	TotalAmount_Field = document.getElementById("total");
	Qty_Field = document.getElementById("qty" + fldid);
	ItemTotal_Field = document.getElementById("tot" + fldid);
	Summary_Field = document.getElementById("roitemSummary");
	
	xqty = 0.0;
	iqty = 0.0;
	xtot = 0.0;
	tqty = 0.0;

	if (TotalQty_Field.innerHTML != "&nbsp;") {
		tqty = parseInt(TotalQty_Field.innerHTML);
	}

	if (TotalAmount_Field.innerHTML != "&nbsp;") {
		xtot = round_decimals(TotalAmount_Field.innerHTML,2);
		}
	if (Qty_Field.innerHTML != "&nbsp;") {
		xqty = round_decimals(Qty_Field.innerHTML,0);
		}

	iqty = xqty;
	saletot = 0.0;

	saletot = ItemTotal_Field.innerHTML - xtot;
	
	if (add == true) {
		xqty ++;
		tqty ++;
		
	} else {
		if (xqty > 0) {
			xqty --;
			tqty --;
		}
	}

//	if (xqty > 0) {
		Summary_Field.value = SearchAndReplace(Summary_Field.value,fldid, ';',fldid + '_' + xqty + ';');
//	} else {
//		Summary_Field.value = SearchAndReplace(Summary_Field.value,fldid, ';','');
//	}

	TotalQty_Field.innerHTML = tqty;
	Qty_Field.innerHTML = xqty;
	xtot = xtot - iqty * price;
	ItemTotal_Field.innerHTML = round_decimals(xqty * price,2);
	xtot = xtot + xqty * price;
	TotalAmount_Field.innerHTML = round_decimals(xtot,2);

}



function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {
   var value_string = rounded_value.toString()
   var decimal_location = value_string.indexOf(".")
   if (decimal_location == -1) {
   		decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
     decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function SearchAndReplace(Content, StartAt, EndAt, ReplaceWith) {

   var tmpContent = Content;
   var startContent = "";
   var newContent = "";
   var intStart = 0;
   var intEnd = 0;

   // Get the start and end points
   intStart = tmpContent.toUpperCase().indexOf(StartAt.toUpperCase());
   startContent = tmpContent.substring(intStart);	
   intEnd = intStart + startContent.toUpperCase().indexOf(EndAt.toUpperCase());

   if (intStart == -1) {
	newContent = Content + ReplaceWith
	}
   else {
	repStr = tmpContent.substr(intStart,intEnd - intStart + 1);
   	newContent = tmpContent.replace(repStr,ReplaceWith);
    }
   return newContent;

}

function IsNumeric(obj) {
   var sText = obj.value;
   var ValidChars = "0123456789.";
   var IsNumber = true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
	 obj.value = sText.replace(Char,'');
         }
      }

   return IsNumber;
   
 }