/*

	NOTE: Any form validation methods should be included in:
	/assets3/common/javascript/scformvalidation.js
	
*/

function chooseImage() {

	var pre = '<img src="assets/images/home/'
	var suf = '.jpg" width="527" height="351" alt="St. Margarets Golf and Country Club" vspace="0" hspace="0" border="0">';
	
	var x = Math.random();
	var imageName;
	if ( x < .25 ) imageName = 'harrington';
	else if ( x < .5 ) imageName = 'harrington';
	else if ( x < .75 ) imageName = 'clubhouse';
	else imageName = 'clubhouse';
	
	document.open();
	document.writeln( pre + imageName + suf );
	document.close();
}

function chooseChampion() {

	var pre = '<img src="assets/images/common/'
	var suf = '.jpg" width="194" height="134" alt="Past Champions">';
	
	var x = Math.random();
	var imageName;
	if ( x < .40 ) imageName = 'side_harrington';
	else if ( x < .80 ) imageName = 'side_davies';
	else imageName = 'side_horton';
	
	document.open();
	document.writeln( pre + imageName + suf );
	document.close();
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function stripNonNumbers( _input ) {
	
	var result = "";
	var l = _input.length;
	var i = 0;

	for ( i=0; i<l; i++ ) {
		var c = _input.substr(i,1);
		if ( !isNaN( parseInt( c ) ) ) { result += c; }
	}

	return result;
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}


function clearSelect( select )
{
   var selLength = select.length;
  if(selLength>0)
  {
    for(i=selLength-1; i>=0; i--)
  	{
  		select.options[i] = null;
	}
  }
}

function moveElementUp(select) {
  	var name = dwr.util.getValue("menupages");
	var selLength = select.length;
	var elementsSel = select.options;
	var elementsList = new Array();
   	for(i=0;i<select.length; i++)
 	{
		if(select.options[i].selected)
		{
			if(i>0)
			{
	 			var  elText = select.options[i].text;
	 			var elVal = select.options[i].value;
	 			var swpEl = select.options[i-1].text;
	 			var swpVal = select.options[i-1].value;
	 			
	 			select.options[i].text = swpEl;
	 			select.options[i].value = swpVal;
	 			select.options[i-1].text = elText;
	 			select.options[i-1].value = elVal;
	 			select.options[i-1].selected=true;	 			
	 			select.options[i].selected=false;	 			
	 		} 
 		} 
 		
	}
}

function moveElementDown(select) {

  	var name = dwr.util.getValue("menupages");
	var selLength = select.length;

   	for(i=0;i<select.length; i++)
 	{
		if(select.options[i].selected)
		{
			if(i<selLength-1)
			{
	 			var elText = select.options[i].text;
	 			var elVal = select.options[i].value;
	 			var swpEl = select.options[i+1].text;
	 			var swpVal = select.options[i+1].value;
	 			
	 			select.options[i].text = swpEl;
	 			select.options[i].value = swpVal;
	 			select.options[i+1].text = elText;
	 			select.options[i+1].value = elVal;
	 			select.options[i+1].selected=true;	 			
	 			select.options[i].selected=false;	 
	 			i++;			
	 		} 
 		}  		
	}
}

function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
function moveOptions(theSelFrom, theSelTo)
{  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  var elementsList = new Array();
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }

  if(NS4) history.go(0);
}

function copyOptions(theSelFrom, theSelTo)
{  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  var elementsList = new Array();
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      //deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }

  if(NS4) history.go(0);
}

/*
Following 2 functions move elements up / Down in a select list (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C5F00602363)
Both functions are pretty straightforward. First, make sure it makes sense to move 
the entry (if there's only one value, then there's no sense in trying to move it) 
and make sure the user has selected an entry. Second, make sure the item can be 
moved (the first item cannot be moved up, the last item cannot be moved down). 
If the item can be moved, then swap the text and value with it's correct neighbor. 
That's it. Here's the "move up list" function:

*/
function moveUpList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == 0 ) {
               alert("The first entry in the list cannot be moved up.");
            } else {
               // Get the text/value of the one directly above the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected-1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected-1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected-1].text = moveText2;
               listField[selected-1].value = moveValue2;
               listField.selectedIndex = selected-1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function moveDownList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == listField.length-1 ) {
               alert("The last entry in the list cannot be moved down.");
            } else {
               // Get the text/value of the one directly below the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected+1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected+1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected+1].text = moveText2;
               listField[selected+1].value = moveValue2;
               listField.selectedIndex = selected+1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function hidediv(id) {
	//safe function to hide an element with a specified 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';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified 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 checkAllCheckboxes( field ) {

	for ( i = 0; i < field.length; i++ )
		field[i].checked = true;
}

function uncheckAllCheckboxes( field ) {

	for ( i = 0; i < field.length; i++ )
		field[i].checked = false ;
}


function SetAllCheckBoxes( FormName, FieldName, CheckValue ) {

	if ( !document.forms[FormName] )
		return;
		
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if( !objCheckBoxes )
		return;
		
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for ( var i = 0; i < countCheckBoxes; i++ )
			objCheckBoxes[i].checked = CheckValue;
}

function toggleAllCheckBoxes( FormName, FieldName ) {

	if ( !document.forms[FormName] )
		return;
		
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if ( !objCheckBoxes )
		return;
		
	var countCheckBoxes = objCheckBoxes.length;
	if ( !countCheckBoxes )
		return;

	for ( var i=0; i<countCheckBoxes; i++ )
		objCheckBoxes[i].checked = !objCheckBoxes[i].checked;
			
}

function PopulateContentTags()
{
  	var txtSelectedValuesObj = document.getElementById('content-tags');
 	var selectedArray = new Array();
  	var selObj = document.getElementById('plugin-tags');
  	var i;
  	var count = 0;
  	for (i=0; i<selObj.options.length; i++) {
    	if (selObj.options[i].selected) {
      	selectedArray[count] = selObj.options[i].value;
      	count++;
    	}
 	}
  	txtSelectedValuesObj.value = selectedArray;
}

function PopulateIncludedContent(includedName, hiddenName)
{
  	var txtSelectedValuesObj = document.getElementById(hiddenName);
 	var selectedArray = new Array();
  	var selObj = document.getElementById(includedName);
  	var i;
  	var count = 0;
  	for (i=0; i<selObj.options.length; i++) {
    	if (selObj.options[i].selected) {
      	selectedArray[count] = selObj.options[i].value;
      	count++;
    	}
 	}
  	txtSelectedValuesObj.value = selectedArray;
}


// EVERYTHING BELOW HERE IS FORM VALIDATION
// IT USED TO BE BOTH HERE AND IN scformvalidation.js
// BUT IT IS NOW ALL IN scformvalidation.js FOR CONSISTENCY

/*

function sc_valid_url( _url, _min, _max ) { 

	if ( _url < _min  || _url > _max  )
		return false;

	var v = new RegExp(); 
	v.compile("^(https?|ftp|file)://.+$"); 
	if ( !v.test( _url ) ) 
		return false; 

	return true;
} 

function valid_url( _url, _min, _max ) { 

	if ( _url < _min  || _url > _max  )
		return false;

	var v = new RegExp(); 
	//v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); 
	//v.compile("/^[a-z]+:\/\//i"); 
	v.compile("^(https?|ftp|file)://.+$"); 
	if ( !v.test( _url ) ) 
		return false; 

	return true;
} 

function valid_url( _url ) { 

	if ( _url < 5  || _url > 256  )
		return false;
		
	var v = new RegExp(); 
	v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); 
	if ( !v.test( _url ) ) 
		return false; 
	
	return true;
}

function valid_phone( _currentValue ) {

	_currentValue = stripNonNumbers( _currentValue );
	if ( (_currentValue.length < 8) || (_currentValue.length > 32) ) { return false; }
	else { return true; }
}

function valid_phone( _currentValue, _min, _max ) {
	_currentValue = stripNonNumbers( _currentValue );
	if ( ( _currentValue.length < _min ) || ( _currentValue.length > _max ) ) { return false; }
	else { return true; }
}

function valid_email( _currentValue, min, max ) {

	var _emailError = isEmail( _currentValue );
	if ( _emailError.length > 0 ) { return false; }
	else { return true; }
}

function isEmail( _input, min, max ) {
	
	var err = "";
	var blankSpace = " ";
	var foundAt = false;
	var foundDot = false;

	var l = _input.length;
	var i = 0;

	if ( ( _input.length < (min*1) ) || ( _input.length > (max*1) ) ) {
		err = "Email address must use between " + min + " and " + max + " characters.";
		return err;
	}

	for ( i=0; i<l; i++ ) {
		var c = _input.substr(i,1);

		if ( c == blankSpace ) { 
			err = "Email address cannot contain spaces."	
			result = false;
		}
		if ( c == "@" ) { foundAt = true; }
		if ( c == "." ) { foundDot = true; }
	}

	if ( foundAt == false ) { err = "Email address must contain an \'@\' symbol to be valid.";  }
	if ( foundDot == false ) { err = "Email address must contain a \'.\' symbol to be valid.";  }

	return err;
}

function trimLeadingZeros( n ) {

	while ( true ) {

		if ( ( n.length > 1 ) && ( n.indexOf( "0" ) == 0 ) ) n = n.substr( 1 );
		else break;
	}
	return n;
}

function validateInt( n ) {

	if ( ( n == null ) || ( n == undefined ) || ( n.length < 1 ) ) return false;
	n = trimLeadingZeros( n );
	return ( "" + parseInt(n) ) == n;
}

function isValidDate() {
	
	if( validateInt(this.year) == false ) return false;
	if( validateInt(this.month) == false ) return false;
	if( validateInt(this.date) == false ) return false;
	
	if ( ( this.year > 0 ) && ( this.year < 3000 ) ) {;}
	else { return false; }
	if ( ( this.month >= 0 ) && ( this.month <= 12 ) ) {;}
	else { return false; }
	if ( ( this.date >= 0 ) && ( this.date <= 31 ) ) {;}
	else { return false; }
	
	return true;
}

function dateToString() {

	return this.year + "/" + this.month + "/" + this.date;
}

function compareDate( _date ) {

	if ( ( this.year*1 == _date.year*1 ) && ( this.month*1 == _date.month*1 ) && ( this.date*1 == _date.date*1 ) ) return 0;
	
	if ( this.year*1 < _date.year*1 ) return -1;
	if ( this.year*1 > _date.year*1 ) return 1;
	
	if ( this.month*1 < _date.month*1 ) return -1;
	if ( this.month*1 > _date.month*1 ) return 1;
	
	if ( this.date*1 < _date.date*1 ) return -1;
	if ( this.date*1 > _date.date*1 ) return 1;
	
	return 0;
}

function getGIIDateNow() {

	var now = new Date();
	var newDate = new GIIDate( now.getFullYear(), now.getMonth() + 1, now.getDate() );
	return newDate;
}

function GIIDate( y, m, d ) {

	y += ""; m += ""; d += "";
	this.year = y;
	this.month = trimLeadingZeros( m );
	this.date = trimLeadingZeros( d );
	
	this.compareDate = compareDate;
	this.isValidDate = isValidDate;
	this.getGIIDateNow = getGIIDateNow;
	this.toString = dateToString;
}


function validateNewsletterForm( f, min, max ) {

	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;

	if ( !valid_email( f.email.value, min, max ) ) {
		if ( _focusField == null ) _focusField = f.email;
		_error_mid += _tab + 'Please enter a valid e-mail address where where the newsletter will be sent.\n';
	}

	if ( _error_mid.length > 0 ) {
		alert ( _error_start + _error_mid + "\n" + _error_end );
		_focusField.focus();
		return false;
	}
	
	return true;
}
*/
