// function to check whether the value is blank
// return true if value is blank
function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}
function isBlank(val) {
	if (val == null) { return true; }
	for (var i=0; i < val.length; i++) {
		if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n")) { return false; }
		}
	return true;
	}

///function to check whether the value is amount
function isAmount(num) {
	var string="1234567890.,";
	if (string.indexOf(num) != -1) {
		return true;
		}
	return false;
	}

///function to check whether the value is amount
function isDigit(num) {
	var string="1234567890,";
	if (string.indexOf(num) != -1) {
		return true;
		}
	return false;
	}

///function to check the whether the value is Integer
function isInteger(val,param1) 
{
	for (var i=0; i < val.length; i++) 
	{
		if(param1=="amount")
		{
			if (!isAmount(val.charAt(i))) 
			{ 
				return false; 
			}
		}
		else
		{
			if (!isDigit(val.charAt(i))) 
			{ 
				return false; 
			}
		}
	}
	return true;
}

// function to check the value is integer
function checkInt(val,param1)
{
	value=val.value
	if(param1=="amount")
	{
		ret=isInteger(value,"amount");
	}
	else
	{
		ret=isInteger(value)	;
	}
	ret1=isBlank(value);
	/////if value is left blank
	if(ret1==true)
	{
		if(param1=="amount")
		{
			val.value="0.00";
		}
		else
		{	
			val.value="0";	
		}
		val.focus();
	}
	else
	{
		//////else show the error
		if(ret==false)
		{
			if(param1=="amount")
			{			
				alert("Please enter Amount value");
				val.value="0.00";
			}
			else
			{	
				alert("Please enter numeric value");
				val.value="0";	
			}
			val.focus();
		}
		if(param1=="amount")
		{			
			value=formatCurrency(value,'amount');
			val.value=value;
		}
		else
		{
			val.value=value;
		}
	}		
}
///////function to make an Array
function makeArray(IntarrSize) {

  for (var n = 0; n < IntarrSize; n++)
    this[n] = "";

  return this;

}
/////////function equivalent to explode
function customSplit(strvalue, separator, arrayName) 
{
  var n = 0;

  if (separator.length != 0) {
    while (strvalue.indexOf(separator) != -1) {
      eval("arr"+n+" = strvalue.substring(0, strvalue.indexOf(separator));");
      strvalue = strvalue.substring(strvalue.indexOf(separator)+separator.length,
          strvalue.length+1);
      n++;
    }
    eval("arr" + n + " = strvalue;");
    arraySize = n+1;
  }
  else {
    for (var x = 0; x < strvalue.length; x++) {
      eval("arr"+n+" = \"" + strvalue.substring(x, x+1) + "\";");
      n++;
    }
    arraySize = n;
  }

  eval(arrayName + " = new makeArray(arraySize);");

  for (var i = 0; i < arraySize; i++)
    eval(arrayName + "[" + i + "] = arr" + i + ";");

  return arraySize;
}

/////FUNCTION TO FORMAT THE CURRENCY
function formatCurrency(num,param) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	if(param=='amount')
	{
		return (((sign)?'':'-') + num + '.' + cents);
	}
	else
	{
		return (((sign)?'':'-') + num);
	}
}

// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
function isValidDate(dateObj) {

dateStr=dateObj.value;
if(dateStr=="")
	{
	return false;
	}
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("`"+dateStr + "` is not a valid date format.")
	dateObj.select();
return false;
}
month = matchArray[3]; // parse date into variables
day = matchArray[1];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
dateObj.select();
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
dateObj.select();
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
dateObj.select();
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
dateObj.select();
return false;
   }
}
return true;
}


function dateDiff(first, second) {
date1 = new Date();
date2 = new Date();
diff  = new Date();

size=customSplit(first, "/", "First") ;
size1=customSplit(second, "/", "Second") ;
date1temp = new Date(First[1]+"/"+First[0]+"/"+First[2]+" 00:00:00AM");
date1.setTime(date1temp.getTime());

date2temp = new Date(Second[1]+"/"+Second[0]+"/"+Second[2]+" 00:00:00AM");
//date2temp = new Date("10/10/2002 00:00:00AM");
date2.setTime(date2temp.getTime());

// sets difference date to difference of first date and second date

diff.setTime(date1.getTime() - date2.getTime());

timediff = diff.getTime();

weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
timediff -= days * (1000 * 60 * 60 * 24);

hours = Math.floor(timediff / (1000 * 60 * 60)); 
timediff -= hours * (1000 * 60 * 60);

mins = Math.floor(timediff / (1000 * 60)); 
timediff -= mins * (1000 * 60);

secs = Math.floor(timediff / 1000); 
timediff -= secs * 1000;

//dateform.difference.value = weeks + " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds";
total=eval(eval(weeks * 7) + days);

return total; // form should never submit, returns false
}



/**
 * 给指定表单中指定名称的复选框置选中标志
 * @param formObj    - 表单对象
 * @param eleNameArr - 存放指定元素名称的数组
 * @checkedFlag      - 选中标志
 * @param 
 */
function setCheckedFlag(formObj,eleNameArr,checkedFlag){
  for(var i=0;i<formObj.elements.length;i++){  
     var ele = formObj.elements[i];
     for(var j=0;j<eleNameArr.length;j++){
       if(ele.name == eleNameArr[j]){
          ele.checked = checkedFlag;
          break;
       }	
     }
  }
}


/**
 * 获得指定表单中指定名称的复选框的选中数量
 * @param formObj    - 表单对象
 * @param eleNameArr - 存放指定元素名称的数组
 * @param 
 */
function getCheckedRecordNum(formObj,eleNameArr){

  var checkedNum = 0;
  for(var i=0; i<formObj.elements.length; i++){

     var ele = formObj.elements[i];
     for(var j=0;j<eleNameArr.length;j++){
       if(ele.name == eleNameArr[j] && ele.checked){
          checkedNum ++;
          break;
       }	
     }	
  }
  
  //
  return checkedNum;
}

/**
 * 去除字符串头尾的空格
 * @param str 被TRIM的字符串
 */
function trim(str){
   return(trimExt(str,' '));	
}


/**
 * 去除字符串头尾的指定的符号
 * @param str 被TRIM的字符串
 * @param ch 指定的字符
 */
function trimExt(str,ch){
   if(str == null) return(null);
   	
   //去除头上的空格	
   var start = 0,end = 0;   //记录字符串的起始和结束位置
   var i = 0;
   while(i<str.length){
   	  //判断是否为空格
   	  if(str.charAt(i) == ch){
   	  	i++;
   	  }else{
   	    start = i;
   	    break;
   	  }
   }
   
   i = str.length -1;
   while(i>=0){
   	  //判断是否为空格
   	  if(str.charAt(i) == ch){
   	  	i--;
   	  }else{
   	    end = i + 1;
   	    break;
   	  }
   }   
   
   return(str.substring(start,end));
}


/**
 * 检查是否整数
 * @param value   要检查的值
 * @param return  是整数就返回TRUE
 */
function isNumber(value){
	if(trim(value)!=''&&!isNaN(value)&&(value.indexOf('.')==-1)){
		return(true);
	}
	
	return(false);
}

/**
 * 根据原先的值初始化下拉列表框
 * @param selectObj - 下拉列表框对象
 * @param oldValue  - 原先的值
 */
function initSelectWithValue(selectObj,oldValue){
  for(var i=0;i<selectObj.length;i++){
    if(selectObj.options[i].value == oldValue){
      selectObj.options[i].selected = true;
      break;
    }
  }
}

/**
 * javascript中的urlencode函数
 * 因为js中的escape的处理和java中的encode不完全一样
 * 除了才有了这个函数
 * @param str - 需要被encode的字符串
 */
function encode(str){
  if(str == null) return '';

  //先做一次编码
  var retStr = escape(str);

  //处理url中所有的+号，该字符escape中不处理
  if(str.indexOf('+') != -1){
    retStr = retStr.replace(/\+/g,'%2B');
  }
  
  //
  return retStr;
}
 

/**
 * 编码
 * 这个函数暂时不需要  
 * @param str - 
 */
function XMLEncode(str){
     str = trim(str);
     str = str.replace("&","&amp;");
     str = str.replace("<","&lt;");
     str = str.replace(">","&gt;");
     str = str.replace("'","&apos;");
     str = str.replace("\"","&quot;");
     return str;
}

/**
 * 解码
 * 这个函数暂时不需要  
 * @param str - 
 */
function XMLDecode(str){
     str = trim(str);
     str = str.replace("&amp;","&");
     str = str.replace("&lt;","<");
     str = str.replace("&gt;",">");
     str = str.replace("&apos;","'");
     str = str.replace("&quot;","\"");
     return str;
}

/**
 * 调节图片的长度和宽度做限制
 * 长度和宽度哪个超出就限制哪个
 * img src="image" onload="adjustImageSize(this,45,160)"
 * @param imageObj  - img 控件
 * @param maxHeight - 图片长度限制
 * @param maxWidth  - 图片宽度限制
 */
function adjustImageSize(imageObj,maxHeight,maxWidth){
  if(maxHeight<imageObj.height){
    imageObj.height=maxHeight;
  }
  if(maxWidth<imageObj.width){
    imageObj.width=maxWidth;
  }
}

function setImgSize(theURL,sImage){
var imgObj;
var sizeStand = 200;

imgObj = new Image();
imgObj.src = theURL;

if ((imgObj.width != 0) && (imgObj.height != 0)) {
if(imgObj.width > imgObj.height) { 
var iHeight = imgObj.height*sizeStand/imgObj.width;
sImage.height = iHeight;
sImage.width = sizeStand;
} else {
var iWidth = imgObj.width*sizeStand/imgObj.height;
sImage.width = iWidth;
sImage.height= sizeStand;
}
}else{
sImage.width = 200;
sImage.height= 200;
}
}

function setBigImgSize(theURL,sImage){
var imgObj;
var sizeStand = 200;

imgObj = new Image();
imgObj.src = theURL;

if ((imgObj.width != 0) && (imgObj.height != 0)) {
if(imgObj.width > imgObj.height) { 
var iHeight = imgObj.height*sizeStand/imgObj.width;
sImage.height = iHeight*2;
sImage.width = sizeStand*2;
} else {
var iWidth = imgObj.width*sizeStand/imgObj.height;
sImage.width = iWidth*2;
sImage.height= sizeStand*2;
}
}else{
sImage.width = 360;
sImage.height= 360;
}

}

function bbimg(o){
	var zoom=parseInt(o.style.zoom, 10)||100;zoom+=event.wheelDelta/12;if (zoom>0) o.style.zoom=zoom+'%';
	return false;
}
//临时跟踪函数开始
function clk(tl)
{if(document.images)
{new Image().src="http://tracelog.www.alibaba.com/null.gif?tracelog="+escape(tl);
}
return true;}
//临时跟踪函数结束

var initText = new Array('Enter product keyword here');
function clearSearchText(obj){
	if (obj==null) return ;
	for (var i = 0; i < initText.length; i++) {
	    if (initText[i]==obj.value){
	  	obj.value='';
	  	return ;
	  }
   }
}

