
var originalCodedForm;
var xmlHttpObj;
var xmlHttpResponseObj;
var loadUserAttributeGroupPermissionsFA = 'customGroups.userAttributeGroupPermissions_userView_1';
var submitUserAttributeGroupPermissionsFA = 'customGroups.userAttributeGroupPermissions_userView_2';

function ajaxLoadEditor(id_type, id_value){
	xmlHttpObj=GetXmlHttpObject()
	if (xmlHttpObj==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="/_tmgCMS_index.cfm?";
	url=url+"fuseaction="+loadUserAttributeGroupPermissionsFA;
	url=url+"&"+id_type+"="+id_value;
	url=url+"&id_type="+id_type;
	url=url+"&sid="+Math.random();
	xmlHttpObj.onreadystatechange=showEditor;
	
	xmlHttpObj.open("GET",url,true);
	xmlHttpObj.send(null);
	
	//move the box to a location near the click
	var linksSpan=$('access_'+id_value);
   	var linksSpanY=findY(linksSpan);
   	$('ajax_container2').style.top=(linksSpanY-120)+"px";
	var linksSpanX=findX(linksSpan);
   	$('ajax_container2').style.left=(linksSpanX+20)+"px";
	
}


function submitEditor(){
	xmlHttpResponseObj=GetXmlHttpObject()
	var codedForm=ajaxEncodeForm(document.forms["userAttributePermissionsEditorForm"]);
	//if(codedForm==originalCodedForm){									
	//	hideEditor();
	//	return false;
	//}
	var query=(codedForm+"&save=1");
	//xmlHttp.onreadystatechange=updateEditorState;
	/*xmlHttp.onreadystatechange=function(){
  	 updateEditorState(xmlHttp);
 	}*/
	var url="/_tmgCMS_index.cfm?";
	url=url+"fuseaction="+submitUserAttributeGroupPermissionsFA;
	xmlHttpResponseObj.onreadystatechange=function() { 
        if (xmlHttpResponseObj.readyState==4)
            if (xmlHttpResponseObj.status==200)
                updateEditorState(xmlHttpResponseObj);
    }

	xmlHttpResponseObj.open("POST",url,true);
	xmlHttpResponseObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttpResponseObj.send(query);
}


/* EDITOR */

function showEditor(){ 
	if (xmlHttpObj.readyState==4){ 
		if (xmlHttpObj.status==200){
			showAll('ajax_container2');
			$('ajax_container2').innerHTML=xmlHttpObj.responseText;			
		}
	}
} 

function hideEditor(){
	hideAll('ajax_container2');
}

function updateEditorState(xmlHttpResponseObj){
	/*notify it has been saved?*/
	$('ajax_container2').innerHTML=xmlHttpResponseObj.responseText;
	if ($('saveResult').innerHTML=='true'){
		hideEditor();
		// if there is an error the page will ask to try again			
	}
}


function disableByName(theFormField) {

	var allelements = document.forms["userAttributePermissionsEditorForm"].elements;
	var udgselects = false;
	
	//initial loop - check if existing fields are checked	
	for (i = 0; i < allelements.length; i++) {
		if (allelements[i].type == 'checkbox' && allelements[i].name == 'udg_id' && allelements[i].checked == true) {
			udgselects = true;
		}
	}
	
	//actual loop to reset elements
	for (i = 0; i < allelements.length; i++) {
		if (allelements[i].name != 'meonly') {
			if (allelements[i].type == 'checkbox' && allelements[i].name != theFormField.name && theFormField.checked == true) {
				allelements[i].checked == false;
				allelements[i].disabled = true;
			} else {
				if (udgselects != true) {
					allelements[i].disabled = false;
				}
			}
		}
	}

}

