function openWin(url, name, width, height, toolbar, statusbar, scrollbar, resizable) {
	var properties = ""
	if (width != null) {
		properties += "width=" + width + ",";
	}
	if (height != null) {
		properties += "height=" + height + ",";
	}
	if (toolbar > 0) {
		properties += "toolbar,";
	}
	if (statusbar > 0) {
		properties += "status,";
	}
	if (scrollbar > 0) {
		properties += "scrollbars,";
	}
	if (resizable > 0) {
		properties += "resizable";
	}
	if (properties != "") {
		window.open(url, name, properties);	
	}
	else {
		window.open(url, name);
	}
}

function replaceAll( str, from, to ) 
{
  var idx = str.indexOf( from );


  while ( idx > -1 )
  {
      str = str.replace( from, to ); 
      idx = str.indexOf( from );
  }

  return str;
}

//////////////////// Usage ////////////////////////////////////
//    var string = "{6} 123 Main St {7} Vancouver CA {3}";   //
//    string = replaceAll( string, "{6}", "#" );             //
//    string = replaceAll( string, "{7}", "," );             //
//    string = replaceAll( string, "{3}", " " );             //
//////////////////// Usage ////////////////////////////////////

function stripHTMLTags(xszHTMLText)
{
  var htmlTagExpr= /<\S[^>]*>/g;
  zszPlainText = xszHTMLText.replace(htmlTagExpr,"");
  return zszPlainText;
}

function setCookie(jvarName, jvarValue, jvarDay)
{
	var cName = jvarName + "=" + escape(jvarValue);
	
	if (jvarDay)
	{
		var cDate = new Date();
		cDate.setTime(cDate.getTime() + (jvarDay*24*60*60*1000));
		var expDate = "; expires=" + cDate.toGMTString();
	}
	else var expDate = "";
	
	document.cookie = cName + expDate + ";path=/";	
	
}



function getCookie(jvarName)
{
     var docCookie = document.cookie;
     var index = docCookie.indexOf(jvarName + "=");
	
    if (index == -1)
    	return null;
	index = docCookie.indexOf("=", index) + 1;
	var endstr = docCookie.indexOf(";", index);
	if (endstr == -1) 
		endstr = docCookie.length;
	
    return unescape(docCookie.substring(index, endstr));
}

// ********* 

function getMediaRepoImage(thefield){	 
	var imgval = showModalDialog('/editor/popups/insert_image_name.html', article, 'resizable: no; help: no; status: no; scroll: no; ');	 
	if(!imgval==""){	 
         thefield.value = imgval;
	     // document.article.SummaryImageLarge.value = imgval;
	} 
}

function toggleAuditTrial(jvarLabel, jvarID)
{
	var auditTrialDiv = document.getElementById(jvarID);
	var orgImage = jvarLabel.childNodes[0].src;
	
	if(auditTrialDiv.style.display == 'none')
	{
		auditTrialDiv.style.display = 'block';
		jvarLabel.childNodes[0].src = "/jasmine3.0/images/jasmine/icon_down.gif";
	}
	else
	{
		auditTrialDiv.style.display = 'none';
		jvarLabel.childNodes[0].src = "/jasmine3.0/images/jasmine/icon_right.gif";
	}
}

function Trim(TRIM_VALUE)
{
  if(TRIM_VALUE.length < 1)
  {
    return"";
  }

  TRIM_VALUE = RTrim(TRIM_VALUE);
  TRIM_VALUE = LTrim(TRIM_VALUE);
  if(TRIM_VALUE == "")
  {
    return "";
  }
  else
  {
    return TRIM_VALUE;
  }
}

function RTrim(VALUE)
{
  var w_space = String.fromCharCode(32);
  var v_length = VALUE.length;
  var strTemp = "";
  if(v_length < 0)
  {
    return"";
  }
  var iTemp = v_length -1;

  while(iTemp > -1)
  {
    if(VALUE.charAt(iTemp) == w_space)
    {
    }
    else
    {
      strTemp = VALUE.substring(0,iTemp +1);
      break;
    }
    iTemp = iTemp-1;
  }
  return strTemp;
}

function LTrim(VALUE)
{
  var w_space = String.fromCharCode(32);
  if(v_length < 1)
  {
    return"";
  }
  var v_length = VALUE.length;
  var strTemp = "";

  var iTemp = 0;

  while(iTemp < v_length)
  {
    if(VALUE.charAt(iTemp) == w_space)
    {
    }
    else
    {
      strTemp = VALUE.substring(iTemp,v_length);
     break;
    }
    iTemp = iTemp + 1;
  }
  return strTemp;
}

function datediff(date1, date2)
{
  var d1 = date1;
  var d2 = date2;
  var day = 1000*60*60*24;
  var TwoHours = 1000*60*60*2;
  var diff = Math.ceil(((d2.getTime()-d1.getTime())-TwoHours)/(day));
  return diff;
}

function dateadd(date1, noofdays)
{
  var d1 = date1;
  var day = 1000*60*60*24;
  var TwoHours = 1000*60*60*2;
  var noofms = (noofdays * day) + TwoHours;
  var resultdate = d1.getTime() + noofms;
  return new Date(resultdate);
}
/*for toggling between 2 DIV(by ID), therefore one of the DIV has to manually insert "display:none;"*/
function switchDIV(jvarDIV, jvarDIV2)
{
	var divOne = document.getElementById(jvarDIV);
	var divTwo = document.getElementById(jvarDIV2);
		
	divOne.style.display = "none";
	divTwo.style.display = "block";
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}


function ConvertYYYYMMDD(DateToConvert, Seperator)
{
	retdate = "";
	if (DateToConvert!="") 
		{
		try
			{
			var datecomp = DateToConvert.split(Seperator);
			retdate = datecomp[2] + Right("0"+datecomp[1],2) + Right("0"+datecomp[0],2);
			}
		catch (e)
			{		
			}
		}
	
	return(retdate);
}
function listSelectAll(jvarId)
{
	var obj = document.getElementById(jvarId);
	
	for(var i = 0; i < obj.length; i++)
	{
	
		try
		{
			obj[i].selected = true;
		}
		catch(e)
		{
			alert(e);
			obj[i].setAttribute('selected', true);
		}
	}
}

// remember the the HTML should be TRIM before using this function e.g. no extra space/tab/carriage return
// In DOM, IE and firefox with take in the extra space/tab/carriage return as Child....
function moveBetweenListBox(jvarFromBoxId,jvarToBoxId)
{	
	var fromBox = document.getElementById(jvarFromBoxId);
	var toBox   = document.getElementById(jvarToBoxId);

    var aryFromBox = new Array();
    var aryToBox   = new Array();
	
	var TEXT_LABEL   = "label";
	var TEXT_VALUE   = "value";
	var EMPTY_STRING = "";
	
	var LABEL = 0;
	var VALUE = 1;
	var TEXT  = 2;	
	
	for(var i = fromBox.length - 1; i >= 0; i--)//backward remove, so the selected index wont be affected
	{
		if(fromBox[i].selected)
		{
                    try{
			var parent   = null;			
			var self     = fromBox[i];
			
			var strLabel = "";					
			var strValue = self.getAttribute(TEXT_VALUE);
			var strText  = self.text;
			
			if(fromBox[i].parentNode.getAttribute(TEXT_LABEL) != null)
			{			
				parent   = fromBox[i].parentNode;
				strLabel = parent.getAttribute(TEXT_LABEL);
			}
						
			aryFromBox.push(new Array(strLabel, strValue, strText));
			
			var childRemoved = false;
			
			if(parent != null)
			{	
				if(parent.childNodes.length <= 1)
				{
					parent.parentNode.removeChild(parent);//children will be removed as well
					childRemoved = true;
				}
			}
			
			if(!childRemoved)
				fromBox.remove(i);

                  }
                catch(e)
                {
                 //alert("1"+e);
                }

		}
	}
	

try
{
	for(var x = 0; x < aryFromBox.length; x++)//move to ToBox
	{
		var groupAdded = false;
		
		if(toBox.childNodes.length > 0)
		{
			for(var j = 0; j < toBox.childNodes.length; j++)
			{
				try
				{
					var existingLabel = toBox.childNodes[j].getAttribute(TEXT_LABEL);
					
					if(existingLabel != EMPTY_STRING)
					{
						if(existingLabel == aryFromBox[x][LABEL])
						{
							var group     = toBox.childNodes[j];					
							var newOption = createOPTION(aryFromBox[x][VALUE], aryFromBox[x][TEXT]);
							
							group.appendChild(newOption);
							groupAdded = true;
							break;
						}
					}
				}
				catch(e){}
			}
		}
		if(!groupAdded)
		{
			if(aryFromBox[x][LABEL] != EMPTY_STRING)
			{
				var newOptGroup   = document.createElement('OPTGROUP');
				newOptGroup.label = aryFromBox[x][LABEL];					
				var newOption     = createOPTION(aryFromBox[x][VALUE], aryFromBox[x][TEXT]);
				
				newOptGroup.appendChild(newOption);					
				toBox.appendChild(newOptGroup);
			}
			else
			{
				var newOption  = createOPTION(aryFromBox[x][VALUE], aryFromBox[x][TEXT]);
				toBox.appendChild(newOption);
			}
		}
	}

	}
catch(e)
{
 //alert("2"+e);
}	
}

//Clone Method To Copy Options Instead of Moving

function copyBetweenListBox(jvarFromBoxId,jvarToBoxId)
{	
	var fromBox = document.getElementById(jvarFromBoxId);
	var toBox   = document.getElementById(jvarToBoxId);

    var aryFromBox = new Array();
    var aryToBox   = new Array();
	
	var TEXT_LABEL   = "label";
	var TEXT_VALUE   = "value";
	var EMPTY_STRING = "";
	
	var LABEL = 0;
	var VALUE = 1;
	var TEXT  = 2;	
	
	for(var i = fromBox.length - 1; i >= 0; i--)//backward remove, so the selected index wont be affected
	{
		if(fromBox[i].selected)
		{
			var parent   = null;			
			var self     = fromBox[i];
			
			var strLabel = "";					
			var strValue = self.getAttribute(TEXT_VALUE);
			var strText  = self.text;
			
			if(fromBox[i].parentNode.getAttribute(TEXT_LABEL) != null)
			{			
				parent   = fromBox[i].parentNode;
				strLabel = parent.getAttribute(TEXT_LABEL);
			}
						
			aryFromBox.push(new Array(strLabel, strValue, strText));
			
			/*var childRemoved = false;
			
			if(parent != null)
			{	
				if(parent.childNodes.length <= 1)
				{
					parent.parentNode.removeChild(parent);//children will be removed as well
					childRemoved = true;
				}
			}
			
			if(!childRemoved)
				fromBox.remove(i);*/
		}
	}
	
	for(var x = 0; x < aryFromBox.length; x++)//move to ToBox
	{
		var groupAdded = false;
		
		if(toBox.childNodes.length > 0)
		{
			for(var j = 0; j < toBox.childNodes.length; j++)
			{
				try
				{
					var existingLabel = toBox.childNodes[j].getAttribute(TEXT_LABEL);
					
					if(existingLabel != EMPTY_STRING)
					{
						if(existingLabel == aryFromBox[x][LABEL])
						{
							var group     = toBox.childNodes[j];					
							var newOption = createOPTION(aryFromBox[x][VALUE], aryFromBox[x][TEXT]);
							
							group.appendChild(newOption);
							groupAdded = true;
							break;
						}
					}
				}
				catch(e){alert(e);}
			}
		}
		if(!groupAdded)
		{
			if(aryFromBox[x][LABEL] != EMPTY_STRING)
			{
				var newOptGroup   = document.createElement('OPTGROUP');
				newOptGroup.label = aryFromBox[x][LABEL];					
				var newOption     = createOPTION(aryFromBox[x][VALUE], aryFromBox[x][TEXT]);
				
				newOptGroup.appendChild(newOption);					
				toBox.appendChild(newOptGroup);
			}
			else
			{
				var newOption  = createOPTION(aryFromBox[x][VALUE], aryFromBox[x][TEXT]);
				toBox.appendChild(newOption);
			}
		}
	}		
}
 












function createOPTION(jvarValue, jvarText)
{
	var newOption       = document.createElement('OPTION');
	newOption.value     = jvarValue;
	newOption.innerText = jvarText; // for IE
	newOption.text      = jvarText; // for firefox
	
	return newOption;
}



function formatNumber(nStr) // add common to number. eg 1000 -> 1,000
		{
			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;
		}



function splitString(jvarString, jvarDelimiter)
{
	return jvarString.split(jvarDelimiter);
}

String.prototype.trim = function()// trim front and back
{
	return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");
}
String.prototype.trimFully = function()// trim front, middle and back
{
	return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"").replace(/\s+/g," ");
}

function convertToJSHashMap(jkeyArray,jvalArray)
{
  var jsEngineMap = new HashMap();
  for( i=0;i<jkeyArray.length;i++)
   {
         
         jsEngineMap.put(new String(jkeyArray[i]),new String(jvalArray[i]));
   }
   return jsEngineMap;
}
function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {
var i, j;
var prompt;

// empty existing items
for (i = selectCtrl.options.length; i >= 1; i--)
	selectCtrl.options[i] = null; 

j = 1;
//alert("selectCtrl" + selectCtrl.name + ":" + itemArray)
if (itemArray != null) 
	{
	// add new items
	for (i = 0; i < itemArray.length; i++) 
		{
		selectCtrl.options[j] = new Option(itemArray[i][0]);
		if (itemArray[i][1] != null) 
			selectCtrl.options[j].value = itemArray[i][1]; 			
		j++;
		}
	// select first item (prompt) for sub list
	selectCtrl.options[0].selected = true;
   }
}
// This is for setting target select value given a source array and source select object
/* TODO use this function for future dynamic record drop down settings
function setValueFromFillArray(targetValue,SourceArray,SourceObj,TargetObj) 
	{
	var found = false;
	for (i = 0; i < SourceArray.length; i++) 
		{
		for (x=0; x < SourceArray[i].length; x++)
			{			
			if (SourceArray[i][x][1] == targetValue)
				{				
				SourceObj.options[i+1].selected = true;
				fillSelectFromArray(TargetObj, ((SourceObj.selectedIndex == -1) ? null : SourceArray[SourceObj.selectedIndex-1]));
				SelectDropDownIgnoreCase(TargetObj,targetValue, false);
				found=true;
				break;
				}							
			}
		if (found == true)
			break;
		}	
   }
*/
/* Function for encoding URL
*/
 function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

/* Function for decoding URL 
*/
function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
} 


function getRadioValue(field) {

    var value="";
     for(var i = 0; i < field.length; i++)
        {
        if(field[i].checked)
            value = field[i].value;
        }
  return value;
}



function getListValues(field)
    {
    var values="";
     for(var i = 0; i < field.length; i++)
        {
        if(field[i].selected)
            values += ",'"+field[i].value+"'";
        }
    if(values != "")
        values=values.substr(1);
    return(values);
    }


