function GetBeginTime(asClockLayer, asYear, asMonth, asDay, asHour, asMinute, asSecond, aiServerTime, aiAdj) {
// require dnylayercontent.js
	var BeginTime = new Date(asYear, asMonth - 1, asDay, asHour, asMinute, asSecond);
	var CurTime = new Date();
	var Adj = aiAdj;			// Adjust difference between user and server, e.g. user time=2:05pm, server time=2:00pm, adj=+5 min
	if (aiServerTime != 0) {
		Adj = CurTime.valueOf() - aiServerTime * 1000;
	}
	
	var Diff = CurTime.valueOf() - BeginTime.valueOf() - Adj;
	var iSecUnit = 1000;
	var iMinUnit = iSecUnit * 60;

	var iMin = Math.floor( Diff / iMinUnit);
	var iSec = Math.floor( (Diff - iMin * iMinUnit) / iSecUnit);

	//var s = CurTime.valueOf() + " | " + aiServerTime+ " | " + aiServerTime + "  |   " + iMin + ":" + iSec;
	var sMin = "00" + iMin;
	var sSec = "00" + iSec;
	sMin = sMin.substr( sMin.length - 2 );
	sSec = sSec.substr( sSec.length - 2 );
	s = sMin + ":" + sSec;
	ChangeLayerContent(asClockLayer,s);
	setTimeout("GetBeginTime('"+asClockLayer+"',"+asYear + ","+asMonth + ", " + asDay + ","+ asHour+","+ asMinute+","+ asSecond +",0,"+Adj+")", 1000);
}

function GetBeginTime2(asClockLayer, asYear, asMonth, asDay, asHour, asMinute, asSecond, aiServerTime, aiAdj) {
// require dnylayercontent.js
	var BeginTime = new Date(asYear, asMonth - 1, asDay, asHour, asMinute, asSecond);
	var CurTime = new Date();
	var Adj = aiAdj;			// Adjust difference between user and server, e.g. user time=2:05pm, server time=2:00pm, adj=+5 min
	if (aiServerTime != 0) {
		Adj = CurTime.valueOf() - aiServerTime * 1000;
	}
	
	var Diff = CurTime.valueOf() - BeginTime.valueOf() - Adj;
	var iSecUnit = 1000;
	var iMinUnit = iSecUnit * 60;

	var iMin = Math.floor( Diff / iMinUnit);
	var iSec = Math.floor( (Diff - iMin * iMinUnit) / iSecUnit);

	var s = CurTime.valueOf() + " | " + aiServerTime+ " | " + aiServerTime + "  |   " + iMin + ":" + iSec;
	var sMin = "00" + iMin;
	var sSec = "00" + iSec;
	sMin = sMin.substr( sMin.length - 2 );
	sSec = sSec.substr( sSec.length - 2 );
	s = sMin;
	// + ":" + sSec;
	ChangeLayerContent(asClockLayer,s);
	setTimeout("GetBeginTime2('"+asClockLayer+"',"+asYear + ","+asMonth + ", " + asDay + ","+ asHour+","+ asMinute+","+ asSecond +",0,"+Adj+")", 60000);
}

function GetCurrentTime(asClockLayer) {
// require dnylayercontent.js
	var runTime = new Date();
	var hours = runTime.getHours();
	var minutes = runTime.getMinutes();
	var seconds = runTime.getSeconds();

	if (minutes <= 9) {
		minutes = "0" + minutes;
	}
	if (seconds <= 9) {
		seconds = "0" + seconds;
	}
	var s = hours + ":" + minutes + ":" + seconds;

	ChangeLayerContent(asClockLayer,s);
	setTimeout("GetCurrentTime('"+asClockLayer+"')", 1000);
}

function ChangeURLMenu (aoSel, abRestore) {
	var s = aoSel.options[aoSel.selectedIndex].value;
	if (s != '') {
		self.location.href = s;
	}
	if (abRestore) {
		aoSel.selectedIndex = 0;
	}
}

function Check_All(aoform, asParentCheckBoxName, asChildCheckBoxName){
	for (var i=0;i<aoform.elements.length;i++) {
		var e = aoform.elements[i];
		if (e.name == asChildCheckBoxName && e.type == 'checkbox') {
			e.checked = eval("aoform." + asParentCheckBoxName).checked;
		}
	}
}
function Remove_Check_All(aoCheckBoxParent, abChecked){
	if (!abChecked){
		aoCheckBoxParent.checked = false;
	}
}
function JoinCheckboxCheckedValues (aobjCheckbox) {
	var s = "";
	if (!aobjCheckbox.length) {
		if (aobjCheckbox.checked) {
			s = aobjCheckbox.value;
		}
	} else {
		for (i=0; i<aobjCheckbox.length; i++)  {
			if (aobjCheckbox[i].checked) {
				if (s != "") {
					s += ",";
				}
				s += aobjCheckbox[i].value;
			}
		}
	}
	return s;
}

function SetImageSize(aobjImg, aiMaxWidth, aiMaxHeight) {
	var x = aobjImg.width;
	var y = aobjImg.height;

	var r1 = aiMaxWidth / x ;
	var r2 = aiMaxHeight / y;

	if (r2 < r1 && r2 > 1) {
		r1 = r2;
	}
	if (r1 < 1) {
		x = r1 * x;
		y = r1 * y;
		aobjImg.width = x;
		aobjImg.height = y;
	}
}

/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object   the table row
 * @param   string   the action calling this script (over, out or click)
 * @param   string   the default background color
 * @param   string   the color to use for mouseover
 * @param   string   the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
 // e.g, setPointer(this, "over", "#FFFFFF", "#FF0000", "#FFFF00")
function setPointer(theRow, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor = theMarkColor;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()) {
        if (theAction == 'out') {
            newColor = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor = theMarkColor;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor = (thePointerColor != '')
                     ? thePointerColor
                     : theDefaultColor;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function


function Replace(asString, asSearch, asReplace){
	ysTemp = asString.split(asSearch);
	return ysTemp.join(asReplace);
}

function jsLeft(asString, aiLength){
	var iStrLen = asString.length;
	return asString.substring(0, aiLength);
}
function jsRight(asString, aiLength){
	var iStrLen = asString.length;
	return asString.substring(asString.length - aiLength, asString.length);
}

function jsDateAdd(asInterval, aiNumber, adtDate){
	var dtOldDate = new Date(adtDate);
	var iOldVarDate = dtOldDate.valueOf();
	var iNewVarDate = iOldVarDate;
	var iAddNum = 0;
	var iSecUnit = 1000;
	var iMinUnit = iSecUnit * 60;
	var iHourUnit = iMinUnit * 60;
	var iDayUnit = iHourUnit * 24;
	var iWeekUnit = iDayUnit * 7;
	
	switch (asInterval){
		case "s" : 
			iAddNum = aiNumber * iSecUnit;
			iNewVarDate += iAddNum;
			break;
		case "n" : 
			iAddNum = aiNumber * iMinUnit;
			iNewVarDate += iAddNum;
			break;
		case "h" : 
			iAddNum = aiNumber * iHourUnit;
			iNewVarDate += iAddNum;
			break;
		case "d" : 
			iAddNum = aiNumber * iDayUnit;
			iNewVarDate += iAddNum;
			break;
		case "w" : 
			iAddNum = aiNumber * iWeekUnit;
			iNewVarDate += iAddNum;
			break;
		case "m":
			iNewVarDate =  jsDateAddMonth(aiNumber, adtDate);
			break;
		case "y":
			iNewVarDate =  jsDateAddMonth(aiNumber * 12, adtDate);
			break;
	}
	var dtReturnDate = new Date(iNewVarDate);
//	alert(iAddNum+"\n"+dtOldDate + "\n" +dtReturnDate);
	return dtReturnDate;
}

function jsDateAddMonth(aiNumber, adtDate){
	var dtOldDate = new Date(adtDate);
	var iSec = dtOldDate.getSeconds();
	var iMin = dtOldDate.getMinutes();
	var iHour = dtOldDate.getHours();
	var iDay = dtOldDate.getDate();
	var iMonth = dtOldDate.getMonth();
	var iYear = dtOldDate.getYear();
	var iYearChange = 0;

	iMonth += aiNumber;

	iYearChange = Math.ceil(iMonth / 12);
	iYear += iYearChange;
	iMonth -= iYearChange * 12;

	var dtReturnDate = new Date(iYear, iMonth, iDay, iHour, iMin, iSec);
	return dtReturnDate.valueOf();
}

function jsFormatDate(aoDate, asStyle){
	var dtDate = new Date(aoDate);
	
	var sFormatDate = jsRight('0000'+dtDate.getFullYear(), 4) + '/' + jsRight('00'+(dtDate.getMonth()+1), 2) + '/' + jsRight('00'+dtDate.getDate(), 2);
	return sFormatDate;

}

function FormatNumber (asNum, aiDecPlace) {
	var f = CDbl(asNum);
	if (f == NaN) {
		return NaN;
	}
	var d = Math.pow(10, aiDecPlace);
	f *= d;
	f = Math.round(f) / d;
	s = f.toString();
	dp = s.indexOf(".");
	if (dp == -1) {
		dp = s.length;
	}
	intPart = s.substr(0, dp);
	decPart = s.substr(dp+1, s.length) + RepeatString("0", aiDecPlace);
	decPart = decPart.substr(0, aiDecPlace);
	return AddComma(intPart, 3) + "." + decPart;
}
function AddComma(asString, aiPartLength){
	var ysTemp = String2Array(asString, aiPartLength);
	return ysTemp.join(",");
}

function String2Array(asString, aiPartLength){
	var iStrLen = asString.length;
	var ysArray = new Array();
	var sTemp = asString;
	var i = 0;
	var j = 0;
	
	do {
		var start = asString.length-aiPartLength;
		if (start < 0) {
			start = 0;
		}
		sTemp = asString.substr(start, asString.length);
		if (sTemp == ""){
			break;
		}
		ysArray[i] = sTemp;
		j += aiPartLength;
		asString = asString.substr(0, asString.length-aiPartLength);
		i++;
	} while (j <= iStrLen);
	ysArray.reverse();
	return ysArray;
}

function RepeatString(asString, iNoOfTime){
	var s = "";
	for (i = 0; i<iNoOfTime; i++){
		s += asString;
	}
	return s;
}
function CDbl(asNum){
	return parseFloat(asNum);
}

function PreviewHTML(asString) {
	var s = asString;
	win = window.open(", ", 'Preview', 'toolbar = no, status = no');
	win.document.write("" + s + "");
	win.document.close();
	win.window.focus();
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else 
	countfield.value = maxlimit - field.value.length;
}

function IsValidEmail(asTempString)
{
   var atCount = 0;
   var periodCount = 0;
   var strLen = asTempString.length;
   var i;
   var temp;
   var validchar= /^\w|[@.-]$/; //valid char = a-z, A-Z, @ , . , _ , -

   if (asTempString == '')
     return false;
     
   for (i = 0; i < strLen; i++)
      {
      temp = asTempString.substring(i, i+1);
	  if (temp == '@') 
         atCount++;
      if (temp == '.') 
         periodCount++;
	  if (!validchar.test(temp)){
//		alert(temp)
		return false;
		}
	  }
   if ((atCount == 1) && (periodCount > 0))
      return true;
   else
      return false;

}

function ChangeQty(aoQty, aiQtyChange, aiMin_Max){
	var iProdQty = parseInt(aoQty.value);
	iProdQty += aiQtyChange;
	if (aiQtyChange > 0) {
		if (iProdQty > aiMin_Max) {
			iProdQty = aiMin_Max;
		}
	} else {
		if (iProdQty < aiMin_Max) {
			iProdQty = aiMin_Max;
		}
	}
	aoQty.value = iProdQty;
}

function SetSelectedIndex(aoSelect, asFindValue){
	for (i=0; i < aoSelect.options.length; i++) {
		if (asFindValue == aoSelect.options[i].value) {
			aoSelect.options.selectedIndex = i;
			return;
		}
	}
}

function SetChecked(aoSelect, aoCheck, aoInput, asFindValue){
	if (aoSelect.options[aoSelect.options.selectedIndex].value == asFindValue){
		aoCheck.checked = true;
		aoInput.disabled = false;
	} else {
		aoCheck.checked = false;
		aoInput.value = '';
		aoInput.disabled = true;
	}
}

function ChangeMatchRateStatus(aoSelect, aiMatchRateCompId) {
	f = document.form1;
	var s = aoSelect.options[aoSelect.selectedIndex].value;
	if (s != '') {
		for (var i=0;i<f.elements.length;i++) {
			var e = f.elements[i];
			if (e.type == 'select-one') {
				if (e.name.indexOf('MatchRateStatus_' + aiMatchRateCompId) != -1) {
					SetSelectedIndex(e, s);
				}
			}
		}
	}
	aoSelect.selectedIndex = 0;
}

function openBrWindow(theURL,winName,features) { //v2.0
	var w1 = window.open(theURL,winName,features);
}
