/**
* global variable which save the selected element
*/
var oldSelectElement=new Array();
/**
*add a remove method to Array prototype
*@param index int the index of object which you want to remove in the array
*/
Array.prototype.remove=function(index) { 
  //if index is less than 0]
  if(index<0){  
    return this;
  } else{
  	var temp=this.slice(0,index).concat(this.slice(index+1,this.length));
    return temp;
  }
}
// Cross-browser implementation of element.addEventListener()
function addListener(element,type,expression,bubbling){
	bubbling = bubbling || false;
	if(window.addEventListener)	{
		 // Standard		
		 element.addEventListener(type, expression, bubbling);
		 return true;
	 } else if(window.attachEvent) {
		  // IE		
		  element.attachEvent('on' + type, expression);
		  return true;	
	  } else 
	  return false;
}
function adhere(id){
  if(id){
    window.location=contextpath+'/mediakits.do?path=aboutUs&type=AboutUs';
  }else{
    window.location=contextpath+'/aboutus.do?path=aboutUs&type=AboutUs';
  }
}
/**
* set the body's height
*/
function setMainBody(add)
{
  var top;
  var isMozilla = (navigator.appName.indexOf("Netscape") != -1);
  if (isMozilla) {top = 90;} else { top= window.screenTop;}
  var bodyHeight = window.screen.availHeight-top-140;
  var formHeight = bodyHeight;
  if (null != add)
  formHeight -= add;
  if (bodyHeight > 1280 ) return;
  document.getElementById("main_body").style.height=bodyHeight+"px";
  document.getElementById("main_form").style.height=formHeight+"px";
}
/**
* set the popup window's height
*/
function setMainHeight()
{
	var bodyHeight = window.document.body.offsetHeight;
	var bodyWidth=window.document.body.offsetWidth;
	document.getElementById('main_form').style.height=bodyHeight -  32;
}

function submitByAjax(url,formName,divId)
{
	if(formName!=null && formName!="")
	{
		var formValue=document.forms[formName]?document.forms[formName]:document.getElementById(formName);
		formValue.action=contextpath+url;
		formValue.submit();
	}
	else
	{
		window.location.href=contextpath+url;
	}
}
function showModalCenter(URLStr,width,height)
{
	var rValue=window.showModalDialog(URLStr,"","dialogWidth:"+width+"px; dialogHeight:"+height+"px; center:yes;status:no;help:no;");	
	return rValue;
}

/**
deal with the rows select
param :element <tr>
       isSingleChecked: if sigle check,pass "true".Aslo can null. 
to use this ,add onclick="selectItem(this)" to the <tr> tag
and make sure the first <td> tag contain that 
//------------------------------ add by daogang_tang
<input type="hidden" name="isSelected" value="false"> is the first property under the <td> tag
//------------------------------
<input type="hidden" name="isSelected" value="false">
<input type="hidden" name="id" value="primarykey">.
the two names may change,but the order can not change.
when the hidden value of isSelected is true,it is mean the item is selected
when the hidden value of isSelected is false,it is mean the item is not selected.
*/
function selectItem(element,isSingleChecked)
{
	if(element.type!="checkbox")
	{
		var selectElement=element.firstChild.firstChild;
		if(selectElement != null && selectElement.type=="checkbox")
		{
			selectElement.checked=true;
			setElementProperty(element,selectElement);
			unselectOther(element);
		}
	}else
	{
		setElementProperty(element.parentNode.parentNode,element);
		if (null != isSingleChecked && isSingleChecked) unselectOther(element.parentNode.parentNode);
	}
}
/**
* set the element backgroudColor and remove oldElement from global variable :oldSelectElement
*/
function setElementProperty(element,checkedElement)
{
	for(var i=0;i<oldSelectElement.length;i++)
	{
		if(oldSelectElement[i]==element)
		{
			oldSelectElement=oldSelectElement.remove(i);
			break;
		}
	}
	
	if (checkedElement.checked)
	{
		element.style.backgroundColor="#c8dcff";
		oldSelectElement[oldSelectElement.length]=element;
	}
	else
	{
		element.style.backgroundColor="";
	}
}

/*unselect other element*/
function unselectOther(element)
{
    var i=0;
	while(i<oldSelectElement.length)
	{
		if(oldSelectElement[i]==element)
		{
			i++;
			continue;
		}else
		{
			//if element's parent equals array's current element's parent then remove element from array
			if(element.parentNode==oldSelectElement[i].parentNode)
			{
				oldSelectElement[i].children[0].firstChild.checked=false;
				oldSelectElement[i].style.backgroundColor="";
				oldSelectElement=oldSelectElement.remove(i);
			}else
			{
				i++;
			}
		}
	}
}
/*clear oldSelectElement*/
function clearArray()
{
   var i=0;
	while(i<oldSelectElement.length)
	{
		if(oldSelectElement[i].children[0]!=null)
		{
				oldSelectElement[i].children[0].firstChild.checked=false;
				oldSelectElement[i].style.backgroundColor="";
		}
				oldSelectElement=oldSelectElement.remove(i);
	}
}

function ajax_personal_fun(){
setMainBody();}
//Add by shifeng_wang ??????firefox??????
  var isFirefox2=false;
  var firefox = navigator.userAgent.match(/firefox\/\d+.\d+/i);
  if (firefox) {
   var firefox_version = parseFloat(firefox[0].match(/\d+.\d+/)[0]);
   if (firefox_version >= 2.0) {
    isFirefox2 = true;
   }
  }


var browerType=new function(){
	this.isOpera=(window.opera&&navigator.userAgent.match(/opera/gi))?true:false;
	this.isIE=(!this.isOpera&&document.all&&navigator.userAgent.match(/msie/gi))?true:false;
	this.isSafari=(!this.isIE&&navigator.userAgent.match(/safari/gi))?true:false;
	this.isGecko=(!this.isIE&&navigator.userAgent.match(/gecko/gi))?true:false;
	this.isFirefox=(!this.isIE&&navigator.userAgent.match(/firefox/gi))?true:false;
};
/**
 *	open a custom window
 *	@param: _sUrl ??????????url??
 *	@param: _sWidth ???????
 *	@param: _sHeight ???????
 *	@param: _sTitle ???????
 *	@param: _sScroll ??????????
 */
function openWindow(_sUrl, _sWidth, _sHeight, _sTitle, _sScroll, eve) {
//Add by shifeng_wang open a custom window fit on firefox2.0 and IE
		
   	var oEdit = new dialog();
	oEdit.init();
	oEdit.set('title', _sTitle ? _sTitle : "??????" );
	oEdit.set('width', _sWidth);
	oEdit.set('height', _sHeight);
	if(eve)
	oEdit.setEventObj(eve);
	oEdit.open(_sUrl, _sScroll ? 'yes' : 'no');
	
	//Following codes is used to set dialogBoxShadow height to height of dialogBox and dialogFrame if dialogBox height dosn't equal dialogBoxShadow.
	var heightBox = $('dialogBox').offsetHeight;
	var heightShadow = $('dialogBoxShadow').style.height;
	var heightFrame = $('dialogFrame').style.height;
	if(heightBox!=heightShadow)
	{
		$('dialogFrame').style.height = $('dialogBoxShadow').offsetHeight-25;
		$('dialogBox').style.height = $('dialogBoxShadow').offsetHeight;
	}
	
	return oEdit;
}

/**
 * close window
 */
function closeWindow()
{
	parent.document.getElementById('dialogBox').style.display='none';
	parent.document.getElementById('dialogBoxShadow').style.display = "none";
	//parent.document.getElementById('dialogBody').innerHTML = '';
}

/**
 *
 */
function closeWindowWithInitMenuPosition()
{
	closeWindow();
}
/**
 * ?????callback??????????
 * */
function setDivFocus()
{
	if(document.getElementsByTagName('input')!=null && document.getElementsByTagName('input').length>0)
	{
		document.getElementsByTagName('input')[0].focus();
	}
}
/*
*Submit by ajax in the open window
*/
function changeMainForOpen(actionPath,formName,divId)
{
	submitByAjax(actionPath,formName,divId);
}

/*
*Check all or uncheck all
*/
function selectAll(selectId,checkBoxName)
{
			
	var isChecked = false;
	if(document.getElementById(selectId).checked)
	{
		isChecked = true;
	}
	for(var i=0;i<document.getElementsByName(checkBoxName).length;i++)
	{
		document.getElementsByName(checkBoxName)[i].checked=isChecked;
	}
}
/*
*  open google maps
*/
function openMaps(street,city,state){
	var width = 554;
	var height = 556;
	var top = (window.screen.height - height)/2;
	var left = (window.screen.width - width)/2;
	var maps = window.open('efpMapsAction.do?street='+street+'&city='+city+'&state='+state, 'maps', 'width='+width+',height='+height+',top='+top+',left='+left+',directories=no,menubar=no,status=no,toolbar=no,scrollbars=no')
}

/*    SunMan ??????cookie          */
var title = 'HELP';
var width ='300px';
var height = '50px';
var path ='';
var link ='';
//function runHelp(pagePath,content,titleStr,w,h,l)
function runHelp(l)
{
	/*str = content;
	if(str.indexOf('Help')!=-1)
	str = str.substring(0,str.indexOf('Help'));
	if(titleStr)
	title = titleStr;
	if(w)
	width = w;
	if(h)
	height = h;*/
	
	link = l;
	
	/*path =pagePath;//??????????????????
	
	var cookieValue = getCookie(path);
	
	if(cookieValue == null)
	{
		//alert('null-- set cookie'+path);
		setCookie(path,'1');
		writeDiv();
	}
	else
	{
		if(cookieValue!='not')//????????????cookie???????????????
		{
			//alert('writeDiv');
			writeDiv();
		}
	}*/
}
//-- cookie
	  function   setCookie(name,value)  
	  {  
		  var   Days   =   365;   //???   cookie   ????????????   30   ???  
		  var   exp     =   new   Date();         //new   Date("December   31,   9998");  
		  exp.setTime(exp.getTime()   +   Days*24*60*60*1000);  
		  //alert(name   +   "="+   escape(value)   +";expires="+   exp.toGMTString());
		  document.cookie   =   name   +   "="+   escape(value)   +";expires="+   exp.toGMTString();  
	  }  
	 function getCookie(str)
	 {
		 var tmp,reg=new RegExp("(^| )"+str+"=([^;]*)(;|$)","gi");
		 if(tmp=reg.exec(document.cookie))return(tmp[2]);
		 return null;
	 }
    function  delCookie(name)  
   	{  
      var   exp   =   new   Date();  
      exp.setTime(exp.getTime() - 1);  
      var   cval=getCookie(name);  
      if(cval!=null)   
	  document.cookie=name   +"="+cval+";expires="+exp.toGMTString();  

   	}

function ifShow()
{
	if("OK" == messageStatus1)
	{
		setCookie(path,'not');
	}
	else
	{
		return;
	}
}
function closeDiv()
{
	var ifChecked = document.getElementById('ifChecked');
	if(ifChecked.checked)
	{
		showMessageBox(" ","Don't show me again ?",1,"ifShow");
	}
	var divObj = document.getElementById('helpDiv');
	divObj.parentNode.removeChild(divObj);
}


function writeDiv()
{
	var cssStr ="width:450px;position:relative;border:1px solid #B8CBE7;font-family:arial;font-size:11px;padding-bottom:10px;padding-top:5px;text-align:left;padding-left:5px;color:#46597E;background:#FFFFEe;margin:auto";
	var spanStyle = 'cursor: pointer;cursor:hand;';
	var containerCss = 'width:100%;font-size:11px;font-family:arial;margin: 0px auto 10px auto;position:relative;top:6px;text-align:center;clear:both;';
	var cssStrContainer = 'width:420px;';
	document.write("<div id='helpDiv' style='"+containerCss+"'><div style='"+cssStr+"'><div style='"+cssStrContainer+"'>"+str
	+" <a href=# onclick=window.open(link,'newwindow','height=500,width=650,top=100,left=200,Resizable=yes,scrollbars=yes') style='text-underline-position: below ; text-decoration: underline;'>HELP</a>"+'"'+".&nbsp;&nbsp;<br><input type=checkbox id='ifChecked' ><span onclick=document.getElementById('ifChecked').click() style='"+spanStyle+"'><b>"+title+"</b></span></div><img src='images/close_popup.gif' onclick='closeDiv()' style='position:absolute;top:5px;right:5px;'></div></div>");
}
//openWindow(link,650,500,'TOTURIALS','no')
Number.prototype.formatNumber=function(total)
{ 
            var pennies = total * 100 ;
			pennies = Math.round(pennies) ;
			strPennies = "" + pennies ;
			len = strPennies.length ;
			var formatnum = strPennies.substring(0, len - 2) + "." + strPennies.substring((len - 2), len);
            return formatnum;
           
} 
Number.prototype.formatNumberTail=function(pointPsti)
{ 
       if(this=='')return false; 
            
            if(typeof(pointPsti)=='undefined')
            { 
                var pointPsti=3; 
            }else
            { 
            	if(isNaN(pointPsti))
            	{
            		pointPsti=3
            	}; 
            } 
            
            var num=this+'', numDc='', temp=''; 
            
            if(num.indexOf('.')>-1)
            { 
            	ptPs=num.indexOf('.'); numDc=num.substr(ptPs); num=num.substr(0,ptPs);
            } 
            for(var i=num.length-1; i>=0;temp+=num.substr(i,1), i--); 
            
            var re=new RegExp('(.{'+pointPsti+'})','g'); 
                temp=temp.replace(re,'$1,'); num='';  
                               
            for(var i=temp.length-1; i>=0; num+=temp.substr(i,1), i--); 
                num=num.replace(/^\,|\,$/,'')+numDc; 
            return num; 
           
} 
function checkIsInteger(str)
{
    //??????????
    if(str == "")
        return true;
    if(/^(-?)(d+)$/.test(str))
        return true;
    else
        return false;
}//
////////////////////////////
//
var initHeight=0;

function IframeHeight()
{
//	var scroHeight=document.getElementById(frameId).document.body.scrollHeight;
	var innerFrame=window.frames[frameId]?window.frames[frameId]:document.getElementById(frameId);
	if(innerFrame&&innerFrame.document.body!=null){
		var scroHeight=window.frames[frameId].document.body.scrollHeight+100;	
		if (initHeight==scroHeight && initHeight!=0)
		{
			document.all(frameId).style.height=scroHeight; 
			return;
		}
		else
		{
			initHeight=window.frames[frameId].document.body.scrollHeight+100;		
			
		}
	}
	window.setTimeout('IframeHeight()',100);
}

//////////////////////////////////////////Menu//////////////////////////
//cache a menu for each IMG
var menuCache = new Array();
function showMenu(id,posX,posY){
	var popupMenu = menuCache[id];
	hideAllMenus();
	hideCursorMenus();
	if ( null == popupMenu )
	{
		if(document.getElementById('selectItems'+id).value=="")
	    return;
		if (null == popupMenu)
		{
		   popupMenu = new jsDOMenu(100, "cursor");
		   var items=document.getElementById('selectItems'+id).value.split(';');
	 	   for(var i=0;i<items.length;i++){
	 	   		if(items[i] != ''){
		 			var item=items[i].split(',');
		 			popupMenu.addMenuItem(new menuItem(item[0],"",contextpath+item[1]));		 			
	 			}
		   }		   
		}
		popupMenu.show();
		menuCache[id] = popupMenu;
	}
	else
	{
	 	popupMenu.show();
	}
 	popUpMainMenuPosition(popupMenu.menuObj,parseInt(posX),parseInt(posY));
 	setPopUpMenu(popupMenu);
 	activatePopUpMenuBy(0, 2);
}
function hideMenu(){
	hideAllMenus();
	hideCursorMenus();
}

function getGlobalItems(globalIndex,evt){
	if(document.getElementById("items"+globalIndex)&&document.getElementById("items"+globalIndex).value!='' ){
		showMenu(globalIndex,posX,posY);
	}else{
		doPost(contextpath+"/supportLanguageslist.do","globalform"+globalIndex,"globaldiv"+globalIndex);
	}
}
//Open window of need image
var findex=0,lindex=20;
var currPage =1;
var f=0,l=20;
var primaryId;
function showImgPage()
{
	if(browerType.isFirefox){
		openWindow(contextpath+'/readblob.do?firstIndex='+findex+'&lastIndex='+lindex,620,615,needImageTitle);
	}else if(browerType.isIE){
		openWindow(contextpath+'/readblob.do?firstIndex='+findex+'&lastIndex='+lindex,635,622,needImageTitle);
	}else if(browerType.isSafari){
		openWindow(contextpath+'/readblob.do?firstIndex='+findex+'&lastIndex='+lindex,635,622,needImageTitle);
	}else{
		openWindow(contextpath+'/readblob.do?firstIndex='+findex+'&lastIndex='+lindex,635,622,needImageTitle);
	}
}


