// JavaScript Document
function goTo(url)
{
	window.location = url;
}

function onToggleProgress()
{
    var elem = document.getElementById('progress');
    if (elem.className == 'hidden')
    {
        setElementText('toggleProgress', 'hide progress');
        elem.className = 'normal';
    }
    else
    {
        setElementText('toggleProgress', 'show progress');
        elem.className = 'hidden';
    }
}

function onMoreLink(idVal)
{
    var elem = document.getElementById(idVal);
    if (elem.className == 'hidden')
    {
        setElementText('link' + idVal, '...close');
        elem.className = 'normal';
    }
    else
    {
        setElementText('link' + idVal, 'more...');
        elem.className = 'hidden';
    }
}

function setElementText(elem, text)
{
	elem = document.getElementById(elem);
	elem.innerHTML = text;
}

function onToggleRow(idVal, action)
{
    if (action == 'expand')
    {
        document.getElementById('expand' + idVal).style.display = 'none';
        document.getElementById('collapse' + idVal).style.display = 'inline';
        document.getElementById('row' + idVal).style.display = '';
    }
    else
    {
        document.getElementById('expand' + idVal).style.display = 'inline';
        document.getElementById('collapse' + idVal).style.display = 'none';
        document.getElementById('row' + idVal).style.display = 'none';
    }

}
function onToggleArea(idVal, action)
{
    if (action == 'show')
    {
        document.getElementById(idVal).style.display = '';
    }
    else
    {
        document.getElementById(idVal).style.display = 'none';
    }

}
function setClassById(idVal, classVal)
{
    getObject(idVal).className = classVal;
}

function getObject(objectId) 
{
    if (document.all && !document.getElementById) 
    {
        return document.all(objectId);
    } 
    else 
    {
        return document.getElementById(objectId);
    }
}

function expandAllRows() 
{
    for (i=1;i<=rowCount;i++)
    {
        document.getElementById('expand' + i).style.display = 'none';
        document.getElementById('collapse' + i).style.display = 'inline';
        document.getElementById('row' + i).style.display = '';
    }
}

function collapseAllRows() 
{
    for (i=1;i<=rowCount;i++)
    {
        document.getElementById('expand' + i).style.display = 'inline';
        document.getElementById('collapse' + i).style.display = 'none';
        document.getElementById('row' + i).style.display = 'none';
    }
}

/* help functions */

var browser=new yg_Browser();
function yg_Browser()
{
    d=document;
    this.agt=navigator.userAgent.toLowerCase();
    this.major=parseInt(navigator.appVersion);
    this.dom=(d.getElementById);
    this.ns=(d.layers);
    this.ns4up=(this.ns && this.major>=4);
    this.ns6=(this.dom&&navigator.appName=="Netscape");
    this.op=(window.opera);
    if(d.all)this.ie=1;else this.ie=0;
    this.ie4=(d.all&&!this.dom);
    this.ie4up=(this.ie&&this.major>=4);
    this.ie5=(d.all&&this.dom);
    this.ie6=(d.nodeType);
    this.sf=(this.agt.indexOf("safari")!=-1);
    this.win=((this.agt.indexOf("win")!=-1)||(this.agt.indexOf("16bit")!=-1));
    this.winme=(this.agt.indexOf("win 9x 4.90")!=-1);
    this.xpsp2=(this.agt.indexOf("sv1")!=-1);
    this.mac=(this.agt.indexOf("mac")!=-1);
}

//switch layers for different browsers");
var ie4 = (document.all) ? true : false;
var ns4 = (document.layers) ? true : false;
var ns6 = (document.getElementById && !document.all) ? true : false;
function hidelayer(lay)
{
    if (ie4) {document.all[lay + "_help"].style.visibility = "hidden";}
    if (ns4) {document.layers[lay + "_help"].visibility = "hide";}
    if (ns6) {document.getElementById([lay + "_help"]).style.display = "none";}
    if (lay == window.currentHelp) {
        window.currentHelp = false;
    }
    document.images[lay + "_image"].src = "/nsaarc/images/help_medium.gif";
}
function showlayer(lay)
{
    oldLay = window.currentHelp;
    hideCurrentHelp();
    if (lay != oldLay) {
        if (ie4) {document.all[lay + "_help"].style.visibility = "visible";}
        if (ns4) {document.layers[lay + "_help"].visibility = "show";}
        if (ns6) {document.getElementById([lay + "_help"]).style.display = "block";}
        window.currentHelp = lay;
        document.images[lay + "_image"].src = "/nsaarc/images/help_medium_light.gif";
    }
}
function hideCurrentHelp() {
    if (window.currentHelp) {
        hidelayer(window.currentHelp);
        window.currentHelp = false;
    }
}

function onZipcodeChange(href, zipCode)
{
    var zipcode = document.editForm.zipCode.value;
	if (zipcode.length == 5)
	{
        var url = href + '&command=counties&zipCode=' + zipCode;
        
		//document.editForm.command.value = 'counties';
		//var inputs = document.getElementsByTagName('input');
		//for (var i = 0; i < inputs.length; i++)
		//{
        //    var currentInput = inputs[i];
        //    url += currentInput.name + '=' + currentInput.value + '&';
		//}
        //alert(url);
        //document.editForm.command.value = 'save';
        var xmlhttp = getXMLHttpRequest();
        xmlhttp.open('POST', url, true);
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4)
            {
                var county = document.getElementById('county');
                county.innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.send(null);

    }
}

function clearParentOrganization()
{
    document.getElementById('parentDuns').value = '';
    document.getElementById('parentInfo').innerHTML = '<em>No parent organization found</em>';
}

function findParentOrganization(href)
{
    var companyDuns = document.getElementById('companyDuns').value;
    var parentDuns = document.getElementById('parentDuns');
    var dunsElm = document.getElementById('search');
    var duns = dunsElm.value;
    var info = document.getElementById('parentInfo');
    var error = '';
    
    // default the parent duns value
    parentDuns.value = '';
    
    if (duns == companyDuns )
    {
        error = 'You cannot use YOUR organization\'s DUNS number for your parent organization.  Please use a different DUNS number.';
    }
	else if (duns.length == 9 && isNumeric(duns))
	{
	    info.innerHTML = 'Searching CCR...';
        var url = href + '&command=duns&duns=' + duns;
        var xmlhttp = getXMLHttpRequest();
        xmlhttp.open('POST', url, true);
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4)
            {
                var response = xmlhttp.responseText;
                //alert(response);
                if (response.length == 0)
                {
                    //parentDuns.value = '';
                    response = '<span class="errorText">No match found in CCR for DUNS \'' + duns + '\'.  Please verify that the number you entered is correct and try again.</span>';
                    //dunsElm.focus();
                }
                else
                {
                    parentDuns.value = duns;
                }
                info.innerHTML = response;
                
            }
        };
        xmlhttp.send(null);
    }
    else
    {
        //parentDuns.value = '';
        error = 'Invalid DUNS entered.  Please enter a valid DUNS number.';
        //dunsElm.focus();
    }
    
    if (error != '')
    {
        info.innerHTML = '<span class="errorText">' + error + '</span>';
        dunsElm.focus();
    }
}

function isNumeric(elem)
{
    var numericExpression = /^[0-9]+$/;
    if(elem.match(numericExpression))
    {
		return true;
	}
    return false;
}

function onUpdateCounties()
{
    document.editForm.command.value = '';
    document.editForm.submit();
}

function onSaveAdd()
{
    document.editForm.command.value = 'save-add';
    document.editForm.submit();
}

function onDelete(id, desc)
{
    var result = confirm("Are you sure you want to delete this record [" + desc + "]?");
    if (result)
    {
        document.deleteForm.id.value = id;
        document.deleteForm.submit();
    }
}

function onDeleteAddress(id, desc)
{
    var result = confirm("Are you sure you want to delete address '" + desc + "'?  If you continue, the contacts for this address will be re-assigned to your Physical Address.");
    if (result)
    {
        document.deleteForm.command.value = 'delete-address';
        document.deleteForm.id.value = id;
        document.deleteForm.submit();
    }
}

function onDeleteContact(id, desc)
{
    var result = confirm("Are you sure you want to delete contact '" + desc + "'?");
    if (result)
    {
        document.deleteForm.command.value = 'delete-contact';
        document.deleteForm.id.value = id;
        document.deleteForm.submit();
    }
}

function addNAICSCode(href, code)
{
    var url = href + '&command=add&naicsCode=' + code;
    var xmlhttp = getXMLHttpRequest();
	xmlhttp.open('POST', url, true);
	xmlhttp.onreadystatechange=function()
    {
	   if (xmlhttp.readyState==4)
        {
            var codeId = document.getElementById(code);
            codeId.innerHTML = xmlhttp.responseText;
        }
    };
    xmlhttp.send(null);
}

function checkDocumentUploadForm(form)
{
    if (empty(form.title.value))
    {
        alert('Please enter a title for the document.');
        form.title.focus();
        return false;
    }

    return true;
    
}

function empty(val)
{
    if (val == null || trim(val).length == 0)
        return true;
     else
        return false;
}

function trim(s)
{
    while (s.substring(0,1) == ' ')
    {
        s = s.substring(1, s.length);
    }
    while (s.substring(s.length-1, s.length) == ' ')
    {
        s = s.substring(0,s.length-1);
    }
    return s;
}

function onToggleProductLine(productLineKey, visible, hasChildren)
{
    if (!hasChildren)
        return;

    var elemName = 'childrenDiv' + productLineKey;
    if (visible)
	{
        setElementClass(elemName, 'children');
    }
    else
    {
        setElementClass(elemName, 'hidden');
    }
}
