/* scserver.js - Javascript common to all Site Caddy websites
 */

//
// --------- Page preview ------------
//

function alertNoLinks(evt) {
	window.alert('Links are disabled on Preview pages');
	if (evt.preventDefault) {
		evt.preventDefault();
	}
	return false;
}

function disableLinks() {
	var lLinks = document.links;	// avoids silly Eclipse warning
	for (var i=0; i < lLinks.length; i++) {
		var oLink = lLinks[i];
		if (oLink.addEventListener) {
			oLink.addEventListener("click", alertNoLinks, false);
		} else if (oLink.attachEvent) {
			oLink.attachEvent('onclick', alertNoLinks);
		}
	}
}

//
// --------- Menus ------------
//
//	For drop down menus.
//

startList = function() {
 if (document.all&&document.getElementById) {
  navRoot = document.getElementById("primaryNav");
  for (i=0; i<navRoot.childNodes.length; i++) {
   node = navRoot.childNodes[i];
   if (node.nodeName=="LI") {
    node.onmouseover=function() {
     this.className+=" over";
    }
    node.onmouseout=function() {
     this.className=this.className.replace(" over", "");
    }
   }
  }
 }
}

//
// --------- Shop Caddy ------------
//
//	This will be moved to the Shop Caddy JS file when Module Manager
//  is in place.  - NS
//

function validateReqProdAttrs(frm) {
	try {
		var bOK = true;
		for (var i=0; i<frm.elements.length; i++) {
			var elt = frm.elements[i];
			if ((elt.type == 'select-one' || elt.type == 'select-multiple') &&
					elt.className.match(/.*isRequired.*/)) {
				bOK = bOK && (elt.value !== "");
			}
		}
		if (!bOK) {
			window.alert('Please select all required options (denoted with an asterisk) ' +
			      		 'before adding this product to your cart.');
		}
		return bOK;
	} catch(e) {
		return true;
	}
}

//safe function to hide an element with a specified id
function hidediv( id ) {

	if ( document.getElementById ) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if ( document.layers ) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

//safe function to show an element with a specified id
function showdiv( id ) {

		  
	if ( document.getElementById ) { // DOM3 = IE5, NS6
		document.getElementById( id ).style.display = 'block';
	}
	else {
		if ( document.layers ) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

function normalizeCountryCode( countryCode ) {

	if ( countryCode == null || countryCode == '' || countryCode == 'undefined' || countryCode.length < 1 )
		return 'XX';

	if ( countryCode != 'US' && countryCode != 'CA' && countryCode != 'IE' && countryCode != 'UK' && countryCode != 'AU' && countryCode != 'XX' ) 
		return 'XX';

	return countryCode;
}

function displayCountryDetailsMaster( countryCode ) {


	hidediv( 'labelStateUS' );
	hidediv( 'labelStateCA' );
	hidediv( 'labelStateIE' );
	hidediv( 'labelStateUK' );
	hidediv( 'labelStateAU' );
	hidediv( 'labelStateXX' );

	hidediv( 'fieldStateUS' );
	hidediv( 'fieldStateCA' );
	hidediv( 'fieldStateIE' );
	hidediv( 'fieldStateUK' );
	hidediv( 'fieldStateAU' );
	hidediv( 'fieldStateXX' );

	hidediv( 'labelCodeUS' );
	hidediv( 'labelCodeCA' );
	hidediv( 'labelCodeIE' );
	hidediv( 'labelCodeUK' );
	hidediv( 'labelCodeAU' );
	hidediv( 'labelCodeXX' );

	showdiv( 'labelState' + countryCode );
	showdiv( 'fieldState' + countryCode );	
	showdiv( 'labelCode' + countryCode );
}
