function resetTabColors(tab_name_array) {
	for (i in tab_name_array) {
		var tab = document.getElementById(i);
		if (tab) {
			if (tab_name_array[i] == 0) {
				tab.style.backgroundColor = "#FFFFFF";
			} else {
				tab.style.backgroundColor = "#A3C6CC";
			}
		}
	}
}

function showTab(listing_id) {
	var listings = $('listing_items').getElementsBySelector('[class="listings"]');
	var len = listings.length;
	for (var i = 0; i < len; i++) {
		if ($(listings[i]).id == listing_id) {
			$(listings[i]).setStyle({display:'inline'});
		} else {
			$(listings[i]).setStyle({display:'none'});
		}
	}
	
	var tabs = $('user_nav').getElementsBySelector('[title="tab"]');
	var len = tabs.length;
	for (var i = 0; i < len; i++) {
		if ($(tabs[i]).id == 'tab_' + listing_id) {
			$(tabs[i]).setStyle({backgroundColor:'#fff'});
		} else {
			$(tabs[i]).setStyle({backgroundColor:'#eee'});
		}
	}
}

function large_image_popup(url) {
	var w = window.open(url, 'image_zoom_in', 'scrollbars=no,menubar=no,height=600,width=600,resizable=yes,toolbar=no,location=no,status=no');
	return w;
}

function open_new_small_window(url) {
	var w = window.open(url, 'jaclo_window', 'scrollbars=yes,menubar=yes,height=800,width=800,resizable=yes,toolbar=yes,location=yes,status=yes');
	return w;
}

function changeFinishThumb(img) {
	$('finish_thumb').src = img;
}

function changeFinishDrop(id) {
	var length = $('finish_dropdown').options.length;
	var i;
	for (i=0; i<length; i++) {
		if ($('finish_dropdown').options[i].value == id) {
			$('finish_dropdown').options[i].selected = true;
		}
	}
}

function submitEmail() {
	var email = document.getElementById('register_email');
	
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z])+\.)+([a-zA-Z]{2,4})+$/; 
	if (!filter.test(email.value)) {
		alert("Please enter a valid email address");
		return false;
	}
		
	var post_body = 'email=' + email.value;
	var html_code = "";
	var dd = document.getElementById('register_form');
	
	new Ajax.Request('/account/process_register_email',{
		
		method:'post', 
		postBody:post_body,
		asynchronous:false, 
		
		onSuccess: function(t) {
			if (t.responseText == 'true') {
				html_code = "<p class='footer_message'>Thank you for submitting your email</p>";
			} else {
				html_code = "<p class='footer_message'>This email is already in our system</p>";
			}
			dd.innerHTML = html_code;
		},
		
		onFailure: function(t) {
			html_code = "<p class='footer_message'>This email is already in our system</p>";
			dd.innerHTML = html_code;
		}
	});
}

function add_to_project (id) {	
	var fid;
	var sid;
	
	if ($('finish_dropdown')) {
		fid = $('finish_dropdown').options[$('finish_dropdown').selectedIndex].value;
	} else {
		fid = 0;
	}
	
	if ($('spray_dropdown')) {
		sid = $('spray_dropdown').options[$('spray_dropdown').selectedIndex].value;
	} else {
		sid = 0;
	}
	
	$('project_response').innerHTML = 'Adding Product...';
	$('project_response').appear({ duration: 0.5, from: 0, to: 1 });
	var post_body = 'id=' + id + '&fid=' + fid + '&sid=' + sid;	
	new Ajax.Request('/my_project/add',{		
		method:'post', 
		postBody:post_body,
		asynchronous:true, 		
		onSuccess: function(t) {
			if (t.responseText != 'new_project') {
				$('project_response').innerHTML = t.responseText;
				get_project_count();
			} else {
				window.location = '/my_project/new_project';
			}
		},		
		onFailure: function(t) {
			$('project_response').innerHTML = 'An error occured, please try again.';
			$('project_response').appear({ duration: 0.5, from: 0, to: 1 });
		}
	});
}

function get_project_count () {
	new Ajax.Request('/my_project/count',{		
		method:'get',
		asynchronous:true, 		
		onSuccess: function(t) {
			if (t.responseText > 0) {
				$('pl').setStyle({color:'#009900'});
				$('pc').setStyle({color:'#009900'});
				$('pc').innerHTML = '(' + t.responseText + ')';
			} else {
				
			}
		}
	});
}

function update_project_finish (id) {
	var fid = $('finish_dropdown' + id).options[$('finish_dropdown' + id).selectedIndex].value;
	var post_body = 'id=' + id + '&fid=' + fid;	
	new Ajax.Request('/my_project/update_finish',{		
		method:'post', 
		postBody:post_body,
		asynchronous:true
	});
}

function remove_from_project (id) {	
	$('item' + id).setStyle({backgroundColor:'#f00'});
	$('item' + id).fade({ duration: 0.5, from: 1, to: 0 });
	var post_body = 'id=' + id;	
	new Ajax.Request('/my_project/remove',{		
		method:'post', 
		postBody:post_body,
		asynchronous:true,
		onSuccess: function(t) {
			get_project_count();
		}
	});
}

function printProject() {
	window.open('/my_project/print_project', 'project_print', '');
}

function updateProject() {
	$('project_form').submit();
}

function emailProject() {
	var form = document.createElement("form");
	form.setAttribute('method','post');
	form.setAttribute('action','/my_project/email_project');
	
	var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", 'to_email');
    hiddenField.setAttribute("value", $('to_email').value);
	
	form.appendChild(hiddenField);
	
	document.body.appendChild(form);
	form.submit();
}