
var originalCodedForm;
var xmlHttpObj;
var xmlHttpResponseObj;
var loadFA = 'customGroups.userBlockEditor_userView_1';
var submitFA = 'customGroups.userBlockEditor_userView_2';

function ajaxLoadBlockEditor(uoc_id, id_value){
	xmlHttpObj=GetXmlHttpObject()
	if (xmlHttpObj==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="/_tmgCMS_index.cfm?";
	url=url+"fuseaction="+loadFA;
	url=url+"&"+uoc_id+"="+id_value;
	url=url+"&uoc_id="+uoc_id;
	url=url+"&sid="+Math.random();
	xmlHttpObj.onreadystatechange=showBlockEditor;
	
	xmlHttpObj.open("GET",url,true);
	xmlHttpObj.send(null);
	
	//move the box to a location near the click
	var linksSpan=$('block_'+id_value);
   	var linksSpanY=findY(linksSpan);
   	$('ajax_container').style.top=(linksSpanY-360)+"px";
	var linksSpanX=findX(linksSpan);
   	$('ajax_container').style.left=(linksSpanX-150)+"px";
}


function submitBlockEditor(){
	xmlHttpResponseObj=GetXmlHttpObject()
	var codedForm=ajaxEncodeForm(document.forms["userBlockEditorForm"]);
	//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="+submitFA;
	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 showBlockEditor(){ 
	if (xmlHttpObj.readyState==4){ 
		if (xmlHttpObj.status==200){
			showAll('ajax_container');
			$('ajax_container').innerHTML=xmlHttpObj.responseText;
			
			//temporary fix for footer issue
			$('footer').style.display='none';
			//$('adfooter').style.display='none';
			$('skyscraperad1').style.display='none';			
		}
	}
} 

function hideEditor(){
	//temporary fix for footer issue
	$('footer').style.display='block';
	//$('adfooter').style.display='block';
	$('skyscraperad1').style.display='block';
	
	hideAll('ajax_container');
}

function updateEditorState(xmlHttpResponseObj){
	/*notify it has been saved?*/
	$('ajax_container').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["userBlockEditorForm"].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;
				}
			}
		}
	}

}

