﻿// format Amount field 12000 > 12000.00
function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = addCommas(minus + s);
	return s;
}    

// insert commas every chacters of a whole number (EX. 23421231.05 > 2,3421,231.05)    
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

//update status bar
function displayStatus(text)
{
    window.status = text;
}

// Check all or uncheck all?
function CheckCheckAll(fmobj) {
  var TotalBoxes = 0;
  var TotalOn = 0;
  for (var i=0;i<fmobj.elements.length;i++) {
    var e = fmobj.elements[i];
    if ((e.name != 'allbox') && (e.type=='checkbox')) {
      TotalBoxes++;
      if (e.checked) {
       TotalOn++;
      }
    }
  }
  if (TotalBoxes==TotalOn) {
    fmobj.allbox.checked=true;
  }
  else {
   fmobj.allbox.checked=false;
  }
}

// keep track on selected documents
function trackDocsToReindex(SelectedTrueFalse, DocumentIDField, DocumentID)
{
    if (SelectedTrueFalse)
    {
        if (DocumentIDField.value.length == 0)
        {
            DocumentIDField.value = DocumentID + ';'
        }   
        else
        {
            DocumentIDField.value = DocumentIDField.value + DocumentID + ';'
        }
    }
    else
    {
        DocumentIDField.value = DocumentIDField.value.replace(DocumentID + ';', '');    
    }
}

// check or uncheck all checkboxes
function checkUncheckAll(documenthitlist, toplevelcheckbox)
{
    if (toplevelcheckbox)
    {
        for (i = 0; i < documenthitlist.length; i++)
        {
            if (documenthitlist[i].type == 'checkbox' && documenthitlist[i].value.length == 10)
            {
	            documenthitlist[i].checked = true ;
	            trackDocsToReindex(true, DocID, documenthitlist[i].value); 
	        }
	    }
	    updateXmlStore();
	}
	else
	{
	    for (i = 0; i < documenthitlist.length; i++)
	    {
	        if (documenthitlist[i].type == 'checkbox')
	        {
	            documenthitlist[i].checked = false ;
            }
        }
        DocID.value = '';
	    XmlStore.value = '';
    }
}   

//updateXmlStore
function updateXmlStore()
{
    XmlStore.value = '<indexes>'
    XmlStore.value = XmlStore.value + '<title>Re-Index Document(s)</title>'
    XmlStore.value = XmlStore.value + '<pxadjust>75</pxadjust>'
    XmlStore.value = XmlStore.value + '<index id="' + reindex_drawer_index_id.value + '" data_type="list">'
    XmlStore.value = XmlStore.value + '<display_name><![CDATA[Status]]></display_name>'
    XmlStore.value = XmlStore.value + '<display_mask><![CDATA[]]></display_mask>'
    XmlStore.value = XmlStore.value + '<value static="false">APPROVED</value>'
    XmlStore.value = XmlStore.value + '<list list_id="' + reindex_drawer_list_id.value + '">'
    XmlStore.value = XmlStore.value + '<list_item >'
    XmlStore.value = XmlStore.value + '<![CDATA[APPROVED]]>'
    XmlStore.value = XmlStore.value + '</list_item>'
    XmlStore.value = XmlStore.value + '</list>'
    XmlStore.value = XmlStore.value + '<dynamicvalues />'
    XmlStore.value = XmlStore.value + '</index>'
    XmlStore.value = XmlStore.value + '</indexes>'
}

// reindex documents only if there is at least one of them selected on the hitlist
function checkIfDocumentsAreSelected(documenthitlist)
{
    var atLeastOneSelected;
    //atLeastOneSelected = false;
    for (i = 0; i < documenthitlist.length; i++)
    {
        if (documenthitlist[i].type == 'checkbox' && documenthitlist[i].value.length == 10 && documenthitlist[i].checked == true)
        {   
            atLeastOneSelected = true;
	    }	    
	}
	if (atLeastOneSelected)
	{	
        reindex();
    }
    else
    {
        alert('You must select one or more invoices before clicking the "Approve Selected Invoices" button.');
    }
}

// uploading defaults
function populateUploadDefaults(strIdxId)
{
    var atLeastOneSelected;
    var strCriteria;
    PageNum = 1;
    SortField = '';
    SortOrder = '';
    //alert('loading upload defaults ... ');
    var theUrl = "../lookupvaluesxml.asp?Index=" + strIdxId + "&pagenum=" + PageNum + "&sortfield=" + SortField + "&sortorder=" + SortOrder
	mstrXML = new String(SendXml("", theUrl))	
	mstrClrXML = mstrXML

    //dras
	//alert('populateUploadDefaults XML : ' + mstrXML);        

	if (mstrXML.toLowerCase() == "session expired")
	{
		if (opener.location.href.toLowerCase().indexOf('/bar.asp') != -1)
		{
			opener.parent.opener.parent.location='/berkowitz/default.asp';
			opener.parent.close();
			window.close();
		}
		else
		{
			opener.parent.location='/berkowitz/default.asp';
			window.close();
		}
	}			  
	
	//retrieve index values and index ids	
	var objXml = new ActiveXObject("MSXML2.DOMDocument.4.0");
	var objNodes;
	objXml.async=false;		
	objXml.loadXML (mstrXML);
	objNodes = objXml.selectNodes("//row/index");
	var strIndexID;
	var strIndexValue;
	var x;
	for (var i = 0;i< objNodes.length;i++)
	{
		strIndexID = objNodes[i].selectSingleNode("@id").text;
		strIndexValue = objNodes[i].selectSingleNode(".").text;
		SaveValue(strIndexValue,strIndexID,true); 
		strIdx = 'i' + strIndexID;
		x = document.getElementById(strIdx)
		x.value = strIndexValue;
	}
	if (StatusField.value.length != '')
	{
	    strIdx = 'i' + StatusField.value;
        x = document.getElementById(strIdx);  
        setSelectedIndex(x, StatusValue.value);
    }
}

// select a drop-down option based on the value
function setSelectedIndex(s, v) {
    for ( var i = 1; i < s.options.length; i++ ) {
        if ( s.options[i].value == v ) {
            s.options[i].selected = true;
            return;
        }
    }
}

// switch images for tabs - client documents, and various types of invoice documents
function updateTabs(s)
{
    //alert('Status: ' + s);
    //client documents
    if (s == '') 
    {
        //alert('client src: ' + document.hitlist.img_client.src)
        strPath = document.hitlist.img_client.src
        document.hitlist.img_client.src = strPath.replace('.gif', '_active.gif');        
    } 
    //pedning 
    if (s == 1) 
    {
        //alert('pending src: ' + document.hitlist.img_pending.src)
        strPath = document.hitlist.img_pending.src
        document.hitlist.img_pending.src = strPath.replace('.gif', '_active.gif') 
        strPath = document.hitlist.img_shade_pending.src;
        document.hitlist.img_shade_pending.src = strPath.replace('.gif', '_active.gif');
    }
    //approved
    if (s == 2)
    {
        //alert('approved src: ' + document.hitlist.img_approved.src)
        strPath = document.hitlist.img_approved.src
        document.hitlist.img_approved.src = strPath.replace('.gif', '_active.gif') 
        strPath = document.hitlist.img_shade_approved.src;
        document.hitlist.img_shade_approved.src = strPath.replace('.gif', '_active.gif');
    }
    //declined
    if (s == 3)
    {
        //alert('declined src: ' + document.hitlist.img_declined.src)
        strPath = document.hitlist.img_declined.src
        document.hitlist.img_declined.src = strPath.replace('.gif', '_active.gif') 
        strPath = document.hitlist.img_shade_declined.src;
        document.hitlist.img_shade_declined.src = strPath.replace('.gif', '_active.gif');
    }
    //paid
    if (s == 4)
    {
        //alert('paid src: ' + document.hitlist.img_paid.src)
        strPath = document.hitlist.img_paid.src;
        document.hitlist.img_paid.src = strPath.replace('.gif', '_active.gif');
        strPath = document.hitlist.img_shade_paid.src;
        document.hitlist.img_shade_paid.src = strPath.replace('.gif', '_active.gif');
    }
}

//updateXmlStoreDefaults for Client Documents uploading purposes
function updateXmlStoreDefaults(IndexId, IndexValue)
{
    var sStore
    var sSearch
    var intSearchPos
    var sToReplace
    var sReplaceWhat
    var sReplaceWith
    
    if (IndexId.length == 10)
    {
        sStore = XmlStore.value;
        sSearch = '<index id="' + IndexId + '"';
        intSearchPos = sStore.indexOf(sSearch);
        //alert(intSearchPos);
        sToReplace = sStore.substr(intSearchPos, 400);
        //alert(sToReplace);
        sReplaceWhat = '<value static="false"><![CDATA[]]></value>';
        //alert(sReplaceWhat);
        sReplaceWith = sReplaceWhat.replace('static="false"><![CDATA[]]>', 'static="true"><![CDATA[' + IndexValue + ']]>');
        //alert(sReplaceWith);
        sToReplaced = sToReplace.replace(sReplaceWhat, sReplaceWith);
        //alert(sToReplaced);
        sStore = sStore.replace(sToReplace, sToReplaced);
        sFinal = sStore.substr(intSearchPos, 400);
        //alert(sFinal);
        //CLIENT_DEFAULT_VAL_2.value = sStore;
        XmlStore.value = sStore;    
    }
}

function updateAllDefaultValues()
{
    updateXmlStoreDefaults(CLIENT_DEFAULT_ID_1.value, CLIENT_DEFAULT_VAL_1.value);
    updateXmlStoreDefaults(CLIENT_DEFAULT_ID_2.value, CLIENT_DEFAULT_VAL_2.value);
    updateXmlStoreDefaults(CLIENT_DEFAULT_ID_3.value, CLIENT_DEFAULT_VAL_3.value);
    updateXmlStoreDefaults(CLIENT_DEFAULT_ID_4.value, CLIENT_DEFAULT_VAL_4.value);
    updateXmlStoreDefaults(CLIENT_DEFAULT_ID_5.value, CLIENT_DEFAULT_VAL_5.value);
    updateXmlStoreDefaults(CLIENT_DEFAULT_ID_5.value, CLIENT_DEFAULT_VAL_5.value);

    // year
    var dToday = new Date();
    //alert(dToday);
    var dM = '0' + dToday.getMonth();
    dM = parseInt(dM.substr(dM.length-2, dM.length)) + 1;
    //alert(dM);
    var dD = '0' + dToday.getDay()
    dD = dD.substr(dD.length-2, dD.length);
    //alert(dD);
    var dY = dToday.getFullYear();
    //alert(dY);
    updateXmlStoreDefaults(CLIENT_DEFAULT_ID_6.value,  dY);
    // doc date
    updateXmlStoreDefaults(CLIENT_DEFAULT_ID_7.value, dM + '/' + dD + '/' + dY);
    
}
