function toggleDisplay(div, clear){
	if (clear)
		clearForm();
	
	el = document.getElementById(div);

	if (document.getElementById('addPageLink').style.display = "none"){
		document.getElementById('addPageLink').style.display = "inline";
		document.getElementById('updatePageLink').style.display = "none";
	}

	//alert(el.style.display);
	if (el.style.display == "none" || el.style.display ==""){
		el.style.display = "inline";
		document.getElementById('addPageBtn').style.display = "none";
		
	}else{
		el.style.display = "none";
		document.getElementById('addPageBtn').style.display = "inline";
	}
}

function clearForm() {
	document.getElementById('title').value = "";
	tinyMCE.activeEditor.setContent("");
	document.getElementById('pageId').value = "";
}

function toggleLoading(){
	el = document.getElementById('loading');

	//alert(el.style.display);
	if (el.style.display == "none" || el.style.display ==""){
		el.style.display = "inline";		
	}else{
		el.style.display = "none";
	}
}

function addToUpdate(){
	addPage_el = document.getElementById('addPageLink');
	updatePage_el = document.getElementById('updatePageLink');
	
	if (addPage_el.style.display == "inline" || addPage_el.style.display == ""){
		addPage_el.style.display = "none";
		updatePage_el.style.display = "inline";
	}else {
		addPage_el.style.display = "inline";
		updatePage_el.style.display = "none";
	}
}

function showUpdateForm(pageId){
	toggleLoading();
	document.getElementById('pageId').value = pageId;
	getPageTitle(pageId);
	getPageContent(pageId);
	
}

function getPageTitle(pageId){
	var xmlhttp = GetXmlHttpObject();
  	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
  		{
			document.getElementById('title').value = xmlhttp.responseText;
  		}
	}
	xmlhttp.open("GET","../wp-content/mu-plugins/myacuweb_defaultpages/myacuweb_functions_defaultPages.php?action=getPageTitle&pageId="+pageId, true);
	xmlhttp.send(null);
}

function getPageContent(pageId){
	var xmlhttp = GetXmlHttpObject();
  	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
  		{
			tinyMCE.activeEditor.setContent(xmlhttp.responseText);
			if (document.getElementById('addPageForm').style.display="none")
				toggleDisplay('addPageForm', false);
			addToUpdate();
			toggleLoading();
  		}
	}
	xmlhttp.open("GET","../wp-content/mu-plugins/myacuweb_defaultpages/myacuweb_functions_defaultPages.php?action=getPageContent&pageId="+pageId, true);
	xmlhttp.send(null);
}

function addPage(title, content, userId){
	toggleLoading();
	var xmlhttp = GetXmlHttpObject();
  	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
  		{
  			document.getElementById('message').innerHTML = xmlhttp.responseText;
  			clearForm();
  			document.getElementById('addPageForm').style.display = "none";
  			document.getElementById('addPageBtn').style.display="inline";
  			updatePageList();
  			toggleLoading();
  		}
	}
	
	xmlhttp.open("POST","../wp-content/mu-plugins/myacuweb_defaultpages/myacuweb_functions_defaultPages.php",true);
	var params = "action=addPage&title="+escape(title)+"&content="+escape(content)+"&userId="+userId;
	
	//Send the proper header information along with the request
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	
	xmlhttp.send(params);
  	
}

function updatePage(title, content, userId, pageId){
	toggleLoading();
	var xmlhttp = GetXmlHttpObject();
	
  	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
  		{
  			document.getElementById('message').innerHTML = xmlhttp.responseText;
  			toggleDisplay("addPageForm", true);
  			updatePageList();
  			toggleLoading();
  		}
	}
	xmlhttp.open("POST","../wp-content/mu-plugins/myacuweb_defaultpages/myacuweb_functions_defaultPages.php",true);
	var params = "action=updatePage&title="+escape(title)+"&content="+escape(content)+"&userId="+userId+"&pageId="+pageId;
	
	//Send the proper header information along with the request
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	
	xmlhttp.send(params);

}

function deletePage(id){
	toggleLoading();
	var xmlhttp = GetXmlHttpObject();
  	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
  		{
  			document.getElementById('message').innerHTML = xmlhttp.responseText;
  			updatePageList();
  			toggleLoading();
  		}
	}
	xmlhttp.open("GET","../wp-content/mu-plugins/myacuweb_defaultpages/myacuweb_functions_defaultPages.php?action=deletePage&id="+id,true);
	xmlhttp.send(null);
}

function updatePageList(){
	toggleLoading();
	var xmlhttp = GetXmlHttpObject();
  	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState==4)
  		{
  			document.getElementById('pageList').innerHTML = xmlhttp.responseText;
  			toggleLoading();
  		}
	}
	xmlhttp.open("GET","../wp-content/mu-plugins/myacuweb_defaultpages/myacuweb_functions_defaultPages.php?action=updatePageList");
	xmlhttp.send(null);
}

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
 	{
  		// code for IE7+, Firefox, Chrome, Opera, Safari
  		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
  	{
  	// code for IE6, IE5
  		return new ActiveXObject("Microsoft.XMLHTTP");
  	}
	return null;
}

/***********************************************
*  Add Site Functionality
***********************************************/

function addToNewSite(pageId, title){
	el = document.getElementById('defaultpages');
	
	var newItem = '<span id="'+pageId+'">';
	newItem += '<input type="hidden" name="defaultpages[]" id="add_'+pageId+'" value="'+pageId+'" />';
	newItem += '<a href="javascript:removeFromNewSite(' + pageId + ');" id="' + pageId + '">[Remove]</a> ' + title + '<br/></span>';
	
	el.innerHTML += newItem;
	document.getElementById('page_'+pageId).style.display="none";
}

function removeFromNewSite(pageId){
	el = document.getElementById('defaultpages');
	el.removeChild(document.getElementById(pageId));
	document.getElementById('page_'+pageId).style.display="";
}

function addAddress(){
	el = document.getElementById('address2');
	
	if (el.style.display == "none") {
		document.getElementById('address2').style.display = "";
		document.getElementById('suite2').style.display = "";
		document.getElementById('city2').style.display = "";
		document.getElementById('state2').style.display = "";
		document.getElementById('zip2').style.display = "";
		document.getElementById('removeAddress').style.display = "inline";
		}
	else {
		document.getElementById('address3').style.display = "";
		document.getElementById('suite3').style.display = "";
		document.getElementById('city3').style.display = "";
		document.getElementById('state3').style.display = "";
		document.getElementById('zip3').style.display = "";
		document.getElementById('addAddress').style.display = "none";
	}
}

function removeAddress(){
	el = document.getElementById('address3');
	
	if (el.style.display == "") {
		document.getElementById('address3').style.display = "none";
		document.getElementById('suite3').style.display = "none";
		document.getElementById('city3').style.display = "none";
		document.getElementById('state3').style.display = "none";
		document.getElementById('zip3').style.display = "none";
		document.getElementById('address3_input').value="";
		document.getElementById('suite3_input').value="";
		document.getElementById('city3_input').value="";
		document.getElementById('state3_input').value="";
		document.getElementById('zip3_input').value="";
		document.getElementById('addAddress').style.display = "inline"
	}
	else {
		document.getElementById('address2').style.display = "none";
		document.getElementById('suite2').style.display = "none";
		document.getElementById('city2').style.display = "none";
		document.getElementById('state2').style.display = "none";
		document.getElementById('zip2').style.display = "none";
		document.getElementById('address2_input').value="";
		document.getElementById('suite2_input').value="";
		document.getElementById('city2_input').value="";
		document.getElementById('state2_input').value="";
		document.getElementById('zip2_input').value="";
		document.getElementById('removeAddress').style.display = "none";
	}
}

function validateAddSiteForm(){
	var msg;
	var error = false;
	if (document.getElementById('subdomain').value == "") {
		msg = "Missing Site Address<br/>";
		error = true;
	}
	if (document.getElementById('title').value == "") {
		msg += "Missing Site Title<br/>";
		error = true;
	}
	if (document.getElementById('email').value == "") {
		msg += "Missing Customer Email<br/>";
		error = true;
	}
		
	if (error) {
		document.getElementById('message').innerHTML = msg;
		return false;
	}
	
	return true;
}

function validatePwd() {
	var invalid = " "; // Invalid character is a space
	var minLength = 6; // Minimum length
	var pw1 = document.password.pass1.value;
	var pw2 = document.password.pass1.value;
	// check for a value in both fields.
	if (pw1 == '' || pw2 == '') {
		alert('Please enter your password twice.');
		return false;
	}
	// check for minimum length
	if (document.password.pass1.value.length < minLength) {
		alert('Your password must be at least ' + minLength + ' characters long. Try again.');
		return false;
	}
	// check for spaces
	if (document.password.pass1.value.indexOf(invalid) > -1) {
		alert("Sorry, spaces are not allowed.");
		return false;
	} 
	if (pw1 != pw2) {
		alert ("You did not enter the same new password twice. Please re-enter your password.");
		return false;
	}

	return true;
}

function confirmDelete(){
	if (confirm("Are you sure you want to delete this page?")){
		return true;
	}else {
		return false;
	}
}

function delete_photo(blog_id){
	var xmlhttp = GetXmlHttpObject();
  	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState==4)
  		{
  			document.getElementById('photo').style.display = "none";
  			document.getElementById('message').innerHTML = xmlhttp.responseText;
  		}
	}
	xmlhttp.open("GET","../wp-content/mu-plugins/myacuweb_defaultpages/myacuweb_functions_defaultPages.php?action=delete_photo&id="+blog_id);
	xmlhttp.send(null);
}

function delete_logo(blog_id){
	var xmlhttp = GetXmlHttpObject();
  	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState==4)
  		{
  			document.getElementById('logo').style.display = "none";
  			document.getElementById('message').innerHTML = xmlhttp.responseText;
  		}
	}
	xmlhttp.open("GET","../wp-content/mu-plugins/myacuweb_defaultpages/myacuweb_functions_defaultPages.php?action=delete_logo&id="+blog_id);
	xmlhttp.send(null);

}


