function resizeImg( imgObj, w, h ){
	if (imgObj.width > w || imgObj.height > h) {
		if (imgObj.height > h && imgObj.width * imgObj.height / imgObj.width <= w) {
			imgObj.height = h;
		} else {
			imgObj.width = w;
		}
	}
}

function popup(filename, w, h){
	if (!w)
		w = 500;
	if (!h)
		h = 550;
	window.open (filename,'pop','width='+w+',height='+h+',statusbar=no,toolbar=no,resizable,scrollbars,dependent')
}

function formatEmail(str){
	str = str.value
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		return true;
	else
		return false;
}

function formatNum(numform){
  if (isNaN(numform.value)||numform.value<0)
    return false;
  else
    return true;
}

function valDate(M, D, Y){
  Months= new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  Leap  = false;
  if((Y % 4 == 0) && ((Y % 100 != 0) || (Y %400 == 0)))
    Leap = true;
  if((D < 1) || (D > 31) || (M < 1) || (M > 12) || (Y < 0))
    return false;
  if((D > Months[M-1]) && !((M == 2) && (D > 28)))
    return false;
  if(!(Leap) && (M == 2) && (D > 28))
    return false;
  if((Leap)  && (M == 2) && (D > 29))
    return false;
  return true;
};

function formatDate(dateform){
  cDate = dateform.value;
  dSize = cDate.length;
  if (dSize!=0){
    sCount= 0;
    for(var i=0; i < dSize; i++)
      (cDate.substr(i,1) == "/") ? sCount++ : sCount;
    if (sCount == 2){
		ySize = cDate.substring(cDate.lastIndexOf("/")+1,dSize).length;
		if (ySize<2 || ySize>4 || ySize == 3){
		  return false;
		 }
		idxBarI = cDate.indexOf("/");
		idxBarII = cDate.lastIndexOf("/");
		strD = cDate.substring(0,idxBarI);
		strM = cDate.substring(idxBarI+1,idxBarII);
		strY = cDate.substring(idxBarII+1,dSize);
		strM = (strM.length < 2 ? '0'+strM : strM);
		strD = (strD.length < 2 ? '0'+strD : strD);
		if(strY.length == 2)
		  strY = (strY > 50  ? '19'+strY : '20'+strY);
    }else{
    	if (dSize != 8)
			return false;
		strD = cDate.substring(0,2);
		strM = cDate.substring(2,4);
		strY = cDate.substring(4,8);
    }
    dateform.value = strD+'/'+strM+'/'+strY;
    if (!valDate(strM, strD, strY))
      return false;
    else
      return true;
  }
}

function checknum(numform){
	if (isNaN(numform.value)||numform.value<0){
		numform.focus();
		alert("Please fill in Correct Number!");
	}
}

function checkdate(date) {
	if (date.value!=""&&!formatDate(date)){
		date.focus();
		alert("Please fill in Correct Date format!");
		return false;
    }else{
        return true;
    }
}

function checkyear(date) {
	if (date.value!=""){
		if(date.value.length == 1 || date.value.length == 3 || isNaN(date.value)){
			date.focus();
			alert("Please fill in Correct Year!");
			return false;
		}
		if(date.value.length == 2){
		  date.value = (date.value > 50  ? '19'+date.value : '20'+date.value);
		}
		if (date.value<1900 || date.value>2050){
			date.focus();
			alert("Please fill in Correct Year!");
		}
    }
}

function checkYYYYMM(date) {
	cDate = date.value;
	dSize = cDate.length;
	if (dSize!=0){
		sCount= 0;
		for(var i=0; i < dSize; i++)
			(cDate.substr(i,1) == "/") ? sCount++ : sCount;
		if (sCount == 1){
			ySize = cDate.substring(0,cDate.lastIndexOf("/")).length;
			if (ySize<2 || ySize>4 || ySize == 3){
				date.focus();
				alert("Please fill in Correct Date!");
				return false;
			}
			idxBarI = cDate.indexOf("/");
			strM = cDate.substring(idxBarI+1,dSize);
			strY = cDate.substring(0,idxBarI);
			strM = (strM.length < 2 ? '0'+strM : strM);
			if(strY.length == 2)
				strY = (strY > 50  ? '19'+strY : '20'+strY);
		}else{
			date.focus();
			alert("Please fill in Correct Date!");
			return false;
		}
		if (!valDate(strM, 1, strY)){
			date.focus();
			alert("Please fill in Correct Date!");
			return false;
		}
		date.value = strY+'/'+strM;
	}
}

function iif(psdStr, trueStr, falseStr){
	if (psdStr){
		return trueStr;
	}else{
		return falseStr;
	}
}

function Round(num,decPoints){
	if (num=='')
		return 0;
	if (!decPoints)
		decPoints = 0;
	decPoints = Math.pow(10,decPoints);
	return Math.round(num*decPoints)/decPoints;
}

function CA(obj,iObj){
	if(!obj.length){
		obj.checked = iObj.checked;
	}else{
		for (var i=0;i<obj.length;i++)
			obj[i].checked = iObj.checked;
	}
}

function CT(obj,iObj){
	if(!obj.length){
		iObj.checked = obj.checked;
	}else{
		var allCheck = true;
		for (var i=0;i<obj.length;i++){
			if (!obj[i].checked)
				allCheck = false;
		}
		iObj.checked = allCheck;
	}
}

function addEvent(obj,type,fn){
    if(obj.addEventListener) obj.addEventListener(type,fn,false);
    else if(obj.attachEvent){
        obj["e"+type+fn]=fn;
        obj[type+fn]=function(){obj["e"+type+fn](window.event);}
        obj.attachEvent("on"+type,obj[type+fn]);
    }
}

function removeEvent(obj,type,fn){
  if(obj.removeEventListener) obj.removeEventListener(type,fn,false);
  else if(obj.detachEvent){
    obj.detachEvent("on"+type,obj[type+fn]);
    obj[type+fn]=null;
    obj["e"+type+fn]=null;
  }
}

function HTMLEncode (str){
   var div = document.createElement("div");
   var text = document.createTextNode(str);
   div.appendChild(text);
   return div.innerHTML;
}

function HTMLDecode (str){
   var div = document.createElement("div");
   div.innerHTML = str;
   return div.innerText;
}

function URLEncode(text)
{
    // The Javascript escape and unescape functions do not correspond
    // with what browsers actually do...
    var SAFECHARS = "0123456789" +                  // Numeric
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +  // Alphabetic
                    "abcdefghijklmnopqrstuvwxyz" +
                    "-_.!~*'()";                    // RFC2396 Mark characters
    var HEX = "0123456789ABCDEF";

    var plaintext = text;
    var encoded = "";
    for (var i = 0; i < plaintext.length; i++ ) {
        var ch = plaintext.charAt(i);
        if (ch == " ") {
            encoded += "+";             // x-www-urlencoded, rather than %20
        } else if (SAFECHARS.indexOf(ch) != -1) {
            encoded += ch;
        } else {
            var charCode = ch.charCodeAt(0);
            if (charCode > 255) {
                alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
                          "(URL encoding only supports 8-bit characters.)\n" +
                          "A space (+) will be substituted." );
                encoded += "+";
            } else {
                encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
            }
        }
    } // for

    //document.URLForm.F2.value = encoded;
    return encoded;
}

function URLDecode(text)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef";
   var encoded = text;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
       if (ch == "+") {
           plaintext += " ";
           i++;
       } else if (ch == "%") {
            if (i < (encoded.length-2)
                    && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
                    && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
                plaintext += unescape( encoded.substr(i,3) );
                i += 3;
            } else {
                alert( 'Bad escape combination near ...' + encoded.substr(i) );
                plaintext += "%[ERROR]";
                i++;
            }
        } else {
           plaintext += ch;
           i++;
        }
    } // while
   //document.URLForm.F1.value = plaintext;
   return plaintext;
}

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_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_findObj(n, d) { //v4.01
  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);
  if(!x && d.getElementById) x=d.getElementById(n); 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];}
}

