var validator = {
	// validates that the field value string has one or more characters in it
	isNotEmpty : function(elem) {
		var str = elem.value;
		var re = /.+/;
		if(!str.match(re)) {
			$('processing').innerHTML = "";
			$('error').innerHTML = "Please fill in the required field. Field: "+elem.title;
			return false;
		} else {
			return true;
		}
	},
	//validates that the entry is a positive or negative number
	isNumber : function(elem) {
		var str = elem.value;
		var re = /^[-]?\d*\.?\d*$/;
		str = str.toString( );
		if (!str.match(re)) {
			$('processing').innerHTML = "";
			$('error').innerHTML = "Enter only numbers into the field. Field: "+elem.title;
			return false;
		}
			return true;
	},
	// validates that the entry is 16 characters long
	isLen16 : function(elem) {
		var str = elem.value;
		var re = /\b.{16}\b/;
		if (!str.match(re)) {
			$('processing').innerHTML = "";
			$('error').innerHTML = "Entry does not contain the required 16 characters.";
			return false;
		} else {
			return true;
		}
	},
	// validates that the entry is formatted as an e-mail address
	isEMailAddr : function(elem) {
		var str = elem.value;
		var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
		if (!str.match(re)) {
			$('processing').innerHTML = "";
			$('error').innerHTML = "Verify the e-mail address format.";
			return false;
		} else {
			return true;
		}
	},
	// validate that the user made a selection other than default
	isChosen : function(select) {
		if (select.selectedIndex == 0) {
			$('processing').innerHTML = "";
			$('error').innerHTML = "Please make a choice from the list. ("+select.title+")";
			return false;
		} else {
			return true;
		}
	},
	// validate that the user has checked one of the radio buttons
	isValidRadio : function(radio) {
		var valid = false;
		for (var i = 0; i < radio.length; i++) {
			if (radio[i].checked) {
				valid = true;
			}
		}
		if (!valid) {
			//alert("Make a choice from the radio buttons. ("+radio.title+")");
			$('processing').innerHTML = "";
			$('error').innerHTML = "Please Choose a Method of Payment.";
			valid = false;
		}
		return valid;
	},
	focusElement : function(formName, elemName) {
		var elem = document.forms[formName].elements[elemName];
		elem.focus( );
		elem.select( );
	}
}

function show_shipping_override() {
	var content = "<form name='shipp_override' id='shipp_override' enctype='multipart/form-data' method='post' action=''>" +
				"<input type='hidden' name='action' value='override_shipping' />" +
				"<p>Please enter amount as instructed by the Gallery Staff</p>" +
				"<p>New Shipping Amount $ <input type='text' name='ship_override' /></p>" +
				"</form>";
	show_popup( "Override Shipping Charges", content, "$('shipp_override').submit()");
}

function print_page(container)
{
	var prtContent = document.getElementById(container);
	WinPrint = window.open('','','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
	WinPrint.document.write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><title>Glen Scrimshaw Gallery - Purchase Invoice</title><link href='print_styles.css' rel='stylesheet' type='text/css' /></head><body>");
	WinPrint.document.write(prtContent.innerHTML);
	WinPrint.document.write("</body></html>");
	WinPrint.document.close();
	WinPrint.focus();
	WinPrint.print();
	WinPrint.close();
}

function myvalidate()
{
	var content = "<div style=\"text-align: center; padding: 10px;\"><br><br><br><br><br><div id='error'></div><div id='processing'>Processing Order ....<img src='images/layout/ajax-loader.gif'></div><br><br><br><br></div>";
	show_popup( "", content, 'null');
	ret = true;
	el = document.proceed.method;
	method = '';
	for (i=0;i<el.length;i++)
	{
		if (el[i].checked)
			method = el[i].value;
	}
	var customer_prov = false;
	if ( $('cust_country').options[ $('cust_country').selectedIndex ].text == 'OTHER' ) {
		$('cust_prov').style.display = 'none';
		$('cust_prov_other').style.display = 'block';
		if ( $('cust_prov_other').text != '' ) customer_prov = $('cust_prov_other').value;
	} else {
		$('cust_prov').style.display = 'block';
		$('cust_prov_other').style.display = 'none';
		if (validator_quiet.isChosen($('cust_prov'))) customer_prov = $('cust_prov').options[ $('cust_prov').selectedIndex ].text;
	}
	if (!validator.isEMailAddr($('cust_email'))) ret = false;
	if (!customer_prov) ret = false;
	if (!validator.isChosen($('cust_country'))) ret = false;
	if (!validator.isNotEmpty($('cust_email'))) ret = false;
	if (!validator.isNotEmpty($('cust_name'))) ret = false;
	if (!validator.isNotEmpty($('cust_address'))) ret = false;
	if (!validator.isNotEmpty($('cust_city'))) ret = false;
	if (!validator.isNotEmpty($('cust_zip'))) ret = false;
	if (!validator.isNotEmpty($('cust_phone'))) ret = false;
	if (!validator.isValidRadio( $('proceed_form').method )) ret = false;
	if (method == '') ret = false;
	
	if (ret)
	{
		var myajax = new Request( {
			  url: "ajax_validate_purchase.php",
			  method: 'post',
			  data : "method="+method,
			  async: false,
			  onComplete: function() {
					if (this.response.text == 'true')
					{
	//					alert('USER AND CART REPORT VALID'+this.response.text);
						ret = true;
						save_user();
						$('proceed_form').submit();
					} else {
						$('processing').innerHTML = 'Purchase Details Incomplete. Please be sure ALL fields are complete.';
						ret = false;
					}
			  }
			  }).send();
	}

//	if (ret) alert("Form Sent");
//	else alert("Form NOT sent");
//	if (ret == 'true') { document.getElementById('proceed_form').submit(); }
//	else { alert('* Purchase Details Incomplete. Please be sure ALL fields in the left column are complete.'); }
}
	
function del_item(itemnum)
{
	var obj = document.getElementById('mini_cart');

	obj.innerHTML = "<div style=\"text-align: center; padding: 10px;\"><h2>Mini - Cart</h2><br><img src='images/layout/ajax-loader.gif'></div>";

	var myajax = new Request({
		  url: "ajax_rm_cart.php",
		  method: 'get', 
		  data: "item="+itemnum,
		  onComplete: function() {
	//		  alert(this.response.text);
			  if (this.response.text == 'false') 
			  {
					fade_out( $('mini_cart') );
			  } else {
				  obj.innerHTML = this.response.text;
			  }
		  }
	}).send();
}

function fade_in( obj )
{
	obj.style.opacity = 0;
	obj.className = '';
	obj.style.visibility = 'visible';
	obj.style.display = 'block';
	
	obj.fade('in');
}

function fade_out( obj )
{
	obj.fade('out');
}

function add_cart(itemnum)
{
	var obj = $('mini_cart');

	obj.innerHTML = "<div style=\"text-align: center; padding: 10px;\"><h2>Mini - Cart</h2><br><img src='images/layout/ajax-loader.gif'></div>";

	if (obj.className == 'hidden') { $('mini_cart').className = 'shown'; }
	
	fade_out( $('cart_icon') );
	fade_in( $('mini_cart') );
	
	var myajax = new Request( {
		  url: "ajax_add_cart.php", 
		  method: 'get', 
		  data: "item="+itemnum,
		  onComplete: function() {
				obj.innerHTML = this.response.text;
		  }
	}).send();
//	$('mini_cart').className = 'shown';
//	alert(itemnum);
}

function close_cart()
{
	fade_in( $('cart_icon') );
	fade_out( $('mini_cart') );
}

function load_user()
{

	user = document.getElementById('cust_email').value;
	pass = document.getElementById('pw').value;
	
	var myajax = new Request({
		  url: "ajax_load_user.php",
		  data : "email="+user+"&pass="+pass,
		  method: 'get', 
		  onComplete: function() {
				ret = [];
				ret = this.response.text;
				if (!ret) 
				{
					alert("Sorry. User not found. Enter your information, including a password, and click 'Save User' for your next visit.");
//					alert("failed  -  "+XMLHttpRequestObject.responseText);
				}
				else 
				{
		//			alert(ret);
					ret = ret.split(",");
					$('cust_name').value = ret[0];
					$('cust_address').value = ret[1];
					$('cust_city').value = ret[2];
					for (var i=0;i<$('cust_country').length; i++)
					{
						if ($('cust_country').options[i].text.toLocaleUpperCase() == ret[3].toLocaleUpperCase()) $('cust_country').options.selectedIndex = i;	
					}
					for (var i=0;i<$('cust_prov').length; i++)
					{
						if ($('cust_prov').options[i].text.toLocaleUpperCase() == ret[6].toLocaleUpperCase()) $('cust_prov').options.selectedIndex = i;	
					}
					$('cust_zip').value = ret[4];
					$('cust_phone').value = ret[5];
					reset_billing();
				}
		  }
	}).send();
//	alert(itemnum);
}

function reset_billing()
{
	var customer_prov = false;
	if ( $('cust_country').options[ $('cust_country').selectedIndex ].text == 'OTHER' ) {
		$('cust_prov').style.display = 'none';
		$('cust_prov_other').style.display = 'block';
		if ( $('cust_prov_other').text != '' ) customer_prov = $('cust_prov_other').value;
	} else {
		$('cust_prov').style.display = 'block';
		$('cust_prov_other').style.display = 'none';
		if (validator_quiet.isChosen($('cust_prov'))) customer_prov = $('cust_prov').options[ $('cust_prov').selectedIndex ].text;
	}
	var save = true;
	var ret = true;
	if (!validator_quiet.isEMailAddr($('cust_email'))) ret = false;
	if (!customer_prov) ret = save = false;
	if (!validator_quiet.isChosen($('cust_country'))) ret = save = false;
	if (!validator_quiet.isNotEmpty($('cust_email'))) ret = false;
	if (!validator_quiet.isNotEmpty($('cust_name'))) ret = false;
	if (!validator_quiet.isNotEmpty($('cust_address'))) ret = false;
	if (!validator_quiet.isNotEmpty($('cust_city'))) ret = false;
	if (!validator_quiet.isNotEmpty($('cust_zip'))) ret = false;
	if (!validator_quiet.isNotEmpty($('cust_phone'))) ret = false;
	var ship = '0';
	if ( document.getElementById('cust_shipping').checked ) ship = '1';

	var poststr = "email=" + encodeURI( $('cust_email').value ) +
				"&pw=" + encodeURI( $('pw').value ) +
				"&name=" + encodeURI( $('cust_name').value ) +
				"&addr=" + encodeURI( $('cust_address').value ) +
				"&city=" + encodeURI( $('cust_city').value ) +
				"&prov=" + encodeURI( customer_prov ) +
				"&country=" + encodeURI( $('cust_country').options[$('cust_country').selectedIndex].text ) +
				"&zip=" + encodeURI( $('cust_zip').value ) +
				"&phone=" + encodeURI( $('cust_phone').value ) + 
				"&comments=" + encodeURI( $('cust_comments').value ) + 
				"&is_shipping=" + ship;
	if (ret) {
		$('customer_icon_x').src = 'images/layout/checkmark.jpg';
		var myajax = new Request({
			 url: "ajax_reset_billing.php",
			 data: poststr,
			 method: 'get', 
			  async: false,
			  onComplete : function() { $('billing_summary_cont').innerHTML = this.response.text; }
		}).send();
	} else {
		$('customer_icon_x').src = 'images/layout/xmark.jpg';
		$('billing_icon_x').src = 'images/layout/xmark.jpg';
	}
}

function check_payment_method()
{
	if ( $('method1').checked ) {
		 $('payment_icon_x').src = 'images/layout/checkmark.jpg';
	}
	if ( $('method2').checked ) {
		 $('payment_icon_x').src = 'images/layout/checkmark.jpg';
	}
	if ( $('method3').checked ) {
		 $('payment_icon_x').src = 'images/layout/checkmark.jpg';
	}
}


function save_user()
{
	var poststr = "email=" + encodeURI( $('cust_email').value ) +
				"&pw=" + encodeURI( $('pw').value ) +
				"&name=" + encodeURI( $('cust_name').value ) +
				"&addr=" + encodeURI( $('cust_address').value ) +
				"&city=" + encodeURI( $('cust_city').value ) +
				"&prov=" + encodeURI( $('cust_prov').options[$('cust_prov').selectedIndex].text ) +
				"&country=" + encodeURI( $('cust_country').options[$('cust_country').selectedIndex].text ) +
				"&zip=" + encodeURI( $('cust_zip').value ) +
				"&phone=" + encodeURI( $('cust_phone').value );

		var myajax = new Request({
			  url: "ajax_save_user.php",
			  data: poststr,
			  method: 'post', 
			  async: false,
				onComplete : function () {
					reset_billing();
				}
		}).send();

}


