// ** --- naming convention: GLS stands for globalspectrum.js function ---** //

//// ******************** Start highligh and select rows in the paging control ******************** ////
//---------- highlight a row based on radio button on_check action  ----------------


function GLSRdoHighlightRow(trIDStart,rdoIDStart,trIndex, totalRadio)
{
	elemRdo = document.getElementById(rdoIDStart+trIndex);
	elemTR= document.getElementById(trIDStart+trIndex);
	for(var i=1; i<totalRadio+1; i++)
	{
		trTmp = document.getElementById(trIDStart+i);
		rdoTmp = document.getElementById(rdoIDStart+i);
	
		if(trTmp != elemTR)
		{		
			trTmp.className = 'nonSelectedRow';			
			rdoTmp.className = 'nonSelectedRow';
		} else {
			if (!elemRdo.checked) {
				elemTR.className = 'nonSelectedRow';
				rdoTmp.className = 'nonSelectedRow';
			}
			else{
				elemTR.className = 'selectedRow';
				rdoTmp.className = 'selectedRow';
				
			}
		}
	}
}

//---------- highlight a row based on check box on_check action ----------------
function GLSChkHighlightRow(chkBox)
{
	var trId = "tr" + chkBox.id;
	elemTR = document.getElementById(trId);
	
	if (chkBox.checked) {
		elemTR.className='selectedRow';
		chkBox.className ='selectedRow';
	}
	else{		
		elemTR.className='nonSelectedRow';
		chkBox.className ='nonSelectedRow';
	}
}

//--------- highlight children checkboxes after a Check All is checked -------------
function GLSChkAllHighlightRow(chkAllBox)
{
//assuming the children checkbox use name of "ChkIdx" while x = number
	for (var i =1; i < document.forms(0).ChkId.length+1; i++) {
		trId = "trChkId" + i;
		elemTR = document.getElementById(trId);
		elemChk = document.getElementById("ChkId"+i);
		
		if (elemChk.checked) {	
			
			elemTR.className='selectedRow';
			elemChk.className ='selectedRow';				
		}
		else{		
			
			elemTR.className='nonSelectedRow';
			elemChk.className ='nonSelectedRow';				
		}
	}
}
//// ******************** End of select and highlight rows in the paging control ******************** ////

//// ******************** Start enable and disable textboxes ******************** ////
//---------------- enable an element ------------------
function GLSEnableElem(elem){
	elem.disabled = false;
	switch(elem.type){
		case "checkbox":
		case "radio":
			elem.className = "transparent";
			break;
		case "button":
			elem.className = "buttonMUSR";
			break;
		default:
			elem.className = "nonSelectRow";
			break;
			
	}

}
//----------------- disable an element ------------------
function GLSDisableElem(elem){
	
	switch(elem.type){
		case "checkbox":
		case "radio":
			elem.checked = false;
			break;
		case "text":
			elem.value ="";
			break;
		case "select-one":
			elem.selectedIndex = 0;
			break;			
	}
	elem.disabled = true;
	elem.className= "disabled";	
	
}
//----------------- enable all element has a name ends with _USR ------------------
function GLSEnableAll(suffix){
	var elemName;
	var tempStr;
	tempStr = '';
	elemName ='';
	
	for (var i=0;i < document.all.length; i++){
		if (document.all(i).name != undefined ){					
			elemName = document.all(i).name;
			if (!isNaN(elemName))
				tempStr = elemName.toString();
			else
				tempStr = elemName;
				
			if (tempStr.length >4 ) {
				if (tempStr.substr(tempStr.length - 4) == suffix){							
					GLSEnableElem(document.getElementById(elemName));
				}
			}
		}
	}
}
function GLSShowElem(elem,show){
	if (show)
		elem.style.visibility = 'visible' ;
	else
		elem.style.visibility = 'hidden';
}

//------------ disable all element has a name ends with _USR ------------------
function GLSDisableAll(suffix){
	var elemName;
	var tempStr;
	tempStr = '';
	elemName ='';

	for (var i=0;i < document.all.length; i++){
		if (document.all(i).name != undefined ){
			
			elemName = document.all(i).name;
			if (!isNaN(elemName))	
				tempStr = elemName.toString();
			else						
				tempStr = elemName;
				
			if (tempStr.length >4 ) {						
				if (tempStr.substr(tempStr.length - 4) == suffix){							
					GLSDisableElem(document.getElementById(elemName));
				}
			}
		}
	}
}
//// ******************** End of enable and disable textboxes ******************** ////
function GLSPopUpWithOpener(actionTypeValue,windowName){
	window1 = window.open("IIS.controller?actionType="+actionTypeValue, windowName, "width=500, height=400, resizable, scrollbars");
	if (!window1.opener)
		window1.opener = window;
}


//----------- validate date fields --------------//
function GLSDateValidation(datefield){
	// The only datefield format is mmddyy, mmddyyyy,mm/dd/yy or mm/dd/yyyy. This function is to add century to the mmddyy input.
	// This function will call general "GLvalidateDateMsg(dateField, isMandatory, errMessage)" in the global.js
	// 2 digit year greater than 50 will be added centuray 20 and else add 19.
	
	if (GLvalidateDateMsg(datefield, false, "Please enter a valid date (mmddyy), (mmddyyyy),(mm/dd/yy) or (mm/dd/yyyy)."))
		return true;
	else {	
		return false;
	}	
}


//-------------------- error handling codes -----------------
function GLSGetError(){
	if (!w || w.closed) {
	w = window.open("","_webxf_error_win","width=390,height=190");
	var d = w.document;
	d.open();
	d.write(str);
	d.close();
	w.focus();
	}
}

//---------------------Convert field value to upper case --------------------------
function GLSToUpper(fld){	
	fld.value = fld.value.toUpperCase();
}

//---------------------check whether an array arry contains a str element.
function arrayContains(str, strArry){
	if (strArry == "" )
		return -1;	
	if ( strArry.indexOf(',') < 0){		
		if ( str.length == strArry.length && strArry.indexOf(str) >=0){			
			return 0;
		}else{			
			return -1;
		}
	}
				
	var arr = strArry.split(',');	
		
	if(str == 'undefined' || arr.length <=0)
		return -1;
	
	for (var ind =0;ind< arr.length;ind++){	
		if (str == arr[ind])
			return ind;
	}
	
	return -1;
}

//----------------------Remove an element from an array based on an index.
function arrayRemoveAt(strArry,ind){
	var arry = strArry.split(',');
	var replaceTextArray = new Array(arry.length-1);

	if (ind < arry.length){	
		
		for (var i = 0;i < arry.length; i++){
			if ( i < ind)
				replaceTextArray[i] = arry[i];
			else {
				if (i > ind)
					replaceTextArray[i-1] = arry[i];
			}
		}		
	}
	return replaceTextArray.toString();
}
