// **********************************************************************
//		Common JavaScript functions
//		Paddle Tennis web site - www.reveregroup.com/paddle
//
//		Created:	08/09/2001	Kristy O'Neal, The Revere Group
// **********************************************************************


// **********************************************************************
//		Method Name:	set_cookie(name, value, expires, path)
//					
//		Description:	Creates cookie with the specified name, value, expires,
//						and path attributes.
//						
//
//				EX INPUT: set_cookie("level", "black", "04/30/2001","/")
//				OUTPUT:	  None
//		
//		Return Type:	None
//
//		Functions Used: None
//
// **********************************************************************
//		Parameter Listing
// **********************************************************************
//		Name:			Type:		Description:
//		--------		--------	-------------------------
//		name			string		The name of the cookie
//
//		value			string		The value of the cookie
//
//		expires			string		When the cookie should expire. If this value is not set,
//									the cookie is never written to the hard drive and 
//									disappears when the browser is closed.
//
//		path			string		By default, a cookie is only available to pages in the 
//									same directory as the page that created it, as well as  
//									any subdirectories. Setting this attribute makes the 
//									cookie visible in other directories as well. If this 
//									value is set to '/', the cookie is available on the 
//									entire site.
//
//		domain			string		By default, a cookie is only available to pages on the
//									same web server as the page that created it. Setting 
//									this attribute makes the cookie visible to other web
//									servers as well. The domain of the cookie cannot be set
//									to a domain other than the domain of your server.
//
//		security		boolean		Boolean value that determines how the cookie is 
//									transmitted over the network. By default, cookies are 
//									insecure and trasnmitted over normal, insecure HTTP 
//									connections. If the security of the cookie is set to be 
//									secure, it is only transmitted when the browser and 
//									server are connected via HTTPS or another secure protocol.
//
//		Notes:			This function currently does not set the domain or 
//						security attributes. If the cookie must be available
//						across multiple domains, this function will need to be
//						modified.
//
// **********************************************************************
function set_cookie(name,value,expires,path) {

	// Create the cookie string with specified name and value
	var cookie = name + '=' + value;

	// If no expires parameter was sent, set the expires value 
	//		for 10 years from today
	if (!expires) {
		// Create a new date
		var expireyear = new Date();
		// Set the date to 10 years from today
		expireyear.setFullYear(expireyear.getFullYear() + 10);
		expires = expireyear;
	}

	// Add the expires attribute to the cookie string
	// Convert the expires date into the proper format
	cookie += '; expires=' + expires.toGMTString();

	// If the path parameter was sent, add the path attribute 
	//		to the cookie string
	if (path) cookie += '; path=' + path;

	// Create the cookie using the cookie string
	document.cookie = cookie;
}


// **********************************************************************
//		Method Name:	read_cookie(name)
//					
//		Description:	Reads the value of a specific cookie.
//
//				EX INPUT: read_cookie("level")
//				OUTPUT:	  "black"
//		
//		Return Type:	String
//
//		Functions Used: None
//
// **********************************************************************
//		Parameter Listing
// **********************************************************************
//		Name:			Type:		Description:
//		--------		--------	-------------------------
//		name			string		Name of cookie to be read
// **********************************************************************
function read_cookie(name) {

	// Get all cookies related to the current document
	var allcookies = document.cookie;
	// If there are no cookies available, return null
	if (allcookies == "") return "null";

	// Get the value of the cookie with the input name
	// Look for the start of the specific cookie
	var start = allcookies.indexOf(name + '=');
	// If the specific cookie does not exist, return null
	if (start == -1) return "null";
	// Get the start of the specific cookie value by skipping 
	//		the name and '='
	start += name.length + 1;
	// Get the end of the specific cookie value by identifying
	//		the location of the ';'
	var end = allcookies.indexOf(';', start);
	// If the specific cookie is not followed by a ';, it is
	//		the last one in the list, so get the location of 
	//		the end of the cookie string
	if (end == -1) end = allcookies.length;

	// Get the value of the specific cookie
	// Return the cookie value
	var cookieval = allcookies.substring(start, end);
	return cookieval;
}
// ***********************************************************************
//  Created by Jennifer McCord  The Revere Group  8/29/01
//  Error handling for Database adding, deleting, and editing
// ***********************************************************************

function DatabaseCheck() {
  var errorMessage
  
  errorMessage = "The database was unable to process your request."
  alert(errorMessage);
 }



function fnValidTournamentDate(field, errorMessage)
{

  var DateField = field;
  var intStatusVal=0;
  var returnStatus = false;

  if(DateField.value != "")
  {
  
  	 intStatusVal=validate_date(DateField);   


	 if(intStatusVal > 0  && intStatusVal != 2)
	 {
		switch (intStatusVal)
		{
				case 1:
	  		        errorMessage.style.display="";
			        errorMessage.innerHTML="Date must be in the format mm/dd/yyyy.";
					break;
				case 2:
	  		        //do nothing the date can be greater than today's date
					break;
				case 3:
	  		        errorMessage.style.display="";
			        errorMessage.innerHTML="Date cannot be less than 150 years.";
					break;
				case 4:
	  		        errorMessage.style.display="";
			        errorMessage.innerHTML="Date is incorrect.";
					break;
				default:
	  		        errorMessage.style.display="";
			        errorMessage.innerHTML="Date is incorrect.";
					break;
        }
	    
	    return true;
	 }   
  //no error occurred so return false 
  errorMessage.style.display="none";
  return false;		
	 
  }
  else
  {
    //validation error occurred return true.
	 errorMessage.style.display="";
	 errorMessage.innerHTML="Date is required.";
	 return true;		  
  }


}



function validate_date(field){

	var DateValue = field.value;
	
		// Check to see if a seperator is already present.
		// bypass the date if a seperator is present and the length greater than 8
		if (DateValue.length==10) {
			if (DateValue.indexOf("/") != 2)
				return 1;
		}
		else{
			return 1;
		}

		//Eliminate all the ASCII codes that are not valid
		var alphaCheck = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-";
		if (alphaCheck.indexOf(DateValue) >= 1) {
		return 1;
		}

		var checkstr = "0123456789";
		var DateTemp=0; 
		// Delete all chars except 0..9 
		for (i = 0; i < DateValue.length; i++) {
			if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
				DateTemp = DateTemp + DateValue.substr(i,1);
			}
		}

		// Always change date to 0+8 digits - string
		if (DateTemp.length!=9) {
				return 1;
		}
		else{
			DateTemp=DateTemp.substr(1,8)
		}
		
		var intVal = CompareDate(DateTemp);
		
		//compare with current year 
		
		switch (intVal){
			case 1:
				return 2;
				break;
			case 2:
				return 3;
				break;
			default:
				break;
		}
		
        //Compares date format 01/01/2004
		var isValid =DateValue.match(/^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{4})))$/)!=null;

		if(isValid)
			return 0;
		else
			return 4;
	
}
	
    //This function Compares Date.
function CompareDate(pDate) 
{
		//Variable Declaration.	 
		var mIntStartDate, mIntEndDate;
		var mIntStartMonth, mIntEndMonth;
		var mIntStartYear, mIntEndYear;
		
		var Today = new Date();

		mIntStartYear=pDate.substr(4,4);
		mIntStartMonth=pDate.substr(0,2);
		mIntStartDate=pDate.substr(2,2);
		
	
		mIntCurrentYear=Today.getFullYear();
		mIntCurrentMonth=Today.getMonth()+1;
		mIntCurrentDate=Today.getDate();

		if (mIntStartYear < (mIntCurrentYear-150)) 
			return 2;//false
		if (mIntStartYear > mIntCurrentYear) 
			return 1;//false
		else if (mIntStartYear < mIntCurrentYear) 
			return 0;//true
		else if (mIntStartMonth > mIntCurrentMonth)
			return 1;//false
		else if (mIntStartMonth < mIntCurrentMonth) 
			return 0;//true
		else if (mIntStartDate > mIntCurrentDate) 
			return 1;//false
		else if (mIntStartDate < mIntCurrentDate) 
			return 0;//true
		else 
			return 0;//true
}
	
//<!------ Date Validation ends here-->


//This function will change focus to the next field.
function changeCursor(from,to,len) {
	if(from.value.length>=len) {
		to.focus();
	}
	return true;
}



function fnAlphabetsOnlyRequired(field, fieldDisplayName)
{
    strTest=field.value;
    ErrorMessage = ""


	if(strTest!="")
	{
		if(!strTest.match("^[A-Za-z .-]+$"))
		{
		    ErrorMessage = "Please enter letters only for " + fieldDisplayName + "."
		}
		else
		{
		    //do nothing
		}
	}
	else
	{
		//validation error has occurred return true

		ErrorMessage = fieldDisplayName + " is required.";
	}

    return ErrorMessage
}			


