IsIE = (document.all);
IsNS = (document.layers);

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")||(parseInt(appVersion)==4)) {
    document.MM_pgW=this.innerWidth; document.MM_pgH=this.innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
//MM_reloadPage(true);

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
  if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function Button_On(Obj)	{ //v3.0
  Obj.style.color='yellow';
}

function Button_Off(Obj){ //v3.0
  Obj.style.color='navy';
}

/***************************************************************
 * Description : Set a layer object's inner HTML code with     *
 *				 an object reference and dynamically go with   * 
 *				 the reference object's position.			   *
 * ----------------------------------------------------------- *	
 * @param   object		Any HTML TAG Object					   *	
 * @param   tagID		The Layer Object ID					   *	 
 * @param   innerHTML	The HTML Code to be added into the Tag *
 *															   *	
 * @return  no return										   *
 * ----------------------------------------------------------- *
 * Date			Added By		Version No.					   *
 * ----			-----------		-----------					   *
 * 10 Mar 2003  Jeff Ching		v1.0 Function Created		   *
 **************************************************************/
function SetNewImgPos(object, tagID){
	var ImgStyleLeft;
	var ImgStyleTop;
	var oDiv;
    var innerHTML = "<img src='images/new.gif' width='28' height='11'>";

	ImgStyleLeft = object.offsetLeft + 65;
	ImgStyleTop = object.offsetTop + 0;
	
	oDiv =  document.getElementById(tagID)
	oDiv.innerHTML = innerHTML;
	oDiv.style.left = ImgStyleLeft;
	oDiv.style.top = ImgStyleTop;
	oDiv.style.zIndex = 2;
}


/***************************************************************
 * Description : Set a layer object's inner HTML code.		   *
 * ----------------------------------------------------------- *	
 * @param   tagID		The Layer Object ID					   *	 
 * @param   innerHTML	The HTML Code to be added into the Tag *
 *															   *	
 * @return  no return										   *
 * ----------------------------------------------------------- *
 * Date			Added By		Version No.					   *
 * ----			-----------		-----------					   *
 * 10 Mar 2003  Jeff Ching		v1.0 Function Created		   *
 **************************************************************/
function setInnerHTML(tagID, innerHTML) {
   var oDiv =  document.getElementById(tagID);
	oDiv.innerHTML += innerHTML;
}

function SetImageSize(aobjImg, aiMaxWidth, aiMaxHeight) {
	var x = aobjImg.width;
	var y = aobjImg.height;

	var r1 = aiMaxWidth / x ;
	var r2 = aiMaxHeight / y;

	if (r2 < r1) {
		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
 */
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 (var i = 0; i<iNoOfTime; i++){
		if (i>5)
		{
			return;
		}
		s += asString;
	}
	return s;
}
function CDbl(asNum){
	return parseFloat(asNum);
}

function PreviewHTML(oText) {
	var s = oText.value;
	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 IsValidEmail2( email )
{
	var emailPatterns = [
		/.+@.+\..+$/i,
		/^\w.+@\w.+\.[a-z]+$/i,
		/^\w[-_a-z~.]+@\w[-_a-z~.]+\.[a-z]{2}[a-z]*$/i,
		/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/i /* modified by jim ma */
		];
	if ( ! emailPatterns[0].test( email ) )
	{
		return false;
	}
	return true;
}	

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 ChangeQty(aoQty, aiQtyChange, aiMin){
	var iProdQty = parseInt(aoQty.value);
	iProdQty += aiQtyChange;
	if (iProdQty <= aiMin) {
		iProdQty = aiMin;
	}
	aoQty.value = iProdQty;
}
function ClearFormValues(f, asExceptNameList) {
	var ysNameList = new Array();
	if (asExceptNameList != "") {
		ysNameList = asExceptNameList.split(",");
	}
	for (var i = 0; i < f.elements.length; i++) {
		var bClear = true;
		for (var j = 0; j < ysNameList.length; j++) {
			if (f.elements[i].name == ysNameList[j]) {
				bClear = false;
				break;
			}
		}
		if (bClear) {
			f.elements[i].value = "";
		}
	}
}
var iLastCheckedIndex = -1;
function AtLeastCheckedOne(objCheckboxRadio){
	var bCheckedOne = 0;
	iLastCheckedIndex = -1;

	if (objCheckboxRadio.length) {
		for (var i = 0; i < objCheckboxRadio.length; i++) {
			if (objCheckboxRadio[i].checked) {
				bCheckedOne = 9;
				iLastCheckedIndex = i;
				break;
			}
		}
	} else {
		if (objCheckboxRadio.checked) {
				bCheckedOne = 1;
				iLastCheckedIndex = 0;
		}
	}
	return bCheckedOne;
// 0 = no selected
// 1 = selected and the radio / checkbox has only ONE option
// 9 = selected and the radio / checkbox has MORE THAN ONE options

}

function Check_All(aoform, sParentName, sChildName, IsChecked){
	var CallUserFunction = null;
	if (arguments.length > 4) {
		CallUserFunction = arguments[4];
	}
	for (var i=0;i<aoform.elements.length;i++) {
		var e = aoform.elements[i];
		if (e.type == 'checkbox' && (e.name == sParentName || e.name == sChildName)) {
			e.checked = IsChecked;
			if ( e.name == sChildName && CallUserFunction) {
				CallUserFunction(e);
			}
		}
	}
}
function Remove_Check_All(aoCheckboxEle, aoform, sParentName){
	if (!aoCheckboxEle.checked){
		for (var i=0;i<aoform.elements.length;i++) {
			var e = aoform.elements[i];
			if (e.type == 'checkbox' && (e.name == sParentName)) {
				e.checked = false;
			}
		}
	}
}
function OptionSelectedValue(aoSelect){
	return aoSelect.options[aoSelect.options.selectedIndex].value;
}


function SetAllDisabled(sExceptList) {
	//use readonly -> disabled
	var elm = document.form1.elements;
	var b = false;
	if (!elm ) {
			return;
	}
	var l = sExceptList.split(",");

	for (var i = 0; i < elm.length; i++) {
		switch (elm[i].type) {
			case "select-one":
			case "select-multiple":
			case "text":
			case "textarea":
			case "file":
			case "checkbox":
			case "radio":
				b = false;
				for (var j =0; j < l.length; j++) {
					if (l[j].length > 0 && l[j] == elm[i].name) {
						b = true;
						break;
					}
				}
				if (!b) {
					elm[i].disabled = true;
//					elm[i].readonly = true;
				}
				break;
		}
	}
}



// function to check a valid date
//------------------------------------------------------------------------------
var lsDateIn="", lsMonthIn ="", lsYearIn="" //local variables

function IsDate(asDateStringIn){ //return error message
	if (!ValidDateFormat(asDateStringIn)) return "Invalid Format";

	if (!GetDateMonthYear(asDateStringIn)) return "Data Missing...";
	var dDate = new Date(lsYearIn,lsMonthIn-1,lsDateIn);

	var sDateStringOut = dDate.getFullYear() + "\/" + (dDate.getMonth()+ 1) + "\/" + dDate.getDate();
	var sDateStringIn = lsYearIn + "\/" + lsMonthIn + "\/" + lsDateIn ;
	if (sDateStringOut != sDateStringIn) return "Out Of Range" ;
	return "";
}

function ValidDateFormat(asDateStringIn){
	opatternDateFormat = /^(\d{4}\/\d{1,2}\/\d{1,2})$/; // mm/dd/yyyy 
	return opatternDateFormat.test(asDateStringIn);
}

function GetDateMonthYear (asDateStringIn){
	var yDateString = asDateStringIn.split("/");
	if (yDateString.length != 3 ) return false;
	
	lsYearIn = yDateString[0];
	lsMonthIn = parseInt(yDateString[1], 10);
	lsDateIn = parseInt(yDateString[2], 10);
	return true;
}
//------------------------------------------------------------------------------

//function to check a valid time
//------------------------------------------------------------------------------
var lsHourIn = "", lsMinuteIn = "", lsSecondIn = "";

function IsTime(asTimeStringIn){ //return error message
	if (!ValidTimeFormat(asTimeStringIn)) return "Invalid Format";

	if (!GetHourMinuteSecond(asTimeStringIn)) return "Data Missing...";
	
	if ((lsHourIn > 23) || (lsHourIn < 0)) return "Out Of Range" ;
	if ((lsMinuteIn > 59) || (lsMinuteIn < 0)) return "Out Of Range" ;
	if ((lsSecondIn > 59) || (lsSecondIn < 0)) return "Out Of Range" ;
	return "";
}

function ValidTimeFormat(asTimeStringIn){
	opatternTimeFormat = /^(\d{2}:\d{2}|\d{2}:\d{2}:\d{2})$/; // 00:00 or 00:00:00
	return opatternTimeFormat.test(asTimeStringIn);
}

function GetHourMinuteSecond (asTimeStringIn){
	var yTimeString = asTimeStringIn.split(":");
	if (yTimeString.length == 2) {
		lsHourIn = parseInt(yTimeString[0], 10);
		lsMinuteIn = parseInt(yTimeString[1], 10);
		lsSecondIn = 0;
		return true
		}
	if (yTimeString.length == 3) {
		lsHourIn = parseInt(yTimeString[0], 10);
		lsMinuteIn = parseInt(yTimeString[1], 10);
		lsSecondIn = parseInt(yTimeString[2], 10);
		return true
		}
	return false;
}
//------------------------------------------------------------------------------

