// Custom Object to store Entity Type  
function groupMap(groupId,groupName,groupParentId,groupType){
		this.groupId=groupId;
		this.groupName=groupName.replace("&amp;","&").replace("&#039;","'");
		this.groupParentId=groupParentId;
		this.groupType=groupType;
		}
function entityTypeMap(entityType,entityTypeDesc,agencyId,agency){
		this.entityType=entityType;
		this.entityTypeDesc=entityTypeDesc.replace("&amp;","&").replace("&#039;","'");
		this.agencyId=agencyId;
		this.agency=agency;
		}
function entityStatusMap(entityType,entityStatus,entityStatusDesc,uenStatus){
		this.entityType=entityType;
		this.entityStatus=entityStatus;
		this.entityStatusDesc=entityStatusDesc.replace("&amp;","&").replace("&#039;","'");
		this.uenStatus=uenStatus;
		}
function addrTypeMap(codeId,codeDesc,status){
		this.codeId=codeId;
		this.codeDesc=codeDesc.replace("&amp;","&").replace("&#039;","'");
		this.status=status;
	}
function selectMap(name,value){
		this.name=name.replace("&amp;","&").replace("&#039;","'");
		this.value=value;
	}		
// Function to remove all Options in Select
// Must iterate from bot up 
function removeAllOptions(obj){
	if(obj !=null && obj!=undefined){
		for(var i=obj.length-1;i>=0;i--){
			obj.remove(i);
		}
	}
}
// Function to select the item in select
function selectListItem(obj,itemValue){
	if(obj !=null && obj!=undefined && itemValue !=null && itemValue!=undefined){		
		for (var i = 0; i < obj.length; i++) {
			if(obj[i].value==itemValue){
			obj[i].selected=true;
			}
		}
	}
}
// Function to select the item in select based on the select name and item id	
	function selectListByItemId(selectName,itemId){
	var selectList=getSelectObject(selectName);
	selectListItem(selectList,itemId);
}
// Function to get reference to the select obj based on the name
	function getSelectObject(selectName){
		var selectList=document.getElementsByTagName("SELECT");
		for(var i=0;i<selectList.length;i++){
			if(selectList[i].name==selectName)
			return selectList[i];
		}
	}
// Function to update Entity Type based on the selected Agency 
	function updateEntityType(parentName,childName){
	
	var obj=getSelectObject(parentName);
	
	if(obj==null || obj==undefined)
			return;
	if(entityTypeLookup ==null && entityTypeLookup==undefined)
			return;
	
	
	// Get the selected Agency Id
	var selectedAgencyId=obj.value;
	var entityTypeSelect=getSelectObject(childName);
	if(entityTypeSelect ==null && entityTypeSelect==undefined)
			return;
			
	// Remove all the options in select 
	removeAllOptions(entityTypeSelect);
	
	// Check for selected Agency Id
	if(selectedAgencyId !=null && selectedAgencyId!=undefined){
			
		// Iterate throught the Entity Status Map Lookup 
		for (var i = 0; i < entityTypeLookup.length; i++) {
			
			var groupParentId=entityTypeLookup[i].agencyId;
			var value=entityTypeLookup[i].entityType;
			var name=entityTypeLookup[i].entityTypeDesc
			
			// Check for match Entity Type and populate the Entity Status
			if(groupParentId.toUpperCase() == selectedAgencyId.toUpperCase()){
				
				// Create a new Option and add into the Select
				var option=new Option(name,value, false,false);
				// Browser Issue
				if(navigator.appName=="Microsoft Internet Explorer"){
					entityTypeSelect.add(option);
				}
				else{
					entityTypeSelect.appendChild(option);
				}
				
			}
			
		}
	}
}

// Function to update Entity Type based on the selected Agency 
function updateEntityTypeForReport(parentName,childName){
	
	var obj=getSelectObject(parentName);
	
	if(obj==null || obj==undefined)
			return;
	if(entityTypeLookup ==null && entityTypeLookup==undefined)
			return;
	
	
	// Get the selected Agency Id
	var selectedAgencyId=obj.value;
	var entityTypeSelect=getSelectObject(childName);
	if(entityTypeSelect ==null && entityTypeSelect==undefined)
			return;
			
	// Remove all the options in select 
	removeAllOptions(entityTypeSelect);
	var option=new Option("","", false,false);
	if(navigator.appName=="Microsoft Internet Explorer"){
		entityTypeSelect.add(option);
	}
	else{
		entityTypeSelect.appendChild(option);
	}
	
	// Check for selected Agency Id
	if(selectedAgencyId !=null && selectedAgencyId!=undefined){
			
		// Iterate throught the Entity Status Map Lookup 
		for (var i = 0; i < entityTypeLookup.length; i++) {
			
			var groupParentId=entityTypeLookup[i].agencyId;
			var value=entityTypeLookup[i].entityType;
			var name=entityTypeLookup[i].entityTypeDesc
			
			// Check for match Entity Type and populate the Entity Status
			if(groupParentId.toUpperCase() == selectedAgencyId.toUpperCase()){
				
				// Create a new Option and add into the Select
				var option=new Option(name,value, false,false);
				// Browser Issue
				if(navigator.appName=="Microsoft Internet Explorer"){
					entityTypeSelect.add(option);
				}
				else{
					entityTypeSelect.appendChild(option);
				}
				
			}
			
		}
	}
}

function updateMultipleEntityType(agencySelectName,entityTypeSelectName){
	var agencySelect=getSelectObject(agencySelectName);
	//alert(agencySelect.name);
	if(agencySelect==null || agencySelect==undefined)
			return;
			
	var entityTypeSelect=getSelectObject(entityTypeSelectName);
	if(entityTypeLookup ==null && entityTypeLookup==undefined)
			return;
	
	removeAllOptions(entityTypeSelect);
	//alert("agencySelect.length: "+agencySelect.length);
	// for each selected agency, add the entity type
	for(var i=0;i<agencySelect.length;i++){
		var selectStatus=agencySelect[i].selected;
		//alert("i:"+i+" "+agencySelect[i].selected);		
		if(selectStatus){		
			//alert("i:"+i+" "+agencySelect[i].selected);
			
			var selectedAgencyId=agencySelect[i].value;
			//alert(selectedAgencyId);
			// Iterate throught the Entity Status Map Lookup 
			
			for (var j = 0; j < entityTypeLookup.length; j++) {			
				
				var agencyId=entityTypeLookup[j].agencyId;
				var value=entityTypeLookup[j].entityType;
				var name=entityTypeLookup[j].entityTypeDesc
				
				// Check for match agency Id
				if(agencyId.toUpperCase() == selectedAgencyId.toUpperCase()){
					
					// Create a new Option and add into the Select
					var option=new Option(name,value, false,false);
					// Browser Issue
					
					if(navigator.appName=="Microsoft Internet Explorer"){
						
						entityTypeSelect.add(option);
					}
					else{
						entityTypeSelect.appendChild(option);
					}
					
				}
				
			}
			
		}
	}
	sortSelect(entityTypeSelect);

}
function addDefaultAllOption(selectName){
	var obj=getSelectObject(selectName);

	// Browser Issue
	if(navigator.appName=="Microsoft Internet Explorer"){
		//addrType.add(naOpt);
		var opt=document.createElement("option");
		opt.setAttribute("text","ALL");
		opt.setAttribute("value","");	
		opt.innerText="ALL";
		obj.insertBefore(opt,obj[0]);
		obj[0].setAttribute("selected","true");
	}
	else{
		//addrType.appendChild(naOpt);
		var opt=new Option("ALL","", false,false);
		obj.insertBefore(opt,obj[0]);
		obj[0].selected=true;
	}
}

function updateEntityStatus(entityTypeSelectName,entityStatusSelectName,uenStatusSelectName){
	//alert(entityTypeSelectId);
	var entityTypeSelect=getSelectObject(entityTypeSelectName);
	var entityStatusSelect=getSelectObject(entityStatusSelectName);
	
	removeAllOptions(entityStatusSelect);
	
	var selectedEntityType=entityTypeSelect[entityTypeSelect.selectedIndex].value;
	//alert(selectedEntityType);
	
	for (var i = 0; i < entityStatusLookup.length; i++) {
		var name=entityStatusLookup[i].entityStatusDesc;
		var value=entityStatusLookup[i].entityStatus;
		
		if(entityStatusLookup[i].entityType.toUpperCase()==selectedEntityType.toUpperCase()){
			var option=new Option(name,value, false,false);
				
				// Browser Issue
				if(navigator.appName=="Microsoft Internet Explorer"){
					entityStatusSelect.add(option);
				}
				else{
					entityStatusSelect.appendChild(option);
				}
		}
		
	}
	//updateUENStatus(uenStatusSelectName,entityStatusLookup[0].uenStatus);
	updateUENStatus(entityStatusSelectName,uenStatusSelectName);

}
function updateUENStatus(entityStatusSelectName,uenStatusSelectName){
	//alert(uenStatusSelectName);
	//alert(uenStatus);
	var entityStatusSelect=getSelectObject(entityStatusSelectName);
	if(entityStatusSelect.length==0)
		return;
	var selectedEntityStatus=entityStatusSelect[entityStatusSelect.selectedIndex].value;
	//alert("selectedEntityStatus: "+selectedEntityStatus);
	for (var i = 0; i < entityStatusLookup.length; i++) {
		var entityStatus=entityStatusLookup[i].entityStatus;
		var uenStatus=entityStatusLookup[i].uenStatus;
		if(selectedEntityStatus.toUpperCase()==entityStatus.toUpperCase()){
			//alert(entityStatus);
			//alert(uenStatus);
			selectListByItemId(uenStatusSelectName,uenStatus);
			break;
		}	
	}
}

	function showhide(id){
		if (document.getElementById){
			obj = document.getElementById(id);
			if (obj.style.display == "none"){
				obj.style.display = "";
			} else {
				obj.style.display = "none";
			}
		}
	}
	
	function toggleArrowImage(obj){
		for(var i=0;i<obj.childNodes.length;i++){
			var srcName=obj.childNodes[i].src;
			if(srcName.indexOf("js_arrow_down")!=-1){
				//alert("down");
				//alert(srcName);
				var img=document.createElement("img");
				img.setAttribute("src",srcName.replace("js_arrow_down","js_arrow_right"));
				obj.removeChild(obj.childNodes[i]);
				obj.appendChild(img);
				break;
				
		
			}
			else{
				//alert("up");
				//alert(srcName);
				//obj.childNodes[i].src=srcName.replace("js_arrow_right","js_arrow_down");
				var img=document.createElement("img");
				img.setAttribute("src",srcName.replace("js_arrow_right","js_arrow_down"));
				obj.removeChild(obj.childNodes[i]);
				obj.appendChild(img);
				break;
				
			}
			
			
		}
		
	}
	
function sortSelect(selectObj){
	var sortArray=new Array();
	
	var selectLength=selectObj.options.length;
	//alert(selectLength);
	for(var i=0;i<selectLength;i++){
		sortArray[sortArray.length]=selectObj.options[i];
		//alert(selectObj.options[i].text);
	}
	sortArray.sort(SORT_OPTION);
	
	for(var i=0;i<selectLength;i++){
		selectObj.appendChild(sortArray[i]);
		//alert(sortArray[i].text);
	}
}

function SORT_OPTION(a,b){
	var firstText=a.text;
	var secondText=b.text;
	if(firstText==null || secondText==null)
		return 0;
		
	if(firstText<secondText){
			return -1;
	}
	else if(firstText>secondText){
			return 1;
	}		
	else
		return 0;	
}

function updateAddrType(parentObj){
	//alert(parentObj.selectedIndex);	
	
	var addrType=getSelectObject("REG_ADDRESS_TYPE");
	var selectedObj=parentObj.options[parentObj.selectedIndex];
	removeAllOptions(addrType);
	
	var naOpt=new Option("NA","", false,false);
	// Browser Issue
	if(navigator.appName=="Microsoft Internet Explorer"){
		addrType.add(naOpt);
	}
	else{
		addrType.appendChild(naOpt);
	}
			
	if(selectedObj.value=="D"){
		for(var i=0;i<drmLookup.length;i++){
			var opt=new Option(drmLookup[i].codeDesc,drmLookup[i].codeId, false,false);
				// Browser Issue
				if(navigator.appName=="Microsoft Internet Explorer"){
					addrType.add(opt);
				}
				else{
					addrType.appendChild(opt);
				}
			
		}
	}
	else if(selectedObj.value=="N"){
		for(var i=0;i<ncaLookup.length;i++){
			var opt=new Option(ncaLookup[i].codeDesc,ncaLookup[i].codeId, false,false);
			
				// Browser Issue
				if(navigator.appName=="Microsoft Internet Explorer"){
					addrType.add(opt);
				}
				else{
					addrType.appendChild(opt);
				}
		}
	}	
}

function updateCorresAddrType(parentObj){
	//alert(parentObj.selectedIndex);	
	
	var addrType=getSelectObject("CORRES_ADDRESS_TYPE");
	var selectedObj=parentObj.options[parentObj.selectedIndex];
	removeAllOptions(addrType);
	
	var naOpt=new Option("NA","", false,false);
	// Browser Issue
	if(navigator.appName=="Microsoft Internet Explorer"){
		addrType.add(naOpt);
	}
	else{
		addrType.appendChild(naOpt);
	}
			
	if(selectedObj.value=="D"){
		for(var i=0;i<drmLookup.length;i++){
			var opt=new Option(drmLookup[i].codeDesc,drmLookup[i].codeId, false,false);
				// Browser Issue
				if(navigator.appName=="Microsoft Internet Explorer"){
					addrType.add(opt);
				}
				else{
					addrType.appendChild(opt);
				}
			
		}
	}
	else if(selectedObj.value=="N"){
		for(var i=0;i<ncaLookup.length;i++){
			var opt=new Option(ncaLookup[i].codeDesc,ncaLookup[i].codeId, false,false);
			
				// Browser Issue
				if(navigator.appName=="Microsoft Internet Explorer"){
					addrType.add(opt);
				}
				else{
					addrType.appendChild(opt);
				}
		}
	}	
}

// Function to validate date
function validateDateGreaterthanToday(obj,inclusive,fieldName){
var dateValue=obj.value;
	if(dateValue!=null && dateValue!=undefined && dateValue.length>0){
		var day=dateValue.substr(0,2);
		var month=(dateValue.substr(3,2))-1;
		var year=dateValue.substr(6,4);		
		var inputDate=new Date(year,month,day);
		
		var currentDateTime=new Date();
		var currentDay=currentDateTime.getDate();
		var currentMonth=currentDateTime.getMonth();
		var currentYear=currentDateTime.getFullYear();
		
		//alert(currentYear+' '+currentMonth+' '+currentDay );		
		var currentDate=new Date(currentYear,currentMonth,currentDay);		
		//alert(inputDate+"\n"+currentDate);
		
		if(inclusive){
			if(inputDate>=currentDate)
				return true;
			else{
				alert(fieldName+' must be equal or after today.');
				return false;
				}
		}
		else{
			if(inputDate>currentDate)
				return true;
			else{
				alert(fieldName+' must be after today.');			
				return false;
				}
		}
		
	}
	else{
		// No need to check the date format if the field is empty
		return true;
		}
	
	return false;
}

// Function to validate date
function validateDateEarlierthanToday(obj,inclusive,fieldName,currentDateField){
var dateValue=obj.value;
	if(dateValue!=null && dateValue!=undefined && dateValue.length>0){
		var day=dateValue.substr(0,2);
		var month=(dateValue.substr(3,2))-1;
		var year=dateValue.substr(6,4);		
		var inputDate=new Date(year,month,day);
		
		var currentDateTime=currentDateField.value;
		//alert(currentDateTime);
		var currentDay=currentDateTime.substr(0,2);
		var currentMonth=(currentDateTime.substr(3,2))-1;
		var currentYear=currentDateTime.substr(6,4);	
		
		//alert(currentYear+' '+currentMonth+' '+currentDay );		
		var currentDate=new Date(currentYear,currentMonth,currentDay);		
		//alert(inputDate+"\n"+currentDate);
		
		if(inclusive){
			if(inputDate<=currentDate)
				return true;
			else{
				alert(fieldName+' must be earlier than or equal to Current Date.');
				return false;
				}
		}
		else{
			if(inputDate<currentDate)
				return true;
			else{
				alert(fieldName+' must be earlier than Current Date.');			
				return false;
				}
		}
		
	}
	else{
		// No need to check the date format if the field is empty
		return true;
		}
	
	return false;
}

/**
 * It will check to see if there is a parent frame,
 * if there inst a parent frame,
 * the javascript will redirect the user to main page,
 * with the desired content in the frame
 * @return
 */
function checkUENFrame() {
	if (parent.frames["header"] && parent.frames["menu"]
			&& parent.frames["msg_main"])
		var msg = "mainFrame exits";
	else
		window.open("/uen/index.do?framecontent=" + document.location, "_self");
}