var backend_mode = 0;
var edit_num = 0;

function launchquote(product_id, backend, edit)
{
	var prefix = window.parent.document.location.protocol;
	if (prefix.indexOf('https') > -1) {
		SITE_URL = SITE_URL.replace('http://', 'https://');
	} else {
		SITE_URL = SITE_URL.replace('https://', 'http://');
	}
	
	var b = 0;
	if (typeof(backend) != 'undefined') { b = 1; }
	if (b) { backend_mode = 1; }
	
	var e = 0;
	if (typeof(edit) != 'undefined') { e = edit; edit_num = edit; }
	
	var url = SITE_URL+'instantquote/'+product_id;
	jQuery.getJSON(url, {ajax:1, backend:b, edit:e}, function(data){ ajax_cb(data); });
	return false;
}

function create_win(product_id)
{
	var html = '';
	html += '<div id="quote_element" style="position:absolute;left:-9000;top:9000;width:700px;">';
	html += '<div id="quote_top"><div id="quote_close" onclick="close_win()">Close</div><div id="quote_title"></div></div>';
	html += '<div id="quote_content">';
	html += '</div>';
	html += '<div id="quote_bottom" style="position:relative">';
	html += '<table cellspacing="0" cellpadding="0" border="0" style="position:absolute; left:180px;top:0px"><tr>';
	if (!backend_mode) {
		html += '<td><input type="image" src="'+SITE_URL+'images/addbasket.gif" onclick="convert_to_order(\''+product_id+'\')"/></td>';
		html += '<td><div style="width:20px"></div></td>';
		html += '<td><input type="image" src="'+SITE_URL+'images/emailme.gif" onclick="email_quote(\''+product_id+'\')" /></td>';
		html += '<td><div style="width:20px"></div></td>';
		html += '<td id="qlimg"><input type="image" src="'+SITE_URL+'images/quotelist.gif" onclick="quotelist()" /></td>';
	} else {
		html += '<td style="padding-left:100px" align="center">';
		html += '<input type="hidden" id="edit_id" value="'+edit_num+'" />';
		html += '<input type="image" src="'+SITE_URL+'images/addquote.gif" onclick="add_quote()"/>';
		html += '</td>';
	}
	html += '</tr></table></div>';
	html += '</div>';
	jQuery('#quote_placeholder').html(html);
	jQuery('select').hide();
}
function close_win()
{
	close_ej_window();
	jQuery('#quote_element').fadeOut('slow');
	jQuery('#quote_element').css({position:'absolute', left:'-9000', top:'-9000', zIndex: '999999999'});
	jQuery('#quote_element').remove();
	jQuery('#quote_content').html('');
	jQuery('#quote_placeholder').html('');
	document.body.style.outline='1px solid transparent';//force reflow
	document.body.style.outline ='0px'
	jQuery('select').show();
}
function ajax_cb(data)
{
	create_win(data.product_id);
	cms_screenBlock();
	var el = jQuery('#quote_content');
	el.html(data.html);
	jQuery('#quote_title').html('Instant Quote For '+data.display_name);
	cms_window('quote_element');
}

function convert_to_order(product_id)
{
	if (!vd_qty()) {
		alert('Please enter a valid Quantity');
		return false;
	}

	var url = '/order/'+product_id;
	window.location.href = url;
	return false;
}

function email_quote(product_id, enquire)
{
    if(!enquire) {
    enquire = false;
}
      if (!vd_qty()) {
		alert('Please enter a valid Quantity');
		return false;
	}
	if(product_id)
        var url = '/emailquote/?pid='+product_id+'&enquire='+enquire;
    else
        var url = '/emailquote/?ql=1&tmp=1';
	window.location.href = url;
	return false;
}

function quotelist()
{
	if (!vd_qty()) {
		alert('Please enter a valid Quantity');
		return false;
	}
	window.location.href = '/quotelist?add=1';
	return false;
}

function vd_qty()
{
	var qty = jQuery('#qty').val();
	if (!qty) { return false; }
	if (!is_int2(qty)) { return false; }
	return true;
}
function is_int2( mixed_var ) {
	var y = parseInt(mixed_var * 1);
	if (isNaN(y)) { return false; }
	return mixed_var == y && mixed_var.toString() == y.toString();
}

