// JavaScript Document

var ContentChanged = false;

function editPage(id)
{
	document.getElementById('pageid').value=id;
	document.getElementById('pageimageid').value=-1;
	document.admin.submit();
}

function editImages(id)
{
	document.getElementById('pageid').value=0;
	document.getElementById('pageimageid').value=id;
	document.admin.submit();
}

function selectTab(id)
{
	request = "admin.php?ajax=tab&id="+id;
	callAjaxFunction( request, true, cb_updateTabContent, 'tabcontent', null )
	
	els = document.getElementsByTagName("TD");
	for( i=0; i<els.length; i++ )
	{
		if( els[i].id.substring(0,4) == "TAB_" )
		{
			if( els[i].id == "TAB_"+name ) els[i].className = "TabSelected";
			else els[i].className = "Tab";
		}
	}
}

function cb_updateTabContent()
{
}

///////////////////////////////////////////////////
////////Trying To Figure Out How This Works////////
///////////////////////////////////////////////////

/*function editKeywords(id)
{
	document.getElementById('pageid').value=id;
	document.getElementById('keywords').value=id;
	document.admin.submit();
}*/

function getSourcePara()
{
	el = event.srcElement;
	while( el.tagName != "P" ) 
	{
		el = el.parentNode;
	}
	return el;
}

function selectParagraph()
{
	els = document.getElementsByTagName("P");
	for( i=0; i<els.length; i++ )
		els[i].className = els[i].className.replace('AdminBorderSelected','AdminBorder');
	
	el = getSourcePara();
//	document.getElementById('Debug').innerHTML = el.id;
	el.className = el.className.replace('AdminBorder','AdminBorderSelected');
	document.admin.paraid.value=el.id;
}

function saveKeywords()
{
	document.admin.command.value="savekeywords";
	document.admin.submit();
}

function saveGlobals()
{
	document.admin.command.value="saveglobals";
	document.admin.submit();
}

function moveLeft()
{
	document.admin.command.value="moveleft";
	document.admin.submit();
}

function moveRight()
{
	document.admin.command.value="moveright";
	document.admin.submit();
}

function moveUp()
{
	document.admin.command.value="moveup";
	document.admin.submit();
}

function moveDown()
{
	document.admin.command.value="movedown";
	document.admin.submit();
}

function addPara()
{
	
}

function editPara()
{
}

function deletePara()
{
	if( confirm("Are you sure you wish to delete this item?") )
	{
		document.admin.command.value="delete";
		document.admin.submit();
	}
}

function showDialog(id)
{
	el = document.getElementById(id);
	if( el ) 
	{
		el.style.display = 'block';
		document.admin.command.value = id;
		el = document.getElementById('DialogMask');
		if( el ) 
			el.style.display = 'block';
	}
}

function hideDialog(id)
{
	el = document.getElementById(id);
	if( el ) 
	{
		el.style.display = 'none';
		document.admin.command.value = id;
		el = document.getElementById('DialogMask');
		if( el ) 
			el.style.display = 'none';
	}
}

function removePage(id)
{
	if( confirm("Are you sure you wish to delete this page?") )
	{
		document.admin.pageid.value = id;
		document.admin.command.value="removepage";
		document.admin.submit();
	}
}

function removePicture(id)
{
	if( confirm("Are you sure you wish to delete this picture?") )
	{
		document.admin.pageid.value = id;
		document.admin.command.value="removepicture";
		document.admin.submit();
	}
}

function removeBannerImage(id)
{
	if( confirm("Are you sure you wish to delete this banner image?") )
	{
		document.admin.pageimageid.value = id;
		document.admin.command.value="removebannerimage";
		document.admin.submit();
	}
}
function removeDocument(id)
{
	if( confirm("Are you sure you wish to delete this document?") )
	{
		document.admin.pageid.value = id;
		document.admin.command.value="removedocument";
		document.admin.submit();
	}
}

function removeEvent(id)
{
	if( confirm("Are you sure you wish to delete this event?") )
	{
		document.admin.pageid.value = id;
		document.admin.command.value="removeevent";
		document.admin.submit();
	}
}

function removeCategory(id)
{
	if( confirm("Are you sure you wish to delete this category?") )
	{
		document.admin.pageid.value = id;
		document.admin.command.value="removecategory";
		document.admin.submit();
	}
}

function removeForm(id)
{
	if( confirm("Are you sure you wish to delete this contact form?") )
	{
		document.admin.pageid.value = id;
		document.admin.command.value="removeform";
		document.admin.submit();
	}
}

function removeProduct(id)
{
	if( confirm("Are you sure you wish to delete this product?") )
	{
		document.admin.pageid.value = id;
		document.admin.command.value="removeproduct";
		document.admin.submit();
	}
}

function ShowTab(name)
{
	els = document.getElementsByTagName("DIV");
	for( i=0; i<els.length; i++ )
	{
		if( els[i].id.substring(0,4) == "TAB_" )
		{
			if( els[i].id == "TAB_"+name ) els[i].style.display = "block";
			else els[i].style.display = "none";
		}
	}
	
	els = document.getElementsByTagName("A");
	for( i=0; i<els.length; i++ )
	{
		if( els[i].id.substring(0,4) == "TAB_" )
		{
			if( els[i].id == "TAB_"+name ) els[i].className = "TabSelector_Selected";
			else els[i].className = "TabSelector";
		}
	}
}

function ChangeDialogImage(selectorid)
{
	elimg = document.getElementById('DialogImage');
	elsel = document.getElementById(selectorid);
	
	if( elimg && elsel ) 
		elimg.src = "image.php?thumb="+elsel.value;
}

function showinserttextrows()
{
	el = document.getElementById('type');
	if(el)
	{
		elURL = document.getElementById('urlrow');
		if( el.value == "hyperlink" ) {
			if( elURL ) elURL.style.display = 'block';
		}
		else {
			if( elURL ) elURL.style.display = 'none';
		}
	}
}

function contentChanged()
{
	ContentChanged = true;
}

function saveContent(pageid,contentid)
{
	if( ContentChanged )
	{
		el = window.event.srcElement;
		if( el )
		{
			request = "?ajax=savecontent&page="+pageid+"&contentid="+contentid;
			data = el.innerHTML;
			callAjaxFunction( request, true, cb_saveContent, 'savecontent', data )
		}
	}
	ContentChanged = false;
}

function cb_saveContent(response,id)
{
}

function callAjaxFunction( request, post, callback, id, data )
{
	var xmlHttp;
	
	try { xmlHttp=new XMLHttpRequest(); } // Firefox, Opera 8.0+, Safari  
	catch (e) {  
    	try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } // Internet Explorer 
  		catch (e) {    
			try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
    		catch (e) { 
				alert("Your browser does not support AJAX!");
				return false;      
			}   
		}  
	} 
	
	var randomnumber=Math.floor(Math.random()*99999999)
	if( post == true || data == null ) request = request.replace( "?", "?rid="+randomnumber+"&" );
	else request = request.replace( "?", "?rid="+randomnumber+"&data="+urlencode(data)+"&" );
	
	xmlHttp.onreadystatechange = function()
	{
		if( xmlHttp.readyState == 4 && xmlHttp.status == 200 ) 
		{
			if( xmlHttp.responseText.length > 0 ) 
				callback( xmlHttp.responseText, id );
			else 
				callback( xmlHttp.responseBody, id );
		}
		delete xmlHttp;
	}
	
	if( post == true ) {
		xmlHttp.open("POST",request,true);
		xmlHttp.send(data);
	} else {
		xmlHttp.open("GET",request,true);
		xmlHttp.send(null);
	}
}

function urlencode( str ) 
{                            
	if( str.length < 1 ) return str;

	var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

                  