var column_to_id_relation = new Array();
var column_to_id_relation_2 = new Array();

function show(id) 
{
	if(document.getElementById(id).style.display == 'none')
	{
		document.getElementById(id).style.display = 'block';
	}
	else
	{
		document.getElementById(id).style.display = 'none';
	}
}

function send_password()
{
	$.ajax ({
			   type: "POST",
			   url: "send_password.php",
			   data: "email=" + document.getElementById('forgotten_pass').value,
			   success: function()
			   {
				  document.getElementById('forgotten_password').innerHTML = 'Your email was sent!';  
			   }
		   });
}

function check_login()
{
	var num_bids; var response;
	

		$.ajax ({
			   type: "POST",
			   url: "login_handler.php",
			   data: "user=" + document.getElementById('login_name').value + "&pass=" + document.getElementById('login_password').value,
			   async: false
	        });
			
	$.ajax ({
			   type: "POST",
			   url: "get_num_bids.php",
			   success: function(msg)
			   {
				  num_bids = msg; 
			   },
			   async: false
		   });

		   
			//alert(num_bids);
	$.ajax ({
			   type: "POST",
			   url: "login_handler.php",
			   data: "user=" + document.getElementById('login_name').value + "&pass=" + document.getElementById('login_password').value,
			   success: function(msg)
			   {
				  document.getElementById('login_response').innerHTML = msg;
				  if(!msg)
				  {
					  if(num_bids < 10)
					  {
						  window.location = "purchase_bids.php"; 
					  }
					 
					  else
					  {
						   window.location = "index.php";
					  }
				  }
				  if(msg.indexOf('admin') != -1)
				  {
					  //window.location = 'admin.php';
					  window.open('https://www.bidmos.com/adminn/admin.php','mywindow','width=700,height=800');
				  }

			   }
		   }); 
}

function send_question()
{
	if($.trim(document.getElementById('question_sender').value).length == 0)
	{
		alert('Name is empty!');
		return;
	}
	if($.trim(document.getElementById('question_sender_email').value).length == 0)
	{
		alert('Email is empty!');
		return;
	}
	if($.trim(document.getElementById('send_question').value).length == 0)
	{
		alert('Description is empty!');
		return;
	}
	$.ajax ({
			   type: "POST",
			   url: "contact_handler.php",
			   data: "question=" + document.getElementById('send_question').value + "&question_sender=" + document.getElementById('question_sender').value +
			   		 "&question_sender_email=" + document.getElementById('question_sender_email').value,
			   success: function()
			   {
				   document.getElementById('question_response').innerHTML = '';
				   document.getElementById('question_response').innerHTML = 'Your question was sent!<a href = "contact.php">New Question</a>';
			   }
			});
}

function key_pressed(e)
{
   var unicode = e.keyCode? e.keyCode : e.charCode;
   if(unicode == '37')
   {
	   scroll_backword();
   }
   if(unicode == '39')
   {
	   scroll_forward();
   }
   if(unicode == '13')
   {
	   check_login();
   }
}

function fetch_upper()
{
	$.ajax
    ({
		  url: "load_data.php",
		  dataType: "xml",
		  success: function(xml)
		  {
			 for(var i = 0; i <= 25; i++)
			 {
				 var product_name = $(xml).find('product_name_' + i).text();
				 document.getElementById('product_name_' + i).innerHTML = product_name;
				 var product_id = $(xml).find('product_id_' + i).text();
				 column_to_id_relation[i] = product_id;
				 var image = $(xml).find('image_' + i).text();
				 document.getElementById('image_' + i).src = image;
			 }
		  },
		  async:false   
    });
}

function fetch_middle()
{
	$.ajax
    ({
		  url: "load_data.php",
		  dataType: "xml",
		  success: function(xml)
		  {
			 for(var i = 0; i <= 25; i++)
			 {
				 for(var j = 0; j <= 25; j++) 
				 {
					 var product_id = $(xml).find('product_id_' + j).text();
					 if(column_to_id_relation[i] == product_id)
					 {
						 var time = $(xml).find('time_' + j).text();
						 document.getElementById('time_' + i).innerHTML = time;
						 var price = $(xml).find('price_' + j).text();
						 document.getElementById('price_' + i).innerHTML = price;
						 var bidder = $(xml).find('bidder_' + j).text();
						 document.getElementById('bidder_' + i).innerHTML = bidder;
					 }
				 }

			 }
		  }   
    });
}


function fetch_lower()
{
	$.ajax
    ({
		  url: "load_data.php",
		  dataType: "xml",
		  success: function(xml)
		  {
			 for(var i = 0; i <= 25; i++)
			 {
				 var product_id = $(xml).find('product_id_' + i).text();
				if(document.getElementById('bid_button_' + i) != null)
				{
				 document.getElementById('bid_button_' + i).alt = product_id;
				}
	   		}
         }  
   }); 
}



function fetch_upper_2(id)
{
	$.ajax
    ({
		  type: "GET",
		  url: "load_single_item.php",
		  dataType: "xml",
		  data: "id=" + id,
		  success: function(xml)
		  {
			 for(var i = 0; i <= 25; i++)
			 {
				 var product_name_show = $(xml).find('product_name_show_' + i).text();
				 document.getElementById('product_name_show_' + i).innerHTML = product_name_show;
				 var product_id_show = $(xml).find('product_id_show_' + i).text();
				 column_to_id_relation_2[i] = product_id_show;
				 var image_show = $(xml).find('image_show_' + i).text();
				 document.getElementById('image_show_' + i).src = image_show;
			 }
		  },
		  async:false   
    });
}

function fetch_middle_2(id)
{
	$.ajax
    ({
		  type: "GET",
		  url: "load_single_item.php",
		  dataType: "xml",
		  data: "id=" + id,
		  success: function(xml)
		  {
			 for(var i = 0; i <= 25; i++)
			 {
				 for(var j = 0; j <= 25; j++) 
				 {
					 var product_id_show = $(xml).find('product_id_show_' + j).text();
					 if(column_to_id_relation_2[i] == product_id_show)
					 {
						 var time_show = $(xml).find('time_show_' + j).text();
						 document.getElementById('time_show_' + i).innerHTML = time_show;
						 
						 var price_show = $(xml).find('price_show_' + j).text();
						 document.getElementById('price_show_' + i).innerHTML = price_show;
						 
						 var bidder_show = $(xml).find('bidder_show_' + j).text();
						 document.getElementById('bidder_show_' + i).innerHTML = bidder_show;

						 var save_show = $(xml).find('save_show_' + j).text();
						 document.getElementById('save_show_' + i).innerHTML = save_show;
						 
					 }
				 }

			 }
		  }   
    });
}


function fetch_lower_2(id)
{
	$.ajax
    ({
		  type: "GET",
		  url: "load_single_item.php",
		  dataType: "xml",
		  data: "id=" + id,
		  success: function(xml)
		  {
			 for(var i = 0; i <= 25; i++)
			 {
				 var product_id_show = $(xml).find('product_id_show_' + i).text();
				 if(document.getElementById('bid_button_show_' + i) != null && document.getElementById('anchor_buy_show_' + i) != null ) 
				 {
					document.getElementById('bid_button_show_' + i).alt = product_id_show;
					document.getElementById('anchor_buy_show_' + i).href = "buy_product.php?id=" + product_id_show;
				}
           }
         }  
   }); 
}
