function testSelects(el) {
	flag = 1;
	selects = el.getElementsByTagName('select');
	for (i=0; i<selects.length; i++) {
		flag = flag * selects[i].value;
	}
	if (0 == flag) {
		alert('Please select a model and quantity to purchase.');
		return false;
	}
}

function mirror_billing() {
	document.getElementById('order_shipping_fname').value = document.getElementById('order_billing_fname').value;
	document.getElementById('order_shipping_lname').value = document.getElementById('order_billing_lname').value;
	document.getElementById('order_shipping_company').value = document.getElementById('order_billing_company').value;
	document.getElementById('order_shipping_addr1').value = document.getElementById('order_billing_addr1').value;
	document.getElementById('order_shipping_addr2').value = document.getElementById('order_billing_addr2').value;
	document.getElementById('order_shipping_city').value = document.getElementById('order_billing_city').value;
	document.getElementById('order_shipping_state').value = document.getElementById('order_billing_state').value;
	document.getElementById('order_shipping_zip').value = document.getElementById('order_billing_zip').value;
	document.getElementById('order_shipping_country').value = document.getElementById('order_billing_country').value;
	document.getElementById('order_shipping_phone').value = document.getElementById('order_billing_phone').value;
	document.getElementById('order_shipping_email').value = document.getElementById('order_billing_email').value;
}

function htmltag(style, id) {
	if ('link' == style)
		var url = prompt("Please enter the destination URL", "http://");
	else if ('email' == style)
		var url = prompt("Please enter the email address");

	var styles = {  bold:['<strong>', '</strong>'],
				italic:['<em>', '</em>'],
				link:['<a href="'+url+'">', '</a>'],
				email:['<a href="mailto:'+url+'">', '</a>'] }

	var textarea = document.getElementById(id);
	if (document.selection) { // code for IE
		textarea.focus();
		var sel = document.selection.createRange();
	}
	if (document.selection && (sel.parentElement == textarea)) { // sanity - for Opera
		sel.text = styles[style][0] + sel.text + styles[style][1];
	} else { // code for Mozilla
		var len = textarea.value.length;
		var start = textarea.selectionStart;
		var end = textarea.selectionEnd;

		if (0 == len) var sel = prompt("Please enter the anchor text");
			else var sel = textarea.value.substring(start, end);

		var replace = styles[style][0] + sel + styles[style][1];
		// Here we are replacing the selected text with this one
		textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
	}
	return false;
}

function togglesection(el_id) {
	var el = document.getElementById(el_id);
	if (el) {
		if ('block' == el.style.display) el.style.display = 'none';
		else el.style.display = 'block';
	}
}

function closeform() {
	document.getElementById('inner_window').style.display = 'none';
}

function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function showform(formstring, type, item) {

	var el = document.getElementById('lightbox');
	if (el) {
		innerXHTML(formstring, 1, el);
		document.getElementById('inner_window').style.display = 'block';

		el.style.top = (getScrollY() + 15) + 'px';
		el.style.marginBottom = (getScrollY() + 25) + 'px';

		if (type && item && ('new' != item))
			get_form_data(type, item);
	}

}

function get_form_data(type, item) {
	var url = encodeURI('form_data.php?type='+type+'&item='+item);

	if (!xmlHttp) var xmlHttp = new XMLHttpRequest();
	else if (xmlHttp.readyState != 0) xmlHttp.abort();

	xmlHttp.open("GET",url,true);

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			if (xmlHttp.status != 200) alert('Error: ' + xmlHttp.status + '(' + url + ')');

			populate_form(jsonParse(xmlHttp.responseText), type);
			xmlHttp.onreadystatechange=null;
		}
	}
	xmlHttp.send(null);
}

function populate_form(data, type) {
	for (i in data) {
		if (document.getElementById(type+"_"+i)) {
			document.getElementById(type+"_"+i).value = data[i];
		}
	}

	if (document.getElementById(type+"_delete_img") && data['img_filename']) {
		document.getElementById(type+"_delete_img").src = 'images/'+data['img_filename'];
		document.getElementById(type+"_delete_img").width = data['img_width'];
		document.getElementById(type+"_delete_img").height = data['img_height'];
	}

}

