function showCount(textBox,countBox,maxLength)
{
    var lengthLeft = maxLength-parseInt(document.getElementById(textBox).value.length);
    if(lengthLeft<0)
        document.getElementById(countBox).innerHTML = '<span style="color:#f00; font-weight:bold;">0</span>';
    else
        document.getElementById(countBox).innerHTML = lengthLeft;
}

function confirmDelete(message)
{
    return confirm(message);
}

function futureAlert()
{
    alert("This functionality has not been implemented.");
}

function confirmOption(message,resultControl)
{
    document.getElementById(resultControl).value = confirm(message);
    return true;
}

 function IsValidDate(year,month,day,mandatory)
 {
    var dtDate;

    if(year==0&&month==0&&day==0)
    {
        return !mandatory;
    }
    else
    {
        month--;
        dtDate=new Date(year,month,day);

        return ((day==dtDate.getDate()) && (month==dtDate.getMonth()) && (year==dtDate.getFullYear()));
    }
 }

/* PRN 2907 */ 
function resizablePopUp( pageurl, name, width, height, resizable, scrollable)
{
	window.open( pageurl, name, "width=" +width + ",height=" + height + ",resizable=" + resizable + ",scrollbars=" + scrollable);
	return false;
}

function SelectDefault(id,defaultText)
{
    document.getElementById(id).focus();
    if(document.getElementById(id).value==defaultText)
        document.getElementById(id).select();
}

function toggleElement(id)
{
    document.getElementById(id).style.display=document.getElementById(id).style.display=='none'?'block':'none';
}

function Validate(array,arguments)
{
    file = arguments.Value;
    if(file != "")
    {
        while (file.indexOf("\\") != -1)
            file = file.slice(file.indexOf("\\") + 1);
        ext = file.slice(file.indexOf(".")).toLowerCase();
        for (var i = 0; i < array.length; i++) 
        {
            if (array[i] == ext) 
            { 
                arguments.IsValid = true;
                return;
            }
        }
    }
    arguments.IsValid = false;
}

function trim(str) {
	return ltrim(rtrim(str, ' '), ' ');
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function makeRequest(url,item,event) {
        e = getEvent(event); 
        if(e["keyCode"]==27)
        {
            closeDiv();
            return;
        }
        else if(e["keyCode"]==38)
            TagKeyUp();
        else if (e["keyCode"]==40)
            TagKeyDown();
        else if (e["keyCode"]==13)
        {
            TagKeyEnter();
           return false;
        }
        else
        {         
            // Parse the text 
            // Check if the current text is more than 3 chars
            var text=item.value;
            var arrWords=item.value.split(",");
            var reqUrl=url;
            gItem=item;
            
            if(text=="")
            {
                document.getElementById("AutoCompleteContainer").style.display="none";
                document.getElementById("AutoCompleteContainer").innerHTML="";
            }
            if(trim(arrWords[arrWords.length-1]).length>0)
            {
                var httpRequest;

                // Keep the CpdMatrixId for the example in a hidden field and 
                // pass in the request hard coded now
                reqUrl+="?Prefix="+trim(arrWords[arrWords.length-1])+"&CpdMatrixId=" + matrixId
                if (window.XMLHttpRequest) { // Mozilla, Safari, ...
                    httpRequest = new XMLHttpRequest();
                    if (httpRequest.overrideMimeType) {
                        httpRequest.overrideMimeType('text/xml');
                        // See note below about this line
                    }
                }
                else if (window.ActiveXObject) { // IE
                    try {
                        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                    }
                    catch (e) {
                        try {
                            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                        } 
                        catch (e) {}
                    }
                }

                if (!httpRequest) {
                    alert('Error :( Cannot create an XMLHTTP instance');
                    return false;
                }
                
                httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
                httpRequest.open('GET', reqUrl, true);
                httpRequest.send('');
            }
            else
            {
                // Clear the display
            }
           
        }
         return true;
    }

    function TagKeyUp()
    {
        if((arrTags == null)||(arrTags.length==0))
            return;
        else
        {
            for(i=0;i<arrTags.length;i++)
            {
                if(arrTags[i].length > 0)
                {
                    var item = document.getElementById(arrTags[i]);
                    if(item != null)
                    {
                        if(item.className=="selected")
                        {
                            changeStyle(document.getElementById(arrTags[i]));
                            if(i == 0)
                            {
                                changeStyle(document.getElementById(arrTags[arrTags.length-2]));
                            }
                            else
                                changeStyle(document.getElementById(arrTags[i-1]));
                            break;
                        }
                    }
                }    
            }
        }
    }

    function TagKeyDown()
    {
        if((arrTags == null)||(arrTags.length==0))
            return;
        else
        {
            for(i=0;i<arrTags.length;i++)
            {
                if(arrTags[i].length > 0)
                {
                    var item = document.getElementById(arrTags[i]);
                    if(item != null)
                    {
                        if(item.className=="selected")
                        {
                            changeStyle(document.getElementById(arrTags[i]));
                            if(i == arrTags.length-2)
                            {
                                changeStyle(document.getElementById(arrTags[0]));
                            }
                            else
                                changeStyle(document.getElementById(arrTags[i+1]));
                            break;
                        }
                    }
                }    
            }
        }
    }

    function TagKeyEnter()
    {
       if((arrTags == null)||(arrTags.length==0))
        return;
      else
      {
        for(i=0;i<arrTags.length;i++)
        {
            if(arrTags[i].length > 0)
            {
                var item = document.getElementById(arrTags[i]);
                if(item!=null)
                {
                    if(item.className=="selected")
                    {
                        selectValue(item);
                    }
                }
            }    
        }
      }
    }

    function alertContents(httpRequest) {
        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
                // Split it into an array 
                arrTags=httpRequest.responseText.split("|");
               if(arrTags.length>0)
                {
                    var strUl="<ul>";
                    var hasContent = false;
                    //strUl+="<li class='heading'>Matching Tags <div style='width:100%;text-align:right;display:inline'><a href='javascript:closeDiv()'>[Close]</a></div></li>";         
                    for(var i=0;i<arrTags.length;i++)
                    {
                        if(arrTags[i].length>1)
                        {
                            if(i==0)
                                strUl+="<li onclick=selectValue(this) onmouseover=changeStyle(this) onmouseout=changeStyle(this) id='"+arrTags[i]+"' class='selected'>"+arrTags[i]+"</li>";
                            else
                                strUl+="<li onclick=selectValue(this) onmouseover=changeStyle(this) onmouseout=changeStyle(this) id='"+arrTags[i]+"' class='normal'>"+arrTags[i]+"</li>";
                                hasContent = true;
                        }
                    }
                    strUl+="<ul>";
                    if(hasContent)
                    {
                        document.getElementById("AutoCompleteContainer").innerHTML=strUl;
                        document.getElementById("AutoCompleteContainer").style.display="block";
                    }
                    else
                        closeDiv();
               }
               else
               {
                    closeDiv();
               }
            } 
          }
    }
    
    function BuildItems(tag)
    {
        return "<li class='normal'>"+tag+"</li>";        
    }
    
    function KeyDown(array, index)
    {
        alert(index);
    }
    
    function changeStyle(item)
    {
        if(item.className=="normal")
            item.className="selected";
        else
            item.className="normal";
    }
    
    function disableEnterKey(e)
    {
         var key;

         if(window.event)
              key = window.event.keyCode;     //IE
         else
              key = e.which;     //firefox

         if(key == 13)
              return false;
         else
              return true;
    }
    
  
    function selectValue(item)
    {
        var arrWords=gItem.value;
        
        // Reset Autocomplete field
        document.getElementById("AutoCompleteContainer").innerHTML="";
        document.getElementById("AutoCompleteContainer").style.display="none";
        gItem.value=arrWords.substring(0,arrWords.lastIndexOf(",")+1)+item.id;
        closeDiv();
    }
    
    function closeDiv()
    {
        document.getElementById("AutoCompleteContainer").innerHTML="";
        document.getElementById("AutoCompleteContainer").style.display="none";
    }
    
    function getEvent(event) {
        return (event ? event : window.event);
    }