//*****************  open item subwindow
function open_order (id, type) {
	if (type == 0)
		obj = get('edit_' + id);
	else if (type == 1)
		obj = get(id);
	else if (type == 2 || type == 3)
		obj = get('next_step');
	pos = getAbsolutePos(obj);

	var screen_h = getClientHeight();
	if (type == 0 || type == 2 || type == 3)
		space = 630;
	else if (type == 1)
		space = 100;

	var pos_x = pos.x - space;
	var distanse = (screen_h - 405) / 2;
	pos_y = distanse - 10;
	if (pos_y < 0) {
		pos_y = (screen_h - 405) / 2;
	}
	hide_select('0');
	order_window=dhtmlmodal.open('orderbox', 'div', 'order_subwindow', '', 'width=701px,height=399px,left=' + pos_x + ',top=' + pos_y + ',resize=0,scrolling=0')
}

//*****************  open order subwindow
function open_item (id, type) {

	obj = get(id);
	pos = getAbsolutePos(obj);
	var screen_h = getClientHeight();
	if (type == 1)
		var space = 500;
	else
		var space = 630;
	var pos_x = pos.x - space;
	var distanse = (screen_h - 405) / 2;
	pos_y = distanse - 10;
	if (pos_y < 0) {
		pos_y = (screen_h - 405) / 2;
	}
	hide_select('0');
	item_window=dhtmlmodal.open('itembox', 'div', 'item_subwindow', '', 'width=527px,height=399px,left=' + pos_x + ',top=' + pos_y + ',resize=0,scrolling=0')
}

//*****************  open zip subwindow
function open_zip (id) {
	obj = get(id);
	pos = getAbsolutePos(obj);
	var screen_h = getClientHeight();
	var space = 35;
	var pos_x = pos.x - space;
	var distanse = (screen_h - 276) / 2;
	pos_y = distanse - 10;
	if (pos_y < 0) {
		pos_y = (screen_h - 276) / 2;
	}
	hide_select('0');
	zip_window=dhtmlmodal.open('zipbox', 'div', 'zip_subwindow', '', 'width=527px,height=270px,left=' + pos_x + ',top=' + pos_y + ',resize=0,scrolling=0')
}

//*****************  open login subwindow
function open_login (id, type, target) {
	obj = get(id);
	pos = getAbsolutePos(obj);
	var screen_h = getClientHeight();
	if (type == 1)
		var space = 193;
	else if (type == 0)
		var space = -195;
	else if (type == 2)
		var space = 500;
	else if (type == 3)
		var space = 455;
	var pos_x = pos.x - space;
	var distanse = (screen_h - 287) / 2;
	pos_y = distanse - 10;
	if (pos_y < 0) {
		pos_y = (screen_h - 287) / 2;
	}
	var hash = get('hash').value;
	hide_select('0');
	login_window=dhtmlmodal.open('loginbox', 'div', 'login_subwindow', '', 'width=531px,height=281px,left=' + pos_x + ',top=' + pos_y + ',resize=0,scrolling=0');
	show_spinner ('output', 'margin-top: 20px; margin-left: 25px;');
	ajaxdisplay('inc/request.php?action=login_form&target=' + target + '&hash=' + hash, '', 'output');
	if (get('login_subwindow')) {
		get('login_subwindow').style.display = 'block';
	}
	get("drag-handle").style.backgroundImage = "url('js/subwindow/modalfiles/login.gif')";
}

//*****************  open secure code subwindow
function open_code (id, type) {

	obj = get(id);
	pos = getAbsolutePos(obj);
	var screen_h = getClientHeight();
	var space = 344;
		
	var pos_x = pos.x - space;
	var distanse = (screen_h - 405) / 2;
	pos_y = distanse - 10;
	if (pos_y < 0) {
		pos_y = (screen_h - 405) / 2;
	}
	hide_select('0');
	var hash = get('hash').value;
	show_spinner ('output', 'margin-top: 20px; margin-left: 25px;');
	ajaxdisplay('inc/request.php?action=show_code&hash=' + hash, '', 'output');
	code_window=dhtmlmodal.open('codebox', 'div', 'code_subwindow', '', 'width=527px,height=399px,left=' + pos_x + ',top=' + pos_y + ',resize=0,scrolling=0')
}

//*****************  open order subwindow
function open_rule (id, type) {

	obj = get(id);
	pos = getAbsolutePos(obj);
	var screen_h = getClientHeight();
	if (id == 'terms')
		var space = 125;
	else if (id == 'b_terms')
		var space = 125;
	else if (id == 'terms1')
		var space = 8;
	else if (id == 'terms2')	
		var space = 268;
	else if (id == 'terms3')	
		var space = 522;
	else if (id == 'terms4')	
		var space = 516;	
		
	var pos_x = pos.x - space;
	var distanse = (screen_h - 405) / 2;
	pos_y = distanse - 10;
	if (pos_y < 0) {
		pos_y = (screen_h - 405) / 2;
	}
	hide_select('0');
	rule_window=dhtmlmodal.open('rulebox', 'div', 'rule_subwindow', '', 'width=527px,height=399px,left=' + pos_x + ',top=' + pos_y + ',resize=0,scrolling=0')
}

//*****************  close subwindows
function close_suggest () {
	item_window.hide();
	hide_select('1');
	get('dhtmlwindowholder').innerHTML = '';
}

//*****************  close window with item
function close_order_widow (id, type, nn) {
	get('o_cancel').style.display = 'none';
	get('o_indicator').style.display = 'block';
	setTimeout("close_order (" + id + "," + type + "," + nn + ")", 1000);
}

//*****************  close window with item
function close_order (id, type, nn) {
	var err = false;
	if (id != '') {
		if (get('options')) {
			var options = get('options').value;
			var options_array = split (':', options);
			num_options = options_array.length;
			for (var i = 0; i < num_options; i++) {
				var option_data = split ('-', options_array[i]);
				var option_id = option_data[0];
				var option_mandatory = option_data[1];
				var option_num = option_data[2];
				var option_value = get('option'+option_id).value;
				if (option_mandatory == 1) {
					if (option_num == 1) {			// radio
						if (option_value == 0) {
							err = true;
							err1 = 'Please choose mandatory fields.';
							get('title'+option_id).style.color = '#cc0000';
							
							get('mandatory'+option_id).className = 'wrong_option';
						}
					} else if (option_num > 1) { 	// check
						if (option_value > 0) {
							if (option_value > option_num) {
								err = true;
								get('title'+option_id).style.color = '#cc0000';
								get('num'+option_id).className = 'wrong_option';
							}
						} else {
							err = true;
							err1 = 'Please choose mandatory fields.';
							get('title'+option_id).style.color = '#cc0000';
							get('mandatory'+option_id).className = 'wrong_option';
						}
					}
				} else if (option_mandatory == 0) {
					if (option_value > 0) {
						if (option_value > option_num) { // check
							err = true;
							get('title'+option_id).style.color = '#cc0000';
							get('num'+option_id).className = 'wrong_option';
						}
					}
				}
			}
		}
	
		if (!err) {
			if (get('all_values')) {
				var output_values = '';
				values = get('all_values').value;
				var values_array = split ('-', values);
				num_values = values_array.length - 1;
				for (var i = 0; i < num_values; i++) {
					if (get("value" + values_array[i]).value == 1) {
						output_values = output_values + values_array[i]+':'; 
					}
				}
			} else {
				output_values = '';
			}
			var hash = get('hash').value;
			var info = encodeURIComponent(get('info').value);
			var qnt = get('qnt').value;
			if (get("discount"))
				mytype = 1;
			else
				mytype = 0;
			if (get("green"))
				green = get('green').value;
			else
				green = 0;

			ajaxdisplay('inc/request.php?action=add_to_basket&item_id=' + id + '&nn=' + nn + '&info=' + info + '&values=' + output_values + '&qnt=' + qnt + '&green=' + green + '&type=' + mytype + '&hash=' + hash, '', 'basket_body');
			if (get('coupon')) {
				favorite = get('favorite').value;
				ajaxdisplay('inc/request.php?action=show_basket&type=1&favorite=' + favorite + '&green=' + green + '&hash=' + hash, '', 'basket');
			}
			if (type == 3) {
				setTimeout('checkout()', 1000);
//				location.href="checkout.html";
			} else {
				order_window.hide();
			}
		}	
	} else {
		if (type == 3) {
			setTimeout('checkout()', 1000);
//			location.href="checkout.html";
		} else {
			hide_select('1');
			order_window.hide();
		}
	}
}

function checkout () {
	window.location.href="https://www.dallasjonesbbq.com/checkout.html";
	//location.href="checkout.html";
}

function close_zip () {
	zip_window.hide();
	hide_select('1');
	get('dhtmlwindowholder').innerHTML = '';
}

function close_login () {
	login_window.hide();
	hide_select('1');
	get('dhtmlwindowholder').innerHTML = '';
}

function close_code () {
	code_window.hide();
	hide_select('1');
	get('dhtmlwindowholder').innerHTML = '';
}

function close_rule () {
	rule_window.hide();
	hide_select('1');
	get('dhtmlwindowholder').innerHTML = '';
}