var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

var http1 = createRequestObject(); 
var http2 = createRequestObject(); 
var http3 = createRequestObject(); 
var http4 = createRequestObject(); 
var http5 = createRequestObject(); 
var http6 = createRequestObject(); 
var http7 = createRequestObject(); 
var http8 = createRequestObject(); 
var http9 = createRequestObject(); 

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 



function clear_form_fields() {
	document.getElementById('username').value = '';
	document.getElementById('password').value = '';
}

function show_signin() {
	document.getElementById('signin').style.display = ''; 	
	document.getElementById('signin_createaccount_hover').style.display = '';
	set_signin_position();
	document.getElementById('signin_createaccount').style.display = 'none';					
}

function redraw_dressing_room(website) {					
	var my_date = new Date();
	var timestamp = my_date.getTime();

	performAJAXGetRequest(http7, website + "phpinc/draw_dressing_room.php?random="+timestamp, show_new_cart);
}

function show_new_cart(){
	if (http7.readyState == 4){ //Finished loading the response
		
		var response = http7.responseText;

		if (response != "") {				
			document.getElementById('dressing_room').innerHTML = response;
			show_dressing_room();
			setTimeout('hide_dressing_room()', 2000);
		}
	}
}

function show_dressing_room() {		
	document.getElementById('dressing_room').style.display = ''; 	
	document.getElementById('dressing_room_hover').style.display = '';
	set_dressingroom_position();
	document.getElementById('dressing_room_div').style.display = 'none';					
}

function hide_signin() {
	document.getElementById('signin').style.display = 'none'; 	
	document.getElementById('signin_createaccount_hover').style.display = 'none';
	document.getElementById('signin_createaccount').style.display = '';
}

function hide_dressing_room() {
	document.getElementById('dressing_room').style.display = 'none'; 	
	document.getElementById('dressing_room_hover').style.display = 'none';
	document.getElementById('dressing_room_div').style.display = '';
}

function set_dressingroom_position() {
	// get the top and left position
	var the_top_value = $('#dressing_room_div').position().top;
	var the_left_value = $('#dressing_room_hover').position().left;						

	// get the width and height
	var the_height_value = $('#dressing_room_div').height();
	var the_width_value = $('#dressing_room_div').width();

	var the_new_top = the_top_value + the_height_value + 8;
	var the_new_left = the_left_value + ($('#dressing_room_div').width() - 248);

	// now draw the menu div
	$("#dressing_room").css({top:the_new_top,left:the_new_left}); 						
	$("#dressing_room").width(250);						
}


function set_signin_position() {
	// get the top and left position
	var the_top_value = $('#signin_createaccount').position().top;
	var the_left_value = $('#signin_createaccount_hover').position().left;						

	// get the width and height
	var the_height_value = $('#signin_createaccount').height();
	var the_width_value = $('#signin_createaccount').width();

	var the_new_top = the_top_value + the_height_value + 8;
	var the_new_left = the_left_value;

	// now draw the menu div
	$("#signin").css({top:the_new_top,left:the_new_left}); 						
	$("#signin").width(300);

	
}

function show_grey_menu(section, subpage, the_section) {
	
	if (the_section != subpage) {
		document.getElementById('cows_menu_item_' + the_section).style.borderColor = '#c7c7c7';
		document.getElementById('cows_menu_item_div_' + the_section).style.color = '#000000';
	} 			
}

function show_blue_menu(section, menu_section) {
	document.getElementById('cows_menu_item_' + menu_section).style.borderColor = '#66a4f9';
	document.getElementById('cows_menu_item_div_' + menu_section).style.color = '#040f9c';
}

function check_account(website){
	var passedEmailAddress = document.getElementById('username').value;
	var passedPassword = document.getElementById('password').value;

	var the_date = new Date().getTime();				

	performAJAXGetRequest(http1, website + "phpinc/checkaccount.php?username="+passedEmailAddress+"&password="+passedPassword+"&random="+the_date, do_the_check);
}

function do_the_check(){
	if (http1.readyState == 4){ //Finished loading the response
		var response = http1.responseText;
		if (response != "") {					
			// response will be 0 if all is well
			if (response == 0) {
				// hide both the errors

				location.reload(true);
				

			// response will be 1 if email address and password don't match
			} else if (response == 1) {
				// hide the not active error
				// show the matching error
				document.getElementById('username_and_password_error_empty').style.display = "";
				document.getElementById('username_and_password_error').style.display = "";

				document.getElementById('not_active_error_empty').style.display = "none";
				document.getElementById('not_active_error').style.display = "none";

				document.getElementById('empty_error_empty').style.display = "none";
				document.getElementById('empty_error').style.display = "none";
			// response will be 2 if user exists but is inactive
			} else if (response == 2) {
				// hide the matching error
				// show the not active error
				document.getElementById('username_and_password_error_empty').style.display = "none";
				document.getElementById('username_and_password_error').style.display = "none";

				document.getElementById('empty_error_empty').style.display = "none";
				document.getElementById('empty_error').style.display = "none";

				document.getElementById('not_active_error_empty').style.display = "";
				document.getElementById('not_active_error').style.display = "";
			} else if (response == 3) {								
				document.getElementById('username_and_password_error_empty').style.display = "none";
				document.getElementById('username_and_password_error').style.display = "none";

				document.getElementById('not_active_error_empty').style.display = "none";
				document.getElementById('not_active_error').style.display = "none";

				document.getElementById('empty_error_empty').style.display = "";
				document.getElementById('empty_error').style.display = "";
			}									
		}
	}
}
