// JavaScript Document

// Conclude whether a file is a image file by its extention
function isImageFile(filePath){
	var reImage = /^.+\.(JPG|GIF|PNG|BMP)$/i ;
	return reImage.test(filePath) ;
}

function SwitchList(container)
{
	 var mainDiv = FindControlByID(container);
	 if(mainDiv)
	 {
		  var ul = mainDiv.getElementsByTagName("ul");
		  if(ul && ul.length > 0)
		  {
			ul[0].className=ul[0].className=="hide" ? "show" : "hide";
			
			if(ul[0].className == "show" )
				SetCookie( "ExpandSectionID", container );
			else
				DeleteCookie( "ExpandSectionID" );
		  }
	 }
}

function SwitchList(container,TagType)
{
	 var mainDiv = FindControlByID(container);
	 
	 if(mainDiv)
	 {
		  var ul = mainDiv.getElementsByTagName(TagType);
		  if(ul && ul.length > 0)
		  {
			ul[0].className=ul[0].className=="hide" ? "show" : "hide";
			
			if(ul[0].className == "show" )
				SetCookie( "ExpandSectionID", container );
			else
				DeleteCookie( "ExpandSectionID" );
		  }
	 }
}

function Redirect(url)
{
	if(Trim(url) != "")
	{
		window.location.href = url;
	}
}
function CheckCoderKey(keywordID,alertMessage)
{
	result = true;
	if(Trim(keywordID.value)=="")
	{
		alert(alertMessage);
		keywordID.focus();
		result = false;
	}
	return result;
}
function HoldingExpandSection( )
{
	var container = GetCookie("ExpandSectionID");
	if(container)
	{
		SwitchList( container );
	}
}

//获得Cookie解码后的值
function GetCookieValue(offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

//设定Cookie值
function SetCookie(name, value)
{
	var expdate = new Date();
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
	document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
	+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
	+((secure == true) ? "; secure" : "");
}

//删除Cookie
function DeleteCookie(name)
{
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}

//获得Cookie的原始值
function GetCookie(name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return GetCookieValue (j);
		
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}



function ShowSection(sender,container)
{
	 var control = FindControlByID(sender);
	 if (control==null)	
	 {
		 ShowAlertMessage("undefined");
	 }
	 ResetSection(container);
	 control.className="tabLabelSelected";
}

function ResetSection(parentID)
{
	var mainDiv = FindControlByID(parentID);
    if(mainDiv)	
	{
		var sectionList=mainDiv.getElementsByTagName("div");
		if(sectionList)
		{
			for(var contentIndex = 0; contentIndex < sectionList.length; contentIndex++)
			{
				sectionList[contentIndex].className="";
			}
		}
	}
}

function FixPageHeight(scale)
{
  var page = FindControlByID('page');
  if(page)
  {
	var offSetHeight = page.offsetHeight;
	var screenHeight = window.screen.availHeight;
		if(offSetHeight/screenHeight < scale)
		{
			document.getElementById('page').style.height = screenHeight * scale +'px';
			document.getElementById('footer').className = 'fixfooter';
		}
	}
	//
}

function ShowAlertMessage(text)
{
 alert(text);
}

function FindControlByID(id)
{
  if(!document.getElementById(id))
  {
	  return null;
  }
  return document.getElementById(id);
}

function Trim(s)
{
	var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
	return (m == null) ? "" : m[1];
}

function GetAbsoluteLocation(element)
{
    if ( arguments.length != 1 || element == null )
    {
        return null;
    }
    var elmt = element;
    var offsetTop = elmt.offsetTop;
    var offsetLeft = elmt.offsetLeft;
    var offsetWidth = elmt.offsetWidth;
    var offsetHeight = elmt.offsetHeight;
    while( elmt = elmt.offsetParent )
    {
          // add this judge
        if ( elmt.style.position == 'absolute' || elmt.style.position == 'relative' 
            || ( elmt.style.overflow != 'visible' && elmt.style.overflow != '' ) )
        {
            break;
        } 
        offsetTop += elmt.offsetTop;
        offsetLeft += elmt.offsetLeft;
    }
    return { absoluteTop: offsetTop, absoluteLeft: offsetLeft,
        offsetWidth: offsetWidth, offsetHeight: offsetHeight };
} 

function OpenWindow(url)
{
  var argc = OpenWindow.arguments.length;
  var argv = OpenWindow.arguments;
  var title = (argc > 1) ? argv[1] : 'Tip';
  var width = (argc > 2) ? argv[2] : 600;
  var height = (argc > 3) ? argv[3] : 500;
  var status = (argc > 4) ? argv[4] : 'no';
  var toolbar = (argc > 5) ? argv[5] : 'no';
  var menubar = (argc > 6) ? argv[6] : 'no';
  var location = (argc > 7) ? argv[7] : 'no';
  var scrollbars = (argc > 8) ? argv[8] : 'no';
  var setting = "width=" + width + ",height=" + height + ",status=" +
   status + ",toolbar=" + toolbar + ",menubar=" + menubar + ",location=" + location +
   ",scrollbars=" + scrollbars;
  var child = window.open(url,title,setting);
  child.focus();
}

function CloseWindow()
{
	//window.close();
}

function SubmitEnterKey(SubminObjID,e)     
{         
	var ev;  
	if(window.navigator.userAgent.indexOf("MSIE")>=1)
	{
		ev = event;
	}
	else
	{
		ev = e;
	}
	if(ev.keyCode == 13)     
	{   
		document.getElementById(SubminObjID).focus();   
	}   
} 

function CheckTextBoxEmpty(senderID,defaultText,errorMsg)
{
	var textbox = FindControlByID(senderID);
	if(textbox.tagName == "INPUT")
	{
		if(Trim(textbox.value) == "" || textbox.value == defaultText)
		{
			if(errorMsg != null && Trim(errorMsg) != "")
			{
				alert(errorMsg);
			}
			return false;
		}
	}
	return true;
};

function Clickclear(thisfield, defaulttext) 
{
	if (thisfield.value == defaulttext) 
	{
		thisfield.value = "";
	}
}
function Clickrecall(thisfield, defaulttext) 
{
	if (thisfield.value == "") 
	{
		thisfield.value = defaulttext;
	}
}

function FixImageWidth(img,width)
{
	if(img.width > width )
	{
		img.width = width;
	}
}
