/**
 *	模块代码：		jfunction.js
 *	模块名称:		公用处理类
 *	引用程序类:		
 *	功能描述:		
 */



/**
 * 判断textObj是否有填写数据,及显示错误消息
*/

function isNull(textObj,message)
{	
	if (textObj.value=="")
	{
		textObj.focus();
		if (message==undefined)
		{
			alert ("This field can not be blank!")
		}else
		{
			alert(message)
		}		
		return true;
	}
	return false;
}


/**
 * 判断noObj是否是整数
*/
function isInteger(noObj)
{
	if (noObj.value=="")
	{
		return true;
	}

	if (isNull(noObj) || (noObj.value.indexOf("-")>-1))
	{
		//alert ("Please input an integer!");
		noObj.focus();
		return false;
	}
	if (isNull(noObj) || (noObj.value.indexOf(".")>-1))
	{
		//alert ("Please input an integer!");
		noObj.focus();
		return false; 
	}
	strValue=noObj.value;
	for (i=0;i<strValue.length;i++)
	{
		if (strValue.charCodeAt(i,1)!=46 && (strValue.charCodeAt(i,1)<48 || strValue.charCodeAt(i,1)>57))
		{
			//alert ("Please input an integer!");
			noObj.focus();
			return false;
		}
	}
	return true;
}

/**
 * 检查checkBox是否被选中 至少选中其中一个
*/
function isCheckBoxChecked(obj,message)
{
	var i=0;
	while (obj[i]!=undefined)
	{		
		if (obj[i].checked==true)
		{
			return true;
		}
		i++;
	}
	if (obj.checked==true)
	{
		return true;
	}
	alert(message);
	return false;
}

/**
 * 检查Radio是否被选中 没有被选中返回false 选中返回选中的值
*/
function getRadioCheckedValue(obj)
{
	var i=0;
	while (obj[i]!=undefined)
	{		
		if (obj[i].checked==true)
		{
			return obj[i].value;
		}
		i++;
	}
	if (obj.checked==true)
	{
		return obj.value;
	}
	alert("请选择一项记录");
	return false;
}


/*
检查EMAIL格式
*/ 

function isValidateEmail(value)
{
	var atIndex=value.indexOf("@");
	var dotIndex=value.lastIndexOf(".");
	return ((atIndex>1) && (dotIndex>atIndex));
}
/*
检查EMAIL格式
*/ 

function isEmail(obj)
{
	if (obj.value=="")
	{
		return true;
	}
	var atIndex=obj.value.indexOf("@");
	var dotIndex=obj.value.lastIndexOf(".");
	if((atIndex>1) && (dotIndex>atIndex)) {
	  return true;
	}
	obj.focus();
	return false;
}
function GetData(url)
{
        try
        {
                DataLoad.src = url;
        }
        catch(e)
        {
                return false;
        }
       // {
       // var timeoutid = setTimeout("GetData()",1000)
     //   }
}

function chkIntvStauts(mustAnswerQuestions,optionalAnswerQuestions,noOfMustAnsQues,noOfOptionalAnsQues,result)
{
  var totalMustAnsques=0;
  var totalOptionalAnsques=0;  
  for (i=0;i<mustAnswerQuestions.length;i++)
  {
  	if(chkAnsStatus(mustAnswerQuestions[i]))
  		totalMustAnsques++;
  }
  for (j=0;j<optionalAnswerQuestions.length;j++)
  {
  	if(chkAnsStatus(optionalAnswerQuestions[j]))
  		totalOptionalAnsques++;
  }
  result[0]=totalMustAnsques;
  result[1]=totalOptionalAnsques;
 // alert(totalMustAnsques+","+totalOptionalAnsques);
  return ((totalMustAnsques>=noOfMustAnsQues) && (totalOptionalAnsques>=noOfOptionalAnsQues));
  
}


function chkAnsStatus(varQuestionId)
{
	//alert(varQuestionId);
	if	(varQuestionId=="00009")
		return false;
	eval("var varAnswer = getRadioCheckedValueEx(form1.radio_"+varQuestionId+")");
	return (varAnswer!="6");
}

function getRadioCheckedValueEx(obj)
{
	
	try
	{ 
		var i=0;
		while (typeof(obj[i])!="undefined")
		{		
			if (obj[i].checked==true)
			{
				return obj[i].value;
			}
			i++;
		}
		return "6";
	}
 	catch(e)
   {
                return "6";
   }
   
}
function openWin(sign)
{
    var link="/bmw/pages/jsp/include/onlinehelp.htm#"+sign;
	window.open (link, 'newwindow', 'height=600, width=517, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no');
}



//
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

/*
*check for the two date
*/
function datecompare(bigdate,smalldate){
     
   var schar="-";
   if(bigdate.substring(0,1)!=0){ 
    	 schar=bigdate.substring(2,3); 
    }
    else{ 
    	 schar=bigdate.substring(3,4);  
    }   
    schar=bigdate.substring(4,5); 
    var bigdateArray=bigdate.split(schar);
    var smalldateArray=smalldate.split(schar);
     
    if(parseInt(bigdateArray[0])<=parseInt(smalldateArray[0])){
        if(parseInt(bigdateArray[0])==parseInt(smalldateArray[0])){
            if(parseInt(bigdateArray[1])<=parseInt(smalldateArray[1])){
                if(parseInt(bigdateArray[1])==(smalldateArray[1])){
                    if(parseInt(bigdateArray[2])>=parseInt(smalldateArray[2])){
                        return true;
                    }
                    else{
                        return false;
                    }
                }
                else{
                    return false;
                }
            }
            else{
                return true;
            }
         }
         else{
            return false;
         }
    }
    else{
        return true;
    }
    
}


//**********************************************************************************************************
//功能：日期检查函数，支持3种年、月、日之间的分隔符 "-"、"."和"/"可以选择年、月、日是否应该完整。
//  正确的日期格式为：2001-2-13 2001 2001-2 2001.2.13  2001.2 2001/2/3，日期范围为 1-1-1 到 9999-12-31 
//  同时，对当前年当前月的天数也做了判断，如：2001-2-29 2001-4-31 都是非法的日期
//参数：strDate ---- 需要判断的日期字符串
//  intFlag: 1 ---- 可以没有日  2 ---- 可以没有日和月 0 ---- 年月日必须齐全
//返回值：true ---- 日期合法 false ---- 日期不合法
function fnCheckDate(strDate,intFlag)
{
 var strCheckDate = strDate + "";     //进一步确认哪来判断的肯定是一串字符串
 
 if(strCheckDate == "")        //空字符串,不是合法的日期字符串，返回false
 {
  return false;
 } 
 
 //判断传进来的数据是那种格式写成日期
 var intIndex = -1;         //利用正则表达式，查找字符串中是否包含某个字符，没找到为-1,否则为 （0 - String.length - 1）
 var arrDate;          //分别存储年月日
 var regExpInfo = /\./;        //正则表达式，匹配第一个出现 "."的位置
 
 //在这里，我之所以不使用replace函数把所有的"."和"/"换成"-",然后分别存储年月日，是因为用户有可能输入 2001/3-2,就判断不出它是不合法日期了
 intIndex = strCheckDate.search(regExpInfo);   //查找是否含有 "."
 if(intIndex == - 1)         //不包含  
 {
  regExpInfo = /-/;
  intIndex = strCheckDate.search(regExpInfo);
  
  if(intIndex == -1)
  {
   regExpInfo = /\//;       //查找是否含有 "/"
   intIndex = strCheckDate.search(regExpInfo); 
   
   if(intIndex == -1)
   {
    arrDate = new Array(strCheckDate);  //只包含年
   }
   else
   {
    arrDate = strCheckDate.split("/");  //2001/3/7 型
   }
  }
  else
  {
   arrDate = strCheckDate.split("-");   //2001-3-7 型
  }
 }
 else
 {
  arrDate = strCheckDate.split(".");    //2001.3.7 型
 }
 
 if(arrDate.length > 3)        //如果分离出来的项超过3，除了年月日还有其它的，不合法日期，返回false
 {
  return false;
 }
 else if(arrDate.length > 0) 
 {
  //判断年是否合法
  if(fnIsIntNum(arrDate[0]))   //是正整数
  {
   if(parseInt(arrDate[0]) < 1800 || parseInt(arrDate[0]) > 9999)  //年范围为1800 - 9999
   {
    return false;
   } 
  }
  else
  {
   return false;     //年不是正整数，错误
  }
   
  //判断月是否合法
  if(arrDate.length > 1)
  {
   if(fnIsIntNum(arrDate[1]))  //是正整数
   {
    if(parseInt(arrDate[1]) < 1 || parseInt(arrDate[1]) > 12)
    {
     return false;
    } 
   }
   else
   {
    return false;
   }
  }
  else //没有月
  {
   if(intFlag != 2)    //必须得有月
   {
    return false;
   }
  }
   
  //判断日是否合法
  if(arrDate.length > 2)
  {
   if(fnIsIntNum(arrDate[2]))  //是正整数
   {
    var intDayCount = fnComputerDay(parseInt(arrDate[0]),parseInt(arrDate[1]));
    if(intDayCount < parseInt(arrDate[2]))
    {
     return false;
    }   
   }
   else
   {
    return false;
   }
  }
  else
  {
   if(intFlag == 0)    //必须得有日
   {
    return false;
   }
  }
 }
 return true;
}

//**********************************************************************************************************
//判断一个数是否为正整数
//参数：strNum ---- 需要判断的字符串
//返回值：true ---- 整数 false ---- 非整数
function fnIsIntNum(strNum)
{
 var strCheckNum = strNum + "";
 if(strCheckNum.length < 1)         //空字符串
  return false;
 else if(isNaN(strCheckNum))         //不是数值
  return false;
 else if(parseInt(strCheckNum) < 1)       //不是正数
  return false; 
 else if(parseFloat(strCheckNum) > parseInt(strCheckNum)) //不是整数 
  return false;
 
 return true;
}

//**********************************************************************************************************
//功能：判断intYear年intMonth月的天数
//返回值：intYear年intMonth月的天数
function fnComputerDay(intYear,intMonth)
{
    var dtmDate = new Date(intYear,intMonth,-1);
    var intDay = dtmDate.getDate() + 1;
    
    return intDay;    
}

//********************************************************************************************************** //功能：去掉字符串前后空格
//返回值：去掉空格后的字符串
function fnRemoveBrank(strSource)
{
 return strSource.replace(/^\s*/,'').replace(/\s*$/,'');
}		