function $Q(v) { return(document.getElementById(v)); }
function $S(v) { return($Q(v).style); }
function $V(v) { return($Q(v).value); }
function $SV(v) { return $Q(v).options[$Q(v).selectedIndex].value; }
function A(v) { console.log('Alert: ', v); }
function tog_img(x, i_1, i_2) { $Q(x).src = $Q(x).src.match(i_1) ||  $Q(x).src == i_1 ? i_2 : i_1; }
function tog_vis(x, on_only) { $S(x).display = $S(x).display == 'none' || !$S(x).display || on_only ? 'block' : 'none'; }


function list_search() {
	var n = trim($Q('list_name').value);
	if (!trim($Q('list_email').value)) {
		if (n == '') {
			alert("Please enter at least 2 characters of the name or an email address.");
			return false;
		} else if (n == 'enter name') {
			alert("Please enter at least 2 characters of the name or an email address.");
			return false;
		} else if (n.length < 2) {
			alert("Please enter at least 2 characters of the name.");
			return false;
		} else return true;
	} else return true;
}

function trim(s) {
	s = s.replace(/^\s+/,'');
	s = s.replace(/\s+$/,'');
	return(s);
}

function ajax_get(url, div, action) {
	if (window.XMLHttpRequest)
	   var xhr = new XMLHttpRequest();
	else if (window.ActiveXObject)
	   var xhr = new ActiveXObject("Microsoft.XMLHTTP");

	xhr.open("GET", url);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	xhr.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
	xhr.onreadystatechange = function () {
    	if (xhr.readyState == 4) {
    		if ($Q(div)) {
    			if ($S(div).display == 'none')
    				$S(div).display = '';
    	    	$Q(div).innerHTML = xhr.responseText;
    	    } else if (div == 1)
    	    	eval(xhr.responseText);

			if (action)
				action(xhr.responseText);
    	}
    }
   	xhr.send(null);
   	return xhr;
}

function number_format(n) {
	if (isNaN(n)) n = "0";
	s = (n == (n = Math.abs(n)));
	n = Math.floor(n*100+0.50000000001);
	d = n%100;
	n = Math.floor(n/100).toString();
	if (d < 10) d = "0" + d;
	for (var i = 0; i < Math.floor((n.length-(1+i))/3); i++)
		n = n.substring(0,n.length-(4*i+3))+','+
	n.substring(n.length-(4*i+3));
	return ((s?'':'-') + n + '.' + d);
}

function only_numbers(field) {
	var n = Number($Q(field).innerHTML.replace(/[^0-9.]/g, ""));
	return isNaN(n)?0:n;
}

function clear_value(field) {
	if (field.value == field.defaultValue)
		field.style.color = field.value = '';
}

function add_wishlist() {
	add_cart();
	if ($Q('wishlist').value == 0) {
		$S('display_list').display = 'block';
		$Q('wishlist').value = $Q('wish_list').value;
	} else {
		if (add_cart()) $Q('add_form').submit();
	}
}

function cart_add_wishlist(id) {
	if ($Q('wishlist'+id).value == 0) {
		$S('display_list'+id).display = 'block';
		$Q('wishlist'+id).value = $Q('wish_list'+id).value;
	} else {
		$Q('add_form_'+id).submit();
	}
}

function add_cart() {
	error = 0;
	var a = $V('prod_vars').split('|');
	var r = $V('prod_req').split('|');
	for (var i in r) {
		if ($Q('var_'+a[i]) && !$V('var_'+a[i])) {
 			$S('choose_error_'+a[i]).display = 'block';
 			error = 1;
		} else if ($Q('choose_error_'+a[i]))
			$S('choose_error_'+a[i]).display = 'none';
	}
	if ($V('prod_qty') < 1) {
		$S('qty_error').display = 'block';
		error = 1;
	} else
		$S('qty_error').display = 'none';
	return error ? false : true;
}

function select_var(js) {
	var a = $V('prod_vars').split('|');
	var r = $V('prod_req').split('|');
	var v = new Array();
	var x = 0;
	for (var i in a)
		if ($Q('var_' + a[i]) && $V('var_' + a[i]).length)
			v[x++] = $V('var_' + a[i]);

	if (!js)
		for (var i in r) {
			if ($Q('var_' + a[i]) && !$V('var_' + a[i]))
				$S('choose_error_' + a[i]).display = 'block';
			else if ($Q('choose_error_' + a[i]))
				$S('choose_error_' + a[i]).display = 'none';
		}

	if (window.XMLHttpRequest)
		var xhr = new XMLHttpRequest();
	else if (window.ActiveXObject)
		var xhr = new ActiveXObject("Microsoft.XMLHTTP");

	xhr.open("GET", '/qjax.php?v=' + v.join(':').replace(/\:$/,""));
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	xhr.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
	xhr.onreadystatechange = function () {
    	if (xhr.readyState == 4) {
			var y = xhr.responseText.split('|');
			if (y.length > 1) {
				$Q('price_block').className = 'show';
				$Q('price_base').className = 'hide';
				$Q('product_stock').innerHTML = y[0];
				if ($Q('product_msrp')) $Q('product_msrp').innerHTML = "<strike>"+y[1]+"</strike>";
				$Q('product_price').innerHTML = y[2];
				if ($Q('product_save')) $Q('product_save').innerHTML = y[3];	
				if ($Q('product_caption')) $Q('product_caption').innerHTML = '';
				if ($Q('product_image')) $Q('product_image').src = '/q_images/inv/m/' + (y[4].length ? y[4] : $V('prod_image'));
				$S('stock_error').display = 'none';
			} else {
				$Q('price_block').className = 'hide';
				$Q('price_base').className = 'show';
				$S('stock_error').display = '';
				$Q('product_stock').innerHTML = 'Please select another option.';
				if ($Q('product_caption')) $Q('product_caption').innerHTML = '';				
				if ($Q('product_image')) $Q('product_image').src = '/q_images/inv/m/' + $V('prod_image');
			}
		}
    }
   	xhr.send(null);
}

function shipping_totalr() {
	if (!st_xhr || st_xhr.readyState == 4) $Q('total_v').innerHTML = number_format(only_numbers('subtotal_v') + only_numbers('tax_v') + only_numbers('ship_v'));
	else setTimeout("shipping_totalr();", 10);
}

function shipping_optionr(option) {
	ajax_get('/qjax.php?a=ship&d=1&s=1&c=' + $V('ship_country') + '&o=' + option, 'ship_v');
	st_xhr = ajax_get('/qjax.php?a=ship&d=1&t=1&c=' + $V('ship_country') + '&st=' + $V('ship_state'), 'tax_v');
	shipping_totalr();
}

function shipping_stater(option) {
	ajax_get('/qjax.php?a=ship&d=1&s=1&c='+$V('ship_country')+'&o='+$V('ship_opt_us')+'&st='+$V('ship_state'), 'ship_v');
	st_xhr = ajax_get('/qjax.php?a=ship&d=1&t=1&c='+$V('ship_country')+'&st='+$V('ship_state'), 'tax_v');
	$S('shipping_option').display = 'block';
	shipping_totalr();	
}

function shipping_country(country) {
	$S('shipping_option').display = 'block';
	if (country == "US") {
		$S('shipping_state').display = 'block';
	} else {
		$S('shipping_state').display = 'none';
	}
	ajax_get('/qjax.php?a=ship&c='+$V('ship_country'), 'ship_option');
	ajax_get('/qjax.php?a=ship&d=1&s=1&c='+$V('ship_country'), 'ship_v');
	st_xhr = ajax_get('/qjax.php?a=ship&d=1&t=1&c='+$V('ship_country')+'&st='+$V('ship_state'), 'tax_v');
	shipping_totalr();
}

function checkout_country(country, sb) {
	if (country == "US") {
		$S('us_fields_'+sb).display = 'block';
		$S('int_fields_'+sb).display = 'none';
	} else {
		$S('us_fields_'+sb).display = 'none';
		$S('int_fields_'+sb).display = 'block';
	}
}

function checkout_shipping(v, x) {
	if (v == 1) {
		if (x) $S('shipping_form').display = 'block';
		else $S('shipping_form').display = 'none';
		$S('shipping_no_gift').display = 'none';
		$S('shipping_yes_gift').display = 'block';
	} else if (v == 2) {
		$S('shipping_form').display = 'none';
	} else {
		$S('shipping_form').display = 'block';
		$S('shipping_no_gift').display = 'block';
		$S('shipping_yes_gift').display = 'none';
	}
}

function light_div(path, w, h) {
	var objOverlay = $Q('overlay');
	var objLightbox = $Q('lightbox');
	var objGhey = $Q('lightboxDiv');

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	ajax_get(path, 'lightboxDiv');
	
	if ($Q('loadingImage')) { $S('loadingImage').display = 'none'; }
	if ($Q('lightboxDetails')) {	$S('lightboxDetails').display = 'none'; }
	if ($Q('lightboxImage')) { $S('lightboxImage').display = 'none'; }
	
	if (navigator.appVersion.indexOf("MSIE")!=-1) pause(250);
	
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - h) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - w) / 2);

	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

	objGhey.style.width = w + 'px';
//	objGhey.style.height = h + 'px';
	
	objLightbox.style.display = 'block';
	$S('lightboxDiv').display = 'block';
}

function email_product() {
	var error = 0;
	var ereg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	if (!$V('friend_email').match(ereg)) {
		error = $Q('email_error').innerHTML = '<b>Error:</b> Your friend\'s email appears invalid!';
		tog_vis('email_error', 1);
	} else if (!$V('your_email').match(ereg)) {
		error = $Q('email_error').innerHTML = '<b>Error:</b> Your email appears invalid!';
		tog_vis('email_error', 1);
	} else if ($V('your_name').length < 2) {
		error = $Q('email_error').innerHTML = '<b>Error:</b> You must enter YOUR name!';
		tog_vis('email_error', 1);
	} else if ($V('captcha').length < 2) {
		error = $Q('email_error').innerHTML = '<b>Error:</b> Please enter your verification word!';
		tog_vis('email_error', 1);
	}
	scroll(0,0);
	if (!error)	{
		ajax_get('/modules/email.php?p='+$V('product')+'&friend_name='+$V('friend_name')+'&friend_email='+$V('friend_email')+'&your_name='+$V('your_name')+'&your_email='+$V('your_email')+'&message='+$V('message')+'&captcha='+$V('captcha'), 'lightboxDiv');
	}
	return false;
}
