﻿function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

String.prototype.trim = function()
 {
    return this.replace(/^\s*|\s*$/g, "");
    }
   
String.prototype.Truncate=function(length)
	{
		if (this.length <= length) return this;
		return this.substr(0, length)+"...";
	}
	
Date.prototype.NextMonth=function()
 {
    var year=this.getYear();
    var month=this.getMonth();
    if(month==11)
    {
        year=year+1;
        month=0;
    }
    else
        month=month+1;
    return new Date(year,month,this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds(),this.getMilliseconds());
}

Date.prototype.toGBShortString=function()
 {
    if(this==null) return '';
    var year=this.getYear();
    var month=this.getMonth()+1;
    var day=this.getDate();
    return year+'-'+month+'-'+day;
}
Date.prototype.toGBString=function()
 {
    if(this==null) return '';
    var year=this.getYear();
    var month=this.getMonth()+1;
    var day=this.getDate();
    return year+'-'+month+'-'+day;
}

Date.prototype.toGBLongString=function()
 {
    if(this==null) return '';
    var year=this.getYear();
    var month=this.getMonth()+1;
    var day=this.getDate();
    return year+'-'+month+'-'+day+' '+this.getHours()+':'+this.getMinutes()+':'+this.getSeconds();
}

Date.prototype.addDays=function(days){
		return new Date(this.getTime() + days*24*60*60*1000);
		}
		
function parseDate(dateString)
{
	var pos1=dateString.indexOf('-',0);
	if(pos1<=0) return null;
	var pos2=dateString.indexOf('-',pos1+1);
	if(pos2<=0) return null;
	
	var year=parseInt(dateString.substr(0,pos1),10);
	var month=parseInt(dateString.substr(pos1+1,pos2 - pos1 -1),10) -1;
	var day=parseInt(dateString.substr(pos2+1,dateString.length - pos2 -1),10);
	//alert(year+":"+month+":"+day);
	return new Date(year,month,day);
}

function FillGrid(grid,rep)
{
	hideMsg();
	try
	{
		if(rep.error!=null)
		{ 
			alert(rep.error.Message);
			hideMsg();
			return;
		}
		localData=rep.value;
		grid.setRowProperty("count", localData.length);
		grid.refresh();
		//name.focus();
	}
	catch(e)
	{
		return;
	}
}

var ajaxLoadingDiv=null;
function createMsgDiv()
{
	ajaxLoadingDiv=document.createElement("DIV");
	ajaxLoadingDiv.innerHTML='<img src="../images/loading.gif" border=0 /><span id="loadingText">装载数据....</span>';
	ajaxLoadingDiv.style.position="absolute";
	ajaxLoadingDiv.style.display="none";
	ajaxLoadingDiv.style.color="red";
	ajaxLoadingDiv.style.fontWeight="bolder";
	ajaxLoadingDiv.style.pixelLeft=400;
	ajaxLoadingDiv.style.pixelTop=300;

	document.body.appendChild(ajaxLoadingDiv);
}

function showMsg(msg)
{
	if(ajaxLoadingDiv==null) createMsgDiv();
	var textObj=ajaxLoadingDiv.children.item(1);
	
	if(msg!=null&&msg.length>0)
	{
		textObj.innerText=msg;
	}
	else 
		textObj.innerText="装载数据....";
	ajaxLoadingDiv.style.pixelLeft=(document.body.offsetWidth - 200 -document.body.scrollLeft)/2;
	ajaxLoadingDiv.style.pixelTop=(document.body.offsetHeight - 20 -document.body.scrollTop)/2;
	ajaxLoadingDiv.style.display="";
}

function hideMsg()
{
	ajaxLoadingDiv.style.display="none";
}

function OpenUnitSelectWindow(ddlValue,text)
{
	var rtn= window.showModalDialog("../SelectUnit.aspx?a="+Math.random(),window,'dialogWidth=450px;dialogHeight=450px;resizable:yes;help:no;status:no');
	var ddlObj=$(ddlValue);
    if(rtn!=null) 
    {	
		ddlObj.value=rtn.unitCode;
		if(arguments.length==2)
		{
			var textObj=$(text);
			textObj.value=rtn.unitName;
		}
	}
}

function OpenCountrySelectWindow(ddlValue,text)
{
	var rtn= window.showModalDialog("../SelectCountry.aspx?a="+Math.random(),window,'dialogWidth=450px;dialogHeight=420px;resizable:yes;help:no;status:no');
	var ddlObj=$(ddlValue);
    if(rtn!=null) 
    {	
		ddlObj.value=rtn.countryCode;
		if(arguments.length==2)
		{
			var textObj=$(text);
			textObj.value=rtn.countryName;
		}
	}
}


//get the selected value from radio group
function getSelectedValueFromRadio(radioId)
{
	var rbl=document.getElementsByName(radioId);
	if(rbl!=null) 
	{
		for(var i=0;i<rbl.length;i++)
		{
			if(rbl[i].checked)
			{
				return rbl[i].value;
			}
		}
	}
	else
		return "";
}
// #############################################################################
	// function to toggle the collapse state of an object, and save to a cookie
	function toggle_collapse(objid)
	{
		
		obj = $("collapseobj_" + objid);	
		img = $("collapseimg_" + objid);
		cel = $("collapsecel_" + objid);
		
		if (!obj)
		{
			// nothing to collapse!
			if (img)
			{
				// hide the clicky image if there is one
				img.style.display = "none";
			}
			return false;
		}

		if (obj.style.display == "none")
		{
			obj.style.display = "";
			//save_collapsed(objid, false);
			if (img)
			{
				img_re = new RegExp("_collapsed\\.gif$");
				img.src = img.src.replace(img_re, '.gif');
			}
			if (cel)
			{
				cel_re = new RegExp("^(thead|tcat)(_collapsed)$");
				cel.className = cel.className.replace(cel_re, '$1');
			}
		}
		else
		{
			obj.style.display = "none";
			//save_collapsed(objid, true);
			if (img)
			{
				img_re = new RegExp("\\.gif$");
				img.src = img.src.replace(img_re, '_collapsed.gif');
			}
			if (cel)
			{
				cel_re = new RegExp("^(thead|tcat)$");
				cel.className = cel.className.replace(cel_re, '$1_collapsed');
			}
		}
		return false;
	}
	
function set_cookie(name, value, expires)
	{
		if (!expires)
		{
			expires = new Date();
		}
		document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
	}

	// #############################################################################
// function to retrieve a cookie
function fetch_cookie(name)
{
	cookie_name = name + "=";
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length)
	{
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name)
		{
			var value_end = document.cookie.indexOf (";", value_begin);
			if (value_end == -1)
			{
				value_end = cookie_length;
			}
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		cookie_begin = document.cookie.indexOf(" ", cookie_begin) + 1;
		if (cookie_begin == 0)
		{
			break;
		}
	}
	return null;
}

function save_collapsed(objid, addcollapsed)
{
	var collapsed = fetch_cookie('witsml_collapse');
	var tmp = new Array();

	if (collapsed != null)
	{
		collapsed = collapsed.split('\n');

		for (var i in collapsed)
		{
			if (collapsed[i] != objid && collapsed[i] != '')
			{
				tmp[tmp.length] = collapsed[i];
			}
		}
	}

	if (addcollapsed)
	{
		tmp[tmp.length] = objid;
	}

	expires = new Date();
	expires.setTime(expires.getTime() + (1000 * 86400 * 365));
	set_cookie('witsml_collapse', tmp.join('\n'), expires);
}


//is select item selected
function isItemSelected(source, arguments)
{
 if (arguments.Value ==""||arguments.Value < 0)
 {
	arguments.IsValid = false;
 }
 else
 {
	arguments.IsValid = true;
 }
}

function openPrintDocument(groupId,type)
{
	window.open("../print/documentprint.aspx?a="+Math.random()+"&id="+groupId+"&type="+type,"",'width=820,height=750,left=50,top=50,scrollbars=yes,resizable=yes,help=no,status=no;',true);
}
//打开多个文档连续打印窗口
function openPrintDocument_mult(ids,type)
{
	window.open("../print/documentprint_mult.aspx?a="+Math.random()+"&ids="+ids+"&type="+type,"",'width=820,height=750,left=50,top=50,scrollbars=yes,resizable=yes,help=no,status=no;',true);
}


function openPassportInfo(xmbmsArray)
{
	var xmbmsString=xmbmsArray.join(",");
	var rtn= window.showModalDialog("../passport/ShowPassportInfoByXmbms.aspx?a="+Math.random()+"&xmbms="+xmbmsString,window,'dialogWidth=900px;dialogHeight=540px;resizable:yes;help:no;status:no');
}
function confirmDelete()
{
	return window.confirm("确定要删除吗?");
}