var row_count = 0;

function AddLoadEvent(func) {
	var old_onload = window.onload;
	
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function () {
			if (old_onload) {
				old_onload ();
			}
			func ();
		}
	}
}

function RemoveJavaScriptError () {
	var el = document.getElementById ("javascript_error");
	el.parentNode.removeChild (el);
}

function SetupForm () {
	var form = document.createElement ("form");
	form.setAttribute ("name", "quote_form");
	form.setAttribute ("id", "quote_form");
	form.setAttribute ("action", "");
	
	var parent = document.getElementById ("quote_container");
	parent.appendChild (form);
}

function SetupContactForm () {	
	var name_cell = document.createElement ("th");
	var name_input_cell = document.createElement ("td");
	
	var name_input = document.createElement ("input");
	name_input.setAttribute ("type", "text");
	name_input.setAttribute ("name", "client_name");
	name_input.setAttribute ("size", "60");
	name_input.setAttribute ("id", "client_name");
	name_cell.appendChild (document.createTextNode ("Name: "));
	name_cell.setAttribute ("style", "width: 100px;");
	name_input_cell.appendChild (name_input);
	var name_row = document.createElement ("tr");
	name_row.appendChild (name_cell);
	name_row.appendChild (name_input_cell);
	
	var email_cell = document.createElement ("th");
	var email_input_cell = document.createElement ("td");
	
	var email_input = document.createElement ("input");
	email_input.setAttribute ("type", "text");
	email_input.setAttribute ("name", "client_email");
	email_input.setAttribute ("id", "client_email");
	email_input.setAttribute ("size", "60");
	email_cell.appendChild (document.createTextNode ("Email: "));
	email_cell.setAttribute ("style", "width: 100px;");
	email_input_cell.appendChild (email_input);
	var email_row = document.createElement ("tr");
	email_row.appendChild (email_cell);
	email_row.appendChild (email_input_cell);
	
	var additional_info_cell = document.createElement ("th");
	var additional_info_input_cell = document.createElement ("td");
	
	var additional_info_input = document.createElement ("textarea");
	additional_info_input.setAttribute ("name", "client_info");
	additional_info_input.setAttribute ("id", "client_info");
	additional_info_input.setAttribute ("rows", "8");
	additional_info_input.setAttribute ("cols", "90");
	additional_info_cell.appendChild (document.createTextNode ("Additional Info: "));
	additional_info_cell.setAttribute ("style", "width: 100px;");
	additional_info_input_cell.appendChild (additional_info_input);
	var info_row = document.createElement ("tr");
	info_row.appendChild (additional_info_cell);
	info_row.appendChild (additional_info_input_cell);
	
	var table = document.createElement ("table");
	table.setAttribute ("style", "width: 100%; margin-bottom: 10px;");
	table.setAttribute ("width", "100%");
	
	var tbody = document.createElement ("tbody");
	tbody.setAttribute ("id", "contact_table");
	tbody.appendChild (name_row);
	tbody.appendChild (email_row);
	tbody.appendChild (info_row);
	
	table.appendChild (tbody);
	
	var form = document.getElementById ("quote_form");
	form.appendChild (table);
}

function SetupQuoteForm () {
	var empty_cell = document.createElement ("th");
	empty_cell.appendChild (document.createTextNode (" "));
	
	var blind_type_field_display = document.createElement ("th");
	blind_type_field_display.setAttribute ("style", "width: 60px;");
	blind_type_field_display.appendChild (document.createTextNode ("Type of Blind"));
	
	var room_field_display = document.createElement ("th");
	room_field_display.appendChild (document.createTextNode ("Location"));
	var new_row = document.createElement ("tr");

	var width_field_display = document.createElement ("th");
	width_field_display.appendChild (document.createTextNode ("Blind Width (Inches)"));
	
	var height_field_display = document.createElement ("th");
	height_field_display.appendChild (document.createTextNode ("Blind Height (Inches)"));
	
	var mount_type_field_display = document.createElement ("th");
	mount_type_field_display.appendChild (document.createTextNode ("Mounting "));
	
	var mount_type_help_link = document.createElement ("a");
	mount_type_help_link.setAttribute ("href", "#");
	mount_type_help_link.onclick = ShowMountHelp;
	mount_type_help_link.appendChild (document.createTextNode ("[?]"));
	
	mount_type_field_display.appendChild (mount_type_help_link);
	
	var header_row = document.createElement ("tr");
	header_row.appendChild (empty_cell);
	header_row.appendChild (blind_type_field_display);
	header_row.appendChild (room_field_display);
	header_row.appendChild (width_field_display);
	header_row.appendChild (height_field_display);
	header_row.appendChild (mount_type_field_display);
	
	var tbody = document.createElement ("tbody");
	tbody.appendChild (header_row);
	tbody.setAttribute ("id", "quote_table");
	
	var table = document.createElement ("table");
	table.appendChild (tbody);
	table.setAttribute ("style", "width: 100%;");
	table.setAttribute ("width", "100%"); // A kludge fix for that POS browser IE
	
	var form = document.getElementById ("quote_form");
	form.appendChild (table);
	
	CreateFooterRow ();
}

function CreateRow () {
	var table = document.getElementById ("quote_table");
	
	var blind_count_cell = document.createElement ("th");
	blind_count_cell.appendChild (document.createTextNode ("Blind #"+(row_count + 1)));
	
	var blind_type_cell = document.createElement ("td");
	var blind_type_drop = CreateBlindTypeDropDown ();
	blind_type_drop.setAttribute ("name", row_count+"[type]");
	blind_type_drop.setAttribute ("id", "type_"+row_count);
	blind_type_cell.appendChild (blind_type_drop);
	
	var room_cell = document.createElement ("td");
	var room_drop = CreateLocationDropDown ();
	room_drop.setAttribute ("name", row_count+"[locale]");
	room_drop.setAttribute ("id", "locale_"+row_count);
	room_cell.appendChild (room_drop);
	
	var width_cell = document.createElement ("td");
	var width_input = document.createElement ("input");
	width_input.setAttribute ("name", row_count+"[width]");
	width_input.setAttribute ("id", "width_"+row_count);
	width_input.setAttribute ("type", "text");
	width_cell.appendChild (width_input);
	
	var height_cell = document.createElement ("td");
	var height_input = document.createElement ("input");
	height_input.setAttribute ("name", row_count+"[height]");
	height_input.setAttribute ("id", "height_"+row_count);
	height_input.setAttribute ("type", "text");
	height_cell.appendChild (height_input);
	
	var mount_cell = document.createElement ("td");
	
	try { // Kludge for POS browser IE
		var inside_mount = document.createElement('<input type="radio" name="'+row_count+"[mount]"+'" />');
	} catch (err) {
		var inside_mount = document.createElement ("input");
		inside_mount.setAttribute ("name", row_count+"[mount]");
		inside_mount.setAttribute ("type", "radio");
	}
	inside_mount.setAttribute ("value", "0");
	inside_mount.setAttribute ("id", "mount_"+row_count);
	
	try {
		var outside_mount = document.createElement('<input type="radio" name="'+row_count+"[mount]"+'" />');
	} catch (err) {
		var outside_mount = document.createElement ("input");
		outside_mount.setAttribute ("name", row_count+"[mount]");
		outside_mount.setAttribute ("type", "radio");
	}
	outside_mount.setAttribute ("value", "1");
	
	mount_cell.appendChild (inside_mount);
	mount_cell.appendChild (document.createTextNode ("Inside"));
	mount_cell.appendChild (document.createElement ("br"));
	mount_cell.appendChild (outside_mount);
	mount_cell.appendChild (document.createTextNode ("Outside"));
	
	var new_row = document.createElement ("tr");
	new_row.setAttribute ("id", "quote_row_"+row_count);
	new_row.appendChild (blind_count_cell);
	new_row.appendChild (blind_type_cell);
	new_row.appendChild (room_cell);
	new_row.appendChild (width_cell);
	new_row.appendChild (height_cell);
	new_row.appendChild (mount_cell);
	
	row_count++;
	
	var footer = document.getElementById ("quote_footer");
	
	table.insertBefore (new_row, footer);
}

function CreateBlindTypeDropDown () {
	var blind_types = new Array ("Click to Change", "Vertical Blind", "Horizontal Blind", "Shutters", "Roller Blind", "Panel Track", "Vienna");
	var drop_down = document.createElement ("select");
	
	for (var i = 0; i < blind_types.length; i++) {
		var new_type = document.createElement ("option");
		
		new_type.setAttribute ("value", ""+i);
		new_type.appendChild (document.createTextNode (blind_types[i]));
		
		drop_down.appendChild (new_type);
	}
	
	return drop_down;
}

function CreateLocationDropDown () {
	var locations = new Array ("Click To Change", "Bathroom", "Living Room", "Family Room", "Master Bedroom", "Ensuite", "Children's Bedroom", "Garage", "Dining Room", "Guest Room", "Office", "Kitchen", "Patio door", "Basement", "Other");
	var drop_down = document.createElement ("select");
	
	for (var i = 0; i < locations.length; i++) {
		var new_type = document.createElement ("option");
		
		new_type.setAttribute ("value", ""+i);
		new_type.appendChild (document.createTextNode (locations[i]));
		
		drop_down.appendChild (new_type);
	}
	
	return drop_down;
}

function CreateFooterRow () {
	var footer_row = document.createElement ("tr");
	footer_row.setAttribute ("id", "quote_footer");
	
	var footer_cell = document.createElement ("td");
	footer_cell.setAttribute ("style", "text-align: right;");
	footer_cell.setAttribute ("colspan", "6");
	footer_row.appendChild (footer_cell);
	
	var add_row_link = document.createElement ("a");
	add_row_link.setAttribute ("href", "#");
	add_row_link.onclick = CreateRow;
	add_row_link.appendChild (document.createTextNode ("Click To Add Blind"));
	
	footer_cell.appendChild (add_row_link);
	
	var submit_button = document.createElement ("input");
	submit_button.setAttribute ("name", "submit");
	submit_button.setAttribute ("id", "form_submit_button");
	submit_button.setAttribute ("type", "button");
	submit_button.onclick = SendQuoteRequest;
	submit_button.setAttribute ("value", "Submit");
	
	var reset_button = document.createElement ("input");
	reset_button.setAttribute ("type", "reset");
	reset_button.setAttribute ("value", "Reset");
	
	var restart_button = document.createElement ("input");
	restart_button.setAttribute ("type", "button");
	restart_button.setAttribute ("value", "Start Over");
	restart_button.onclick = Restart;
	
	var submit_cell = document.createElement ("td");
	submit_cell.setAttribute ("colspan", "6");
	submit_cell.setAttribute ("style", "text-align: center;");
	submit_cell.appendChild (submit_button);
	submit_cell.appendChild (document.createTextNode (" "));
	submit_cell.appendChild (reset_button);
	submit_cell.appendChild (document.createTextNode (" "));
	submit_cell.appendChild (restart_button);
	
	var submit_row = document.createElement ("tr");
	submit_row.appendChild (submit_cell);
	
	var tbody = document.getElementById ("quote_table");
	tbody.appendChild (footer_row);
	tbody.appendChild (submit_row);
}

function Trim (s) {
	return s.replace(/^\s*/, "").replace(/\s*$/, "");
}

function CheckForm () {
	var quote_form = document.getElementById ("quote_form");
	var email_regex = /^([0-9a-zA-Z]+[-._+&amp;])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$/;
	
	if (Trim (quote_form["client_name"].value).length == 0) {
		alert ("Please type in your name.");
		quote_form.client_name.focus ();
		
		return false;
	}
	
	if (!email_regex.test (quote_form["client_email"].value)) {
		alert ("Your email address is not valid.");
		quote_form.client_email.focus ();
		
		return false;
	}
	
	for (var i = 0; i < row_count; i++) {
		if (document.getElementById ("type_"+i).selectedIndex == 0) {
			alert ("Please choose a type of blind for Blind #"+(i+1));
			quote_form[i+"[type]"].focus ();
			return false;
		}
		
		if (document.getElementById ("locale_"+i).selectedIndex == 0) {
			alert ("Please choose a location for Blind #"+(i+1));
			quote_form[i+"[locale]"].focus ();
			
			return false;
		}
		
		if (Trim (document.getElementById ("width_"+i).value).length == 0) {
			alert ("Please type in the width for Blind #"+(i+1));
			quote_form[i+"[width]"].focus ();
			return false;
		}
		
		if (Trim (document.getElementById ("height_"+i).value).length == 0) {
			alert ("Please type in the height for Blind #"+(i+1));
			quote_form[i+"[height]"].focus ();
			return false;
		}
	}
	
	return true;
}

function Restart () {
	var table = document.getElementById ("quote_table");
	
	for (var i = row_count - 1; i > 0; i--) {
		var row = document.getElementById ("quote_row_"+i);
		
		table.removeChild (row);
	}
	
	row_count = 1;
}

function ShowMountHelp ()
{
	my_window = window.open ("mount_help.htm", "mount_help", "width=400,height=300");
	my_window.moveTo ((screen.width / 2) - (300 / 2), (screen.height / 2) - (300 / 2));
}

function PreparePostData ()
{
	var quote_form = document.getElementById ("quote_form");
	var post_data = "";
	
	post_data += "client_name="+quote_form["client_name"].value;
	post_data += "&";
	post_data += "client_email="+quote_form["client_email"].value;
	post_data += "&";
	post_data += "client_info="+quote_form["client_info"].value;
	post_data += "&";
	
	for (var i = 0; i < row_count; i++) {
		if (i != 0) {
			post_data += "&";
		}
		
		post_data += i+"%5Btype%5D="+document.getElementById ("type_"+i).selectedIndex;
		post_data += "&";
		post_data += i+"%5Blocale%5D="+document.getElementById ("locale_"+i).selectedIndex;
		post_data += "&";
		post_data += i+"%5Bwidth%5D="+document.getElementById ("width_"+i).value;
		post_data += "&";
		post_data += i+"%5Bheight%5D="+document.getElementById ("height_"+i).value;
		post_data += "&";
		post_data += i+"%5Bmount%5D="+(document.getElementById ("mount_"+i).checked == true ? 0 : 1);
	}
	
	return post_data;
}

function SendQuoteRequest () {
	if (CheckForm ()) {
		var request = GetHxrObject ();
		
		if (request) {
			request.open("POST", "http://www.rainbowblindsandshutters.com/quote_request.php", true); // The last parameter is set to true for async, false for sync
			request.onreadystatechange = function () { UpdatePage(request); };
			// send the request
			request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			request.send(PreparePostData ());
		} else {
			alert ("Your browser does not support AJAX. If you are using a very old browser, please consider upgrading it.");
		}
	}
}

function GetHxrObject () {
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest ();
	} else if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject ("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhr = new ActiveXObject ("Microsoft.XMLHTTP");
			} catch (e) {
				xhr = false
			}
		}
	}
	
	return xhr;
}

function UpdatePage (request) {
	if (request.readyState == 4) {// Request object has the data
		if (request.status == 200) {// Request ran okay
			alert (request.responseText);
		}
	}
}

AddLoadEvent (RemoveJavaScriptError);
AddLoadEvent (SetupForm);
AddLoadEvent (SetupContactForm);
AddLoadEvent (SetupQuoteForm);
AddLoadEvent (CreateRow);

