/* 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 );
}



/*
JavaScript methods related to the golf booking widget.
*/

/*
 * The booking widget can show a calendar widget.  If the golfer is a guest, then we 
 * show the calendar and give them the opportunity to pick a date for their round.
 * If not, then we don't need a date so we hide the calendar.
 * 
 * @param {Object} formName
 */
function scGolfBookShowHideDate( formName ) {
		
	var f = document.forms[ formName ];
	var selectedOption = f.scGolfBookGolfer.value;
	if ( selectedOption == "guest" )
		f.scGolfBookDateStart.style.display = 'block';
	else
		f.scGolfBookDateStart.style.display = 'none';
}

function scGolfBookValidateBookingWidget( f ) {

	var _error_start = "Sorry, but there was a problem with the information you entered.  Please correct the following errors:\n\n";
	var _error_mid = "";
	var _error_end = "Click OK to continue.";
	var _tab = "- ";
	var _focusField = null;

	var selectedOption = f.scGolfBookGolfer.value;
	var date = f.scGolfBookDateStart.value;
	
	if ( selectedOption.length == 0 ) {
		if ( _focusField === null ) { _focusField = f.scGolfBookGolfer; }
		_error_mid += _tab + 'Please enter type of booking - member or guest.\n';
	}

	if ( selectedOption == 'guest' && ( date.length == 0 || date == 'Choose Date' ) ) {
		if ( _focusField === null ) { _focusField = f.scGolfBookDateStart; }
		_error_mid += _tab + 'Please enter a date for the booking.\n';
	}

	if ( _error_mid.length > 0 ) {
		window.alert ( _error_start + _error_mid + "\n" + _error_end );
		_focusField.focus();
		return false;
	}

	return true;
}

function scGolfBookFormatDate_EUtoBRS( date ) {
	
	//date = date.replace( /\//g, "-" );
	// parse the date
	
	var dateArray = date.split( "-" );
	var dd = dateArray[0];
	var mm = dateArray[1];
	var yy = dateArray[2];
	
	var brsDate = yy + "-" + mm + "-" + dd;
	return brsDate;
}

//http://www.brsgolf.com/royaldublin/visitor_month.php?course_id=1
//http://www.brsgolf.com/royaldublin/visitor_day.php?d_date=2011-04-18
//http://www.brsgolf.com/royaldublin/members_booking.php?operation=member_info
function scGolfBookGoToBookGolf( formName, newURL ) {
		
	if ( newURL == null || newURL == "" )
		newURL = "http://www.brsgolf.com/royaldublin/";
	var f = document.forms[ formName ];

	// validate the input
	if ( !scGolfBookValidateBookingWidget( f ) )
		return;
	
	// get the date, and reformat it for BRS (yyyy-mm-dd)
	var date = f.scGolfBookDateStart.value;
	date = scGolfBookFormatDate_EUtoBRS( date );

	// create the URL based on the type of golfer
	var selectedOption = f.scGolfBookGolfer.value;
	if ( selectedOption == "member" ) {
		
		newURL += "members_booking.php?operation=member_info";
		//newURL += "members_home.php?";
	}
	else if ( selectedOption == "guest" ) {
		
		newURL += "visitor_day.php?";
		newURL += "d_date=" + date;
		newURL += "&course_id1=1";
	}
	else if ( selectedOption == "open" ) {
		
		newURL += "opens_home.php"
	}
	else if ( selectedOption == "availability" ) {
		
		newURL += "visitor_availability_month.php";
	}
	
	window.location = newURL;
}

