/* Fungsi : SecurityError */

function CheckSecurityAll(m) {
   
     var i=document.getElementById(m+'_count').value.replace(/,/g,'')*1;
    
     for(var j=1;j<=i;j++) {
         document.getElementById(m+'_'+j).checked=document.getElementById(m).checked;
       
    }
}

function SecurityError()
{
	alert("Anda tidak memiliki hak akses ke bagian ini, Hubungi Administrator anda !");
}

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function handleEnter (field, event) { var keyCode =
event.keyCode ? event.keyCode : event.which ? event.which :
event.charCode; if (keyCode == 13) { var i; for (i = 0; i <
field.form.elements.length; i++) if (field == field.form.elements[i])
break; i = (i + 1) % field.form.elements.length;
field.form.elements[i].focus(); return false; } else return true; }

/*function handleEnter (field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		var i;
		for (i = 0; i < field.form.elements.length; i++)
			if (field == field.form.elements[i])
				break;

		i = (i + 1) % field.form.elements.length;
		while (field.form.elements[i].name.indexOf('_N_') >= 0 || field.form.elements[i].type == "hidden")
		{
			i = (i + 1) % field.form.elements.length;
		}

		try
		{
			field.form.elements[i].focus();
		}
		catch (er)
		{
			document.getElementById("fnIndexD01").focus();
		}

		if (field.form.elements[i].type == "text" || field.form.elements[i].type == "textarea")
		{
			field.form.elements[i].select();
		}

		return false;
	}
	else
		return true;
} */


function setStatusText()
{
	window.defaultStatus = 'It worked!';
	// make sure this function doesn't return anything
}

function deleteRow(index) {
	var DeleteRow = confirm('Apakah anda yakin akan menghapus data Discount Customer Item ini?');
	if (DeleteRow) {
		ajax_do('getdata.php?code=800&obj1='+document.getElementById("ftItem_CodeD0"+index).value+'&obj2='+document.getElementById("ftCustomer_Code").value);
		return true
	}
	return false;
}

function setActiveSearch(CheckBoxElement) {
	if(CheckBoxElement.checked==true) {
		document.getElementById("div"+CheckBoxElement.id).style.display="block";
	} else {
		document.getElementById("div"+CheckBoxElement.id).style.display="none";
	}
}

function printSelection(node){

  var content=node.innerHTML
  var pwin=window.open('','print_content','width=100,height=100');

  pwin.document.open();
  pwin.document.write('<html><body onload="window.print()">'+content+'</body></html>');
  pwin.document.close();

  setTimeout(function(){pwin.close();},1000);
}

function printdiv(printpage)
{
	var headstr = "<html><head><title></title></head><body>";
	var footstr = "</body>";
	var newstr = document.all.item(printpage).innerHTML;
	var oldstr = document.body.innerHTML;
	document.body.innerHTML = headstr+newstr+footstr;
	window.print();
	document.body.innerHTML = oldstr;
	return false;
}
function exportSelection(node){
	var content=node.innerHTML
	var pwin=window.open('','excel_content','width=1,height=1');

	pwin.document.open();
	//pwin.document.write('<html><body>'+content+'<form name="frmExport" id="frmExport" action="exports.php" method="post"><input type="hidden" name="data" id="data" value=\''+ htmlspecialchars(content,'ENT_QUOTES') +'\'></form><script>document.frmExport.submit();</script></body></html>');
        pwin.document.write('<html><body>'+content+'<form name="frmExport" id="frmExport" action="exports.php" method="post"><textarea  name="data" id="data">'+ htmlspecialchars(content,'ENT_QUOTES') +'</textarea> </form><script>document.frmExport.submit();</script></body></html>');
	
	//pwin.document.write('<html><body>'+htmlspecialchars(content,'ENT_QUOTES') +'></body></html>');
	
        pwin.document.close();

	setTimeout(function(){pwin.close();},4000);
}

//function submitbutton(pressbutton,modul_name,form_name,id,url_plus) {
function submitbutton(pressbutton,modul_name,form_name,id,url_plus,form_url) {
    // prevent clicking button twice
    document.getElementById(pressbutton).disabled = true;
    
    if ( pressbutton == 'Simpan Bank' ) {
			if(ValPenerimaanPiutangBank()==false) return false;
                document.frmMain.mode.value = "simpan_bank";
                document.frmMain.submit();
    }
    else if ( pressbutton == 'Export To Excel' ) {
		exportSelection(document.getElementById('divDetail') );
    }
        
    else if ( pressbutton == 'Tutup' ) {
			if(form_name=='bomcalculation'){
				 window.close();
			}else
		{	
		//window.location=location.pathname;
                  var m=location.href.search('&mode');
                var n;

                if(m>0) {
                    n=location.href.substring(0,m);
                    window.location=n ;
                }
                else {

                    m=location.href.search('.php');

                   if(m>0) {
                         n=location.href.substring(0,m+4);

                          window.location=n+'?p=1' ;
                   }
                   else {
                       window.history.back();
                   }
                }
		}
//		window.history.back();
	}
        
     else if(pressbutton == 'Update Waktu Pengajian') {
              document.frmMain.mode.value = "update_waktu_pengajian";
              document.frmMain.submit();
	}
	else if ( pressbutton == 'Final Bank' ) {
			if(ValPenerimaanPiutangBankx()==false) return false;
//            if(!confirm('Apakah transaksi akan difinal... ')) {
//                    return false;
//                }
//               document.frmMain.mode.value = "final_bank";
//                document.frmMain.submit();
    }else if ( pressbutton == 'Simpan Penerimaan' ) {

			if(ValPenerimaanPiutangPenerimaan()==false) return false;
                document.frmMain.mode.value = "simpan_penerimaan";
                document.frmMain.submit();
    }
	else if ( pressbutton == 'Final Penerimaan' ) {
				if(ValPenerimaanPiutangPenerimaan()==false) return false;
                if(!confirm('Apakah transaksi akan difinal... ')) {
                    return false;
                }
                document.frmMain.mode.value = "final_penerimaan";
                document.frmMain.submit();
    }
	else if ( pressbutton == 'Approve' ) {
				if(FinalApproveTracking()==false) return false;
                if(!confirm('Apakah transaksi akan di Approve... ')) {
                    return false;
                }
                document.frmMain.mode.value = "final_approve";
                document.frmMain.submit();
    }

      var w = 650;
		var h = 600;
		var LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
		var TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
		var settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=1,resizable=1,status=1,menubar=1';

    var str_urlx=location.href.replace(/#/g,'');

	if ( pressbutton == 'Baru' ) {
                window.location= str_urlx + "&mode=edit&id=0";
	}

	else if ( pressbutton == 'Reset' ) {
            document.getElementById(pressbutton).disabled = false;
    
		// document.frmMain.reset();
                window.location= str_urlx + "&mode=edit&id=0";
	}

	else if ( pressbutton == 'Cetak' ) {
                document.getElementById(pressbutton).disabled = false;
                window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+form_name,"report",settings);
	}
        
        // Digunakan untuk setting "SO cetak otomatis DO dan SI"
        else if ( pressbutton == 'Cetak SO' ) {
                // khusus ISNET-ABR , cetak SO otomatis membuat SI dan DO kemudian keluar cetakan
                document.frmMain.mode.value = "final";
                document.frmMain.submit();

                // khusus ISNET-ABR dan Gudang Bandara (GDB & BDT) , cetak SI tidak langsung tapi melalui form sales invoice monitor 
                if ((document.getElementById('ftwarehouse_codeD01').value != 'GDB') && (document.getElementById('ftwarehouse_codeD01').value != 'BDT'))  {
                    window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+form_name,"report",settings);
                }	             
        }
        
        else if ( pressbutton == 'Cetak DO' ) {
                //alert(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+form_name);
                document.getElementById(pressbutton).disabled = false;
                 window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+form_name+'_cetak_do',"report",settings);
	}
        
        else if ( pressbutton == 'Cetak SI' ) {
                //alert(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+form_name);
                document.getElementById(pressbutton).disabled = false;
                 window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+form_name+'_cetak_si',"report",settings);
	}

	else if ( pressbutton == 'Cetak Non Harga' ) {
                window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+form_name+'_cetak_non_harga',"report",settings);
	}
//        else if(pressbutton == 'Cetak(Cross Tab)') {
//             window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+'cetak_slip_crosstab',"report",settings);
//	}
     else if(pressbutton == 'Cetak(Lemburan)') {
             window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+'cetak_slip_lembur',"report",settings);
	}
     else if(pressbutton == 'Cetak(Lemburan Tertunda)') {
             window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+'cetak_slip_lembur_tertunda',"report",settings);
	}
         else if(pressbutton == 'Cetak (Tanpa Nama Company)') {
             window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+form_name+'_cetak_tanpa_nama_company',"report",settings);
	}
       else if(pressbutton == 'Cetak(Cross Tab)') {
          
             window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+form_name+'_cetak_slip_crosstab',"report",settings);
	}
       // lower case dan replace space menjadi '_' 
        else if(pressbutton.substring(0,6) == 'Update') {
		document.frmMain.mode.value = pressbutton.toLowerCase().replace(/ /g,"_");
		document.frmMain.submit();
	}
        else if(pressbutton.substring(0,5) == 'Close') {
            if(!confirm('Transaksi ini akan di CLOSE, anda yakin ?')) {
                return false;
            }
            
            document.frmMain.mode.value = pressbutton.toLowerCase().replace(/ /g,"_");
            document.frmMain.submit();
	}
    else if ( pressbutton == 'Faktur Pajak' ) {
		window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+'salesinvoicevat',"report",settings);
	}
     else if ( pressbutton == 'Faktur Pajak Standar' ) {
                  window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+'salesinvoicevatstandar&fdvat_date='+ document.getElementById('fdvat_date').value,"report",settings);
	}
      else if ( pressbutton == 'Cetak Bank' ) {
                  window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+'PenerimaanPiutangBank',"report",settings);
	}
      else if ( pressbutton == 'Cetak Penerimaan' ) {
                  window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+'PenerimaanPiutangPenerimaan',"report",settings);
	}
      else if ( pressbutton == 'Cetak Sementara' ) {
         
                if(form_name=='bankmasuk') {
                     window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+'bankmasuksementara',"report",settings);
                }
                else if(form_name=='bankkeluar') {
                     window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+'bankkeluarsementara',"report",settings);
              
                }
                 else if(form_name=='pembayaranhutang') {
                     window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+'pembayaranhutangsementara',"report",settings);

                }
                 else if(form_name=='pembayaranhutangimport') {
                     window.open(modul_name+'.php?form=print_'+ modul_name+'&mode=edit&id=1&idd='+ id +'&m='+'pembayaranhutangimportsementara',"report",settings);

                }
	}
        else if ( pressbutton == 'Generate Report' ) {
                document.getElementById(pressbutton).disabled = false;                
                // alert(modul_name+'.report.php?id='+id+url_plus);

                if(form_url.substring(0,6)=='print_') {
                      url_report=modul_name+'.print.php?'+url_plus;
                }
                else {
                    url_report=modul_name+'.report.php?id='+id+url_plus;
                }
                // alert(url_report);
                document.getElementById("iframe_laporan").src=url_report;//modul_name+'.report.php?id='+id+url_plus;
                //alert(document.getElementById("iframe_laporan").src);
        }

    else if(pressbutton=='Export To Excel (Report)') {
        document.getElementById(pressbutton).disabled = false;
    
        var temp_0 =document.getElementById("iframe_laporan").contentWindow.location.href;
        var temp_1=temp_0+'&type=excel&print=1';
        var w = 500;
        var h = 300;
        var LeftPosition =50;// (screen.width) ? (screen.width-w)/2 : 0;
        var TopPosition =50;// (screen.height) ? (screen.height-h)/2 : 0;
        var settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=1,resizable=1,status=0,menubar=1';
        window.open(temp_1,"reportx",settings);
    }
    else if(pressbutton=='Export To Pdf (Report)') {
        document.getElementById(pressbutton).disabled = false;
    
        var temp_0 =document.getElementById("iframe_laporan").contentWindow.location.href;
        var temp_1=temp_0+'&type=pdf&print=1';
        var w = 1000;
        var h = 1000;
        var LeftPosition =0;// (screen.width) ? (screen.width-w)/2 : 0;
        var TopPosition =0;// (screen.height) ? (screen.height-h)/2 : 0;
        var settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=1,resizable=1,status=0,menubar=1';
        window.open(temp_1,"reportx",settings);
    }
    else if ( pressbutton == 'Surat Jalan' ) {
            alert('Surat Jalan');
            return false;
    }
    else if ( pressbutton == 'Invoice' ) {
            window.location= modul_name+'.php?form=sales_invoice&mode=edit&id=0';
    }
    else if ( pressbutton == 'Faktur' ) {
            //alert("detail_link_faktur_pembelian.php?ftPU_No=" + document.frmMain.ftPU_No.value);
            //showModalDialog('http://localhost/gms/detail_link_faktur_pembelian.php?','ftPU_No=' + document.frmMain.ftPU_No.value + '&fcinvoice_amount=' + document.frmMain.fcsub_amount.value + '&ftPI_Link=' + document.frmMain.ftPI_Link.value,'Status:NO;dialogWidth:650px;dialogHeight:650px');
            var w = 650;
            var h = 600;
            var LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
            var TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
            var settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=0,resizable=1'
            var win = window.open("detail_link_faktur_pembelian.php?ftPU_No=" + document.frmMain.ftPU_No.value + "&fcinvoice_amount=" + document.frmMain.fcsub_amount.value + "&ftPI_Link=" + document.frmMain.ftPI_Link.value,"query_form",settings)
    }

    else if ( pressbutton == 'Final' ) {
          if(!confirm('Apakah transaksi akan difinal... ')) {
                return false;
            }
            document.frmMain.mode.value = "final";
            document.frmMain.submit();
    }
    else if ( pressbutton == 'Revisi' ) {
        if(!confirm('Apakah transaksi akan direvisi... ')) {
            return false;
        }

      window.location=modul_name+".php?form="+form_url+"&mode=edit&revisi=1&id="+id ;

    }
    else if ( pressbutton == 'Simpan Revisi' ) {
            document.frmMain.mode.value = "simpan_revisi";
            document.frmMain.submit();

    }
    else if ( pressbutton == 'Simpan' ) {
            document.frmMain.mode.value = "simpan";
            document.frmMain.submit();
    }

    else if ( pressbutton == 'Update' ) {
     if(form_name=='outstandingmingguan') {
        if(!OutstandingMingguanInputQty()) {
            return false;
        }
    }
            document.frmMain.mode.value = "update";
            document.frmMain.submit();
    }
    else if ( pressbutton == 'Update Status' ) {
            document.frmMain.mode.value = "update";
            document.frmMain.submit();
    } 

    else if ( pressbutton == 'Batal' ) {
            if(!confirm('Apakah transaksi akan dibatalkan... ')) {
                return false;
            }
            document.frmMain.mode.value = "batal";
            document.frmMain.submit();
    }

    else if ( pressbutton == 'Hapus' ) {
            if(!confirm('Apakah Data akan dihapus... ')) {
                return false;
            }
            document.frmMain.mode.value = "hapus";
            document.frmMain.submit();
    }


    return true;
}


        
var url_report;
function get_url_report() {
	return url_report;
}

// Set mouse over color
function ew_MouseOver(row) {
	row.mover = true; // Mouse over
	if (typeof(row.oClassName) == "undefined")
		row.oClassName = row.className;
	if (typeof(row.oCssText) == "undefined")
		row.oCssText = row.style.cssText;
	if (!row.selected) {
		row.className = rowmoverclass;
		row.style.cssText = "";
	}
}

// Set mouse out color
function ew_MouseOut(row) {
	row.mover = false; // Mouse out
	if (!row.selected)
		ew_SetColor(row);
}

// Set row color
function ew_SetColor(row) {
	if (row.selected) {
		if (typeof(row.oClassName) == "undefined")
			row.oClassName = row.className;
		if (typeof(row.oCssText) == "undefined")
			row.oCssText = row.style.cssText;
		row.className = rowselectedclass;
	} else if (row.edit) {
		row.className = roweditclass;
	} else {
		if (typeof(row.oClassName) != "undefined")
			row.className = row.oClassName;
		if (typeof(row.oCssText) != "undefined")
			row.style.cssText = row.oCssText;
	}
}

// Set selected row color
function ew_Click(row) {
	if (row.deleteclicked)
		row.deleteclicked = false; // Reset delete button/checkbox clicked
	else {
		var bselected = row.selected;
		ew_ClearSelected(); // Clear all other selected rows
		if (!row.deleterow) row.selected = !bselected; // Toggle
		ew_SetColor(row);
	}
}

// Clear selected rows color
function ew_ClearSelected() {
	var table = document.getElementById(EW_LIST_table_name);
	for (var i = firstrowoffset; i < table.rows.length-lastrowoffset; i++) {
		var thisrow = table.rows[i];
		if (thisrow.selected && !thisrow.deleterow) {
			thisrow.selected = false;
			ew_SetColor(thisrow);
		}
	}
}

function ew_SelectKey(elem) {
	var f = elem.form;
	if (!f.elements["ftstock_group_code[]"]) return;
	if (f.elements["ftstock_group_code[]"][0]) {
		for (var i=0; i<f.elements["ftstock_group_code[]"].length; i++)
			f.elements["ftstock_group_code[]"][i].checked = elem.checked;
	} else {
		f.elements["ftstock_group_code[]"].checked = elem.checked;
	}
}


function ew_CheckKey(elem) {
	var f = elem.form;
	var total = 0;
	var temp = "";
	for (var i=0; i<f.elements["ftPI[]"].length; i++) {
		if(f.elements["ftPI[]"][i].checked) {
			total = total + parseFloat(f.elements["ftPI[]"][i].value);
			temp = temp + f.elements["ftPI_No[]"][i].value + ",";
		}
	}
	document.getElementById("ftPI_Link").value = temp.substring(0, temp.length-1);
	ElementCurrencyByNameSetValue("fcinvoice_amount", total);
}

function ew_CloseKey() {
	var link;
	var f = document.frmLink;
	var fx = window.opener.document.forms[0];
	fx.fcsub_amount.value = f.fcinvoice_amount.value;
	fx.fcamount.value = f.fcinvoice_amount.value;
	fx.ftPI_Link.value = f.ftPI_Link.value;
	/*var f = document.frmLink;
	window.dialogArguments.fcinvoice_amount = f.fcinvoice_amount.value;
	window.dialogArguments.fcamount = f.fcinvoice_amount.value;
	window.dialogArguments.ftPI_Link = f.ftPI_Link.value;
	var allArgs = window.dialogArguments;
	var firstArg = allArgs.substring(0, allArgs.indexOf(";"));*/

	window.close();
}

// Set Currency Format
function ElementCurrencyOnBlur(Element) {
    
    // Check duplicate decimal separator that can cause error calculating
    for(var i=count=0; i<Element.value.length; count+=+(DecimalSeparator===Element.value[i++]));
    if (count>1) {
        Element.value = 0;
        alert("Error Input value. Please check your input again.");
        Element.focus();
    } 
    
    if((document.getElementById("form").value=='pembelian_purchase_order_import')&&(Element.id.toString().substring(0, 2)=="fc")) {
        CurrencyDecimalDigits=4;
    }
     else  if((document.getElementById("form").value=='pembelian_purchase_invoice_local')&&(strstr(Element.id.toString(),'price')!='')) {
        CurrencyDecimalDigits=5;
    }
    else  if((document.getElementById("form").value=='pembelian_purchase_invoice_import')&&(strstr(Element.id.toString(),'price')!='')) {
        CurrencyDecimalDigits=5;
    } 
     else  if((document.getElementById("form").value=='pembelian_harga_beli')&&(strstr(Element.id.toString(),'price')!='')) {
        CurrencyDecimalDigits=5;
    }
    else  if((document.getElementById("form").value=='pembelian_purchase_order_local')&&(strstr(Element.id.toString(),'price')!='')) {
//             alert(Element.id.toString().search('fcprice'));
        CurrencyDecimalDigits=5;
    }
    else if((document.getElementById("form").value=='pembelian_purchase_invoice_local')&&(Element.id.toString().substring(0, 2)=="fc")) {
        CurrencyDecimalDigits=4;
    }
    else if((document.getElementById("form").value=='pembelian_harga_beli')&&(Element.id.toString().substring(0, 2)=="fc")) {
        CurrencyDecimalDigits=4;
//        alert(strstr(Element.id.toString(),'fcprice'));
    }
    else if((document.getElementById("form").value=='pembelian_purchase_order_local')&&(Element.id.toString().substring(0, 2)=="fc")) {
        CurrencyDecimalDigits=4;
    }
    // else if((document.getElementById("form").value=='pembelian_purchase_order')&&(Element.id.toString().substring(0, 2)=="fn")) {
    //    CurrencyDecimalDigits=0;
    // }
    else if((document.getElementById("form").value=='pembelian_penerimaan_barang')&&(Element.id.toString().substring(0, 2)=="fn")) {
        CurrencyDecimalDigits=0;
    }
    else if((document.getElementById("form").value=='bom')&&(Element.id.toString().substring(0, 2)=="fn")) {
        CurrencyDecimalDigits=4;
    }
    else if((document.getElementById("form").value=='calc')&&(Element.id.toString().substring(0, 2)=="fn")) {
        CurrencyDecimalDigits=4;
    }
    else if((Element.id.lastIndexOf('USD')>0)) {
        CurrencyDecimalDigits=4;
    }
    else if((Element.id.lastIndexOf('currency')>0)) {
        CurrencyDecimalDigits=4;
    }
    else if((Element.id.toString().substring(0, 5)=="fnqty")) {
        CurrencyDecimalDigits=2;
    }
    else if((Element.id.toString().substring(0, 2)=="fn")) {
        CurrencyDecimalDigits=0;
    }
    else if((Element.id.toString().substring(0, 2)=="ff")) {
        CurrencyDecimalDigits=2;
    }
    else {
        CurrencyDecimalDigits=2;
    }
	
    // exception for ffdiscount because of tier discount format    
    if((Element.id.toString().substring(0, 10)!="ffdiscount")) {
        Value = QxElementValueNumberStringToFloat(Element, ThousandSeparator, DecimalSeparator);
        Element.value = QxFloatToNumberString(Value, ThousandSeparator, DecimalSeparator, CurrencyDecimalDigits);
    }
    
  
}

function ElementCurrencyALL() {
    var tbs = document.getElementsByTagName("input");

        for(i=0; i<tbs.length; i++)
        {
            if(tbs[i].type == "text")
            {
                if (tbs[i].id.toString().substring(0, 2)=="fc") {
                        ElementCurrencyOnBlur(tbs[i]);
                } else if (tbs[i].id.toString().substring(0, 2)=="fn") {
                        ElementCurrencyOnBlur(tbs[i]);
                } else if (tbs[i].id.toString().substring(0, 2)=="ff") {
                        ElementCurrencyOnBlur(tbs[i]);
                }

            }
        }



}



function ElementCurrencyGetValue(Element) {
	return QxElementValueNumberStringToFloat(Element, ThousandSeparator, DecimalSeparator);
}

function ElementCurrencyByNameGetValue(ElementName) {
	var Obj = document.getElementById(ElementName);
	return ElementCurrencyGetValue(Obj);
}

function ElementCurrencySetValue(Element, Value) {
    if((Element.id.lastIndexOf('USD')>0)) {
        CurrencyDecimalDigits=4;
    }
    else {
        CurrencyDecimalDigits=2;
    }

	Element.value = QxFloatToNumberString(Value, ThousandSeparator, DecimalSeparator, CurrencyDecimalDigits);
}

function ElementCurrencyByNameSetValue(ElementName, Value) {
  var Obj = document.getElementById(ElementName);
  return ElementCurrencySetValue(Obj, Value);
}



// ---------------------------------------------------

function RecalculateGrid_SM(value, value2)
{
	var Qty;
	var TotalQty = 0;

	TransactionDetailCount0 = document.getElementById('TransactionDetailCount0').value;
	for (var i=0; i < TransactionDetailCount0; i++) {
		Qty = ElementCurrencyByNameGetValue(value+i);
		TotalQty = TotalQty + Qty;
	}
	ElementCurrencyByNameSetValue(value2, TotalQty);
}

function RecalculateGrid_PU(value, value2)
{
	var Amount;
	var TotalAmount = 0;

	TransactionDetailCount0 = document.getElementById('TransactionDetailCount0').value;
	for (var i=1; i <= TransactionDetailCount0; i++) {
		Amount = ElementCurrencyByNameGetValue(value+i);
		TotalAmount = TotalAmount + Amount;
	}
	ElementCurrencyByNameSetValue(value2, TotalAmount);
}

function SetAmount_PU()
{
	SubAmount = ElementCurrencyByNameGetValue("fcsub_amount");
	Discount = ElementCurrencyByNameGetValue("fcdiscount");
	VAT = ElementCurrencyByNameGetValue("fcvat");
	OtherCost = ElementCurrencyByNameGetValue("fcother_cost");
	document.getElementById("fcamount").value = (SubAmount-Discount)+VAT+OtherCost;
}

function RecalculateGrid_SK(value, value2)
{
	var Amount;
	var TotalAmount = 0;

	TransactionDetailCount0 = document.getElementById('TransactionDetailCount0').value;
	for (var i=1; i <= TransactionDetailCount0; i++) {
		Amount = ElementCurrencyByNameGetValue(value+i);
		TotalAmount = TotalAmount + Amount;
	}
	ElementCurrencyByNameSetValue(value2, TotalAmount);
}

function RecalculateGrid_SI()
{
	var QtyItem, PriceItem, AmountItem;
	var SubAmount = 0;

	TransactionDetailCount0 = document.getElementById('TransactionDetailCount0').value;
	for (var i=1; i <= TransactionDetailCount0; i++) {
		QtyItem = ElementCurrencyByNameGetValue("ffqtyD0"+i);
		PriceItem = ElementCurrencyByNameGetValue("fcpriceD0"+i);
		AmountItem = QtyItem * PriceItem;
		ElementCurrencyByNameSetValue("fcamountD0"+i, AmountItem);
		SubAmount = SubAmount + AmountItem;
	}
	ElementCurrencyByNameSetValue("fcsub_amount", SubAmount);

	SetAmount_SI();
}

function SetAmount_SI()
{
	SubAmount = ElementCurrencyByNameGetValue("fcsub_amount");
	Discount = ElementCurrencyByNameGetValue("fcdiscount");
	VAT = ElementCurrencyByNameGetValue("fcvat");
	Amount = (SubAmount-Discount)+VAT;
	ElementCurrencyByNameSetValue("fcamount", Amount);
	ElementCurrencyByNameSetValue("fcoutstanding_amount", Amount);
}

function RecalculateGrid_CM()
{
	var QtyItem, PriceItem, NewPriceItem, DifferenceItem;
	var SubAmount = 0;
	TransactionDetailCount0 = document.getElementById('TransactionDetailCount0').value;
	for (var i=0; i < TransactionDetailCount0; i++) {
		QtyItem = ElementCurrencyByNameGetValue("ffqtyD0"+i);
		PriceItem = ElementCurrencyByNameGetValue("fcpriceD0"+i);
		NewPriceItem = ElementCurrencyByNameGetValue("fcNew_PriceD0"+i);
		DifferenceItem = (QtyItem * PriceItem) - (QtyItem * NewPriceItem);
		ElementCurrencyByNameSetValue("fcDifferenceD0"+i, DifferenceItem);
		SubAmount = SubAmount + DifferenceItem;
	}
	ElementCurrencyByNameSetValue("fcsub_amount", SubAmount);

	SetAmount_CM();
}

function SetAmount_CM()
{
	SubAmount = ElementCurrencyByNameGetValue("fcsub_amount");
	Discount = ElementCurrencyByNameGetValue("fcdiscount");
	VAT = ElementCurrencyByNameGetValue("fcvat");
	Amount = (SubAmount-Discount)+VAT;
	ElementCurrencyByNameSetValue("fcamount", Amount);
}

function RecalculateGrid_SR()
{
	var QtyItem, PriceItem, AmountItem;
	var SubAmount = 0, SubQty = 0;
	TransactionDetailCount0 = document.getElementById('TransactionDetailCount0').value;
	for (var i=0; i < TransactionDetailCount0; i++) {
		QtyItem = ElementCurrencyByNameGetValue("ffqtyD0"+i);
		PriceItem = ElementCurrencyByNameGetValue("fcpriceD0"+i);
		AmountItem = (QtyItem * PriceItem);
		ElementCurrencyByNameSetValue("fcamountD0"+i, AmountItem);

		SubQty = SubQty + QtyItem;
		SubAmount = SubAmount + AmountItem;
	}
	ElementCurrencyByNameSetValue("fftotal_qty", SubQty);
	ElementCurrencyByNameSetValue("fcsub_amount", SubAmount);

	SetAmount_SR();
}

function SetAmount_SR()
{
	SubAmount = ElementCurrencyByNameGetValue("fcsub_amount");
	Discount = ElementCurrencyByNameGetValue("fcdiscount");
	VAT = ElementCurrencyByNameGetValue("fcvat");
	Amount = (SubAmount-Discount)+VAT;
	ElementCurrencyByNameSetValue("fcamount", Amount);
}


function RecalculateGrid_CR()
{
	var QtyItem;
	var SubQty = 0;
	TransactionDetailCount0 = document.getElementById('TransactionDetailCount0').value;
	for (var i=1; i <= TransactionDetailCount0; i++) {
		QtyItem = ElementCurrencyByNameGetValue("ffqtyD0"+i);
		SubQty = SubQty + QtyItem;
	}
	ElementCurrencyByNameSetValue("fftotal_qty", SubQty);
}

function RecalculateGrid_CMC()
{
	var DefaultPercentItem = 100.5;
	var QtyItem, PercentItem, NettoItem, PriceItem;
	var SubQty = 0, SubPercent = 0, CapitalPrice = 0;
	TransactionDetailCount0 = document.getElementById('TransactionDetailCount0').value;
	for (var i=1; i <= TransactionDetailCount0; i++) {
		QtyItem = ElementCurrencyByNameGetValue("ffInk_Weight_GrD0"+i);
		SubQty = SubQty + QtyItem;
	}

	for (var i=1; i <= TransactionDetailCount0; i++) {
		QtyItem = ElementCurrencyByNameGetValue("ffInk_Weight_GrD0"+i);
		PercentItem = (DefaultPercentItem / SubQty) * QtyItem;
		ElementCurrencyByNameSetValue("ffInk_Weight_PercentD0"+i, PercentItem);

		NettoItem = ElementCurrencyByNameGetValue("fcNetto_PriceXD0"+i);
		PriceItem = ((PercentItem + 1) / 100) * NettoItem;
		PriceItem = Math.round(Math.round(PriceItem + 99.9999) / 100) * 100;
		ElementCurrencyByNameSetValue("fcNetto_PriceD0"+i, PriceItem);

		CapitalPrice = CapitalPrice + PriceItem;
	}

	ElementCurrencyByNameSetValue("ffTotal_Ink_Weight_GR", SubQty);
	ElementCurrencyByNameSetValue("ffTotal_Ink_Weight_Percent", DefaultPercentItem);

	ElementCurrencyByNameSetValue("fcCapital_Price", CapitalPrice);
	OtherCost = ElementCurrencyByNameGetValue("fcother_cost");
	ElementCurrencyByNameSetValue("fcNetto_Price", CapitalPrice+OtherCost);
	ElementCurrencyByNameSetValue("fcBrutto_Price_NPPKP", CapitalPrice+OtherCost);
}


function RecalculateGrid_CD()
{
	var QtyItem;
	var SubQty = 0;
	TransactionDetailCount0 = document.getElementById('TransactionDetailCount0').value;
	for (var i=0; i < TransactionDetailCount0; i++) {
		QtyItem = ElementCurrencyByNameGetValue("ffqtyD0"+i);
		SubQty = SubQty + QtyItem;
	}
	ElementCurrencyByNameSetValue("fftotal_qty", SubQty);
}

function RecalculateGrid_CRP()
{
	var QtyItem;
	var SubQty = 0;
	TransactionDetailCount0 = document.getElementById('TransactionDetailCount0').value;
	for (var i=1; i <= TransactionDetailCount0; i++) {
		QtyItem = ElementCurrencyByNameGetValue("ffqtyD0"+i);
		SubQty = SubQty + QtyItem;
	}
	ElementCurrencyByNameSetValue("fftotal_qty", SubQty);
}


function RecalculateGrid_PI()
{
	var QtyItem, PriceItem, AmountItem;
	var SubAmount = 0;
	TransactionDetailCount0 = document.getElementById('TransactionDetailCount0').value;

	for (var i=0; i < TransactionDetailCount0; i++) {
		QtyItem = ElementCurrencyByNameGetValue("ffqtyD0"+i);
		PriceItem = ElementCurrencyByNameGetValue("fcpriceD0"+i);
		AmountItem = QtyItem * PriceItem;
		ElementCurrencyByNameSetValue("fcamountD0"+i, AmountItem);

		SubAmount = SubAmount + AmountItem;
	}
	ElementCurrencyByNameSetValue("fcsub_amount", SubAmount);

	Discount = ElementCurrencyByNameGetValue("fcdiscount");
	VAT = ElementCurrencyByNameGetValue("fcvat");
	OtherCost = ElementCurrencyByNameGetValue("fcother_cost");
	Amount = (SubAmount - Discount) + VAT + OtherCost;

	ElementCurrencyByNameSetValue("fcamount", Amount);
	ElementCurrencyByNameSetValue("fcoutstanding_amount", Amount);
}

function SetAmount2()
{
	SubAmount = parseFloat(document.getElementById("fcsub_amount").value);
	Discount = parseFloat(document.getElementById("fcdiscount").value);
	VAT = parseFloat(document.getElementById("fcvat").value);
	OtherCost = parseFloat(document.getElementById("fcother_cost").value);
	Amount = (SubAmount - Discount) + VAT + OtherCost;
	document.getElementById("fcamount").value = Amount;
	document.getElementById("fcoutstanding_amount").value = Amount;
}

function SetAmount3()
{
	var Qty;
	var Price;
	var Amount = 0;
	var TotalAmount = 0;
	var TotalQty = 0;

	TransactionDetailCount = document.getElementById("TransactionDetailCount").value;
	for (var i=0; i < TransactionDetailCount; i++) {
		if (document.getElementById("ffqtyD"+i).value.length > 0) {
			Qty = parseFloat(document.getElementById("ffqtyD"+i).value);
			Price = parseFloat(document.getElementById("fcpriceD"+i).value);
		} else {
			Qty = 0;
			Price = 0;
		}
		Amount = Qty * Price;
		document.getElementById("fcamountD"+i).value = Amount;

		TotalQty  = TotalQty + Qty;
		TotalAmount = TotalAmount + Amount;
	}
	document.getElementById("fftotal_qty").value = TotalQty;
	document.getElementById("fcsub_amount").value = TotalAmount;
	document.getElementById("fcamount").value = TotalAmount;
}

function SetAmount4()
{

	SubAmount = parseFloat(document.getElementById("fcsub_amount").value);
	Discount = parseFloat(document.getElementById("fcdiscount").value);
	VAT = parseFloat(document.getElementById("fcvat").value);
	OtherCost = parseFloat(document.getElementById("fcother_cost").value);
	Amount = (SubAmount - Discount) + VAT + OtherCost;
	document.getElementById("fcamount").value = Amount;
}


function SetAmount5()
{
	var Qty;
	var Price;
	var Amount = 0;
	var TotalAmount = 0;
	var TotalQty = 0;

	TransactionDetailCount = TransactionDetailRowID0;
	for (var i=1; i <= TransactionDetailCount; i++) {
		if (document.getElementById("ffqtyD"+i).value.length > 0) {
			Qty = parseFloat(document.getElementById("ffqtyD"+i).value);
			Price = parseFloat(document.getElementById("fcpriceD"+i).value);
		} else {
			Qty = 0;
			Price = 0;
		}
		Amount = Qty * Price;
		document.getElementById("fcamountD"+i).value = Amount;

		TotalQty  = TotalQty + Qty;
		TotalAmount = TotalAmount + Amount;
	}
	document.getElementById("fftotal_qty").value = TotalQty;
	document.getElementById("ffTotal_Return").value = 0;

	document.getElementById("fcsub_amount").value = TotalAmount;
	document.getElementById("fcamount").value = TotalAmount;
	document.getElementById("fcreturn_amount").value = 0;
	document.getElementById("fcpayment_amount").value = 0;
	document.getElementById("fcoutstanding_amount").value = TotalAmount;
}

function SetAmount6()
{
	SubAmount = parseFloat(document.getElementById("fcsub_amount").value);
	Discount = parseFloat(document.getElementById("fcdiscount").value);
	VAT = parseFloat(document.getElementById("fcvat").value);
	Amount = (SubAmount - Discount) + VAT;
	document.getElementById("fcamount").value = Amount;
	document.getElementById("fcoutstanding_amount").value = Amount;
}


function SetAmount7()
{
	var Qty;
	var Price;
	var Amount = 0;
	var TotalAmount = 0;
	var TotalQty = 0;

	TransactionDetailCount = TransactionDetailRowID0;
	for (var i=1; i <= TransactionDetailCount; i++) {
		if (document.getElementById("ffqtyD"+i).value.length > 0) {
			Qty = parseFloat(document.getElementById("ffqtyD"+i).value);
			Price = parseFloat(document.getElementById("fcpriceD"+i).value);
		} else {
			Qty = 0;
			Price = 0;
		}
		Amount = Qty * Price;
		document.getElementById("fcamountD"+i).value = Amount;

		TotalQty  = TotalQty + Qty;
		TotalAmount = TotalAmount + Amount;
	}
	document.getElementById("fftotal_qty").value = TotalQty;

	document.getElementById("fcsub_amount").value = TotalAmount;
	document.getElementById("fcamount").value = TotalAmount;
}


function SetAmount8()
{
	SubAmount = parseFloat(document.getElementById("fcsub_amount").value);
	Discount = parseFloat(document.getElementById("fcdiscount").value);
	VAT = parseFloat(document.getElementById("fcvat").value);
	Amount = (SubAmount - Discount) + VAT;
	document.getElementById("fcamount").value = Amount;
}


//----------------------------------------------

function htmlspecialchars (string, quote_style, charset, double_encode) {
    // Convert special characters to HTML entities
    //
    // version: 1003.2411
    // discuss at: http://phpjs.org/functions/htmlspecialchars    // +   original by: Mirek Slugen
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nathan
    // +   bugfixed by: Arno
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Ratheous
    // +      input by: Mailfaker (http://www.weedem.fr/)
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +      input by: felix    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: charset argument not supported
    // *     example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
    // *     returns 1: '&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;'
    // *     example 2: htmlspecialchars("ab\"c'd", ['ENT_NOQUOTES', 'ENT_QUOTES']);    // *     returns 2: 'ab"c&#039;d'
    // *     example 3: htmlspecialchars("my "&entity;" is still here", null, null, false);
    // *     returns 3: 'my &quot;&entity;&quot; is still here'
    var optTemp = 0, i = 0, noquotes= false;
    if (typeof quote_style === 'undefined' || quote_style === null) {        quote_style = 2;
    }
    string = string.toString();
    if (double_encode !== false) { // Put this first to avoid double-encoding
        string = string.replace(/&/g, '&amp;');    }
    string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;');

    var OPTS = {
        'ENT_NOQUOTES': 0,        'ENT_HTML_QUOTE_SINGLE' : 1,
        'ENT_HTML_QUOTE_DOUBLE' : 2,
        'ENT_COMPAT': 2,
        'ENT_QUOTES': 3,
        'ENT_IGNORE' : 4    };
    if (quote_style === 0) {
        noquotes = true;
    }
    if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags        quote_style = [].concat(quote_style);
        for (i=0; i < quote_style.length; i++) {
            // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
            if (OPTS[quote_style[i]] === 0) {
                noquotes = true;            }
            else if (OPTS[quote_style[i]]) {
                optTemp = optTemp | OPTS[quote_style[i]];
            }
        }        quote_style = optTemp;
    }
    if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
        string = string.replace(/'/g, '&#039;');
    }    if (!noquotes) {
        string = string.replace(/"/g, '&quot;');
    }

    return string;
	}
        
function LoadInfo()
{

    if (document.getElementById) { // DOM3 = IE5, NS6
    document.getElementById('hidepage').style.visibility = 'hidden';
    }
    else {
    if (document.layers) { // Netscape 4
    document.hidepage.visibility = 'hidden';
    }
    else { // IE 4
    document.all.hidepage.style.visibility = 'hidden';
    }
    }
}
 
function function_exists (function_name) {   
	
    if (typeof function_name == 'string'){
       return (typeof this.window[function_name] == 'function');
   }else{
        return (function_name instanceof Function);
    }
}

function ValTime( strTime) {
    // '00:00:00'
    var strHour=strTime.substr(0, 2);
    var strMinute=strTime.substr(3, 2);
    var strSecond=strTime.substr(6, 2)
    var str1=strTime.substr(2, 1);
    var str2=strTime.substr(5, 1);
   	
    if(strTime.length !=8) {
		
        return false;
    }
    else if((str1!=':')&&(str2!=':')) {
		
        return false;
    }
    else if((strHour*1<0)||(strHour*1>23)) {
		
         return false;
    }
     else if((strMinute*1<0)||(strMinute*1>59)) {
		
         return false;
    }
     else if((strSecond*1<0)||(strSecond*1>59)) {
		
         return false;
    }
    return true;
}

function CDate(strDate) {
    //20/08/2009
    var result='';
    result=strDate.substr(6,4)+'-'+strDate.substr(3,2)+'-'+strDate.substr(0,2);
    return result;
}
//alert(str_url);
function SetCurrency(pname,pvalue) {
     var i=document.getElementsByName(pname).length;
     for(var j=0;j<i;j++) {
        document.getElementsByName(pname)[j].innerHTML=document.getElementById(pvalue).value;
    }

}
function SetValueSelected(Value,ObjSelect) {
    var state =Value;
    var len = ObjSelect.length;
    for (var idx=0; idx < len; idx++)
    {
            if (state==ObjSelect.options[idx].value)
            {
                    ObjSelect.selectedIndex=idx;
                    ObjSelect.options[idx].selected=true;
                    break;
            }
    }
}
function SetIndexSelected(index,ObjSelect) {
    ObjSelect.selectedIndex=index;
}
