questia.Package( "coreweb.common" );


coreweb.common.ComponentUtils = {};
coreweb.common.ComponentUtils.getBrowserSize = function() {
   var myWidth = 0, myHeight = 0;
   if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  } else if( window.innerWidth ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  }
  var dims = new Object();
  dims.width = myWidth;
  dims.height = myHeight;
  return dims;
}

coreweb.common.ComponentUtils.getBodySize = function() {	   
	  var dims = new Object();
	  dims.width = document.body.scrollWidth;
	  dims.height = document.body.scrollHeight;
	  return dims;
	}

coreweb.common.ComponentUtils.getComponentWidth = function( comp ) {
	var width = comp.offsetWidth;
	if ( width == 0 ) {
   	width = parseInt( comp.style.width );
	}
	return width;
}

coreweb.common.ComponentUtils.getComponentHeight = function( comp ) {
	var height = comp.offsetHeight;
	if ( height == 0 ) {
   	height = parseInt( comp.style.height );
	}
	return height;
}

coreweb.common.ComponentUtils.getScrollTop = function() {
  if (document.body && document.body.scrollTop)
     return document.body.scrollTop;
  else if (document.documentElement && document.documentElement.scrollTop)
       return document.documentElement.scrollTop;
  else if (document.documentElement && !document.documentElement.scrollTop)
       return 0;
}

coreweb.common.ComponentUtils.getPosition = function(element) {
    var elName;
    if ( typeof element == 'string' ) {
       elName = element;
       element = document.getElementById( element );
    }

	var left = 0;
	var top = 0;
	if (element.offsetParent) {
		left = element.offsetLeft
		top = element.offsetTop
		while (element = element.offsetParent) {
			left += element.offsetLeft
			top += element.offsetTop
		}
	} else {
	 	left = element.offsetLeft;
	 	top = element.offsetTop;
	}
	var dims = new Object();
    dims.left = left; 
    dims.top = top;
	return dims;
}

coreweb.common.ComponentUtils.closePopup = function( img ) {
   var el = img.parentNode.parentNode;
   el.style.display='none';
}

coreweb.common.ComponentUtils.showPopup = function( divName, srcLink, absX, absY, relX, relY ) {
   var panel = document.getElementById( divName );
   var dims = coreweb.common.ComponentUtils.getBrowserSize();
   panel.style.left = 2500;
   panel.style.top = 0;
   panel.style.display='inline';
   
   var left = 0;
   var top = 0;
   if ( absX ) {
		left = Number(absX);    
   } else if ( relX ) {
		left = srcLink.offsetLeft + Number(relX) + 1000;
	} else {
		left = Math.round((dims.width - panel.offsetWidth)/2);
   }

	if ( absY ) {
		top = Number(absY);
	} else if ( relY ) {
      top = srcLink.offsetTop + Number(relY);
   } else {
      top = Math.round((dims.height - panel.offsetHeight)/2 + coreweb.common.ComponentUtils.getScrollTop()) + 20;
   }
   panel.style.display='none';
   panel.style.left = left + "px";
   panel.style.top = Math.max(top,10) + "px";
   panel.style.display='inline';
}

coreweb.common.ComponentUtils.getElementsByClassName = function(class_name,tagname) {
    var docList = this.all || document.getElementsByTagName( tagname || 'a');
    var matchArray = new Array();

    for (var i = 0; i < docList.length; i++) {
       var classNames = docList[i].className.split(' ');
       for( var cindex = 0; cindex < classNames.length; cindex++ ) {
       	     if ( classNames[cindex] == class_name ) {
                matchArray[matchArray.length] = docList[i];
             }
       } 
    }
    return matchArray;
}

coreweb.common.PopupLinkInitializer = {};
coreweb.common.PopupLinkInitializer.initialize = function() {
	
	var PopupLink = function(linkEl) {
	  var _link = linkEl;
	  this.callback = function(e){
    	 coreweb.common.ComponentUtils.showPopup( _link.getAttribute('popupid'), _link, _link.getAttribute('absX'), _link.getAttribute('absY'), _link.getAttribute('relX'), _link.getAttribute('relY') );
	  }
	  this.onclick = function() {
	    return false;
	  }
	   if ( _link.attachEvent ) {
    	   _link.attachEvent( 'onclick', this.callback );
	   } else {
    	   _link.addEventListener( 'click', this.callback, false ); 
	   }
	  _link.onclick = this.onclick;
   }

   var classArray = coreweb.common.ComponentUtils.getElementsByClassName("popupLink");
   for (var i = 0; i < classArray.length; i++) {
     var link = classArray[i];
     new PopupLink(link);
   }
}

/**
* @param windowName   make sure windowName does not have a 'space'.  The abomination that is IE throws an exception. 
*/
coreweb.common.openWindow = function( url, windowName, width, height, resizable, scrollbars ) { 
	try {
		var x = (screen.width - width)/2; 
		var y = (screen.height - height)/2-20;
		if ( resizable == null ) {
	   		resizable='yes';
		}
		if ( scrollbars == null ) {
	   		scrollbars = 'yes';
		}
		var newWin = window.open(url, windowName,'top='+y+',left='+x+',width='+width+',height='+height+',scrollbars=' + scrollbars +',resizable='+resizable,true);
		if ( window.opera ) {
			if (!newWin.opera)
			alert('To use this feature make sure your browser and tool bar are set to always allow pop-ups from Questia');
		}
		newWin.focus();
		return newWin;
	}
	catch(e) {
		alert('To use this feature make sure your browser and tool bar are set to always allow pop-ups from Questia');
	}
}

/**
* @param windowName   make sure windowName does not have a 'space'.  The abomination that is IE throws an exception. 
*/
coreweb.common.openWindow2 = function( url, windowName, width, height, params ) { 
	var x = (screen.width - width)/2; 
	var y = (screen.height - height)/2-20;
	
	var newWin = window.open(url, windowName,'top='+y+',left='+x+',width='+width+',height='+height+','+params,true);
	newWin.focus();
	return newWin;
}

/**
* @param windowName   make sure windowName does not have a 'space'.  The abomination that is IE throws an exception. 
*/
coreweb.common.openWindow3 = function( url, windowName, params ) { 		
	var newWin = window.open(url, windowName, params);
	newWin.focus();
	return newWin;
}

coreweb.common.openRoboHelpWindow = function(roboHelpUrl) {	
	var newWin = window.open(roboHelpUrl, 'robohelp', 'width=600,height=400,scrolling=yes');
	newWin.focus();
}


/**
 * This method resets form values to an default state.
 * The default state depends on the component:  text-empty, select-first element, check-caller specified
 * @param checkBtnState - the state to set radio and check buttons(true or false)
*/
coreweb.common.resetForm = function(form,checkBtnState,resetCheckBoxes) {
	for (var i=0; i < form.elements.length; i++) {
		var cType = form.elements[i].type;
		if (cType.indexOf('checkbox') > -1 && resetCheckBoxes ) {
		   form.elements[i].checked = checkBtnState;
		}
		if (cType.indexOf('radio') > -1) {
		   form.elements[i].checked = form.elements[i].defaultChecked;
		}
		if ( cType.indexOf('password') > -1 || cType.indexOf('text') > -1) {
			form.elements[i].value = '';
		}
		if (cType.indexOf('select') > -1) {
			for (var k=0; k < form.elements[i].options.length; k++) {
			   bselect = false;
			   if ( k==0 ) {
			      bselect = true;
			   }
				form.elements[i].options[k].selected = bselect; 
			}
		}
	}
}

coreweb.common.getSelectedChildCheckBoxes = function(parentNode,selected) {
	   for (var i=0; i < parentNode.childNodes.length; i++) {
	      var node = parentNode.childNodes[i];
			if ( node.type && node.type.indexOf('checkbox') > -1 && node.checked == true ) {
				selected.push( node );
			}
			coreweb.common.getSelectedChildCheckBoxes( node, selected );
	   }
}

coreweb.common.getSelectedCheckBoxes = function(parentNode) {
	var selected = new Array();
	coreweb.common.getSelectedChildCheckBoxes( parentNode, selected );
	return selected;
}

coreweb.common.getElement = function(parentNode, elementId) {
   for (var i=0; i < parentNode.childNodes.length; i++) {
      var node = parentNode.childNodes[i];
      if ( node.nodeType == 1 ) {
        if ( elementId == node.id ) {
  	       return node;
	     }
	   }
	   node = coreweb.common.getElement( node, elementId );
	   if ( node != null ) {
	      return node;
	   }
   }
   return null;
}

/** Returns a child element with the specified class name of a specified node
*/
coreweb.common.getElementByClass = function(parentNode, elementClass) {
   if ( !parentNode ) {
   	return null;
   }
   for (var i=0; i < parentNode.childNodes.length; i++) {
      var node = parentNode.childNodes[i];
      if ( node.nodeType == 1 ) {
         var classNames = node.className.split(' ');
         for( var cindex = 0; cindex < classNames.length; cindex++ ) {
             if ( classNames[cindex] == elementClass ) {
  	            return node;
	         }
	     }
	   }
	   node = coreweb.common.getElementByClass( node, elementClass );
	   if ( node != null ) {
	      return node;
	   }
   }
   return null;
}

/**
* form - the form which contains the checkboxes
* cboxId - the id of the 'Check All/Uncheck All' box
* cboxClass - the class of the checkboxes in the form to check/uncheck
* checkMsgId - the id of a span whose innerHTML to change to 'Uncheck All/Check All'
*/
coreweb.common.toggleCheckBoxes = function(form, cboxId, cboxClass, checkMsgId ) {
   var allBox = coreweb.common.getElement(form, cboxId);
   var bcheck = allBox.checked;
   if ( bcheck ) {
		document.getElementById( checkMsgId ).innerHTML = 'Uncheck All';
   } else {
		document.getElementById( checkMsgId ).innerHTML = 'Check All';
   }
	for (var i=0; i < form.elements.length; i++) {
	   var cbox = form.elements[i];
		if (cbox.type.indexOf('checkbox') > -1 && cbox.id != cboxId && cbox.className == cboxClass ) {
			cbox.checked = bcheck;
		}
   }
}

coreweb.common.trim = function(stringVal) {
    var m = stringVal.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}


coreweb.common.requestFocus = function( divId ) {
 	try {
	   	document.getElementById( divId ).focus();	 
	} catch( err ) { // ignore 
	}
}

coreweb.common.getParentForm = function( el ) {
   if ( !el ) {
      return null;
   } 
   //questia.Log( 'get parent form: ' + el + '  name: ' + el.nodeName + '   type: ' + el.nodeType );
   var node = el.nodeName == null ? "" : el.nodeName.toLowerCase();
   if ( node == 'form' ) {
      return el;
   } else {
      return coreweb.common.getParentForm( el.parentNode );
   } 
}
