var _RESPONSE_WINDOW = "responseWindow";
//验证是否为字符
function checkFormString(name,ctrl,allowNull,minLen,maxLen)
{
	try
	{
		
		var datastr = ctrl.value;
		var lefttrim = datastr.search(/\S/gi);
		if (lefttrim == -1) 
		{
			if (allowNull) 
			{
				return true;
			} 
			else 
			{
			  alert(name + JS_ERROR_NULL);
			  if (ctrl.type.toLowerCase() != "hidden")
				  ctrl.focus();
			  return false;
			}
		}
		if (datastr.search(/[<>]/gi) != -1) 
		{
			alert(name + JS_ERROR_INVALID_STRING);
			  if (ctrl.type.toLowerCase() != "hidden")
				  ctrl.focus();
			return false;
		}
		if ((minLen > 0) && (datastr.length < minLen))
		{
			alert(name + JS_ERROR_STRING_TOO_SHORT + minLen);
			  if (ctrl.type.toLowerCase() != "hidden")
				  ctrl.focus();
			return false;
		}
		if ((maxLen > 0) && (datastr.length > maxLen))
		{
			alert(name + JS_ERROR_STRING_TOO_LONG + maxLen);
			  if (ctrl.type.toLowerCase() != "hidden")
				  ctrl.focus();
			return false;
		}
		return true;
	}
	catch (e)
	{
		alert(e);
		return false;
	}
}

//验证EMAIL
function checkFormEmail(name, ctrl, allowNull)
{
	var datastr = ctrl.value;
	var lefttrim = datastr.search(/\S/gi);
    if (lefttrim == -1) 
    {
    	if (allowNull) 
		{
      		return true;
    	} 
		else 
		{
      		alert(name + JS_ERROR_NULL);
			  if (ctrl.type.toLowerCase() != "hidden")
				  ctrl.focus();
      		return false;
    	}
  	}

  	var myRegExp = /[a-z0-9](([a-z0-9]|[_\-\.][a-z0-9])*)@([a-z0-9]([a-z0-9]|[_\-][a-z0-9])*)((\.[a-z0-9]([a-z0-9]|[_\-][a-z0-9])*)*)/gi;
  	var answerind = datastr.search(myRegExp);
  	var answerarr = datastr.match(myRegExp);

  	if (answerind == 0 && answerarr[0].length == datastr.length)
  	{
    	return true;
  	}
  if (ctrl.type.toLowerCase() != "hidden")
	  ctrl.focus();
  	alert(name + JS_ERROR_INVALID_EMAIL);
  	return false;
}

//验证是否为一个整数（正整数和负数 0）
function checkFormInt(name,ctrl,allowNull)
{
	var num=ctrl.value;
	if(num.search(/^-?[0-9][0-9]*$/gi)!= -1)
		return true;
	else
	{
		if (num=='')
		{
			if (allowNull)
				return true;
			else
				alert(name + JS_ERROR_NULL);
		}
		else
		{
			alert(name + JS_ERROR_INVALID_NUMERIC);
		}
		ctrl.focus();
		return false;
	}
}

//验证密码（数字和字母）
function checkFormPassword(name,ctrl,allowNull)
{
	var num=ctrl.value;

	if(num.search(/^[a-zA-Z0-9_]*$/gi)!= -1)
		return true;
	else
	{
		if (num=='')
		{
			if (allowNull)
				return true;
			else
				alert(name + JS_ERROR_NULL);
		}
		else
		{
			alert(name + JS_ERROR_INVALID_PASSWORD);
		}
		ctrl.focus();
		return false;
	}
}

//验证是否为一个正数
function checkFormNum(name,ctrl,allowNull)
{
	var num=ctrl.value;
	if(num.search(/^[0-9]+(\.[0-9]*)?$/gi)!= -1)
		return true;
	else
	{
		if (num=='')
		{
			if (allowNull)
				return true;
			else
				alert(name + JS_ERROR_NULL);
		}
		else
		{
			alert(name + JS_ERROR_INVALID_NUMERIC);
		}
		//ctrl.focus();
		return false;
	}
}

//验证字符是否为小于一的实数
function checkFormLessThanOne(name,ctrl,allowNull)
{
	var num=ctrl.value;
	if(num.search(/^[0]+(\.[0-9]*)?$/gi)!= -1)
		return true;
	else
	{
		if (num=='')
		{
			if (allowNull)
				return true;
			else
				alert(name + JS_ERROR_NULL);
		}
		else
		{
			alert(name + JS_ERROR_INVALID_LESS_THAN_ONE);
		}
		//ctrl.focus();
		return false;
	}
}


function checkFormNumWithoutZero(name,ctrl,allowNull)
{
	var num=ctrl.value;
	if(num.search(/^[0-9]*[1-9][0-9]*$/gi)!= -1)
		return true;
	else
	{
		if (num=='')
		{
			if (allowNull)
				return true;
			else
				alert(name + JS_ERROR_NULL);
		}
		else
		{
			alert(name + JS_ERROR_INVALID_NUMERIC);
		}
		//ctrl.focus();
		return false;
	}
}

//验证是否为正数且只有两位以内小数
function checkFormMoneyType(name,ctrl,allowNull)
{
	var num = ctrl.value;
	if(num.search(/^[0-9]+(.[0-9]{1,2})?$/)!= -1)
		return true;
	else
	{
		if (num=='')
		{
			if (allowNull)
				return true;
			else
				alert(name + JS_ERROR_NULL);
		}
		else
		{
			alert(name + JS_ERROR_INVALID_NUMERIC);
		}
		//ctrl.focus();
		return false;
	}
}

function checkFormIntNum(name,ctrl,allowNull)
{
	var num=ctrl.value;
	if(num.search(/^([0-9])+([0-9]*)?$/gi)!= -1)
		return true;
	else
	{
		if (num=='')
		{
			if (allowNull)
				return true;
			else
				alert(name + JS_ERROR_NULL);
		}
		else
			alert(name + JS_ERROR_INVALID_INT);
		  if (ctrl.type.toLowerCase() != "hidden")
			  ctrl.focus();
		return false;
	}
}

//验证是否是日期
function checkFormDate1(name, ctrl, allowNull)
{
	var datastr = ctrl.value;
	var lefttrim = datastr.search(/\S/gi);
    if (lefttrim == -1) 
    {
    	if (allowNull) 
		{
      		return true;
    	} 
		else 
		{
      		alert(name + JS_ERROR_NULL);
			  if (ctrl.type.toLowerCase() != "hidden")
				  ctrl.focus();
      		return false;
    	}
  	}
	var myRegExp = /^([1-2][0-9][0-9][0-9])-(0{0,1}[1-9]|1[0-2])-(0{0,1}[1-9]|[1-2][0-9]|3[0-1])$/gi;
	var res = datastr.search(myRegExp);
  	if (res == 0)
  	{
    	return true;
  	}
  if (ctrl.type.toLowerCase() != "hidden")
	  ctrl.focus();
  	alert(name + JS_ERROR_INVALID_DATE);
  	return false;
}

function checkFormDate(name, ctrl, allowNull)
{
	var datastr = ctrl.value;
	var lefttrim = datastr.search(/\S/gi);
    if (lefttrim == -1) 
    {
    	if (allowNull) 
		{
      		return true;
    	} 
		else 
		{
      		alert(name + JS_ERROR_NULL);
			  if (ctrl.type.toLowerCase() != "hidden")
				  ctrl.focus();
      		return false;
    	}
  	}
	var myRegExp = /^([0-9][0-9][0-9][0-9])-(0{0,1}[0-9]|1[0-2])-(0{0,1}[0-9]|[1-2][0-9]|3[0-1])$/gi;
	var res = datastr.search(myRegExp);
  	if (res == 0)
  	{
    	return true;
  	}
  if (ctrl.type.toLowerCase() != "hidden")
	  ctrl.focus();
  	alert(name);
  	return false;
}

function parseNum(theNum){
  if (theNum.substring(0,1)==0)
    theNum=theNum.substring(1)
  return theNum
}

function parseYMD(theYear,theMonth,theDay) {
  theYear=parseNum(theYear)
  theMonth=parseNum(theMonth)
  theDay=parseNum(theDay)
  if ((theYear < 1900) || (theYear > 3000)){
    return 1
  }

  if (theMonth < 1 || theMonth > 12){
    return 2
  }

  if ((theMonth==1 || theMonth==3 || theMonth==5 || theMonth==7 || theMonth==8 || theMonth==10 || theMonth==12) &&
      (theDay <1 || theDay > 31)
     ){
    return 3
  }

  if ((theMonth==4 || theMonth==6 || theMonth==9 || theMonth==11) &&
      (theDay <1 || theDay > 30)
     ){
    return 3
  }

  if (theYear%400==0 || (theYear%4==0 && theYear%100!=0)){  //  
    if (theMonth==2 && (theDay <1 || theDay > 29) )
      return 3
  }
  else  //  
    if (theMonth==2 && (theDay <1 || theDay > 28) )
      return 3
  return 0
}

function isInvalidDate(theDate,separator){
  default_style=1
  if (theDate.length>10 || theDate.length<8)
    return true
  idx1=theDate.indexOf(separator)
  if (idx1==-1)
    return true
  idx2=theDate.indexOf(separator,idx1+1)
  if (idx2==-1)
    return true
  if (isInvalidDate.arguments.length>2)
  	default_style=isInvalidDate.arguments[2]
  if (default_style<1 || default_style>9){
  	alert("Parameter Error,please check!")
	return true
  }

  if (default_style==1){
  theYear=theDate.substring(0,idx1)
  theMonth=theDate.substring(idx1+1,idx2)
  theDay=theDate.substring(idx2+1)
  }
  if (default_style==2){
  theMonth=theDate.substring(0,idx1)
  theDay=theDate.substring(idx1+1,idx2)
  theYear=theDate.substring(idx2+1)
  }
  if (theDay.length>2)
    return true
  if (parseYMD(theYear,theMonth,theDay)>0)
    return true
  else
    return false
}

function openWindow(n,u,w,h)
{
	var windowLeft = (window.screen.availWidth - w)/2;
	var windowTop = (window.screen.availHeight - h)/2;
	args = "left="+windowLeft+",top="+windowTop+",width="+w+",height="+h+",resizable=yes,scrollbars=yes,status=1";
	window.open(u,n,args);
}

function openStaticWindow(n,u,w,h)
{
	var windowLeft = (window.screen.availWidth - w)/2;
	var windowTop = (window.screen.availHeight - h)/2;
	args = "left="+windowLeft+",top="+windowTop+",width="+w+",height="+h+",resizable=no,scrollbars=no,status=0";
	window.open(u,n,args);
}

function openStaticWindowSetPosition(n,u,w,h,r,t)
{
	var windowLeft = window.screen.availWidth - r - w;
	var windowTop = t;
	args = "left="+windowLeft+",top="+windowTop+",width="+w+",height="+h+",resizable=no,scrollbars=no,status=0";
	window.open(u,n,args);
}

function openModalDialog(u,w,h)
{
//	var windowLeft = (window.screen.availWidth - w)/2;
//	var windowTop = (window.screen.availHeight - h)/2;
//	args = "dialogLeft="+windowLeft+"px;dialogTop="+windowTop+"px;dialogWidth="+w+"px;dialogHeight="+h+"px;";
	return window.showModalDialog(u);
}

function setSelectionOption(the_selection, option_list)
{
  var arr_dir, str_dir;
  the_selection.length=0;
  for(var i=0; i<option_list.length; i++)
  {
    	str_dir=new String(option_list[i]);
        arr_dir=str_dir.split("===");
        the_selection.options[i] = new Option();
		the_selection.options[i].value = arr_dir[0];
		the_selection.options[i].text = arr_dir[1];
  }
}

function selectOptionInSelect(the_selection, value)
{
    for (var i=0; i<the_selection.length; i++)
    {
    if (the_selection.options[i].value == value) 	the_selection.options[i].selected = true;
    }
}

function getSelectionValues(selectCtrl)
{
	var values = '';
	var s;
	if (selectCtrl == null) 
		return values;
	s = selectCtrl;
	if (s.type)
		if (s.checked)
			return s.value;
		else
			return values;
	for (i=0; i<s.length; i++)
	{
		if (s[i].checked)
		{
			if (values == '')
				values += s[i].value;
			else
				values += ',' + s[i].value;
		}
	}
	return values;
}

function selectAllSelections(selectCtrl, selected)
{
	if (selectCtrl == null) 
		return;
	s = selectCtrl;
	if (s.type)
	{
		if (s.disabled != true && s.readonly != true)
			s.checked = selected;
	}
	for (i=0; i<s.length; i++)
	{
		if (s[i].disabled != true && s[i].readonly != true)
			s[i].checked = selected;
	}
}
function validateFormData(theForm)
{
	submitFormInResponseWindow(theForm);
}

function resetFormData(theForm)
{
	theForm.reset();
	return true;
}
//resize image size
function resizeImg(resLimit,limit){
	if (window.document.all.item('res')!=null)
		{resize (window.document.all.item('res'),resLimit);}
	if (window.document.all.item('map')!=null)
		{resize (window.document.all.item('map'),resLimit);}
	var i;
	for (i=0;window.document.all.item('img'+i)!=null;i++){
	resize (window.document.all.item('img'+i),limit);}
}
function resize(obj,limit){
	//var limit = 250;
	if (obj.width >limit || obj.height >limit){
		if (obj.height > obj.width)
			obj.height = limit;
		else
			obj.width = limit;
	}
}

function getPos(el,sProp) {
	var iPos = 0;
	while (el!=null) {
		iPos+=el["offset" + sProp];
		el = el.offsetParent;
	}
	return iPos;
}

function getDays(month, year)
{
	/*
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for...
	2.Years also evenly divisible by 100 are not leap years, except for...
	3.Years also evenly divisible by 400 are leap years.
	*/
	var monthArray1 = new Array();
	var monthArray2 = new Array();
	var monthNo = month - 1;
	// Non-Leap year Month days..
	monthArray1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	// Leap year Month days..
	monthArray2 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	if ((year % 4) == 0) {
		if ((year % 100) == 0 && (year % 400) != 0)
			return monthArray1[monthNo];

		return monthArray2[monthNo];
	} else
		return monthArray1[monthNo];
}

function selectDate(yearSelectId,monthSelectId,daySelectId,defaultDay)
{
	
	var frmYear = document.getElementById(yearSelectId);
	var frmMonth = document.getElementById(monthSelectId);
	var frmDay = document.getElementById(daySelectId);
	var newDate = new Date();
	var year = frmYear.value;
	var month = frmMonth.value;
	if (year > 0 && month > 0)
	{
		var days = getDays(month,year);
		var i;
		frmDay.options.length = 0;
		for (i = 1;i <= days;i ++)
		{
			frmDay.options[i-1]= new Option(i ,i);
			if (i == defaultDay)
				frmDay.options[i-1].selected = true;
		}
	}
}

function dictionary(name,value,connective)
{
	this.name = name;
	this.value = value;
	this.connective = connective;
}

function trim(ss){
   return( (ar=/^\s*([\s\S]*\S+)\s*$|^\s*$/.exec(ss)) ? ar[1] : "" );
}

function showSubList(optionId,subOptionId,arrayName,SubArrayName,selectedValue,defaultStr)
{
	var frmOptionId = document.getElementById(optionId);
	var frmSubOptionId = document.getElementById(subOptionId);
	var parentArray = new Array();
	var subArray = new Array;
	var parentCode,fieldValue,connective,i,j,k=0,parentLength,subLength;
	parentArray = eval(arrayName);
	parentLength = parentArray.length;
	subArray = eval(SubArrayName);
	optionValue = frmOptionId.value;
	parentLength = parentArray.length;
	subLength = subArray.length;
	frmSubOptionId.options.length = 0;
	if (defaultStr == "")
		defaultStr = "请选择";
	frmSubOptionId.options[k]= new Option(defaultStr,"");
	k ++;
	if (optionValue.length > 0)
	{
		for (i = 0;i < parentLength;i ++)
		{
			if (parentArray[i].value == optionValue)
			{
				connective = parentArray[i].connective;
				break;
			}	
		}
		if (connective.length > 0)
		{
			for (j = 0;j < subLength;j ++)
			{
				if (subArray[j].connective == connective)		
				{
					frmSubOptionId.options[k]= new Option(subArray[j].name,subArray[j].value);
					if (subArray[j].value == selectedValue)
					{
						frmSubOptionId.options[k].selected = true;
					}
					k ++;
				}
			}
		}
	}
}

function changeDivDisplayStyle(divId)
{
	var theStyle = document.getElementById(divId).style.display;
	document.getElementById(divId).style.display = theStyle == "none" ? "" : "none";
}
