// Returns true or false if the date is or isn't valid.
// http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C0800602A52
function isValidDate(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || 
      (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
   } else { // The year must be third
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
   }
   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else 
      if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else 
      if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else 
      if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}
function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft+1;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop+1;
}

function fnCheckUnCheck(checkBoxID, checkOpt) {
    var AllCheckBoxObjs = document.getElementsByTagName("input");
    
    if (!AllCheckBoxObjs || AllCheckBoxObjs.length == 0) return;
    
    for(i=0;i<AllCheckBoxObjs.length;i++) {
        if (AllCheckBoxObjs[i].type == "checkbox") {
            var CheckBoxID = new String(AllCheckBoxObjs[i].id);
            if (CheckBoxID.indexOf(checkBoxID) != -1)
                AllCheckBoxObjs[i].checked = checkOpt?true:false;
        }

    }

    if (checkBoxID == 'cblProduct')
        fnRemoveFromOrderedList();
}

var PBMClicked = false;
var displayWarning = false;

function fnRemoveFromOrderedList() {
    if (orderedDrugList == undefined)
        return;

    orderedDrugList = document.getElementById(orderedDrugList.id);
    
    var len = orderedDrugList.options.length;
    for (j = len -1; j >= 0; j--) {
        orderedDrugList.remove(j)
    }
}

function fnProductClicked(cb, listID) {

    objSelect = listID;
    orderedDrugList = listID;
   
    if (cb.checked) {
        var option = document.createElement("option");
        option.value = getCBValue(cb);
        option.text = getCBValue(cb);
        option.innerText = getCBValue(cb);
        
        objSelect.appendChild(option);
      
        //objSelect.add(new Option(getCBValue(cb), getCBValue(cb)))
        }
        else {
            for (j = 0; j < objSelect.options.length; j++) {
                if(objSelect.item(j).innerHTML == getCBValue(cb))
                    objSelect.remove(j)
            }
        }

    }

function fnCheckForPBM(checkBoxID, total, cb, txtWarning)
{
    if (cb.checked && getCBValue(cb) == 'PBM')
    {
        PBMClicked = true;    
        displayWarning = true;
    }
    else if (!cb.checked && getCBValue(cb) == 'PBM')
    {
        PBMClicked = false;    
        displayWarning = false;
    }
    
    if (!PBMClicked)
        return;
    
    if (!displayWarning)
        return;
        
    var cnt=0;
    for (var i = 0; i < total; i++) 
    {
         var cb = document.getElementById(checkBoxID + '_' + i);
         if (cb.checked )
         {
            cnt++;     
         }
    }
    
    if (cnt > 1)
    {
        alert(txtWarning);
        displayWarning = false;
    }
    
    //warningDisplayed = true;
}

function getCBValue(cb)
{
    if( cb.nextSibling.tagName == 'LABEL')
        return cb.nextSibling.innerHTML;
        
}

function fcAlertWarning(checkBoxID, total, msg)
{
    for (var i = 0; i < total; i++) 
    {
         var cb = document.getElementById(checkBoxID + '_' + i);
         if (getCBValue(cb) == 'PBM')
         {
            alert(msg);
         }
    }
}

function replaceAll(text, strA, strB) {
    return text.replace(new RegExp(strA, "g"), strB);
}

function PageQuery(q) {
    if (q.length > 1)
        this.q = q.substring(1, q.length);
    else
        this.q = null;

    this.keyValuePairs = new Array();

    if (q) {
        for (var i = 0; i < this.q.split("&").length; i++) {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }

    this.getKeyValuePairs = function() { return this.keyValuePairs; }

    this.getValue = function(s) {
        for (var j = 0; j < this.keyValuePairs.length; j++) {
            if (this.keyValuePairs[j].split("=")[0] == s)
                return this.keyValuePairs[j].split("=")[1];
        }
        return false;
    }

    this.getParameters = function() {
        var a = new Array(this.getLength());
        for (var j = 0; j < this.keyValuePairs.length; j++) {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
        return a;
    }

    this.getLength = function() { return this.keyValuePairs.length; }
}

function queryString(key) {
    var page = new PageQuery(window.location.search);
    return unescape(page.getValue(key));
}