//-----------------------------------------------------------------------------
// Add a new style to edit on the home page 
//-----------------------------------------------------------------------------

function amerafence_add_new_product(category)
{
	if(gEditing)
		return;
	else gEditing = true;
	
	var div = document.getElementById("productlist");
	if(div == null) {
		alert("The product list div couldn't be found");
		return;
	}

	// Create a dotted div to contain the whole item
	var dynamicForm = document.createElement("div");
	dynamicForm.setAttribute("class", "productinfo");
	dynamicForm.setAttribute("className", "productinfo");
	dynamicForm.style.borderStyle = "dashed";
	dynamicForm.style.borderColor = "blue";
	dynamicForm.style.borderWidth = "2px";
	
	// And another div to contain everything but the save button
	var newDiv = document.createElement("div");
	newDiv.style.overflow = "hidden";
	dynamicForm.appendChild(newDiv);
	
	// Create the thumbnail with it's link
	var thumb = document.createElement("img");
	thumb.setAttribute("src", "images/nothumbnail.jpg");
	thumb.setAttribute("id", "prodthumb");
	thumb.setAttribute("name", "editable");
	thumb.setAttribute("class", "thumbnail");
	thumb.setAttribute("className", "thumbnail");
	thumb.setAttribute("uploaderurl", "thumbUploader.php");
	
	var link = document.createElement("a");
	link.setAttribute("name", "editable");
	link.onclick = function() { xet_dynamic_form_on_image_clicked(thumb, thumb.getAttribute("uploaderurl")); };
	link.appendChild(thumb);
	newDiv.appendChild(link);				
	
	// The title surrounded by h3
	var title = document.createElement("span");
	title.innerHTML = "Click here to edit the title";
	title.setAttribute("id", "prodname");
	title.setAttribute("name", "editable");
	title.setAttribute("type", "string");
	title.onmouseover = function() { this.setAttribute("class", "editable"); };
	title.onmouseout = function() { this.setAttribute("class", ""); };
	title.onmousedown = function() { xet_dynamic_form_on_text_clicked(this); };
	
	var heading3 = document.createElement("h3");
	heading3.appendChild(title);
	newDiv.appendChild(heading3);
	
	// The short description text
	var shortdesc = document.createElement("span");
	shortdesc.innerHTML = "Click here to edit the description";
	shortdesc.setAttribute("id", "proddesc");
	shortdesc.setAttribute("name", "editable");
	shortdesc.setAttribute("type", "text");
	shortdesc.onmouseover = function() { this.setAttribute("class", "editable"); };
	shortdesc.onmouseout = function() { this.setAttribute("class", ""); };
	shortdesc.onmousedown = function() { xet_dynamic_form_on_text_clicked(this); };
	newDiv.appendChild(shortdesc);
	
	// Add a hidden field for sending the new value
	var hidden = document.createElement("span");
	hidden.style.display = "none";
	hidden.setAttribute("id", "new");
	hidden.setAttribute("name", "editable");
	hidden.innerHTML = category;
	dynamicForm.appendChild(hidden);
	
	// We are going to need a custom function for parsing the reply
	onSavedCallback = function(req)
	{
		if(req.readyState == 4 && req.status == 200)
		{	
			// Add a link around the title
			var link = document.createElement("a");
			link.setAttribute("href", "viewProduct.php?id="+req.responseText);
			
			heading3.parentNode.insertBefore(link, heading3);
			link.appendChild(heading3);
		}
	}
		
	// Create the save button and add it to the dottedDiv instead of the newDiv
	var saveButton = document.createElement("input");
	saveButton.setAttribute("type", "button");
	saveButton.setAttribute("value", "Save");
	saveButton.setAttribute("class", "savebtn");
	saveButton.setAttribute("className", "savebtn");
	saveButton.onclick = function() { xet_dynamic_form_on_save_clicked(this, dynamicForm, null, "product_callbacks.php", onSavedCallback); };
	dynamicForm.appendChild(saveButton);
	
	// Finally append the dottedDiv to the page
	div.insertBefore(dynamicForm, div.firstChild);
}

function amerafence_add_new_news(category)
{
	if(gEditing)
		return;
	else gEditing = true;
	
	var div = document.getElementById("newslist");
	if(div == null) {
		alert("The product list div couldn't be found");
		return;
	}

	// Create a dotted div to contain the whole item
	var dynamicForm = document.createElement("div");
	dynamicForm.style.borderStyle = "dashed";
	dynamicForm.style.borderColor = "blue";
	dynamicForm.style.borderWidth = "2px";
	
	// And another div to contain everything but the save button
	var newDiv = document.createElement("div");
	newDiv.style.overflow = "hidden";
	dynamicForm.appendChild(newDiv);

	// The title surrounded by h1
	var title = document.createElement("span");
	title.innerHTML = "Click here to edit the title";
	title.setAttribute("id", "newstitle");
	title.setAttribute("name", "editable");
	title.setAttribute("type", "string");
	title.onmouseover = function() { this.setAttribute("class", "editable"); };
	title.onmouseout = function() { this.setAttribute("class", ""); };
	title.onmousedown = function() { xet_dynamic_form_on_text_clicked(this); };
	
	var heading = document.createElement("h1");
	heading.appendChild(title);
	newDiv.appendChild(heading);
	
	// The news body
	var body = document.createElement("span");
	body.innerHTML = "Click here to edit the news body";
	body.setAttribute("id", "newsbody");
	body.setAttribute("name", "editable");
	body.setAttribute("type", "richtext");
	body.onmouseover = function() { this.setAttribute("class", "editable"); };
	body.onmouseout = function() { this.setAttribute("class", ""); };
	body.onmousedown = function() { xet_dynamic_form_on_text_clicked(this); };
	newDiv.appendChild(body);
	
	// Add a hidden field for sending the new value
	var hidden = document.createElement("span");
	hidden.style.display = "none";
	hidden.setAttribute("id", "new");
	hidden.setAttribute("name", "editable");
	hidden.innerHTML = category;
	dynamicForm.appendChild(hidden);
	
	// We are going to need a custom function for parsing the reply
	onSavedCallback = function(req)
	{
		if(req.readyState == 4 && req.status == 200)
		{	
			// Do nothing
		}
	}
		
	// Create the save button and add it to the dottedDiv instead of the newDiv
	var saveButton = document.createElement("input");
	saveButton.setAttribute("type", "button");
	saveButton.setAttribute("value", "Save");
	saveButton.setAttribute("class", "savebtn");
	saveButton.setAttribute("className", "savebtn");
	saveButton.onclick = function() { xet_dynamic_form_on_save_clicked(this, dynamicForm, null, "news_callbacks.php", onSavedCallback); };
	dynamicForm.appendChild(saveButton);
	
	// Finally append the dottedDiv to the page
	div.insertBefore(dynamicForm, div.firstChild);
}

function amerafence_view_product_on_edit_clicked(button, id)
{
	// Let's handle the two added file links
	/*var pdflink = document.getElementById("pdflink");
	pdflink.onclick = function() 
	{ 
		var callback = function(link) { pdflink.setAttribute("href", link); }; 
		var uploaderURL = "pdfFileUploader.php?id="+id;
		
		xet_file_uploader_show(callback, uploaderURL)
		
		return false; 
	};
	
	var dwglink = document.getElementById("dwglink");
	dwglink.onclick = function() 
	{ 
		var callback = function(link) { dwglink.setAttribute("href", link); }; 
		var uploaderURL = "pdfFileUploader.php?id="+id;
		
		xet_file_uploader_show(callback, uploaderURL)
		
		return false; 
	};
	
	// Let's make an onSaved callback, triggered after the dynamicform has been told to save
	var onSavedCallback = function()
	{
		pdflink.onclick = 0;
		dwglink.onclick = 0;
	};*/
	
	// Enable editing mode on the form
	xet_dynamic_form_on_edit_clicked(button.parentNode, button.parentNode.parentNode, 'product_callbacks.php', null);
}

function amerafence_view_page_on_edit_clicked(button)
{
	xet_dynamic_form_on_edit_clicked(button.parentNode, button.parentNode.parentNode, 'page_callbacks.php');
}

//-----------------------------------------------------------------------------
// File uploader for pages, on selected callback 
//-----------------------------------------------------------------------------

function amerafence_file_uploader_select_image()
{
	var select = document.getElementById("imageList");
	var index = select.selectedIndex;
	var filename = select.options[index].value;

	window.parent.xet_file_uploader_on_upload_completed(filename);
}

//-----------------------------------------------------------------------------
// Request Info form 
//-----------------------------------------------------------------------------

function amerafence_request_info_on_submit_clicked(formElem, button)
{
	// Check that all required fields are filled in
	var i;
	var emptyFields = false;
	
	var inputs = formElem.getElementsByTagName("input");	
	for(i = 0; i < inputs.length; i++)
	{
		var input = inputs[i];
		if(input.getAttribute("required") == "true")
		{
			if(input.value == "")
			{
				input.style.borderColor = "#ff0000";
				
				if(!emptyFields)
					input.focus();
								
				emptyFields = true;
			}
			else input.style.borderColor = "#426a76";
		}
	}
	
	var textareas = formElem.getElementsByTagName("textarea");
	for(i = 0; i < textareas.length; i++)
	{
		var textarea = textareas[i];
		if(textarea.getAttribute("required") == "true")
		{
			if(textarea.value == "")
			{
				textarea.style.borderColor = "#ff0000";
				emptyFields = true;
			}
		}
	}
	
	var optgroups = formElem.getElementsByTagName("fieldset");
	for(i = 0; i < optgroups.length; i++)
	{
		var optgroup = optgroups[i];
		if(optgroup.getAttribute("required") == "true")
		{
			var checkedOption = false;
			
			// Go through each radio button and check that at least one is true
			var radios = optgroup.getElementsByTagName("input");
			for(j = 0; j < radios.length; j++)
			{
				var radio = radios[j];
				
				if(radio.checked)
				{
					// got an option let's bail out, all is good
					checkedOption = true;
					break;
				}
			}
			
			if(!checkedOption)
			{
				optgroup.style.borderColor = "#ff0000";
				optgroup.style.borderWidth = "1px";
				optgroup.style.borderStyle = "solid";
				emptyFields = true;
			}
		}
	}
	
	if(emptyFields)
		return false;
		
	// Let's submit the form results using AJAX if possible
	
	// Create the request object and set the recv callback
	// If the request object is not supported return true and let the form handle itself
	// the standard way
	var req;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else return true;
	
	req.onreadystatechange = function() 
	{ 
		if(req.readyState == 4 && req.status == 200)
			formElem.parentNode.innerHTML = req.responseText;	
	};
	
	// Create the postString
	var postString = "";
	
	var inputs = formElem.getElementsByTagName("input");	
	for(i = 0; i < inputs.length; i++)
	{
		var input = inputs[i];

		if(input.getAttribute("type") == "radio" && input.checked == false)
			continue;
			
		if(input.getAttribute("type") == "radio")
			var id = input.getAttribute("name");
		else var id = input.getAttribute("id");
		
		var value = input.value;
			
		// Append the appropiate concatenation char
		if(postString != "")
			postString += "&";
			
		// Append the id/value key
		postString += id + "=" + value;
	}
	
	var textareas = formElem.getElementsByTagName("textarea");	
	for(i = 0; i < textareas.length; i++)
	{
		var textarea = textareas[i];

		var id = textarea.getAttribute("id");
		var value = textarea.value;
			
		// Append the appropiate concatenation char
		if(postString != "")
			postString += "&";
			
		// Append the id/value key
		postString += id + "=" + value;
	}
	
	// Form a correct link and send the post request
	var callbackURL = configBaseURL + "requestInfo_callbacks.php";
	req.open("POST", callbackURL, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send(postString);
	
	// Change the button so it reads Sending...
	button.setAttribute("value", "Sending...");
	
	// Return false to stop the submit button from getting in the way
	return false;
}

//-----------------------------------------------------------------------------
// Request Info form 
//-----------------------------------------------------------------------------

function amerafence_on_customize_clicked(type, submenu)
{
	// Create the request object and set the recv callback
	// If the request object is not supported return true and let the form handle itself
	// the standard way
	var req;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else return true;
	
	req.onreadystatechange = function() 
	{ 
		if(req.readyState == 4 && req.status == 200) 
		{
			// Replace the menu
			submenu.innerHTML = req.responseText;
			
			// Toggle the menu, this should expand it
			var listitem = submenu.parentNode;	
			menu_toggle_submenu(listitem);
			
			// Change the button
			var images = listitem.getElementsByTagName("img");
			images[0].setAttribute("src", "images/"+type+".jpg");
		}		
	};
	
	// Form a correct link and send the post request
	var callbackURL = configBaseURL + "customize_callbacks.php?getmenu="+type;
	req.open("GET", callbackURL, true);
	req.send(null);
	
	// Close the menu
	var listitem = submenu.parentNode;
	menu_toggle_submenu(listitem);
	
	// Change the button
	var images = listitem.getElementsByTagName("img");
	images[0].setAttribute("src", "images/loading.jpg");
}

//-----------------------------------------------------------------------------
// File uploader for pages, on selected callback 
//-----------------------------------------------------------------------------

function amerafence_file_uploader_select_file()
{
	var select = document.getElementById("fileList");
	var index = select.selectedIndex;
	var filename = select.options[index].value;

	window.parent.xet_file_uploader_on_upload_completed(filename);
}

