//script and formatting modified by KLB, 07/21/2006
/****************************************************
*	        DOM Image rollover:
*		by Chris Poole
*		http://chrispoole.com
*               Script featured on http://www.dynamicdrive.com
*		Keep this notice intact to use it :-)
****************************************************/

function init() {
	if (!document.getElementById) return
	
	var imgOriginSrc;
	var imgTemp = new Array();
	var imgarr = document.getElementsByTagName('img');
	
	for (var i = 0; i < imgarr.length; i++) 
	{
		if (imgarr[i].getAttribute('hsrc')) 
		{
			imgTemp[i] = new Image();
			imgTemp[i].src = imgarr[i].getAttribute('hsrc');
			imgarr[i].onmouseover = function() {
				imgOriginSrc = this.getAttribute('src');
				this.setAttribute('src',this.getAttribute('hsrc'))
			}
			
			imgarr[i].onmouseout = function() {
				this.setAttribute('src',imgOriginSrc)
			}
		}
	}
  
  	//sets action for cbxShipToBilling if cbxShipToBilling is on the page
	//added by KLB
	if (document.getElementById( 'cbxShipToBilling' )) 
	{
		var cbxShipToBilling = document.getElementById( 'cbxShipToBilling' );
		
		cbxShipToBilling.onchange = function() { 
			checkForm();
			return true; 
		};
	}
	
	//hides frmShipTrack if frmShipTrack is on the page
	//added by KLB
	if (document.getElementById( 'frmShipTrack' )) 
	{
		hideElement('frmShipTrack');
	}
	
}//end init function

/****************************************************
*	populates and disables shipping address inputs if user checks "Same as Billing Address"
*	note: if user does not have javascript enabled, inputs will not be populated or disabled on check
****************************************************/
function checkForm() {
	var ShipToBilling = document.getElementById( 'cbxShipToBilling' ).checked;
	var ShippingInfo = new Array();
	ShippingInfo[0] = ['add1ship', document.getElementById( 'add1' ).value];
	ShippingInfo[1] = ['add2ship', document.getElementById( 'add2' ).value];
	ShippingInfo[2] = ['cityship', document.getElementById( 'city' ).value];
	ShippingInfo[3] = ['stateship', document.getElementById( 'state' ).value];
	ShippingInfo[4] = ['zipship', document.getElementById( 'zip' ).value];
	ShippingInfo[5] = ['nameship', document.getElementById( 'name' ).value];
	
	if (ShipToBilling)
	{
		for (var i=0; i < ShippingInfo.length; i++)
		{
			document.getElementById( ShippingInfo[i][0] ).value = ShippingInfo[i][1];
			document.getElementById( ShippingInfo[i][0] ).style.background = '#F0EBCE';
			document.getElementById( ShippingInfo[i][0] ).disabled = true;
		}
	} else {
		for (var i=0; i < ShippingInfo.length; i++)
		{
			document.getElementById( ShippingInfo[i][0] ).value = '';
			document.getElementById( ShippingInfo[i][0] ).style.background = '#F8F7EA';
			document.getElementById( ShippingInfo[i][0] ).disabled = false;
		}
	}
}//end checkForm function

/****************************************************
*	hides elements whose id is the parameter
****************************************************/
function hideElement(element) {
	
	document.getElementById( element ).style.display = 'none';
	
}

/****************************************************
*	shows elements whose id is the parameter
****************************************************/
function showElement(element) {
	
	document.getElementById( element ).style.display = 'block';
	
}

/****************************************************
*	shows, styles, and populates frmShipTrack
****************************************************/
function markAsShipped(confNo) {
	
	document.getElementById('txtError').innerHTML = '';
	
	document.getElementById( 'frmShipTrack_cancel' ).style.display = 'block';
	document.getElementById( 'txtConfNo' ).value = confNo;
	document.getElementById( 'txtConfNo' ).readonly = true;
	document.getElementById( 'txtConfNo' ).style.border = 'none';
	document.getElementById( 'frmShipTrack' ).style.display = 'block';
	document.getElementById( 'frmShipTrack' ).style.position = 'fixed';
	document.getElementById( 'frmShipTrack' ).style.top = '20%';
	//to come: center pop-up form in browser
	document.getElementById( 'frmShipTrack' ).style.left = '25%';

}


/****************************************************
*	validates frmShipTrack 
*	if fields are empty, form is not submitted
****************************************************/
function submitForm() {
	
	var strError = "";
	var confNo = document.getElementById('txtConfNo').value;
	var trackNo = document.getElementById('txtTrackNo').value;
	
	if ( isEmpty(confNo) ) { 
		strError = "Confirmation Number"; 
	}
	if ( isEmpty(trackNo) ) {
		if (strError) { strError += " and "; } 
		strError += "Tracking Number"; 
	}
	
	if (!strError) 
	{
		document.getElementById('frmShipTrack').submit();
	} else {
		strError = "Please enter a " + strError;
		document.getElementById('txtError').innerHTML = strError;
	}
	
}//end function submitForm


/****************************************************
*	checks to see if a form field is empty or contains only spaces
****************************************************/
function isEmpty(strText) {

	var lenString = strText.length;
	
	var i = 0;
	while (i < lenString) 
	{
		if(strText.substring(i,i+1) != " ") 
		{
			return false;
		}
		i++;
	}
	
	return true;
}//end isEmpty function


window.onload = function() {
	init();	
}