// JavaScript Document
var checkflag = "false";
function check(field) {
	if (checkflag == "false") {
		if(isNaN(field.length)){
			field.checked=true;
		}else{
			for (i = 0; i < field.length; i++) { 
				field[i].checked = true;
			}
		}
		checkflag = "true"; 
		return "Uncheck"; 
	} else {
		if(isNaN(field.length)){
			field.checked=false;
		}else{
			for (i = 0; i < field.length; i++) { 
				field[i].checked = false; 
			}
		}
		checkflag = "false"; 
		return "Check"; 
	}
}

function GetSafeCode(ob) { 
	if(document.getElementById(ob)) 
	document.getElementById(ob).innerHTML = '<img src="'+Dv_CodeFile+'"/>';
}

function CheckLoginForm(ob)
{
	var form_ob=jQuery(ob);
	var user_name=form_ob.find("#UserName");
	var password=form_ob.find("#PassWord");
	var safecode=form_ob.find("#SafeCode");
	if(user_name.val()=='')
	{
		alert("UserName is required");
		user_name.focus();
		return false;
	}
	if(password.val()=='')
	{
		alert("PassWord is required");
		password.focus();
		return false;
	}
	if(safecode.val()=='')
	{
		alert("Code is required");
		safecode.focus();
		return false;
	}
}

function DrawImage(ImgD,w){ 
 var image=new Image(); 
 image.src=ImgD.src; 
 if(image.width>0 && image.height>0){ 
  if(image.width/image.height>= 1){ 
   if(image.width>w){
    ImgD.width=w; 
    ImgD.height=(image.height*w)/image.width; 
   }else{ 
    ImgD.width=image.width;
    ImgD.height=image.height; 
   } 
  } 
  else{ 
   if(image.height>w){
    ImgD.height=w; 
    ImgD.width=(image.width*w)/image.height; 
   }else{ 
    ImgD.width=image.width;
    ImgD.height=image.height; 
   } 
  } 
 }
} 

function AddFavorite(sURL, sTitle)
{
    try
    {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e)
    {
        try
        {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e)
        {
            alert("Error");
        }
    }
}

function SetHome(obj,vrl){
	try{
		obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
}
catch(e){
		if(window.netscape) {
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}
			catch (e) {
				alert("Error");
			}
			var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',vrl);
		 }
	}
}
/**********************
图片loading效果+自动按比例缩放+支持单图和图片列表
@imgid:jquery 对象
@w:图片缩放的最大尺寸
***********************/
function LoadImage(imgid,url,w){ 
	if(!url)
	{
		imgid.attr({src: onerror_pic_path, width: 50, height: 38});
		return false;
	}
	
    var img=new Image();
	var img_h;
	var img_w;
	if(img.complete)
	{
		if(img.height>0&&img.width>0)
		{
			if(img.width/img.height>= 1){ 
				if(img.width>w)
				{
					img_w=w; 
					img_h=(img.height*w)/img.width; 
				}
				else
				{ 
					img_w=img.width;
					img_h=img.height; 
				} 
			} 
			else
			{ 
				if(img.height>w)
				{
					img_h=w; 
					img_w=(img.width*w)/img.height; 
				}
				else
				{ 
					img_w=img.width;
					img_h=img.height; 
				} 
			}
			imgid.attr({src: url, width: img_w, height: img_h});
		}
	}
	img.onload=function(){
		if(img.height>0&&img.width>0)
		{
			if(img.width/img.height>= 1){ 
				if(img.width>w)
				{
					img_w=w; 
					img_h=(img.height*w)/img.width; 
				}
				else
				{ 
					img_w=img.width;
					img_h=img.height; 
				} 
			} 
			else
			{ 
				if(img.height>w)
				{
					img_h=w; 
					img_w=(img.width*w)/img.height; 
				}
				else
				{ 
					img_w=img.width;
					img_h=img.height; 
				} 
			}
			imgid.attr({src: url, width: img_w, height: img_h});
		}
	}
	img.onerror=function(){imgid.attr({src: onerror_pic_path, width: 50, height: 38});}
	img.src=url; 
}

function GetImageSize(url,w){
	var img_arr=new Array();
	img_arr[0]="";
	img_arr[1]="";
	if(!url)
	{
		return img_arr;
	}
	var img=new Image();
	img.src=url; 
	var img_h;
	var img_w;
	if(img.complete)
	{
		if(img.height>0&&img.width>0)
		{
			if(img.width/img.height>= 1){ 
				if(img.width>w)
				{
					img_w=w; 
					img_h=(img.height*w)/img.width; 
				}
				else
				{ 
					img_w=img.width;
					img_h=img.height; 
				} 
			} 
			else
			{ 
				if(img.height>w)
				{
					img_h=w; 
					img_w=(img.width*w)/img.height; 
				}
				else
				{ 
					img_w=img.width;
					img_h=img.height; 
				} 
			}
			img_arr[0]=img_w;
			img_arr[1]=img_h;
		}
		return img_arr;
	}
	img.onload=function(){
		if(img.height>0&&img.width>0)
		{
			if(img.width/img.height>= 1){ 
				if(img.width>w)
				{
					img_w=w; 
					img_h=(img.height*w)/img.width; 
				}
				else
				{ 
					img_w=img.width;
					img_h=img.height; 
				} 
			} 
			else
			{ 
				if(img.height>w)
				{
					img_h=w; 
					img_w=(img.width*w)/img.height; 
				}
				else
				{ 
					img_w=img.width;
					img_h=img.height; 
				} 
			}
			img_arr[0]=img_w;
			img_arr[1]=img_h;
		}
		return img_arr;
	}
	img.onerror=function(){return img_arr;}
}
