function addToPage(id) {
	var params='action=addtopage&id=' + id; 
	if (id==75)
	{
      var feedURL=$('feedurl').value;
      if (feedURL.length<5)
      {
	    alert('you must specify a valid feed url');
        return false;
      } else {
        params+='&feedurl='+escape(feedURL);
 
      }
    } 
	var aj = new Ajax.Request(  
    'ajaxresponder.php', {  
      method:'get',  
      parameters: params, 
      onComplete: getAddToPageResponse
    }  
   );  
 }  

function getAddToPageResponse(oReq) {
	eval(oReq.responseText);
	if (added==0)
		$(myid).innerHTML = '<img src=\'i/added.gif\'>';
	else
	  $(myid).innerHTML = "<form><input type='button' value='Add Another' onClick='addToPage("+module_id+")'>";
 }  
 
function switchCam(module_id){
  var thiscam=$('cam');
  document.getElementById('module_'+module_id+'_title').innerHTML = thiscam.options[thiscam.selectedIndex].text; 
	var aj = new Ajax.Request(
		'ajaxresponder.php', {
			method:'get',  
	        parameters: 'action=switchcam&camid='+$('cam').value,
	        onComplete:getCamResponse
       }  
   );
}

function getCamResponse(oReq) {
	if (oReq.responseText){
		//alert(oReq.responseText);
	   $('cam_swf').innerHTML = oReq.responseText ;
	}  
 }	

var modid = '';
function switchRegion(module_id){
	modid = module_id;
  var thisregion=document.getElementById('region_'+module_id);
  document.getElementById('module_'+module_id+'_title').innerHTML = thisregion.options[thisregion.selectedIndex].text; 
  	var aj = new Ajax.Request(
		'ajaxresponder.php', {
			method:'get',  
	        parameters: 'moduleid='+module_id+'&action=switchregion&region='+thisregion.value,
	        onComplete:getRegionResponse
       }  
   );
}


function getRegionResponse(oReq) {
		if (oReq.responseText){
		//	alert(oReq.responseText);
		   $('region_swf_'+modid).innerHTML = oReq.responseText ;
		}  
	 }	

    function rssPrefs(id){
	  var aj = new Ajax.Request(
		'ajaxresponder.php', {
			method:'get',  
	        parameters: 'action=rssprefs&module_id='+id+'&'+Form.serialize('rssprefs'),
	 onComplete:prefresponse
	      }  
	   );
}

function switchMap(module_id){
	var thisregion=document.getElementById('mapregion');
  	document.getElementById('module_'+module_id+'_title').innerHTML = thisregion.options[thisregion.selectedIndex].text; 
  	var regionSelect = thisregion.value;
  	var index = regionSelect.indexOf("|");
	var regionID = -1;
  	if(index > 0) {
		var region = regionSelect.substr(0, index);
		var regionID = regionSelect.substr(index+1, regionSelect.length);
	}

  	var aj = new Ajax.Request(
		'ajaxresponder.php', {
			method:'get',  
	        parameters: 'action=switchmap&region='+region+'&locationID='+regionID,
	        onComplete:getMapResponse
       }  
   );
}


function getMapResponse(oReq) {
	if (oReq.responseText){
	//	alert(oReq.responseText);
	   $('map_swf').innerHTML = oReq.responseText ;
	}  
}	

    function rssPrefs(id){
	  var aj = new Ajax.Request(
		'ajaxresponder.php', {
			method:'get',  
	        parameters: 'action=rssprefs&module_id='+id+'&'+Form.serialize('rssprefs'),
	 onComplete:prefresponse
	      }  
	   );
}

   function prefresponse(oReq){
	
	    $('container').innerHTML = oReq.responseText ; 
	    Sortable.create('container',{onUpdate:updateSort,only:'module',ghosting:true,constraint:false,tag:'div', handle:'handle', hoverclass:'dropmarker'});
	
}



	function createFeed(module_id){
	  	var aj = new Ajax.Request(
			'ajaxresponder.php', {
				method:'get',  
		        parameters: 'action=createfeed&'+Form.serialize('newrss'),
		        onComplete: function(data) {
					if (data.responseText){
						$('module_'+module_id).innerHTML = data.responseText ;
					}
				}
	       }  
	   );
	}

function removeElement(divID) {
  var d=document.getElementById('container');
  var olddiv = document.getElementById(divID);
  d.removeChild(olddiv);
  removeModule(divID);
  updateSort(); 
}

function removeModule(divID) {
	var aj = new Ajax.Request(
		'ajaxresponder.php', {
			method:'get',  
	        parameters: 'action=removemodule&module='+divID
       }  
   );	
}

function toggleClose(moduleID){
  	
  var closeDIV = document.getElementById('module_'+moduleID+'_close');
  if (closeDIV.style.display=="none"){
    closeDIV.style.display="block";
     $('closebutton_'+moduleID).innerHTML = "<input type='image'  src='i/x.gif' onClick='toggleClose("+moduleID+")'>" ; 

  }
  else if (closeDIV.style.display=="block"){
    closeDIV.style.display="none";
    $('closebutton_'+moduleID).innerHTML = "<input type='image'  src='i/x.gif' onClick='toggleClose("+moduleID+")'>" ; 
  }
}		
		
function toggleEditor(moduleID){
  	
  var editDIV = document.getElementById('module_'+moduleID+'_edit');
  if (editDIV.style.display=="none"){
    editDIV.style.display="block";
     $('editbutton_'+moduleID).innerHTML = "<input type='image'  src='i/close.gif' onClick='toggleEditor("+moduleID+")'>" ; 

  }
  else if (editDIV.style.display=="block"){
    editDIV.style.display="none";
    $('editbutton_'+moduleID).innerHTML = "<input type='image'  src='i/edit.gif' onClick='toggleEditor("+moduleID+")'>" ; 
  }
}


function updateSort(){
//	myarray=Sortable.sequence('container'); 
//	adloc=myarray.indexOf('ad');
//	myarray.splice(adloc,1) ; 
//	myarray.splice(2,0,'ad');
//	Sortable.setSequence('container', myarray) ; 

	var aj = new Ajax.Request(
		'ajaxresponder.php', {
			method:'get',  
            parameters: 'action=sortcookie&' + Sortable.serialize('container'),
            onComplete: getSortResponse
         }  
    );
}  

function getSortResponse(oReq) {
	if (oReq.responseText){
		alert(oReq.responseText); 
	}  
 }




function switchSuit(module_id){
	var manufacturer=document.getElementById('manufacturer').value;
	
	
	document.getElementById('module_'+module_id+'_title').innerHTML = 'Swimsuit of the Day: ' + manufacturer ; 
	
	var aj = new Ajax.Request(
		'ajaxresponder.php', {
			method:'get',  
	        parameters: 'action=swimsuit&manufacturer='+manufacturer,
	        onComplete:getSuitResponse
         }  
     );
}  


function getSuitResponse(oReq) {
		if (oReq.responseText){

		   $('showsuit').innerHTML = oReq.responseText ;
		}  
	 }	

function updateCustomContent(module_id) {
	var thiscontent=$('custom_content_'+module_id+'_edit');
	var thistitle=$('custom_content_'+module_id+'_title');
	$('module_'+module_id+'_title').innerHTML = thistitle.value;
	  	var aj = new Ajax.Request(
			'ajaxresponder.php', {
				method:'get',  
		        parameters: 'action=customcontent&module_id='+module_id+'&content='+escape(thiscontent.value)+'&title='+thistitle.value,
				onSuccess: toggleEditor(module_id),
		        onComplete: function(data) {
					if(data.responseText) {
						$('custom_content_'+module_id+'_code').innerHTML = data.responseText;
					}
				}
	       }  
	   );
}

function getModule(module_id, this_module_id, user) {
	var aj = new Ajax.Updater(
		'module_'+this_module_id,
		'php/index_page_functions.php', {
			parameters: 'action=loadmodule&this_module_id='+this_module_id+'&user='+user,
			evalScripts: true
		}
	);
}