eval(function(a,b,c,d){while(b--)if(c[b])a=a.replace(new RegExp(d+b+d,'g'),c[b]);return a}("function ajaxFormatURL(1){2 3=document.getElementById(1);2 4='';for(2 a=0;a<3.5.6;a++){switch(3.5[a].type){7'checkbox':{8(3.5[a].9==10){8(4.6>0)4=4+'*11*';4=4+3.5[a].12+'='+3.5[a].13}14}7'file':{14}7'hidden':{8(4.6>0)4=4+'*11*';4=4+3.5[a].12+'='+3.5[a].13;14}7'image':{14}7'password':{8(4.6>0)4=4+'*11*';4=4+3.5[a].12+'='+3.5[a].13;14}7'radio':{8(3.5[a].9==10){8(4.6>0)4=4+'*11*';4=4+3.5[a].12+'='+3.5[a].13}14}7'15-multiple':{14}7'15-one':{8(4.6>0)4=4+'*11*';4=4+3.5[a].12+'='+3.5[a].13;14}7'text':{8(4.6>0)4=4+'*11*';4=4+3.5[a].12+'='+3.5[a].13;14}7'textarea':{8(4.6>0)4=4+'*11*';4=4+3.5[a].12+'='+3.5[a].13;14}}}4=encodeURI(4);4=4.16(/\\+/g,'% 2B');4=4.16(/&/g,'% 26');4=4.16(/\\*11\\*/g,'&');return 4}",17,'|the_form_name|var|the_form|to_be_returned|elements|length|case|if|checked|true|AMP|name|value|break|select|replace'.split('|'),'\\b'))
function ajaxSubmit(method,target,asynchronous,variables_to_send){var req=null;if(window.XMLHttpRequest){req=new XMLHttpRequest()}else{if(window.ActiveXObject){req=new ActiveXObject("Microsoft.XMLHTTP")}}req.onreadystatechange=function(){if(req.readyState==4){if(req.status==200){var returned_value=req.responseText;eval(returned_value)}else{}}};req.open(method,target,asynchronous);if(method=='POST'){req.setRequestHeader('Content-Type','application/x-www-form-urlencoded')}req.send(variables_to_send)}

// this is a small helper extension i stole from
// http://www.texotela.co.uk/code/jquery/reverse/
// it merely reverses the order of a jQuery set.
$.fn.reverse = function() {
    return this.pushStack(this.get().reverse(), arguments);
};

// create two new functions: prevALL and nextALL. they're very similar, hence this style.
$.each( ['prev', 'next'], function(unusedIndex, name) {
    $.fn[ name + 'ALL' ] = function(matchExpr) {
        // get all the elements in the body, including the body.
        var $all = $('body').find('*').andSelf();

        // slice the $all object according to which way we're looking
        $all = (name == 'prev')
             ? $all.slice(0, $all.index(this)).reverse()
             : $all.slice($all.index(this) + 1)
        ;
        // filter the matches if specified
        if (matchExpr) $all = $all.filter(matchExpr);
        return $all;
    };
});


  function moveAdBlock(ad_number, move_by) {
    var the_ad = '#movable_ad_block__' + ad_number;
    var move_direction = 'up';
	var move_distance = 1;
	switch(move_by) {
	  case 'up10': { move_direction = 'up'; move_distance = 10; break; }
	  case 'up1': { move_direction = 'up'; move_distance = 1; break; }
	  case 'down1': { move_direction = 'down'; move_distance = 1; break; }
	  case 'down10': { move_direction = 'down'; move_distance = 10; break; }
	  default: { move_direction = 'up'; move_distance = 0; break; }
	}  
	if(move_distance > 0) {
	  for(var a=0; a<move_distance; a++) {
// alert(a + '!!');
	    if(move_direction == 'up') {
          var the_previous_sibling = $(the_ad).prev();
		  if(the_previous_sibling.length != 0) {
		  //alert(the_previous_sibling.attr('id'));
		    if(the_previous_sibling.attr('id').substring(0,18) != 'movable_ad_block__') {
			  the_previous_sibling = moveAdBlockFindLastChild(the_previous_sibling);
			}  
//alert(the_previous_sibling.attr('id'));
if($.isFunction(the_previous_sibling.get)) {
//  if(the_previous_sibling.get(0).tagName == 'BR') { a--; }
}  
            var the_ad_html = $(the_ad).remove();
	        the_ad_html.insertAfter(the_previous_sibling);
            if($.isFunction(the_previous_sibling.children)) {
			  the_previous_sibling.insertAfter(the_ad_html);
			}  
//            the_previous_sibling.insertAfter('AAAA');
//			return true;
		  }
		  else {
			var the_previous_parent = $(the_ad).parent();
			if(the_previous_parent.attr('id') != 'maincontainer') {
//alert('Parent:' + the_previous_parent.attr('id'));
//alert(the_previous_parent.get(0).tagName + '**');
	          the_previous_parent = moveAdBlockFindLastParent(the_previous_parent);
              var the_ad_html = $(the_ad).remove();
	          the_ad_html.insertAfter(the_previous_parent);
              the_previous_parent.insertAfter(the_ad_html);
			}  
		  }	
	    }
	    else {
		  the_next_element = $(the_ad).next();
          if(!($(the_ad).parent().attr('id') == 'maincontainer' && the_next_element.length == 0)) {

		  
//alert($(the_ad).parent().attr('id'));
if($.isFunction(the_next_element.get)) {
//  alert(the_next_element.get(0).tagName);
  if(the_next_element.get(0) && the_next_element.get(0).tagName == 'BR') { a-=2; }
}  
 
		    if(the_next_element.length != 0) {
		      the_next_element.append($(the_ad));
              if(the_next_element.children().length) {
			    for(var b=1; b<the_next_element.children().length; b++) {
			      moveAdBlock(ad_number, 'up1');
			    }
		      }
		    }
		    else {
 		      var the_next_parent = $(the_ad).parent();
			  if(the_next_parent.attr('id') != 'maincontainer') {
	            the_next_parent = moveAdBlockFindLastParent(the_next_parent);
			  }
              var the_ad_html = $(the_ad).remove();
	          the_ad_html.insertBefore(the_next_parent);
              the_next_parent.insertBefore(the_ad_html);
			}  
		  }
		}
	  }
	}
  }
  
  function moveAdBlockFindLastChild(the_sibling) {
    while(the_sibling.children().length != 0) {
	  the_sibling = the_sibling.children()[(the_sibling.children().length-1)];
//	  if(($.isFunction(the_sibling.get))) { alert(the_sibling.get(0).tagName); }
//	  if(the_sibling.get(0).tagName != 'BR') {
	    if(!($.isFunction(the_sibling.children))) { return the_sibling; }
//	  }	
	}
	return the_sibling;
  }

  function moveAdBlockFindLastParent(the_parent) {
	if(the_parent.parent().children()) {
	  while(the_parent.parent().children().length <= 1) {
	    the_parent = the_parent.parent();
	  }
	} 
	return the_parent;
  }
  
  function saveAdBlockPosition(ad_number, the_table, element_number) {
	var the_ad = '#movable_ad_block__' + ad_number;
	$(the_ad).replaceWith('ADBLOCKHERE');
	var the_html = document.getElementById('maincontainer').innerHTML;
	the_html = the_html.replace("\n", '');
	the_html = escape(the_html);
    ajaxSubmit('POST', webpage_url + 'oddsandends.php', true, 'move_ad_block=Yes&mysql_table=' + the_table + '&element_number=' + element_number + '&the_html=' + the_html);	
  }
