var oCurrItem = null;

function ToggleDisplay(oItem)
{
	if (oItem.style.display == '')
			oItem.style.display = 'none';
	else
		{
			if (oCurrItem != null)
				oCurrItem.style.display = 'none';
			oItem.style.display = '';
			oCurrItem = oItem;
		}
}

function SwitchDisplay(oItem)
{
	if (oItem.style.display == '')
			oItem.style.display = 'none';
	else
		{
			oItem.style.display = '';
		}
}

function MoveUpLBItem(oList)
{
	var nSelIndex = oList.selectedIndex;
		
	if (nSelIndex<0)
		alert("Select a field to move");
	else if (nSelIndex>0)
	{
		sSaveText = oList.options[nSelIndex].text;
		sSaveValue = oList.options[nSelIndex].value;
			
		oList.options[nSelIndex].text = oList.options[nSelIndex-1].text;
		oList.options[nSelIndex].value = oList.options[nSelIndex-1].value;
			
		oList.options[nSelIndex-1].text = sSaveText;
		oList.options[nSelIndex-1].value = sSaveValue;
			
		oList.selectedIndex--;
	}
}
	
function MoveDnLBItem(oList)
{
	var nSelIndex = oList.selectedIndex;
		
	if (nSelIndex<0)
		alert("Select a field to move");
	else if (nSelIndex < oList.length-1)
	{
		sSaveText = oList.options[nSelIndex].text;
		sSaveValue = oList.options[nSelIndex].value;
			
		oList.options[nSelIndex].text = oList.options[nSelIndex+1].text;
		oList.options[nSelIndex].value = oList.options[nSelIndex+1].value;
			
		oList.options[nSelIndex+1].text = sSaveText;
		oList.options[nSelIndex+1].value = sSaveValue;
			
		oList.selectedIndex++;
	}
}

//number of  selected options in a multiselect SELECT 
function GetMSelItemsCount(oMSelItem)
{
	var nItem, nTotSel = 0;

	for(nItem = 0; nItem < oMSelItem.options.length; nItem++)
		if(oMSelItem.options[nItem].selected)
			nTotSel++
				
	return nTotSel;
}

//number of items in a inputs group (inputs with the same name)
function GetItemsCount(oMItem)
{
	if (!oMItem.length) 
		return 1;
	return oMItem.length;
}

//value of the selected item in an inputs group (radio inputs with the same name)
function GetSelItemValue(oMItem)
{
	if (!oMItem.length) 
		return (oMItem.checked ? oMItem.value : 0);
	
	for(var nItem=0; nItem < oMItem.length; nItem++)
		if(oMItem[nItem].checked)
			return oMItem[nItem].value;
		
	return null;
}

//number of checked inputs in a inputs group (inputs with the same name)
function GetSelItemsCount(oMItem)
{
	var nTotSel = 0;
	
	if (!oMItem.length) 
		return (oMItem.checked ? 1 : 0);
		
	for(var nItem=0; nItem < oMItem.length; nItem++)
		if(oMItem[nItem].checked)
			nTotSel++
		
	return nTotSel;
}

//concatenate checked inputs in a inputs group (inputs with the same name)
function GetSelItemsStr(oMItem, sPrefix)
{
	var szSel = '';
	
	if (!oMItem.length) 
		return (oMItem.checked ? sPrefix + oMItem.value : '');
		
	for(var nItem=0; nItem < oMItem.length; nItem++)
		if(oMItem[nItem].checked)
			szSel = szSel + sPrefix + oMItem[nItem].value + ',';
		
	return szSel;
}

function SelItems(oMItem, bChecked)
{
	var nItem;
	
	if (!oMItem.length)
		oMItem.checked = bChecked
	else
		for(nItem=0; nItem<oMItem.length; nItem++)
			oMItem[nItem].checked = bChecked
}

//set checked for all oField items like cbAll
function CheckAll(cbAll, oField)
{
    if (!oField) return;

    var newValue = cbAll.checked;
    SelItems(oField, newValue);
}

function isValidNum(sValue)
{
	sValue = sValue.replace(',', '')
	return !isNaN(sValue)
}
	
function parseMoney(sValue)
{
	if (sValue)
	{
		sValue = sValue.replace(/,/g, "");
		return parseFloat(sValue)
	}
	else
		return 0;
}

function SafeparseMoney(sValue)
{
	if (sValue != '' && isValidNum(sValue) )
	{
		sValue = sValue.replace(/,/g, "");
		return parseFloat(sValue)
	}
	else
		return 0;
}

/* like ASP FmtMoney */	
function FormatAmountFull(nValue, nDecimals)
{
	return number_format(nValue, nDecimals, '.', ',');
}
	
/* like ASP FmtGenNum */
function FormatAmount(nValue, nDecimals)
{
	return number_format(nValue, nDecimals, '.', '');
}
	
function FormatPerc(nValue, nDecimals)
{
	return number_format(nValue, nDecimals, '.', '');
}
	
function number_format(a, b, c, d) 
{
	// number_format(number, decimals, comma, formatSeparator)
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if(!f[0]) f[0] = '0';
	if(!f[1]) f[1] = '';
	if(f[1].length < b){
		g = f[1];
		for(i = f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j += 3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '': c;
	return f[0] + c + f[1];
}
	
function SelFileFolder(szFileExt, szFolder, szWebBase, nAbsLink, nSelFolder)
{
	var szRet, szFolder
	var szNewFolder, nPos
	var nPreview = 0, nViewAll = 0
	var sURL
	
	if (szFolder == '') 
		szFolder = szWebBase;
	
	while (true)
	{
		sURL = szWebBase + "Common/SelFile.asp?FileExts=" + szFileExt + "&StartFolder=" + szFolder + 
																		"&AbsLink=" + nAbsLink + "&Preview=" + nPreview + "&ViewAll=" + nViewAll + "&SelFolder=" + nSelFolder
		szRet = window.showModalDialog(sURL, null, "dialogWidth:800px;dialogHeight:600px")
		if (szRet == '')
			break;		
		
		if (IsLeft(szRet, "file="))
		{
			szRet = szRet.substring(5)
			if (!IsLeft(szRet, "/"))
				szRet = Slash(szFolder, (szFolder != "")) + szRet
			return szRet
		}
		else if (IsLeft(szRet, "folder="))
		{
			szNewFolder = szRet.substring(7)
			if (szNewFolder == "..")
			{
				nPos = szFolder.lastIndexOf("/")
				if (nPos>=0)
					szFolder = szFolder.substring(0, nPos)
				else
					szFolder = ""
			}
			else
			{
				szFolder = Slash(szFolder, true) + szNewFolder //(szFolder != "")
			}
		}	
		else if (IsLeft(szRet, "preview=0"))
			nPreview = false
		else if (IsLeft(szRet, "preview=1")) 
			nPreview = true
		else if (IsLeft(szRet, "viewall=0")) 
			nViewAll = false
		else if (IsLeft(szRet, "viewall=1")) 
			nViewAll = true
	}
	
	return '';
}

function SelectColor(oColorTextBox, sBaseFolder)
{
	var sColor = oColorTextBox.value;
	if (sColor = window.showModalDialog(sBaseFolder + "CMC/ColorPicker.asp?StartColor=" + escape(sColor), null, "dialogWidth:500px;dialogHeight:350px"))
	{
		oColorTextBox.value = sColor;
		return true;
	}
	else
		return false;
}
	
function IsLeft(sString, sValue)
{
	var szComp = sString.substring(0, sValue.length)
	if (szComp.toUpperCase() == sValue.toUpperCase())
		return true;
	else
		return false;
}
	
function IsRight(sString, sValue)
{
	var szComp = sString.substring(sString.length - sValue.length)
	if (szComp.toUpperCase() == sValue.toUpperCase())
		return true;
	else
		return false;
}
	
function BackSlash(szURLPath , bValue)
{
  szURLPath = trim(szURLPath)
  if (IsRight(szURLPath, "\\"))
	{
		if (bValue == false)
      szURLPath = szURLPath.substr(0, szURLPath.length-1);
  }
  else
    if (bValue == true && !IsRight(szURLPath, "\\"))
      szURLPath = szURLPath + "\\"
  return szURLPath;
}
	
function Slash(szURLPath , bValue)
{
  szURLPath = trim(szURLPath)
  if (IsRight(szURLPath, "/"))
  {
		if (bValue == false)
      szURLPath = szURLPath.substr(0, szURLPath.length-1);
  }
  else
  {
    if (bValue == true && !IsRight(szURLPath, "/"))
    {
      szURLPath = szURLPath + "/"
    }
  }
  return szURLPath;
}

/*
	function trim(stringToTrim) 
	{
		return stringToTrim.replace(/^\s+|\s+$/g,"");
	}
*/

// Removes leading whitespaces
function LTrim( value ) 
{
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) 
{
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) 
{
	return LTrim(RTrim(value));
}

function CapLetter(sName)
{
	return sName.substring(0,1).toUpperCase() + sName.substring(1).toLowerCase()
}

function CapWords(val) 
{
  newVal = '';
  val = val.split(' ');
  for(var c=0; c < val.length; c++) 
	{
    newVal += CapLetter(val[c]) + ' ' //.substring(0, 1).toUpperCase() + val[c].substring(1, val[c].length) + ' ';
  }
  return newVal;
}

function QuickSelect(oQField, oField, bStart)
{
	var nFound = 0;
	for (var nItem=1; nItem < oField.options.length; nItem++)
	{
		var sSearch = oQField.value
		var sValue = oField.options[nItem].innerHTML
		sValue = sValue.toUpperCase()
		if (bStart == 1)
		{
			sValue = sValue.substring(0, sSearch.length)
			if (sValue.toUpperCase() == sSearch.toUpperCase() )
			{
				oField.options[nItem].selected = true
				nFound = 1;
				break;
			}
		}
		else
			if (sValue.indexOf(sSearch.toUpperCase()) >= 0)
			{
				oField.options[nItem].selected = true
				nFound = 1;
				break;
			}
	}
	return nFound;
}

// search an item in the drop down list starting with sSearch; returns the optioin index or null if not found
// case sensitive   
function FindSelectValue(oField, sSearch)
{
	for (var nItem = 0; nItem < oField.options.length; nItem++)
	{
		var sValue = oField.options[nItem].innerHTML;
		if (sValue.indexOf(sSearch) >= 0)
			return nItem;
	}
	return null;
}

//SelectItem <select object>, <string to search>, <match mode>
//nMatchMode: 0=exact, 1=start, 2=substring
//note: not case sensitive
function SelectItem(oField, sSearch, nMatchMode)
{
	var nFound = 0;
	for (var nItem=1; nItem < oField.options.length; nItem++)
	{
		var sValue = oField.options[nItem].innerHTML
		sValue = sValue.toUpperCase()
		if (nMatchMode==0)
		{
			if (sValue.toUpperCase() == sSearch.toUpperCase() )
			{
				oField.options[nItem].selected = true
				nFound = 1;
				break;
			}
		}
		else if (nMatchMode == 1)
		{
			sValue = sValue.substring(0, sSearch.length)
			if (sValue.toUpperCase() == sSearch.toUpperCase() )
			{
				oField.options[nItem].selected = true
				nFound = 1;
				break;
			}
		}
		else
			if (sValue.indexOf(sSearch.toUpperCase()) >= 0)
			{
				oField.options[nItem].selected = true
				nFound = 1;
				break;
			}
	}
	return nFound;
}

String.prototype.pad = function(l, s, t){
  return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
      + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
      + this + s.substr(0, l - t) : this;
}
		
function SecToTime(nSecs)
{
	var sHour = parseInt(Math.floor(nSecs / 3600)).toString()
	var sMin = parseInt(Math.floor((nSecs % 3600) / 60)).toString()
		
	return sHour.pad(2, '0', 0) + ':' + sMin.pad(2, '0', 0)
}

//parse the time string and return the value in seconds
function IsValidTime(timeStr) 
{
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) 
		return -1;
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }

	if (hour < 0  || hour > 23) 
		return -1;
	if  (hour > 12 && ampm != null) 
		return -1;
	if (minute<0 || minute > 59) 
		return -1;
	if (second != null && (second < 0 || second > 59)) 
		return -1;

	return hour * 3600 + minute * 60 + second;
}

/*
if (IsDate(frmQBox.AssetAssignmentFromDate.value))
	alert('is a date');
else
	alert('is not a date');
var nValue = CompDates(frmQBox.AssetAssignmentFromDate.value , frmQBox.AssetAssignmentToDate.value )
alert(nValue);
return;
*/

function IsDate(sDate)
{
  var dDate = new Date(sDate);

  return (!isNaN(dDate))
}

function CompDates(sFrom, sTo)
{
  var dFrom = new Date(sFrom);
  var dTo = new Date(sTo);

  if (dFrom < dTo)
		return -1
	else if (dFrom > dTo)
		return 1;
	else
		return 0;
}

function daysInMonth(iMonth, iYear)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}

function fmtDateMDY(dDate)
{
	return (dDate.getMonth() + 1) + '/' + dDate.getDate() + '/' + dDate.getFullYear();
}

function DateAddDays(sDate, nDays)
{
	var dNewDate = new Date(sDate);
	dNewDate.setDate(dNewDate.getDate() + nDays);
	return dNewDate;
}

function RoundBy5(nPrice)
{
	return Math.round(nPrice * 2/ 10)* 10 /2
}

function GetItemPosLeft(myTarget)
{
	var left = 0, szSaveBorder;
	while(myTarget!= document.body) 
	{
		//szSaveBorder = myTarget.style.border;
		//myTarget.style.border = '2px dotted #df0000'
		//alert(myTarget.tagName + ' id=' + myTarget.id + ' left=' + myTarget.offsetLeft);
		//myTarget.style.border = szSaveBorder;
		left += myTarget.offsetLeft;      
		myTarget = myTarget.offsetParent;
		//break;
	}
	return left;
}
	
function GetItemPosTop(myTarget)
{
	var top = 0;
	while(myTarget!= document.body) 
	{      
		top += myTarget.offsetTop;      
		myTarget = myTarget.offsetParent;
		//break;
	}
	return top;
}

function CCardValidate(s) 
{
	//DINERS = 13, AMEX = 15, OTHER = 16
	// remove non-numerics
	var v = "0123456789";
	var w = "";
	for (i=0; i < s.length; i++) 
	{
		x = s.charAt(i);
		if (v.indexOf(x,0) != -1)
			w += x;
	}
		
	// validate number
	j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) 
		return false;
	k = Math.floor(j);
	m = Math.ceil(j) - k;
	c = 0;
	for (i=0; i<k; i++) 
	{
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);
}

function CountSubStr(szBuffer, szSubStr)
{
	var nPos=szBuffer.indexOf(szSubStr);
	var nCount=0;
					
	szBuffer=szBuffer.toLowerCase();
	szSubStr=szSubStr.toLowerCase();
	nPos=szBuffer.indexOf(szSubStr);
	while(nPos>=0)
		{
			nCount++;
			nPos=szBuffer.indexOf(szSubStr, nPos+1);
		}
	return nCount;
}

function RemHTMLTags(strInputCode)
{
 	return strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
}


