function isNumber(n) {
   return !isNaN(parseFloat(n)) && isFinite(n);
}


function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function showResult(xmlHttp, id)
{
	if (xmlHttp.readyState == 4)
	{
		var response = xmlHttp.responseText;

		id.innerHTML = unescape(response);
	}
}

function state_box(country, user_id)
{
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url    = relative_path + 'ajax_files/states_box.php';
	var action    = url + '?country_id=' + country.value;

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4)
		{
			var response = xmlHttp.responseText;
			document.getElementById('stateBox').innerHTML = response;
		}
	};
	xmlHttp.open("GET", action, true);
	xmlHttp.send(null);
}

function shipping_calculator(is_submit)
{
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url    = relative_path + 'ajax_files/shipping_calculator.php';
	var action = url + '?sc_item_id=' + document.getElementById("sc_item_id").value + '&sc_quantity=' + document.getElementById("sc_quantity").value +
		'&sc_country=' + document.getElementById("sc_country").value;

	if (document.getElementById("sc_state"))
	{
		action = action + '&sc_state=' + document.getElementById("sc_state").value;
	}

	if (document.getElementById("sc_zip_code"))
	{
		action = action + '&sc_zip_code=' + document.getElementById("sc_zip_code").value;
	}

	if (document.getElementById("sc_carrier"))
	{
		action = action + '&sc_carrier=' + document.getElementById("sc_carrier").value;
	}

	if (is_submit == 1)
	{
		action = action + '&form_calculate_postage=1';
	}

	document.getElementById('shCalcBox').innerHTML = '<table width="100%" border="0" cellspacing="2" cellpadding="3" class="border">' +
		'<tr><td class="c3" colspan="2">' + sc_title +'</td></tr>' +
		'<tr class="c5">' +
		'	<td><img src="themes/' + site_theme + '/img/pixel.gif" width="150" height="1"></td>' +
		'	<td width="100%"><img src="themes/' + site_theme + '/img/pixel.gif" width="1" height="1"></td>' +
		'</tr>' +
		'<tr><td height="100" align="center" colspan="2"><img src="images/loading-ajax.gif"></td></tr></table>';
		//alert(action);

	xmlHttp.onreadystatechange = function() {showResult(xmlHttp, document.getElementById("shCalcBox"));};
	xmlHttp.open("GET", action, true);
	xmlHttp.send(null);
}

function delete_media_async(box_id, media_type, auction_id) {
	// first we remove the entry from the page
	var file_name = document.getElementById('hidden_'+box_id).value;

	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url    = relative_path + 'ajax_files/upload_file.php';
   var params = 'do=remove&file_name=' + file_name + '&auction_id=' + auction_id;
	var action = url + '?' + params;

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4)
		{
			var response = xmlHttp.responseText;
			alert(response);

			document.getElementById('box_'+box_id).innerHTML = '';
			document.getElementById('box_'+box_id).className = 'thumbnail_display_empty';
			document.getElementById('hidden_'+box_id).value='';

			var nb_uploads = document.getElementById('nb_uploads_' + media_type).value;
			nb_uploads--;
			
			document.getElementById('nb_uploads_' + media_type).value = nb_uploads;

			document.getElementById('btn_upload_' + media_type).disabled = false;
			document.getElementById('item_file_upload_' + media_type).disabled = false;
			document.getElementById('item_file_url_' + media_type).disabled = false;
			document.getElementById('item_file_embed_' + media_type).disabled = false;
		}
	};
	
	xmlHttp.open("POST", action, true);
   //Send the proper header information along with the request
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", params.length);
   xmlHttp.setRequestHeader("Connection", "close");       
   xmlHttp.send(null);

}

function upload_media_async(form_name, media_type, file_path, file_url, file_embed, nb_uploads, max_uploads, upload_id) {	
	var radio_buttons = new Array();
	for (var i=0; i<form_name.length; i++)
	{
		if (form_name.elements[i].type == 'radio' && form_name.elements[i].checked == true)
		{
			radio_buttons[form_name.elements[i].name] = form_name.elements[i].value;
		}
	}

	jsHttp = new JsHttpRequest();
	xmlHttp=GetXmlHttpObject();
	
	if (jsHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var media_name = '';
	switch (media_type)
	{
		case 1:
			media_name = 'ad_image';
			break;
		case 2:
			media_name = 'ad_video';
			break;
		case 3:
			media_name = 'ad_dd';
			break;		
	}
	
	media_box_name = 'display_media_boxes_' + media_name;

	file_embed = base64Encode(file_embed);
	var url    = relative_path + 'ajax_files/upload_file.php';
   var params = 'do=add&media_type=' + media_type + '&file_url=' + file_url +
		'&file_embed=' + file_embed + '&nb_uploads=' + nb_uploads + '&upload_id=' + upload_id
	var action = url + '?' + params;

	var thumbnail_div = document.getElementById('display_media_boxes_' + media_name);
	var new_content = document.createElement('div');
	
	if (file_embed != '')
	{
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4)
			{
				var response = xmlHttp.responseText;
				results = response.split('|');

				if (results[5] != '')
				{
					alert (results[5]);
				}
				else
				{
					new_content.innerHTML = results[3];

					while (new_content.firstChild) {
						thumbnail_div.appendChild(new_content.firstChild);
					}

					var hidden_div = document.getElementById('hidden_media_boxes');
					var hidden_content = document.createElement('div');
					hidden_content.innerHTML = '<input type="hidden" name="' + results[1] + '[]" id="hidden_' + results[2] + '" value="' + results[4] + '">';

					while (hidden_content.firstChild) {
						hidden_div.appendChild(hidden_content.firstChild);
					}
					nb_uploads++;
				}

				document.getElementById('div_file_' + media_type).innerHTML = document.getElementById('div_file_' + media_type).innerHTML;
				document.getElementById('item_file_url_' + media_type).value = '';
				document.getElementById('item_file_embed_' + media_type).value = '';

				if (nb_uploads>=max_uploads)
				{
					document.getElementById('btn_upload_' + media_type).disabled = true;
					document.getElementById('item_file_upload_' + media_type).disabled = true;
					document.getElementById('item_file_url_' + media_type).disabled = true;
					document.getElementById('item_file_embed_' + media_type).disabled = true;
				}
				document.getElementById('nb_uploads_' + media_type).value = nb_uploads;
			}
		};
		xmlHttp.open("POST", action, true);
      //Send the proper header information along with the request
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", params.length);
      xmlHttp.setRequestHeader("Connection", "close");      
		xmlHttp.send(null);
	}
	else
	{
		//jQuery.blockUI({ message: '<p style="padding: 10px; font-size: 16px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold;">' + upl_progress_msg + '</p>' });
		
		var image_loading = document.createElement('div');
		image_loading.innerHTML = '<img src="' + relative_path + 'images/loading-media.gif">';
		thumbnail_div.appendChild(image_loading); 	
		
		jsHttp.onreadystatechange = function() {
			if (jsHttp.readyState == 4)
			{
				var response = jsHttp.responseText;
				results = response.split('|');

				if (results[5] != '')
				{
					thumbnail_div.removeChild(image_loading);
					alert (results[5]);
					//setTimeout(jQuery.unblockUI, 500);
				}
				else
				{
					new_content.innerHTML = results[3];

					while (new_content.firstChild) {
						thumbnail_div.replaceChild(new_content.firstChild, image_loading);
					}
					//setTimeout(jQuery.unblockUI, 500);

					var hidden_div = document.getElementById('hidden_media_boxes');
					var hidden_content = document.createElement('div');
					hidden_content.innerHTML = '<input type="hidden" name="' + results[1] + '[]" id="hidden_' + results[2] + '" value="' + results[4] + '">';

					while (hidden_content.firstChild) {
						hidden_div.appendChild(hidden_content.firstChild);
					}
					nb_uploads++;
				}

				document.getElementById('div_file_' + media_type).innerHTML = document.getElementById('div_file_' + media_type).innerHTML;
				document.getElementById('item_file_url_' + media_type).value = '';
				document.getElementById('item_file_embed_' + media_type).value = '';

				if (nb_uploads>=max_uploads)
				{
					document.getElementById('btn_upload_' + media_type).disabled = true;
					document.getElementById('item_file_upload_' + media_type).disabled = true;
					document.getElementById('item_file_url_' + media_type).disabled = true;
					document.getElementById('item_file_embed_' + media_type).disabled = true;
				}
				document.getElementById('nb_uploads_' + media_type).value = nb_uploads;		
				
				for (var i=0; i<form_name.length; i++)
				{
					if (form_name.elements[i].type == 'radio' && radio_buttons[form_name.elements[i].name] == form_name.elements[i].value)
					{
						form_name.elements[i].checked = true;
					}
				}						
			}			
		};
		jsHttp.open("POST", action, true);
		jsHttp.send( {file: file_path} );
	}
}

function select_category(category_id, box_id, prefix, reverse, click_select, listing_type, list_in)
{
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url    = relative_path + 'ajax_files/select_category.php';
	var action    = url + '?category_id=' + category_id + '&target_box_id=' + box_id +
	'&prefix=' + prefix + '&reverse_categories=' + reverse + '&click_select=' + click_select + 
	'&listing_type=' + listing_type + '&list_in=' + list_in;

	//		xmlHttp.onreadystatechange = function() { showResult(xmlHttp, box_id); };
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4)
		{
			var response = xmlHttp.responseText;
			//document.getElementById(box_id).innerHTML = response;
			if (response.indexOf('change_category') == 0 && click_select != true) 
			{ 
				eval(response); 
			} 
			else 
			{ 
				document.getElementById(box_id).innerHTML = response; 
			}
		}
	};
	xmlHttp.open("GET", action, true);
	xmlHttp.send(null);
}

function change_category(category_id, prefix, reverse)
{
	var category_name = 'category_id';
	if (prefix != 'main_')
	{
		category_name = 'addl_category_id';
	}

	document.getElementById(category_name).value = category_id;
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url    = relative_path + 'ajax_files/change_category.php';
	var action    = url + '?category_id=' + category_id + '&reverse=' + reverse;

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4)
		{
			var response = xmlHttp.responseText;
			document.getElementById(prefix+'category_display').innerHTML = response;
		}
	};
	xmlHttp.open("GET", action, true);
	xmlHttp.send(null);

	document.getElementById(prefix+'category_field').innerHTML = '';
}

//function date_countdown(end_time) 
//{	
//	var event = new Date(end_time * 1000);
//	var now = new Date();
//	var output = '';
//	
//	var minute = 60;
//	var hour = 60 * minute;
//	var day = 24 * hour;
//	
//	var time_left = (event - now)/1000;
//	time_left = Math.floor(time_left);
//
//	if (end_time > 0)
//	{
//		if (time_left < 0)
//		{	
//			output = '<span class="redfont">' + closed_msg + '</span>';
//		}
//		else
//		{
//			var days_left = Math.floor(time_left/day);
//	
//			var hours = time_left - (days_left * day);
//			var hours_left = Math.floor(hours/hour);
//	
//			var minutes = hours - (hours_left * hour);
//			var minutes_left = Math.floor(minutes/minute);
//	
//			var seconds = minutes - (minutes_left * minute);
//			var seconds_left = Math.floor(seconds);
//		
//			output = ((days_left>0) ? days_left + ' ' + ((days_left==1) ? day_msg : days_msg) + ', ' : '') +
//				((hours_left>0 || days_left>0) ? hours_left + h_msg : '') + 
//				' ' + minutes_left + m_msg + ' ' + seconds_left + s_msg;
//		}
//		
//		return output;
//	}
//	else
//	{
//		return na_msg;
//	}
//}
//
//var refresh_counter = 20; // will only query the database every 10 seconds
//function refresh_countdown(id, auction_id, end_time)
//{
//	xmlHttpRf=GetXmlHttpObject();
//
//	if(document.getElementById(id))
//	{
//
//		if (xmlHttpRf==null)
//		{
//			alert ("Browser does not support HTTP Request");
//			return;
//		}				
//	
//		var url    = relative_path + 'ajax_files/refresh_countdown.php';
//		var action    = url + '?auction_id=' + auction_id + '&refresh_counter=' + refresh_counter + '&end_time=' + end_time;	
//		
//		refresh_counter--;
//		
//		if (refresh_counter < 0)
//		{
//			refresh_counter = 20;
//			
//			xmlHttpRf.onreadystatechange = function() {
//				if (xmlHttpRf.readyState == 4)
//				{
//					var response = xmlHttpRf.responseText;
//					
//					results = response.split('|');
//						
//					end_time = results[0];
//					
//					document.getElementById(id).innerHTML = results[1];
//				}
//			};
//			
//			xmlHttpRf.open("GET", action, true);
//			xmlHttpRf.send(null);
//		}	
//		else
//		{
//			document.getElementById(id).innerHTML = date_countdown(end_time);
//		}
//		
//		setTimeout(function(){refresh_countdown(id, auction_id, end_time)}, 1000);
//	}
//}

function edit_field(div_id, caption, table, field_id, value_id, field_changed, field_owner)
{
	textbox_id = div_id + '_' + field_changed;
	document.getElementById(div_id).innerHTML = '<input type="text" name="' + field_changed + '" size="50" value="' + caption + '" id="' + textbox_id + '" maxlength="70" /> ' + 
		'<input type="button" value="' + save_msg + '" onclick="save_field(\'' + textbox_id + '\', \'' + div_id + '\', \'' + caption + '\', \'' + table + '\', \'' + field_id + '\', \'' + value_id + '\', \'' + field_changed + '\', \'' + field_owner + '\');" />';
}

function save_field(textbox_id, div_id, caption, table, field_id, value_id, field_changed, field_owner)
{
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url    = relative_path + 'ajax_files/save_field.php';
	var action    = url + '?table=' + table + '&field_id=' + field_id + '&value_id=' + value_id + 
		'&field_changed=' + field_changed + '&field_owner=' + field_owner + 
		'&changed_value=' + document.getElementById(textbox_id).value;

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4)
		{
			var response = xmlHttp.responseText;
			document.getElementById(div_id).innerHTML = response + 
				' [ <a href="javascript:;" onclick="edit_field(\'' + div_id + '\', \'' + response + '\', \'' + table + '\', \'' + field_id + '\', ' + value_id + ', \'' + field_changed + '\', \'' + field_owner + '\');">' + edit_msg + '</a> ]';

			if (location_reload == true) {
				location.reload(true);
			}
		}
	};
	xmlHttp.open("POST", action, true);
	xmlHttp.send(null);	
}

var ie4 = false;
if(document.all) {
	ie4 = true;
}

function getObjectDetails(id) 	{
	if (ie4) {
		return document.all[id];
	} else {
		return document.getElementById(id);
	}
}

function toggle_default(divId) {
	var d = getObjectDetails(divId);

	if (d.style.display == '') {
		d.style.display = 'none';
	} else {
		d.style.display = '';
	}
}

function toggle_simple(divId, fieldId) {
	var d = getObjectDetails(divId);
	var fld = getObjectDetails(fieldId);

	if (d.style.display == '') {
		d.style.display = 'none';

		if (fld.value) {
			fld.value = '';
		}
	} else {
		d.style.display = '';
	}
}

function toggle_double(divId, fieldId, fieldIdB) {
	var d = getObjectDetails(divId);
	var fld = getObjectDetails(fieldId);
	var fld1 = getObjectDetails(fieldIdB);

	if (d.style.display == '') {
		d.style.display = 'none';

		if (fld.value)	{
			fld.value = '';
		}
		if (fld1.value) {
			fld1.value = '';
		}
	} else {
		d.style.display = '';
	}
}

function toggle_radio(divId, radioId, value_display) {
	var d = getObjectDetails(divId);
	var r = getObjectDetails(radioId);

	if (r.value == value_display)
	{
		d.style.display = '';
	} else {
		d.style.display = 'none';
	}
}

function page_redirect(id)
{
	if (document.getElementById(id).value != '')
	{
		document.location.href =document.getElementById(id).value;
	}
	return false;
}

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

function pg_update_settings(id, input_array)
{
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url    = relative_path + 'ajax_files/direct_payment_box.php';
	var action    = url + '?id=' + id + '&user_id=' + s_usr;

	var chk_disabled = false;
	for ( var i in input_array )
	{
		if (i == parseInt(i))
		{
			action += '&' + input_array[i] + '=' + document.getElementById(input_array[i]).value;

			if (document.getElementById(input_array[i]).value == '')
			{
				chk_disabled = true;
			}
		}
	}

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4)
		{
			var response = xmlHttp.responseText;
		}
	};
	xmlHttp.open("GET", action, true);
	xmlHttp.send(null);

	document.getElementById('checkbox_' + id).disabled = chk_disabled;
	if (chk_disabled == true)
	{
		document.getElementById('checkbox_' + id).checked = false;
	}
	document.getElementById(id).style.display = 'none';
	return false;
}

function change_pic ( image_path, id )
{
	document.getElementById(id).src = image_path;
}

var myPopup = '';

function openPopup(url) {
	myPopup = window.open(url,'popupWindow','width=640,height=150,status=yes');
   if (!myPopup.opener) myPopup.opener = self;
}
function converter_open(url) {
	output = window.open(url,"popDialog","height=220,width=700,toolbar=no,resizable=yes,scrollbars=yes,left=10,top=10");
}

// qtip
$(document).ready(function() 
{
   // Use the each() method to gain access to each elements attributes
   $('.auction-tooltip a[rel]').each(function()
   {
      $(this).qtip(
      {
         content: {
            // Set the text to an image HTML string with the correct src URL to the loading image you want to use
            text: 'Loading...',
            url: $(this).attr('rel'), // Use the rel attribute of each element for the url to load
            title: {
               text: $(this).attr('title'), // Give the tooltip a title using each elements text
               button: 'Close' // Show a close link in the title
            }
         },
         position: {
            corner: {
               target: 'rightMiddle', // Position the tooltip above the link
               tooltip: 'leftMiddle'
            },
            adjust: {
               screen: true // Keep the tooltip on-screen at all times
            }
         },
         show: { 
            when: 'mouseover', 
//            when: 'click', 
            delay: 500, 
            solo: true // Only show one tooltip at a time
         },
         hide: 'unfocus',
         style: {
            tip: false, // Apply a speech bubble tip to the tooltip at the designated tooltip corner
            border: {
               width: 0,
               radius: 4
            },
            name: 'light', // Use the default light style
            width: 400 // Set the tooltip width
         }
      })
   });
   
   $('#btn-read-reviews, #btn-write-review').click(function() {
   	var auction_id = $(this).attr('title');
		$.post(
			'ajax_files/product_reviews.php', 
			{
				auction_id: auction_id, 
				clicked: $(this).attr('id')
			},
			function(data) {
				if (data.redirect == true)
				{
					window.location.replace('login.php?redirect=auction_details.php?auction_id=' + auction_id);
				}
				else
				{
					$('#auction-name').html(data.name);					
					$('#reviews-output').html(data.content);					
					$('#jqm-reviews').jqmShow();
				}
			}, 
			'json'
		);	
   });
});


function getNewMessages()
{
	$.ajax({
		url: relative_path + 'ajax_files/get_messages.php',			
			async: true, 
			success: function(data)
			{		
				if (data)
				{					
					$('#messages-dialog-result').html(data);
					$('#jqm-messages').jqmShow();	
				}				
			}
	});
}


function getNotifications()
{
	$.ajax({
		url: relative_path + 'ajax_files/live_notifications.php',
		async: true,
		success: function(data)
		{
			if (data)
			{				
				if($('#jqm-notifications').is(':hidden') && $('#jqm-decline-offer').is(':hidden') && $('#jqm-counter-offer').is(':hidden'))
				{
					$('#notifications-dialog-result').html(data);
					$('#jqm-notifications').jqmShow();
				}
			}
		}
	});
	
	setTimeout(function() {getNotifications()}, 10000);
}

$(document).ready(function() {
	$('#message-proceed').live('click', function() {
		window.location.replace(relative_path + 'members_area.php?page=messaging&section=received');
	});
	
	$('#message-cancel').live('click', function() {
		$('#jqm-messages').jqmHide();		
	});	

	$('#decline_form_cancel').live('click', function() {
		$('#jqm-decline-offer').jqmHide();		
	});	

	$('#counter_form_cancel').live('click', function() {
		$('#jqm-counter-offer').jqmHide();		
	});	
	
	// add events for the offer buttons
	$('.accept-offer').live('click', function () {
		var auction_id = $(this).closest('div.auction-data').find('[name=offer_auction_id]').val();
		var offer_id = $(this).closest('div.auction-data').find('[name=offer_id]').val();
		var offer_type = $(this).closest('div.auction-data').find('[name=offer_type]').val();
		var reload = $(this).closest('div.auction-data').find('[name=location_reload]').val();
		
		$.ajax({
			url: relative_path + 'ajax_files/manage_offers.php',
			async: true,
			dataType: 'json', 
			data: ({
				action : 'accept_offer', 
				auction_id : auction_id, 
				offer_id : offer_id, 
				offer_type : offer_type, 
				reload : reload
			}),
			success: function(data)
			{
				$('#notifications-dialog-result').html(data.display);
				if (data.reload == 'true')
				{
					location.reload();
				}
			}
		});		
	});
	
	$('.withdraw-offer').live('click', function () {
		var auction_id = $(this).closest('div.auction-data').find('[name=offer_auction_id]').val();
		var offer_id = $(this).closest('div.auction-data').find('[name=offer_id]').val();
		var offer_type = $(this).closest('div.auction-data').find('[name=offer_type]').val();
		var offer_page = $(this).closest('div.auction-data').find('[name=offer_page]').val();
		
		window.location.replace('members_area.php?page=' + offer_page + '&section=view_offers&' + 
			'auction_id=' + auction_id + '&do=withdraw_offer&offer_type=' + offer_type + '&offer_id=' + offer_id);
	});
	
	$('.decline-offer, .counter-offer').live('click', function () {
		var auction_id = $(this).closest('div.auction-data').find('[name=offer_auction_id]').val();
		var page = $(this).closest('div.auction-data').find('[name=offer_page]').val();
		var offer_id = $(this).closest('div.auction-data').find('[name=offer_id]').val();
		var offer_type = $(this).closest('div.auction-data').find('[name=offer_type]').val();
		
		$('[name=auction_id]').val(auction_id);
		$('[name=page]').val(page);
		$('[name=offer_id]').val(offer_id);
		$('[name=offer_type]').val(offer_type);
		
		$('#jqm-notifications').jqmHide();				
	});
	
	$('.decline-offer').live('click', function () {
		$('#jqm-decline-offer').jqmShow();
	});

	$('.counter-offer').live('click', function () {
		$('#jqm-counter-offer').jqmShow();
	});
	
	
	// counter offer process 
	$('#counter_form_proceed, #decline_form_proceed').live('click', function () {			
		$.ajax({
			url: relative_path + 'ajax_files/manage_offers.php',
			async: true,
			dataType: 'json', 
			data: ({
				action : $(this).closest('form').find('[name=do]').val(), 
				auction_id : $(this).closest('form').find('[name=auction_id]').val(), 
				offer_id : $(this).closest('form').find('[name=offer_id]').val(), 
				offer_type : $(this).closest('form').find('[name=offer_type]').val(), 
				amount: $(this).closest('form').find('[name=amount]').val(), 
				offer_comment: $(this).closest('form').find('[name=offer_comment]').val(), 
				decline_reason: $(this).closest('form').find('[name=decline_reason]').val(), 
				reload: $(this).closest('form').find('[name=location_reload]').val()
			}),
			success: function(data)
			{
				$('#notifications-dialog-result').html(data.display);
				$('#jqm-counter-offer').jqmHide();
				$('#jqm-decline-offer').jqmHide();
				$('#jqm-notifications').jqmShow();
				
				if (data.reload == 'true')
				{
					location.reload();
				}				
			}
		});		
	});
   
   $('[name="sh_rates"]').click(function() {
      get_shipping_rates();
   });
   
   $('[name="quantity"]').change(function() { 
      var q = $(this).val();
      $('[name="sh_quantity').val(q);
   });
});

function get_shipping_rates()
{
   $.ajax({
      url: relative_path + 'ajax_files/shipping_rates.php', 
      async: true, 
      dataType: 'json', 
      data: ({
         auction_id: $('[name="sh_auction_id"]').val(), 
         quantity: $('[name="sh_quantity"]').val(), 
         country: $('[name="sh_country"]').val()
      }), 
      success: function(data)
      {
         var result = '';
         $('.sh-result').remove();
         $.each(data, function(k, v) {
            result = result + '<div class="oh sh-result">' +
               '  <div class="sh-first">' + v.amount_first + '</div>' +
               '  <div class="sh-addl">' + v.amount_addl + '</div>' +
               '  <div class="sh-to">' + v.country_name + '</div>' + 
               '  <div>' + v.method_name + '</div><br>' +
               '</div>';
         });
         $('.sh-title').after(result);
      }
   })
}

function decline_popup_open(offer_type, offer_id)
{
	self.scrollTo(0,0);

	$('#decline_offer').show();
	$('#decline_offer_type').val(offer_type);
	$('#decline_offer_id').val(offer_id);

	return false;
}

function counter_popup_open(offer_type, offer_id)
{
	self.scrollTo(0,0);

	$('#counter_offer').show();
	$('#counter_offer_type').val(offer_type);
	$('#counter_offer_id').val(offer_id);

	return false;
}

function popup_form_proceed()
{
	form_name.submit();
	return true;
}

function display_direct_purchase_box()
{
   var attributes = '';
   
   $('.product-attribute').each(function() { 
      attributes += $(this).attr('id') + ': ' + $(this).val() + '; ';
   });
   
   $.ajax({
      url: relative_path + 'ajax_files/direct_purchase_box.php', 
      async: true, 
      dataType: 'html', 
      data: ({
         auction_id: $('#calc_auction_id').val(), 
         quantity: $('#quantity').val(), 
         price: $('#calc_price').val(), 
         shipping_type: $('[name="shipping_option"]:checked').val(),
         pg_id: $('[name="payment_gateway"]:checked').val(), 
         attributes: attributes
      }), 
      success: function(data)
      {
         $('#direct-purchase-box').html(data);
      }
   })
}
