

function initPage() {
	
	if (is_color_select) {
		initColorSelection();
	}
	
	
	if (is_find_wine) {
		initFindWine();
	}
	
}


function initFindWine() {
	var the_form = document.getElementById(find_wine_form_id);
	the_form.submit();
}


function initColorSelection() {
	// get all imgs
	img_tags = document.getElementsByTagName('img');
	
	//alert(img_tags.length);
	for (var x=0; x<img_tags.length; x++) {
		//alert(img_tags[x].className);
		
		if (img_tags[x].className == 'colorChip') {
			img_tags[x].onclick = selectColor;
		}
	}
}


function selectColor() {
	// get the sibling nodes for the img node
	var sibling_nodes = this.parentNode.childNodes;
	
	// loop the siblings
	for (var x=0; x<sibling_nodes.length; x++) {
		//if the sibling is a span
		if (sibling_nodes[x].nodeName == 'SPAN') {
			// alter the text of the span tot the value of the img alt
			sibling_nodes[x].firstChild.nodeValue = this.alt;
		}
		
		// if the node is an img
		if (sibling_nodes[x].nodeName == 'IMG') {
			// reset any previously selected img
			sibling_nodes[x].style.borderColor = '#fff';
		}
	}
	
	this.style.borderColor = '#000';
	
	var color_input_node = document.getElementById('prodColorInput');
	color_input_node.value = this.alt;
}


function removeCartItem(form_id, index, qty) {
	updateCartQty(index, qty);
	
	var the_form = document.getElementById(form_id);
	the_form.submit();
}

function updateCartQty(index, qty) {
	var qty_field = document.getElementById('qty[' + index + ']');
	qty_field.value = qty;
}







//==================================================
// BEGIN BILL SHIP INFO 
//==================================================

var billInputs = new Array('fname', 'lname', 'address', 'address2', 'city', 'zip', 'phone','company');
var billSelects = new Array('state');
var shipInputs = new Array('shipFirst', 'shipLast', 'shipAddress', 'shipAddress2', 'shipCity', 'shipZip', 'shipPhone', 'shipCompany');
var shipSelects = new Array('shipState');


function fillShipInfo(obj) {
	if (obj.checked == true) {
		setShipInfo('bill');
	} else {
		setShipInfo('reset');
	}
}

//------------------------------------------------------------------------------------------------------------------------------------

function setShipInfo(data) {
	var form = document.orderForm;
	var limitInputs = billInputs.length;
	var limitSelects = billSelects.length;
	
	if(data == 'bill') {
		for(var x=0; x<limitInputs; x++) {
			
			form[ shipInputs[x] ].value = form[ billInputs[x] ].value;
		}
		
		for(var x=0; x<limitSelects; x++) {
			form[ shipSelects[x] ].options.selectedIndex = form[ billSelects[x] ].options.selectedIndex
		}
		
	}else{
		for(var x=0; x<limitInputs; x++) {
			form[ shipInputs[x] ].value = '';
		}
		
		for(var x=0; x<limitSelects; x++) {
			form[ shipSelects[x] ].options.selectedIndex = 0;
		}
		
	}
}

function validateForm_BillShip(the_form) {
	
	var is_valid = true;	// we assume it is a valid form
	// custom code can be added to this fucntion if form specific actions are required
	
	// call the standard form validation function
	is_valid = validateForm(the_form);
	
	return is_valid;

}

//==================================================
// END BILL SHIP INFO 
//==================================================


function validateForm_paymentForm(the_form) {
	
	var is_valid = true;	// we assume it is a valid form
	// custom code can be added to this fucntion if form specific actions are required
	
	// call the standard form validation function
	is_valid = validateForm(the_form);
	
	return is_valid;

}


//------------------------------------------------------------------------------------------------------------------------------------

function getBrowserSize() {
	var dimensions = new Array();
	var width = 640;
	var height = 480;
	
	// see if the browser has support for width / height
	if (document.documentElement.clientHeight) {
		width = document.documentElement.clientWidth;	
		height = document.documentElement.clientHeight;
	} else if (document.body.clientHeight) {
		width = document.body.clientWidth;	
		height = document.body.clientHeight;
	} else if (window.innerHeight) {
		width = window.innerWidth;	
		height = window.innerHeight;
	}
	
	dimensions['width'] = width;
	dimensions['height'] = height;
	
	
	return dimensions;
}

//------------------------------------------------------------------------------------------------------------------------------------

function openNewWindow(url) {
	var sizes = getBrowserSize();
	var width = Math.round( sizes['width'] * .75 );
	var height = Math.round( sizes['height'] * .75 );
	
	//alert(sizes['width'] + ' ' + width);
	
	var param_str = 'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=' + width + ', height=' + height + ', left=0, top=0';
	
	window.open(url, 'open_window', param_str);
}

//------------------------------------------------------------------------------------------------------------------------------------

function displayCertText(element, id, state) {
	var img_coords = findPos(element);
	//alert(img_coords[0]);
	
	var text_node = document.getElementById(id);
	
	if (state == 'block') {
		text_node.style.left = (parseInt(img_coords[0]) + 15) + 'px';
		text_node.style.top = (parseInt(img_coords[1]) + 65) + 'px';
	}
	
	text_node.style.display = state;
	
}

//------------------------------------------------------------------------------------------------------------------------------------

function findPos(obj) {
	
	
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	//return [curleft,curtop];
	var coords = new Array(curleft,curtop);
	
	
	return coords;
}


//------------------------------------------------------------------------------------------------------------------------------------