function confirmDelete(delUrl) {
	if (confirm("Are you sure you want to delete this file?")) {
		document.location = delUrl;
	}
}

function confirmRevert(revUrl) {
	if (confirm("Are you sure you want to revert this page?")) {
		document.location = revUrl;
	}
}

function popPageCode() {
	if (document.getElementById("page_url") && document.getElementById("page_title") && document.getElementById("page_url").value == '' && document.getElementById("page_title").value != '') {
		var str = document.getElementById("page_title").value;
		var newstr = str.replace(/\s+/g, '-').toLowerCase();
		document.getElementById("page_url").value = newstr;
	}
}

function confirmDeleteAlias(revUrl) {
	if (confirm("Are you sure you want to delete this alias?")) {
		document.location = revUrl;
	}
}

function confirmDeleteSiteUser(revUrl) {
	if (confirm("Are you sure you want to delete this user?")) {
		document.location = revUrl;
	}
}

function showhide(div) {
	if (document.getElementById(div)) {
		document.getElementById(div).style.display = (document.getElementById(div).style.display == 'none' ? 'block' : 'none');	
	}
}

function showdropdown(fieldid) {
	document.getElementById(fieldid).style.display = "block";	
}

function selectThis(selvalue) {
	document.getElementById('select_fieldID').innerHTML = selvalue;
	document.getElementById('dropdown_fieldID').style.display = "none";
}

function toggleTabs(div) {
    $('#tabs li').each(function(idx, item) {
		var divBody = item.id + '_body';
		
		if (div == item.id) {
			item.className = 'sel';
			document.getElementById(divBody).style.display = 'block';
			document.getElementById("hid_last_page").value = div;
		} else {
			item.className = '';
			if (document.getElementById(divBody)) {document.getElementById(divBody).style.display = 'none';}
		}
		
    });
}

//execute when the HTML file's (document object model: DOM) has loaded
$(document).ready(function() {

$('#add_alias').click(function() {

	var page_alias_title = $('#page_alias_title').val();
	var page_alias_url = $('#page_alias_url').val();
	
	if (!page_alias_title || page_alias_title == 'Page Alias Title' || !page_alias_url || page_alias_url == 'Page Alias URL') {
		$('#alias_error').html('Please enter an alias title and url.');
		return false;
	}	
	
	var form_data = {
		page_id: $('#page_id').val(),
		page_alias_title: $('#page_alias_title').val(),
		page_alias_url: $('#page_alias_url').val(),
		page_alias_type: $('#page_alias_type').val(),
		ajax: '1'		
	};
	
	$.ajax({
		url: "/alias/get_alias",
		type: 'POST',
		data: form_data,
		success: function(msg) {
			$('#alias_content').html(msg);
			$('#page_alias_title').val('');
			$('#page_alias_url').val('');
			$('#page_alias_error').val('');
		}
	});
	
	return false;
});	
	
	
$('#page_url').blur(function() {

 // get the value from the username field                              
 var pageurl = $('#page_url').val();
 var pageid = $('#page_id').val();
  if (pageurl != '') {
	 // Ajax request sent to the CodeIgniter controller "ajax" method "page_url taken"
	 // post the pageurl field's value
	 $.post('/pageurl/check_url',
	   { 'pageurl':pageurl, 'pageid':pageid },
	
	   // when the Web server responds to the request
	   function(result) {
	
		 // clear any message that may have already been written
		 $('#bad_url').replaceWith('');
		 
		 // if the result is TRUE write a message to the page
		 if (result) {
		   $('#page_url').after('<div id="bad_url" style="color:red;">' +
			 '<p>(That page code is already taken. Please choose another.)</p></div>');
		 } else {
			 $('#page_url').after('<div id="bad_url" style="color:green;">' +
			 '<p>(That page code is available.)</p></div>'); 
			}
	   });
  }
 });
});  
