//-----------------------------------------------------------------------------
// NOHCompany.com 
// Copyright (c) 2006 The Ad Firm
// Developed by Xavier Amado (xavier@theadfirm.com || xamado@gmail.com)
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// This is crufty code I can't decide if this should be part of a xetDynamicForm
// or not...
//-----------------------------------------------------------------------------

function xet_dynamic_form_on_delete_clicked(link, dynamicFormID, callbackURL, styleId)
{
	// Need you daddyyyyyy againnnnnnnnn
	var dynamicForm = document.getElementById(dynamicFormID);
	if(!dynamicForm) {
		alert("DynamicForm specified by ID not found");
		return;
	}
	
	var parent = dynamicForm.parentNode;
	
	// Let's just hide the link
	link.style.display = "none";
	
	// Change the style of the preview div
	dynamicForm.setAttribute("class", "dynamicform_active");
	dynamicForm.setAttribute("className", "dynamicform_active");
	
	// Let's insert a small bar on the bottom to ask for confirmation
	var smallBar = document.createElement("div");
	smallBar.setAttribute("class", "confirmationBar");
	smallBar.setAttribute("className", "confirmationBar");
	smallBar.innerHTML = "Are you sure?";
	
	var yesLink = document.createElement("a");
	yesLink.setAttribute("href", "javascript:void(0);");
	yesLink.onmousedown = function() { style_on_delete_confirm_clicked(dynamicForm, callbackURL, styleId); };
	yesLink.innerHTML = "Yes";
	yesLink.style.marginleft = "10px";
	smallBar.appendChild(yesLink);
	
	var noLink = document.createElement("a");
	noLink.setAttribute("href", "javascript:void(0);");
	noLink.onmousedown = function() { style_on_delete_cancel_clicked(link, dynamicForm, smallBar); };
	noLink.innerHTML = "No";
	noLink.style.marginleft = "10px";
	smallBar.appendChild(noLink);
	
	dynamicForm.appendChild(smallBar);
}

function style_on_delete_confirm_clicked(dynamicForm, callbackURL, styleId)
{
	// Delete the whole div
	var parent = dynamicForm.parentNode;
	parent.removeChild(dynamicForm);
	
	// Create the request object and set the recv callback
	var req;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.onreadystatechange = function() {  };

	// Form a correct link and send the get request
	var callbackURL = configBaseURL + callbackURL;
	var postData = "delete=" + styleId;
	req.open("POST", callbackURL, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send(postData);
}

function style_on_delete_cancel_clicked(link, dynamicForm, smallBar)
{
	// Show the link again
	link.style.display = "block";
	
	// Change the style of the preview div
	dynamicForm.setAttribute("class", "dynamicform");
	dynamicForm.setAttribute("className", "dynamicform");
	
	// And delete the smallBar
	dynamicForm.removeChild(smallBar);
}

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

function style_add_new_style()
{
	if(gEditing)
		return;
	else gEditing = true;
	
	var div = document.getElementById("styleslist");
	if(div == null) {
		alert("The style list div couldn't be found");
		return;
	}

	// Create a dotted div to contain the whole item
	var dynamicForm = document.createElement("div");
	dynamicForm.setAttribute("class", "dynamicform_active");
	dynamicForm.setAttribute("className", "dynamicform_active");
	
	// 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", "stylethumb");
	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", "stylename");
	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", "styledesc");
	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 storing the new value
	var hidden = document.createElement("span");
	hidden.style.display = "none";
	hidden.setAttribute("id", "new");
	hidden.setAttribute("name", "editable");
	hidden.innerHTML = "true";
	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", "browse.php?style="+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, "style_callbacks.php", onSavedCallback); };
	dynamicForm.appendChild(saveButton);
	
	// Finally append the dottedDiv to the page
	div.insertBefore(dynamicForm, div.firstChild);
}

function house_add_new_house(styleId)
{
	if(gEditing)
		return;
	else gEditing = true;
	
	var div = document.getElementById("houseslist");
	if(div == null) {
		alert("The houses list div couldn't be found");
		return;
	}

	// Create a dotted div to contain the whole item
	var dynamicForm = document.createElement("div");
	dynamicForm.setAttribute("class", "dynamicform_active");
	dynamicForm.setAttribute("className", "dynamicform_active");
	
	// 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("id", "housethumb");
	thumb.setAttribute("src", "images/nothumbnail.jpg");
	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", "housename");
	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", "housedesc");
	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 storing the new value
	var hidden = document.createElement("span");
	hidden.style.display = "none";
	hidden.setAttribute("id", "new");
	hidden.setAttribute("name", "editable");
	hidden.innerHTML = "true";
	dynamicForm.appendChild(hidden);
	
	// Add another hidden field to store the style
	var style = document.createElement("span");
	style.style.display = "none";
	style.setAttribute("id", "style");
	style.setAttribute("name", "editable");
	style.innerHTML = styleId;
	dynamicForm.appendChild(style);
	
	// 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", "view.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, "house_callbacks.php", onSavedCallback); };
	dynamicForm.appendChild(saveButton);
	
	// Finally append the dottedDiv to the page
	div.insertBefore(dynamicForm, div.firstChild);
}

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

function nohc_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);
}

//-----------------------------------------------------------------------------
// Handle uploading of pdf file 
//-----------------------------------------------------------------------------

function nohc_view_plan_brochure_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; 
	};
	
	// Let's make an onSaved callback, triggered after the dynamicform has been told to save
	var onSavedCallback = function()
	{
		pdflink.onclick = 0;
	};
	
	// Enable editing mode on the form
	xet_dynamic_form_on_edit_clicked(button.parentNode, 'houseform', 'product_callbacks.php', onSavedCallback);
}

//-----------------------------------------------------------------------------
// Handle offline/online pages
//-----------------------------------------------------------------------------

function nohc_browse_put_online(link, houseId)
{
	// Create the request object and set the recv callback
	var req;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.onreadystatechange = function() {  };

	// Form a correct link and send the get request
	var callbackURL = configBaseURL + "house_callbacks.php";
	var postData = "online=" + houseId;
	req.open("POST", callbackURL, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send(postData);
	
	// Update the link
	var img = link.getElementsByTagName("img")[0];
	img.setAttribute("src", "images/offline.png");
	var text = link.getElementsByTagName("span")[0];
	text.innerHTML = "Put offline";
	
	link.onmousedown = function () { nohc_browse_put_offline(link, houseId); };	
}

function nohc_browse_put_offline(link, houseId)
{
	// Create the request object and set the recv callback
	var req;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.onreadystatechange = function() { };

	// Form a correct link and send the get request
	var callbackURL = configBaseURL + "house_callbacks.php";
	var postData = "offline=" + houseId;
	req.open("POST", callbackURL, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send(postData);
	
	// Update the link
	var img = link.getElementsByTagName("img")[0];
	img.setAttribute("src", "images/online.png");
	var text = link.getElementsByTagName("span")[0];
	text.innerHTML = "Put online";
	
	link.onmousedown = function () { nohc_browse_put_online(link, houseId); };	
}

function nohc_page_put_online(link, pageId)
{
	// Create the request object and set the recv callback
	var req;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.onreadystatechange = function() {  };

	// Form a correct link and send the get request
	var callbackURL = configBaseURL + "page_callbacks.php";
	var postData = "online=" + pageId;
	req.open("POST", callbackURL, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send(postData);
	
	// Update the link
	var img = link.getElementsByTagName("img")[0];
	img.setAttribute("src", "images/offline.png");
	var text = link.getElementsByTagName("span")[0];
	text.innerHTML = "Put offline";
	
	link.onmousedown = function () { nohc_page_put_offline(link, pageId); };	
}

function nohc_page_put_offline(link, pageId)
{
	// Create the request object and set the recv callback
	var req;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.onreadystatechange = function() { };

	// Form a correct link and send the get request
	var callbackURL = configBaseURL + "page_callbacks.php";
	var postData = "offline=" + pageId;
	req.open("POST", callbackURL, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send(postData);
	
	// Update the link
	var img = link.getElementsByTagName("img")[0];
	img.setAttribute("src", "images/online.png");
	var text = link.getElementsByTagName("span")[0];
	text.innerHTML = "Put online";
	
	link.onmousedown = function () { nohc_page_put_online(link, pageId); };	
}

