﻿function DrawImage(ImgD, maxW, maxH){ 
	var image=new Image(); 
	image.src=ImgD.src; 
	if(image.width>0 && image.height>0){ 
		if(image.width > maxW || image.height > maxH){
		    if(image.width / maxW > image.height / maxH){
			    ImgD.width=maxW; 
			    ImgD.height=(image.height*maxW)/image.width; 
		    }else{
		        ImgD.height=maxH;
		        ImgD.width=(image.width*maxH)/image.height;
		    }
		}else{ 
			ImgD.width=image.width; 
			ImgD.height=image.height; 
		}  
	} 
} 
function upperCase(){
	if((event.keyCode>96)&&(event.keyCode<123))event.keyCode-=32;
}
function lowerCase(){
	if((event.keyCode>64)&&(event.keyCode<91))event.keyCode+=32;
}
function validNormalChar(){
	//disenabled enter char !@#$%^&* etc.
	if ((event.keyCode > 32 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97)){
		event.returnValue = false;
	}else{
		event.returnValue = true;
	}	
}
function nonSingleQuotes(){
	if (event.keyCode==34 || event.keyCode==39){
		event.returnValue = false;
	}else{
		event.returnValue = true;
	}
}
function validNumber(){
	if (event.keyCode < 45 || event.keyCode > 57){
		event.returnValue = false;	
	}else{
		event.returnValue = true;	
	}
}
function validTelephone()
{
	e=window.event;
	var keyCode=e.keyCode;	
	if((!(keyCode>=48 && keyCode<=57)) && (!(keyCode==46)) && (!(keyCode==40)) && (!(keyCode==41)) && (!(keyCode==43)) && (!(keyCode==45)) && (!(keyCode==13))){	
		e.keyCode=0
	}
}
function validNumeric(){
	e=window.event;
	var keyCode=e.keyCode;	
	if((!(keyCode>=48 && keyCode<=57)) && (!(keyCode==46)) && (!(keyCode==13))){	
		e.keyCode=0
	}
}
function validInt(){
	e=window.event;
	var keyCode=e.keyCode;			
	if((!(keyCode>=48 && keyCode<=57)) && (!(keyCode==13))){	
		e.keyCode=0
	}
}
function moveFocus()
{
	if(event.keyCode==13)
	{ 
		event.keyCode=9;
	}
}
function validateString(myfield, s) {
    alert(myfield)
    alert(s)
	if (notNull(myfield.value) && notBlank(myfield.value))
		return true
	else {
		myfield.focus()
		alert("Please enter " + s + "!")
		return false
	}
}
function validatePic(myfield, s){
  if (myfield.value!="")
  {
        if ((myfield.value.toLowerCase().indexOf(".gif") <= -1) &&
         (myfield.value.toLowerCase().indexOf(".jpg") <= -1))
        {
             alert("Sorry, only can upload .gif or .jpg file.");
    	     myfield.focus();
             return false;
	    }
  }
}

function notNull(str) {
	if (str.length == 0)
		return false
	else
		return true
}

function notBlank(str) {
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) != " ")
			return true
	}
	return false
}

function validate(field) {
   var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
   var ok = "yes";
   var temp;
   for (var i=0; i<field.value.length; i++) {
       temp = "" + field.value.substring(i, i+1);
       if (valid.indexOf(temp) == "-1")
	      ok = "no";
   }
   if (ok == "no") {
       alert(" Only characters and numbers are accepted!");
       field.focus();
        field.select();
		return false;
   }
   return true;
}


function isDigits(str) {
	var i
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if (mychar < "0" || mychar > "9")
			return false
	}
	return true
}

function isNumber(str) {
	numdecs = 0
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if ((mychar >= "0" && mychar <= "9") || mychar == ".") {
			if (mychar == ".")
				numdecs++
		}
		else
			return false
	}
	if (numdecs > 1)
		return false
	return true
}

function ismail(mail) 
{ 
    return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(mail)); 
} 

function checkFieldData_onLoad(hiddenID)
{
	if(document.all.item(hiddenID).value!="")
		{
				window.alert(document.all.item(hiddenID).value);
				document.all.item(hiddenID).value="";
		}
}
function formatNumber(value,num) 
{ 
    var a,b,c,i 
    a = value.toString(); 
    b = a.indexOf('.'); 
    c = a.length; 
    if (num==0) 
    { 
        if (b!=-1) 
            a = a.substring(0,b); 
    } 
    else 
    { 
        if (b==-1) 
        { 
            a = a + "."; 
            for (i=1;i<=num;i++) 
            a = a + "0"; 
        } 
        else 
        { 
            a = a.substring(0,b+num+1); 
            for (i=c;i<=b+num;i++) 
            a = a + "0"; 
        } 
    } 
    return a 
}


function GetPassResult(volt,rval){
	var b,i,d,g;
	b="B";
	i="I";
	d="D";
	g="G";
	
	if(volt < 4.16){
		if(rval<100){
			return b;
		}
		else if(rval<200){
			return i;
		}
		else if(rval<400){
			return d;
		}	
		else{
			return g;
		}				
	}
	else if(volt<11.40){
		if(rval<200){
			return b;
		}
		else if(rval<400){
			return i;
		}
		else if(rval<800){
			return d;
		}	
		else{
			return g;
		}				
	}
	else if(volt<22.80){
		if(rval<250){
			return b;
		}
		else if(rval<500){
			return i;
		}
		else if(rval<1000){
			return d;
		}	
		else{
			return g;
		}				
	}
	else if(volt<69.0){
		if(rval<300){
			return b;
		}
		else if(rval<600){
			return i;
		}
		else if(rval<1200){
			return d;
		}	
		else{
			return g;
		}				
	}
	else{
		if(rval<400){
			return b;
		}
		else if(rval<800){
			return i;
		}
		else if(rval<1600){
			return d;
		}	
		else{
			return g;
		}				
	}
}

String.prototype.Trim = function(){ return Trim(this);}
String.prototype.LTrim = function(){return LTrim(this);}
String.prototype.RTrim = function(){return RTrim(this);}

function LTrim(str)
{
    var i;
    for(i=0;i<str.length;i++)
    {
        if(str.charAt(i)!=" "&&str.charAt(i)!="")break;
    }
    str=str.substring(i,str.length);
    return str;
}

function RTrim(str)
{
    var i;
    for(i=str.length-1;i>=0;i--)
    {
        if(str.charAt(i)!=" "&&str.charAt(i)!="")break;
    }
    str=str.substring(0,i+1);
    return str;
}
function Trim(str)
{
    return LTrim(RTrim(str));
}

function jsVideo(url, width, height)
{
    var ext = url.substring(url.length - 3).toUpperCase();
    
    if(".RM" == ext)
    {
        return jsRMVideo(url, width, height);
    }
    else
    {
        return jsMPVideo(url, width, height);   
    }
}

function jsRMVideo(url, width, height)
{
    var htmlVideo = "<embed src=\"" + url + "\" type=\"audio/x-pn-realaudio-plugin\"console=\"Clip1\" controls=\"ImageWindow\" height=\"" + height + "\" width=\"" + width + "\" autostart=\"1\">";
    htmlVideo = htmlVideo + "<br /><embed width=\"" + width + "\" height=\"36\" controls=\"ControlPanel\" console=\"Clip1\" type=\"audio/x-pn-realaudio-plugin\" autostart=\"1\" >";
        
    return htmlVideo;
}

function jsMPVideo(url, width, height)
{
    var htmlVideo = "<object id=\"player\" height=\"" + height + "\" width=\"" + width + "\" classid=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\">";
    htmlVideo = htmlVideo + "<param NAME=\"AutoStart\" VALUE=\"-1\">";
    htmlVideo = htmlVideo + "<param NAME=\"Balance\" VALUE=\"0\">";
    htmlVideo = htmlVideo + "<param name=\"enabled\" value=\"-1\">";
    htmlVideo = htmlVideo + "<param NAME=\"EnableContextMenu\" VALUE=\"-1\">";
    htmlVideo = htmlVideo + "<param NAME=\"url\" VALUE=\"" + url + "\">";
    htmlVideo = htmlVideo + "<param NAME=\"PlayCount\" VALUE=\"1\">";
    htmlVideo = htmlVideo + "<param name=\"rate\" value=\"1\">";
    htmlVideo = htmlVideo + "<param name=\"currentPosition\" value=\"0\">";
    htmlVideo = htmlVideo + "<param name=\"currentMarker\" value=\"0\">";
    htmlVideo = htmlVideo + "<param name=\"defaultFrame\" value=\"\">";
    htmlVideo = htmlVideo + "<param name=\"invokeURLs\" value=\"0\">";
    htmlVideo = htmlVideo + "<param name=\"baseURL\" value=\"\">";
    htmlVideo = htmlVideo + "<param name=\"stretchToFit\" value=\"0\">";
    htmlVideo = htmlVideo + "<param name=\"volume\" value=\"50\">";
    htmlVideo = htmlVideo + "<param name=\"mute\" value=\"0\">";
    htmlVideo = htmlVideo + "<param name=\"uiMode\" value=\"mini\">";
    htmlVideo = htmlVideo + "<param name=\"windowlessVideo\" value=\"0\">";
    htmlVideo = htmlVideo + "<param name=\"fullScreen\" value=\"0\">";
    htmlVideo = htmlVideo + "<param name=\"enableErrorDialogs\" value=\"-1\">";
    htmlVideo = htmlVideo + "<param name=\"SAMIStyle\" value>";
    htmlVideo = htmlVideo + "<param name=\"SAMILang\" value>";
    htmlVideo = htmlVideo + "<param name=\"SAMIFilename\" value>";
    htmlVideo = htmlVideo + "</object>";
        
    return htmlVideo;
}
            
function IsEmail(email)
{
    if(email!='')
    {
        if(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(email))
        {
            return true;
        }
    }
    
    return false;
}