// common.js

function enable(toEnable){
	document.getElementById(toEnable).disabled=false;
}

function disable(toDisable){
	document.getElementById(toDisable).disabled=true;
}

function getSelectedRadioValue(radioGroup) {
	var selectedRadioValue = "";
	var radIndex;
	for (radIndex = 0; radIndex < radioGroup.length; radIndex++) {
		if (radioGroup[radIndex].checked) {
			return radioGroup[radIndex].value;
		}
	}
}

function hideMessages() {
	if(isMessagesVisible){
		messageDiv.innerHTML="";
		isMessagesVisible = false;
	}
} 

var isMessagesVisible = false;
function displayMessages(headerMsg,messages,width, styleClass,prefix, warningButtonText) {
	if (warningButtonText == null || warningButtonText== undefined)
		warningButtonText = "";
	var html = '';
	var arraySize = messages.length;
	var heightSize = 60;
	heightSize = heightSize + (arraySize*30);
	html = html + '<span class="' + styleClass + '"><table width="'+width+'"><tr><td><p>'+headerMsg+'</p>'
	html += '<UL>';
	for (i= 0; i < arraySize; i++){
		var msg = messages[i];
		html += '<li>' + msg + '</li>';
	}
	html += '</UL></td></tr><tr><td align="center" valign="top">' ;
	html += warningButtonText + '</td></tr></table></span>';
	if (prefix != null || prefix !=undefined || prefix != ""){
		var div = prefix + "messageDiv";
		document.getElementById(div).innerHTML= html;
	}else{
		messageDiv.innerHTML = html;
	}
	isMessagesVisible = true;

}

function setTabOrderAndFocus(anOrderedTabList, formName, focusElement){
	setTabOrder(anOrderedTabList, formName);

	if(focusElement != null){
		focusElement.focus();
	}
}	

var firstElementForTabFocus;
function setTabOrder(anOrderedTabList, formName) {
// NOTE: Tab index must start at 1 instead of 0!!!
//       If you do not want the item to be able tab to 
//		 then set  the tabIndex value to a negative number.
// remove the anchors and the links from the tab order completely
// they can be added back in with the tab order tile
	document.body.tabIndex = -1;
	for(var i = 0; i < document.anchors.length; i++){
		document.anchors[i].tabIndex = -1;
	}
	for(var i = 0; i < document.links.length; i++){
		document.links[i].tabIndex = -1;
	}

	var tabOrder = 1;
	var lastElement = null;
	if (formName== null)
		formName='forms[0]';
	// NExt two lines MS ie only
	document.body.tabIndex = -1;
	if (document.forms[0]!=undefined)
	document.forms[0].tabIndex = -1;
	formName = formName+'.';
	for (i=1; i < anOrderedTabList.length; i++){
		tabItemName= anOrderedTabList[i];
		
		var element = null;
		if (tabItemName.match(/\./)== null && document.forms[0]!=undefined){
			try{
				element =  eval('document.'+formName +tabItemName);
			}catch(e){
				//alert(e.description);
			}
		}
		if (element == null)
			element = document.getElementById(tabItemName);  // if element is not a form element (i.e. anchor link)

		if (element == null  || element.type=='hidden'){
			
			continue;
		}
		
		
		if (tabOrder==1){
			firstElementForTabFocus =element;
	   		if(!firstElementForTabFocus.disabled)
	   		{
	   			firstElementForTabFocus.focus();
	   		}
		}
				
 		if (element.length >0 && (element[0].type == 'radio' || element[0].type == 'checkbox')){
 			
           	for (k=0; k < element.length; k++){  
           		if (element[k].disabled!=false)
           			continue;
				element[k].tabIndex=tabOrder;
				tabOrder++;
			}		    
       	} else { 
       		if (element.disabled!=false)
           			continue;
			element.tabIndex=tabOrder; 
			
			lastElement = element;
			tabOrder++;
		}
	 }
	 
	 if(lastElement != null) {
		 if (firstElementForTabFocus != null && !firstElementForTabFocus.disabled) {
		 	 lastElement.attachEvent("onblur",function setFocusAtFirstTabIndex() {firstElementForTabFocus.focus();} );
		 }
	 }
}

function hide(valId){
	var val = document.getElementById(valId);
	if (val == null)
		return;
	val.style.display='none';
}

function show(valId){
	var val = document.getElementById(valId);
	if (val == null)
		return;
	val.style.display='';
}

function togglePlusMinusDivSection(div, img){
	if (document.getElementById(div).style.display=='none'){
		showPlusMinusDivSection(div, img);
	}else{
		hidePlusMinusDivSection(div, img);	
	}
}

function setPlusMinusDivSection(div, img, state){
	if (state==""){
		//No saved state. Do nothing.
	}
	else if (state=='show'){
		showPlusMinusDivSection(div, img);
	}
	else if(state=='hide'){
		hidePlusMinusDivSection(div, img);	
	}
}

function showPlusMinusDivSection(div, img){
	show(div);
	img.src='images/minus.jpg';
	setCookieValue(div,"show");	
}

function hidePlusMinusDivSection(div, img){
	hide(div);
	img.src='images/plus.jpg';
	setCookieValue(div,"hide");
}


function setCookieValue(c_name,value) {
	document.cookie=c_name+ "=" +escape(value);
}

function getCookieValue(c_name){
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    { 
	    c_start=c_start + c_name.length+1; 
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    } 
	  }
	return "";
}