﻿/**************************************************
 **************************************************
 
           GENERAL UTIL FUNCTIONS

**************************************************
**************************************************/

function output(text)
{
    alert(text);
}

/* **************************************************/
function ValidateTextBoxInput(inputId, pattern)
{
    // this function is to be used to
    // validate the content of a textbox
    var ctrl = $get(inputId);
    if(ctrl)
    {
        var val = ctrl.value;
        if(val == '')
        {
            output("Please enter a value!");
            return null;
        }
        else
        {
            var reg = new RegExp(pattern);
            if(!reg.exec(val))
            {
                output("Invalid input: '" + val + "'");
                return null;
            }
            else
            {
                return val;
            }            
        }
    }
    else
    {
        output(inputId + " cannot be found!");
        return null;
    }
}
/* **************************************************/
function IsTextBoxInputContains(inputId, pattern)
{
    // this function is to be used to
    // match a particular pattern
    var ctrl = document.getElementById(inputId);
    if(ctrl)
    {
        var val = ctrl.value;
        var reg = new RegExp(pattern);
        if(!reg.exec(val))
        {            
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        output(inputId + " cannot be found!");
        return false;
    }
}
/* **************************************************/
function getURLParam(strParamName){
  var returnString = "";
  var url = window.location.href;
  if ( url.indexOf("?") > -1 ){
    var queryString = url.substr(url.indexOf("?")).toLowerCase();
    var queryStringArr = queryString.split("&");
    for ( var iParam = 0; iParam < queryStringArr.length; iParam++ ){
      if (
            queryStringArr[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
            var aParam = queryStringArr[iParam].split("=");
            returnString = aParam[1];
            break;
      }
    }
  }
  return unescape(returnString);
} 
/* **************************************************/
function writeToLabel(lblId, text)
{
    var lbl = $get(lblId);
     if (document.all) 
     {
        lbl.innerText = text;
     }
     else
     {
        lbl.textContent = text;     
     }
}
/* **************************************************/
function writeToPanel(pnlId, html, isPreserveContent)
{
    var pnlId = $get(pnlId);
    if(isPreserveContent)
    {
        pnlId.innerHTML += html;
    }
    else
    {
        pnlId.innerHTML = html;
    }
}
/* **************************************************/
function formatDistanceDisplay(distInMeters)
{
    // step 1 validate input
    if(distInMeters)
    {
            var reg = new RegExp("^[0-9]+\.?[0-9]*$");
            if(!reg.exec(distInMeters))
            {
                output('Distance submitted to the "formatDistanceDisplay" function is invalid: ' + distInMeters );
                return null;
            }
            else
            {                
                if((distInMeters / 1000) < 1)
                {
                    //distInMeters = distInMeters.toFixed(1);
                    return distInMeters + " metres";
                }
                else
                {
                  var roundedVal = distInMeters / 1000;
                  //roundedVal = roundedVal.toFixed(1);
                   return roundedVal + " kilometres";                
                }                
            }    
    }
    else
    {
        alert('No value submitted to the "formatDistanceDisplay" function!'); 
    }
}
/* **************************************************/
function ExtractRadioButtonSelection(rdoName)
{
    var rdo = document.getElementsByName(rdoName);
    if(rdo)
    {
        var selection = '';
        for(var i=0; i<rdo.length; i++)
        {
            if(rdo[i].checked)
            {
                selection = rdo[i].value;
                return selection;
            }    
        }
    }
    else
    {
        alert("Cannot find radio button(s): " + rdoName);
        return null;
    }
}
/* **************************************************/
function ExtractCheckboxButtonSelection(chckName)
{
    var chck = document.getElementsByName(chckName);
    if(chck)
    {
        var selection = [];
        for(var i=0; i<chck.length; i++)
        {
            if(chck[i].checked)
            {
                selection.push(chck[i].value);
            }    
        }
        return selection;
    }
    else
    {
        alert("Cannot find checkbox: " + chckName);
        return null;
    }
}

/* **************************************************/
function SetDropDownListSelection(dpdId, index)
{
    var dpd = $get(dpdId);    
    dpd.selectedIndex = index;    
}
/* **************************************************/
function regIsMacth(text, pattern, message)
{
    var reg = new RegExp(pattern);
    if(!reg.exec(text))
    {
        if((message) && (message != ''))
        {
            output(message);
        }
        return false;    
    }
    else
    {   
        return true;
    }
}
/* *************************************************** */
function formatString(string, emptyText)
{
    if((!string)||(string == ''))
    {
        string = emptyText;
    }
    return string;
}
/* *************************************************** */
function showHidePanel(pnlId, isChangeDisplayStyle, isReturnVoid, triggerButtonId, buttonTextShow, buttonTextHide, isButton)
{
    var pnl = document.getElementById(pnlId);
    var triggerButton = document.getElementById(triggerButtonId);
    var showText = "show";
    var hideText = "hide";
    if(buttonTextShow)
        showText = buttonTextShow;
    if(buttonTextHide)
        hideText = buttonTextHide;    
    if(pnl)
    {
        if(triggerButton)
        {
             if(isChangeDisplayStyle)
            {
                if(pnl.style.display == "none")
                {
                    pnl.style.display = "block";
                    if(isButton)
                    {
                        triggerButton.value = hideText
                    }
                    else
                    {
                        triggerButton.innerHTML = hideText;
                    }
                    
                }
                else
                {
                    pnl.style.display = "none";
                     if(isButton)
                    {
                        triggerButton.value = showText
                    }
                    else
                    {
                        triggerButton.innerHTML = showText;
                    }
                }             
            }
            else
            {
                if(pnl.style.visibility == "hidden")
                {
                    pnl.style.visibility = "visible";
                }
                else
                {
                    pnl.style.visibility = "hidden";
                }
            }
        }
        else
        {
            alert("Cannot find trigger button: " + triggerButtonId);
        }        
       
    }
    else
    {
        alert("Cannot find panel: " + pnlId);
    }
    if(isReturnVoid)
    {
        return void(0);
    }    
}
/* *************************************************** */
function trimTrailingComma(stringToTrim) {
    if((stringToTrim)&&(stringToTrim.length > 0))
    {
        return stringToTrim.replace(/,$/,"");
    }
    else
    {
        return '';
    }                        
}
/* *********************************************** */
// ensure the return value is not null
function isSet(value, valueName)
{
    if(!value)
    {
        if(valueName)
        {
            alert("Cannot find value: '" + valueName + "'!");
        }
        else
        {
            alert("isSet found NULL value!");
        }
        return null;        
    }
    return value;
}
/* *********************************************** */
// takes in a specially formatted icon url contant
// and return and "iconUrl" object
function iconUrlParsing(url)
{
    var arr = url.split(",");
    var iu = new epothecary.mapping.IconUrl(arr);
    return iu;
}

/* *********************************************** */
// given a activity rating fugure this function build up a 
// list of star
function buildRatingStarsAsHtml(currentRating)
{
    var html = '';
    html += "<table style='width:auto;'><tr>";
    var i = 0;
    for(i = 0; i<5; i++)
    {
        html += "<td>";
        var starFilename = "EmptyStar.png";
        if(i <= currentRating)
        {
            starFilename = "FilledStar.png";
        }
        html += "<img id='imgActivityRating" + i + "' width='13' height='13' src='images/rating/" + starFilename + "' alt='Overall pharmacy activity rating'  />";
        html += "</td>";
    }
    html += "</tr></table>";
    return html;
}
/* *********************************************** */
function getElementById(id)
{
    var ele = document.getElementById(id);
    if(!ele)
    {
        output("Element id: " + id + " cannot be found!");
    }
    return ele;
}
