//------------------------------------------------------------
// Common Prototype definition
//------------------------------------------------------------

String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

Map = function(){
	this._keys = new Array();
	this._vals = new Array();
};

Map.prototype.put = function(key,value){
	
	if(isNull(key)){
		return;
	}
	
	var idx = -1;
	for(var i=0;i<this._keys.length;i++){
		if(this._keys[i]==key){
			idx = i;
			break;
		}
	}
	if(idx<0){
		this._keys.push(key);
		this._vals.push(value);
	}else{
		this._vals[idx]=value;
	}
}

Map.prototype.get = function (key) {
	
	if(isNull(key)){
		return;
	}
	
	for(var i=0;i<this._keys.length;i++){
		if(this._keys[i]==key){
			return this._vals[i];
		}
	}	
	
	return null;
}


//------------------------------------------------------------
// Common funcitons
//------------------------------------------------------------
function addActionListener (elementObj,eventDesc,functionName){
	  if(window.attachEvent){elementObj.attachEvent('on'+eventDesc,functionName);}
	  else if(window.addEventListener) {elementObj.addEventListener(eventDesc,functionName,false);}
}

function removeActionListener (elementObj,eventDesc,functionName){
	  if(window.detachEvent){elementObj.detachEvent('on'+eventDesc,functionName);}
	  else if(window.removeEventListener){elementObj.removeEventListener(eventDesc,functionName,false);}
}

// set a value to cookie
function setCookieAttribute(cookieName,cookieValue){
  try{
    document.cookie = cookieName+'=' + escape(cookieValue) + ';' ;
  }catch(ex){
  }
}

// get a value to cookie
function getCookieAttribute(cookieName){
  try{
  	var cookieValue = null;
    var cookieString = document.cookie;
    var pairs = cookieString.split(';');
    for(var i=0;i<pairs.length;i++){
    	var pair  = pairs[i].split('=');
    	var name  = pair[0].trim();
    	var value = pair[1];
    	if( name==cookieName ){
    		if(isBlank(value)){    		
    			continue;
    		}
    		cookieValue = unescape(value);
    	}
    }
    return cookieValue;
  }catch(ex){
    return null;
  }
}

function isBlank(str){
	return null==str || 'undefined'==typeof(str) || ''==str;
}

function isNull(obj){
	return null==obj || 'undefined'==typeof(obj) || 'undefined'==obj;
}


//--------------------------------------------------------------------------

// override window.open function
var _openWindow = window.open;	


// override the original widow open function

function popupWindow(sURL, sName, sFeatures, bReplace,loginId,hasRetValue){
	try{
		if (sName != null && loginId != null && loginId != '') {
			sName = sName + '_' + loginId;
		}
		if ( isNull(sURL) )
			return null;
		var retVal;
		if (sName == null && sFeatures == null && bReplace == null) {
			retVal = _openWindow(sURL);
		}
		else if (sName != null && sFeatures == null && bReplace == null) {
			retVal = _openWindow(sURL, sName);
		}
		else if (sName != null && sFeatures != null && bReplace == null) {
			retVal = _openWindow(sURL, sName, sFeatures);
		}
		else {
			retVal = _openWindow(sURL, sName, sFeatures, bReplace);
		}
		if (retVal != null) {
			retVal.focus();
		}					
		if (sFeatures != null && retVal != null) {
			var winWidth = null;
			var winHeight = null;
			var winTop = null;
			var winLeft = null;
			var featuresArr = sFeatures.split(",");
			for (var num = 0; num < featuresArr.length; num++) {
				if (featuresArr[num].indexOf("width=") > -1) {
					winWidth = featuresArr[num].substring(featuresArr[num].indexOf("=") + 1, featuresArr[num].length);
				}
				if (featuresArr[num].indexOf("height=") > -1) {
					winHeight = featuresArr[num].substring(featuresArr[num].indexOf("=") + 1, featuresArr[num].length);
				}
				if (featuresArr[num].indexOf("top=") > -1) {
					winTop = featuresArr[num].substring(featuresArr[num].indexOf("=") + 1, featuresArr[num].length);
				}
				if (featuresArr[num].indexOf("left=") > -1) {
					winLeft = featuresArr[num].substring(featuresArr[num].indexOf("=") + 1, featuresArr[num].length);
				}								
				

			}
			if (winWidth != null && winHeight != null) {
				try {
					retVal.resizeTo(winWidth,winHeight );
				}catch(e){
				}
			}
			if (winTop == null) {
				winTop = 50;
			}
			if (winLeft == null) {
				winLeft = 100;
			}
			
			if (winTop != null && winLeft != null) {
				try {
					retVal.moveTo(winLeft,winTop);
				}catch(e){
				}
			}

		}
		// Browser allow popup but some tools block it, such as google bar
		// In this case, the test window can popup but the real window can 
		// not, do check it again.
		if( retVal ){		
			retVal.focus();
		}else if(isNull(retVal)){	
			redirectToPage('errorPage','__errorType=popup.os');
		}else{
			redirectToPage('errorPage','__errorType=popup.os');
		}
		if (hasRetValue) {
			return retVal;
		}
	}catch(ex){
		//alert(ex);
	}	
}


window.open = popupWindow;

//--------------------------------------------------------------------------

function redirectToPage(pageId,params){
	if(isBlank(pageId)){
		return;
	}
	var pageLable = global_pageLables.get(pageId);
	if(isBlank(pageLable)){
		alert('[redirectToPage] can not find label of page '+pageId);
		return;
	}
	var eHref =  isBlank(params) ? '?_nfpb=true&_pageLabel=' + pageLable : '?_nfpb=true&_pageLabel=' + pageLable+'&'+params; 
	var pathName = window.location.pathname;
	var idx = window.location.href.indexOf(pathName);
	eHref = window.location.href.substring(0,idx) + pathName + eHref ;							
	window.top.location = eHref;
}

function parentWindowRedirectToPage(pageId,params){
	if(isBlank(pageId)){
		return;
	}
	var pageLable = global_pageLables.get(pageId);
	if(isBlank(pageLable)){
		alert('[parentWindowRedirectToPage] can not find label of page '+pageId);
		return;
	}
	var eHref =  isBlank(params) ? '?_nfpb=true&_pageLabel=' + pageLable : '?_nfpb=true&_pageLabel=' + pageLable+'&'+params; 				
	if (isNull(window.opener) || window.opener.closed)		
	{
		eHref = global_portalDesktopUrl + eHref;
		var newWindow = window.open(eHref, global_portalWin, 'status=yes,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes');
		window.close();
	}
	else
	{				
		var pathName = window.opener.location.pathname;
		var idx = window.opener.location.href.indexOf(pathName);
		eHref = window.opener.location.href.substring(0,idx) + pathName + eHref ;	
		window.opener.location = eHref;
		window.opener.focus();
		window.close();
	}
}


//for warping table's content

function wrapCol(tableId){
	if ((navigator.userAgent.indexOf('Firefox')>0)){
			
		var tab=document.getElementById(tableId);
        for(var i=1 ;i<tab.rows.length;i++)
        { 
            for(var j=0;j<tab.rows[i].cells.length;j++){
            	var td = tab.rows[i].cells[j];
            	if(td.innerHTML.indexOf('<img')< 0&&td.innerHTML.indexOf('&nbsp;')< 0&&td.innerHTML.indexOf('<sup')< 0) {
		            if(td && td.childNodes[0] && td.childNodes[0].innerHTML==null){
						td.innerHTML = td.innerHTML.replace(new RegExp('&amp;', 'g'), '&').split('').join('<WBR/>');
					}else{
						td.childNodes[0].innerHTML = td.childNodes[0].innerHTML.replace(new RegExp('&amp;', 'g'), '&').split('').join('<WBR/>');
					}
				}
            }    
        }
	}
}

//-------------------Trading popupWindow 

function popupWindowTo(url,windowName){
  var targetUrl=applicationUrl+url;
  window.top.open(targetUrl, windowName, 'scrollbars=yes,width=800,height=620');
}