var ModalDialogWindow;
var ModalDialogInterval;
var ModalDialog = new Object;
var refreshing = false;

ModalDialog.value = '';
ModalDialog.eventhandler = '';

var xml_request = getXmlHttpRequestObject();
var event_listeners = new Object();

function getXmlHttpRequestObject()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		return false;
	}
}

function refresh_page()
{
	
	if (refreshing == false) {
		//only run this function once!!!
		refreshing = true;
		document.location.replace(document.location.href);
	}

}

function ModalDialogMaintainFocus()
{
	try
	{
		if (ModalDialogWindow.closed)
		{
			window.clearInterval(ModalDialogInterval);
			eval(ModalDialog.eventhandler);
			return;
		}

//			ModalDialogWindow.focus();
	}
	catch (everything)
	{

	}
}

function ModalDialogRemoveWatch()
{
	ModalDialog.value = '';
	ModalDialog.eventhandler = '';
}

function ModalDialogShow(page_to_show, do_refresh)
{
	var do_refresh = (do_refresh == null) ? true : do_refresh;

	ModalDialogRemoveWatch();

	if(do_refresh === true)
	{
		ModalDialog.eventhandler = "refresh_page()";
	}

	var args='width=800,height=610,left=325,top=300,toolbar=0,';
	args += 'location=0,status=0,menubar=0,scrollbars=1,resizable=0'; 

	ModalDialogWindow=window.open("","",args);
	ModalDialogWindow.document.open('');
	ModalDialogWindow.document.location = page_to_show;
	ModalDialogWindow.document.close();
	ModalDialogWindow.focus();
	ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()", 5);
}

function isset(varname)
{
  return(typeof(window[varname])!='undefined');
}

function process_form(form)
{	
	// get the container div
	var entry_details = document.getElementById('form_details');

	// get all the form fields and generate a post string
	var field_types = new Array('input', 'textarea', 'select');

	var post_variables = "";

	for (var i = 0; i < field_types.length; i++) 
	{
		var fields = entry_details.getElementsByTagName(field_types[i]);

		for (var j = 0; j < fields.length; j++) 
		{
			if (post_variables != "") 
			{
				post_variables += "&";
			}

			var field = fields[j];
			var field_details = get_field_details(field);

			if(field_details !== false)
			{
				post_variables += encodeURIComponent(field_details[0]) + "=" + encodeURIComponent(field_details[1]);
			}
		}
	}

	if ((xml_request.readyState == 4) || (xml_request.readyState == 0))
	{
		xml_request.open("POST", "sendform.php", true);
		xml_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xml_request.onreadystatechange = form_response;
		xml_request.send("f=" + form + "&" + post_variables);

		// disable the save button
		var submit_button = document.getElementById('btn_submit');

		submit_button.disabled = true;
		submit_button.value = "Processing Form...please wait...";
	}
}

function form_response()
{
	if (xml_request.readyState == 4) 
	{
		var message_field = document.getElementById('msg_details');
		var form_div = document.getElementById('form_details');

		var doc = xml_request.responseXML;

		if (doc == null) 
		{
			alert("There has been a problem sending your enquiry.");

			message_field.innerHTML = xml_request.responseText;
		}
		else 
		{
			var root_node = doc.documentElement;

			var status = root_node.getElementsByTagName('status')[0].firstChild.nodeValue;
			var message = root_node.getElementsByTagName('message')[0].firstChild.nodeValue;

			if (status == "success") 
			{
				// the entry was updated successfully
				//var id = root_node.getElementsByTagName('id')[0].firstChild.nodeValue;
				//var entry_id = document.getElementById('entry_id');
				//entry_id.value = id;

				/*var url_variables = parse_url_variables();

				var new_location = 'edit.php?t=' + url_variables['t'] + '&e=' + id;

				if (url_variables['search'] != '') 
				{
					new_location += '&search=' + url_variables['search'];
				}

				document.location.href = new_location;*/
				message_field.innerHTML = '';
				form_div.innerHTML = message;
				
			}
			else 
			{
				alert("There has been a problem sending your enquiry.");
				message_field.innerHTML = message;
			}

			
		}

		// enable the save button
		var save_button = document.getElementById('btn_submit');

		save_button.disabled = false;
		save_button.value = "Submit";
	}
}

function increase_price(name,value)
{
	
	var current = stripHTML(document.getElementById("fullPrice").innerHTML);
if(name.checked == true) {
var newprice = parseFloat(current)+parseFloat(value);
}else{
var newprice = parseFloat(current)-parseFloat(value);
}

	document.getElementById("fullPrice").innerHTML = Math.round(newprice * 100) / 100;
}

function stripHTML(oldString) {

   var newString = "";
   var inTag = false;
   for(var i = 0; i < oldString.length; i++) {
   
        if(oldString.charAt(i) == '<') inTag = true;
        if(oldString.charAt(i) == '>') {
              if(oldString.charAt(i+1)=="<")
              {
              		//dont do anything
	}
	else
	{
		inTag = false;
		i++;
	}
        }
   
        if(!inTag) newString += oldString.charAt(i);

   }

   return newString;
}

function get_field_details(field)
{
	switch (field.tagName)
	{
		case "INPUT":
			if((field.type.toUpperCase() == "CHECKBOX") && (field.checked === false))
			{
				return false;
			}
			else
			{
				var name = field.name;
				var value = field.value;
			}

			break;

		case "SELECT":
			var name = field.name;
			var selected_index = field.selectedIndex;
			var value = field.options[selected_index].value;

			break;

		case "TEXTAREA":
			var name = field.name;

			if (navigator.userAgent.indexOf("MSIE") >= 0)
			{
				var value = field.innerHTML;
			}
			else
			{
				var value = field.value;
			}

			break;
	}

	return new Array(name, value);
}