//functions
function Save()
	{	
		var errorMessage = "Please correct the following: \n\n";
	    var errorFound = 0;
		
		if ( document.getElementById('givingForm').ddState.selectedIndex == 0) 
			{errorMessage += "- State is required\n"; errorFound = 1; }		
		
	
	if ( trim(document.getElementById('givingForm').txtFirstName.value) == '') 
			{errorMessage += "- First name is required\n"; errorFound = 1; }

		if ( trim( document.getElementById('givingForm').txtLastName.value ) == '') 
			{errorMessage += "- Last name is required\n"; errorFound = 1; }

		if ( trim(document.getElementById('givingForm').txtAddress.value) == '') 
			{errorMessage += "- Address is required\n"; errorFound = 1; }
		
		if ( trim(document.getElementById('givingForm').txtCity.value) == '') 
			{errorMessage += "- City is required\n"; errorFound = 1; }

		if ( trim(document.getElementById('givingForm').txtZip.value) == '') 
			{errorMessage += "- Zip is required\n"; errorFound = 1; }

		if ( trim(document.getElementById('givingForm').txtHomePhone.value) == '') 
			{errorMessage += "- Home phone is required\n"; errorFound = 1; }
			
		if ( trim(document.getElementById('givingForm').txtAmount.value) == '') 
			{errorMessage += "- Donation Amount is required\n"; errorFound = 1; }
		else if ( NaN(trim(document.getElementById('givingForm').txtAmount.value))) 
			{errorMessage += "- Donation Amount can only contain numbers\n"; errorFound = 1; }

		if (errorFound == 1)
	    {
	        alert(errorMessage);
	    } 
	    else
	    {	
			document.getElementById('givingForm').hMode.value = 'save';
			document.getElementById('givingForm').action= '/process_giving/donation_registration.php';
			document.getElementById('givingForm').submit();
		}
	}
	
	      
      function Cancel()
	{
		document.location.href='http://www.ronniefloyd.com';
     }


//commmons
// set string to proper casing
function PCase(STRING)
{
	var strReturn_Value = "";
	var iTemp = STRING.length;
	if(iTemp==0)
	{
		return"";
	}
	var UcaseNext = false;
	strReturn_Value += STRING.charAt(0).toUpperCase();
	for(var iCounter=1;iCounter < iTemp;iCounter++)
	{
		if(UcaseNext == true)
		{
			strReturn_Value += STRING.charAt(iCounter).toUpperCase();
		}
		else
		{
			strReturn_Value += STRING.charAt(iCounter).toLowerCase();
		}
		var iChar = STRING.charCodeAt(iCounter);
		if(iChar == 32 || iChar == 45 || iChar == 46)
		{
			UcaseNext = true;
		}
		else
		{
			UcaseNext = false
		}
		if(iChar == 99 || iChar == 67)
		{
			if(STRING.charCodeAt(iCounter-1)==77 || STRING.charCodeAt(iCounter-1)==109)
			{
				UcaseNext = true;
			}
		}
	
	} //End For

	return strReturn_Value;
} //End Function


	
	function ValidName(string) 
	{
		var notAllowed = '0123456789!@#$%^&*()+=|\~`{[}]:;,<>'; // define non-valid characters
		
	    for (var i=0; i< string.length; i++) 
	    {
	        if (notAllowed.indexOf(string.charAt(i)) > 0) 
	            return false;
	    }
	    
	    var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	    string = string.toUpperCase();
	    
	    // make sure there is at least one letter in the string instead of all dashes
	    for (var i=0; i< string.length; i++) 
	    {
	        if (alpha.indexOf(string.charAt(i)) > 0) 
	            return true;
	    }
	    
	    return false;
	}
	
	function IsAlpha(string) 
	{
		var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	    string = string.toUpperCase();
	    
	    for (var i=0; i< string.length; i++) 
	    {
	        if (alpha.indexOf(string.charAt(i)) < 0) 
	            return false;
	    }
	    
	    return true;
	}
	
	function ValidAddress(string) 
	{
		var notAllowed = '!@$%^&*()+=|\~`{[}]:;,<.>'; // define non-valid characters
		
	    for (var i=0; i< string.length; i++) 
	    {
	        if (notAllowed.indexOf(string.charAt(i)) > 0) 
	            return false;
	    }
	    return true;
	}

	
function isValidDate(dateStr) 
{
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

	// To require a 4 digit year entry, use this line instead:
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		//alert("Date is not in a valid format.")
		return false;
	}

	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
		//alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31) {
		//alert("Day must be between 1 and 31.");
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		//alert("Month "+month+" doesn't have 31 days!")
		return false
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>2929 || (day==29 && !isleap)) {
			//alert("February " + year + " doesn't have " + day + " days!");
			return false;
		 }
	}
	
	if (year < 1970 || year > 2015)
		return false;
		
	return true; // date is valid

}
	

/********
 * Opens a new window
********/

var curPopupWindow = null;

function openWindow(url, winName, width, height, center, winType) 
{

   var xposition = 50; // Postions the window vertically in px
   var yposition = 50; // Postions the window horizontally in px
   var location, menubar, resizable, scrollbars, status, titlebar;

   if ((parseInt(navigator.appVersion) >= 4 ) && (center)){
       xposition = (screen.width - 800) / 2;
       yposition = (screen.height - 600) / 2;
   } 
   
   if (winType == "1") {           // winType 1 is for regular popup windows
      location=1;
      menubar=1;
      resizable=1;
      scrollbars=1;
      status=1;
      titlebar=1;
   } else if (winType == "2") {   // winType 2 is for Quick Tour like popups
      location=0;
      menubar=0;
      resizable=0;
      scrollbars=0;
      status=0;
      titlebar=1;
   } else if (winType == "3") {   // winType 3 is for footer like popups
      location=0;
      menubar=0;
      resizable=1;
      scrollbars=1;
      status=0;
      titlebar=1;
   } else {                       // if no arg is passed for winType
      location=1;
      menubar=1;
      resizable=1;
      scrollbars=1;
      status=1;
      titlebar=1;
   } 
   
   // Features to specify for a new window
   args = "width=" + width + ","
   + "height=" + height + ","
   + "location=" + location + ","
   + "menubar=" + menubar + ","
   + "resizable=" + resizable + ","
   + "scrollbars=" + scrollbars + ","
   + "status=" + status + ","
   + "titlebar=" + titlebar + ","
   + "toolbar=0,"
   + "hotkeys=0,"
   + "screenx=" + xposition + ","  //NN Only
   + "screeny=" + yposition + ","  //NN Only
   + "left=" + xposition + ","     //IE Only
   + "top=" + yposition;           //IE Only
   
	// Performs the opening of the window (and closing of a window already opened for that page).
	if (curPopupWindow != null) {
		curPopupWindow.close();
	}
	curPopupWindow = window.open(url, winName, args, false);
	curPopupWindow.focus();
}

function NaN(contents) 
{	
	// not a number
    if (((contents / contents) != 1) && (contents != 0)) 
    	return true;
    else
    	return false;
}

/********
 * Verify that an email addres is valid
********/

function isValidEmail(emailad) {

   var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
   var check=/@[\w\-]+\./;
   var checkend=/\.[a-zA-Z]{2,4}$/;

   if(((emailad.search(exclude) != -1) ||
       (emailad.search(check)) == -1) ||
       (emailad.search(checkend) == -1)){
      return false;
   } else {
      return true;
   }
}

/********
*   Removes leading and trailing spaces from the passed string. Also removes
*   consecutive spaces and replaces it with one space. If something besides
*   a string is passed in (null, custom object, etc.) then return the input.
* 
*   created by: Scott Yancey
********/
function trim(inputString) {
   
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

/********
 * Validate phone number for international countries
********/

function isValidPhoneNumber(num, requiredDigits) {
    var digits = 0;
    if (num == null) return false;
    for( i=0; i<num.length; i++ ){
        var c = num.charCodeAt(i);  
        //convert the i-th character to ascii code value
        if( (c>=48) && (c<=57) ) digits++;
    }    
    return (digits >= requiredDigits);
}

/********
 * Validate max characters for textarea
********/
function TrackCount(fieldObj,countFieldName,maxChars)
{
  var countField = eval("fieldObj.form."+countFieldName);
  var diff = maxChars - fieldObj.value.length;

  // Need to check & enforce limit here also in case user pastes data
  if (diff < 0)
  {
    fieldObj.value = fieldObj.value.substring(0,maxChars);
    diff = maxChars - fieldObj.value.length;
  }
  countField.value = diff;
}

/********
 * Validate max characters for textarea
********/
function LimitText(fieldObj,maxChars)
{
  var result = true;
  if (fieldObj.value.length >= maxChars)
    result = false;
  
  if (window.event)
    window.event.returnValue = result;
  return result;
}



