

function checkValidEmail2(theField) { 
	
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theField.value)) {
		// we're OK
	} else {
		alert("Your email address does not appear to be valid.  Please re-enter.")
		return false;
	}

}

function StakeToNumber(stakeString) {

	if (stakeString == "Anaheim East") { return 1; }
    if (stakeString == "Anaheim") { return 2; }
    if (stakeString == "Barstow") { return 3; }
    if (stakeString == "Carlsbad") { return 4; }
    if (stakeString == "Chino") { return 5; }
    if (stakeString == "Chula Vista") { return 6; }
    if (stakeString == "Corona") { return 7; }
    if (stakeString == "Cypress") { return 8; }
    if (stakeString == "Del Mar") { return 9; }
    if (stakeString == "El Cajon") { return 10; }
    if (stakeString == "El Centro") { return 11; }
    if (stakeString == "Escondido South") { return 12; }
    if (stakeString == "Escondido") { return 13; }
    if (stakeString == "Fontana") { return 14; }
    if (stakeString == "Fullerton") { return 15; }
    if (stakeString == "Garden Grove") { return 16; }
    if (stakeString == "Hemet") { return 17; }
    if (stakeString == "Hesperia") { return 18; }
    if (stakeString == "Huntington Beach North") { return 19; }
    if (stakeString == "Huntington Beach") { return 20; }
    if (stakeString == "Irvine") { return 21; }
    if (stakeString == "Jurupa") { return 22; }
    if (stakeString == "Laguna Niguel") { return 23; }
    if (stakeString == "Menifee") { return 24; }
    if (stakeString == "Mission Viejo") { return 25; }
    if (stakeString == "Moreno Valley") { return 26; }
    if (stakeString == "Murrieta") { return 27; }
    if (stakeString == "Newport Beach") { return 28; }
    if (stakeString == "Ontario") { return 29; }
    if (stakeString == "Orange") { return 30; }
    if (stakeString == "Palm Desert") { return 31; }
    if (stakeString == "Penasquitos") { return 32; }
    if (stakeString == "Placentia") { return 33; }
    if (stakeString == "Poway") { return 34; }
    if (stakeString == "Rancho Cucamonga") { return 35; }
    if (stakeString == "Redlands") { return 36; }
    if (stakeString == "Rialto") { return 37; }
    if (stakeString == "Riverside West") { return 38; }
    if (stakeString == "Riverside") { return 39; }
    if (stakeString == "San Bernardino") { return 40; }
    if (stakeString == "San Clemente") { return 41; }
    if (stakeString == "San Diego East") { return 42; }
    if (stakeString == "San Diego North") { return 43; }
    if (stakeString == "San Diego Sweetwater") { return 44; }
    if (stakeString == "San Diego") { return 44; }
    if (stakeString == "Santa Margarita") { return 45; }
    if (stakeString == "Santee") { return 46; }
    if (stakeString == "Temecula") { return 47; }
    if (stakeString == "Upland") { return 48; }
    if (stakeString == "Victorville") { return 49; }
    if (stakeString == "Vista") { return 50; }
    if (stakeString == "Yucaipa") { return 51; }
    if (stakeString == "Yucca Valley") { return 52; }
    if (stakeString == "Other") { return 53; }

	return 0;
}

function StateToNumber(stateString) {
	if (stateString == "AZ") { return 1; }
	if (stateString == "CA") { return 2; }
	if (stateString == "ID") { return 3; }
	if (stateString == "NV") { return 4; }
	if (stateString == "OR") { return 5; }
	if (stateString == "UT") { return 6; }
	if (stateString == "WA") { return 7; }
	return 2;
}

function Day_of_WeekToNumber(dayString) {
	if (dayString == "0") { return 0; }
	if (dayString == "1") { return 1; }
	if (dayString == "2") { return 2; }
	if (dayString == "3") { return 3; }
	if (dayString == "4") { return 4; }
	if (dayString == "5") { return 5; }
	if (dayString == "6") { return 6; }
	return 3;
}


function DailyToNumber(dailyString) {
	if (dailyString == "1") { return 1; }
	return 0;
}


function Temple_DistrictToNumber(templeString) {
	if (templeString == "Albuquerque New Mexico") { return 1; }
	if (templeString == "Anchorage Alaska"		) { return 2; } 
	if (templeString == "Atlanta Georgia"		) { return 3; } 
	if (templeString == "Baton Rouge Louisiana"	) { return 4; } 
	if (templeString == "Billings Montana"		) { return 5; } 
	if (templeString == "Birmingham Alabama"	) { return 6; } 
	if (templeString == "Bismarck North Dakota"	) { return 7; } 
	if (templeString == "Boise Idaho"			) { return 8; } 
	if (templeString == "Boston Massachusetts"	) { return 9; } 
	if (templeString == "Bountiful Utah"		) { return 10; } 
	if (templeString == "Cardston Alberta"		) { return 11; } 
	if (templeString == "Chicago Illinois"		) { return 12; } 
	if (templeString == "Columbia River Washington"	) { return 13; } 
	if (templeString == "Columbia South Carolina"	) { return 14; } 
	if (templeString == "Columbus Ohio"			) { return 15; }
	if (templeString == "Dallas Texas"			) { return 16; } 
	if (templeString == "Denver Colorado"		) { return 17; } 
	if (templeString == "Detroit Michigan"		) { return 18; } 
	if (templeString == "Draper Utah"			) { return 19; } 
	if (templeString == "Edmonton Alberta"		) { return 20; } 
	if (templeString == "Fresno California"		) { return 21; } 
	if (templeString == "Harrison New York"		) { return 22; } 
	if (templeString == "Houston Texas"			) { return 23; } 
	if (templeString == "Idaho Falls Idaho"		) { return 24; } 
	if (templeString == "Jordan River Utah"		) { return 25; } 
	if (templeString == "Kona Hawaii"			) { return 26; } 
	if (templeString == "Laie Hawaii"			) { return 27; } 
	if (templeString == "Las Vegas Nevada"		) { return 28; } 
	if (templeString == "Logan Utah"			) { return 29; } 
	if (templeString == "Los Angeles California") { return 30; }
	if (templeString == "Louisville Kentucky"	) { return 31; } 
	if (templeString == "Lubbock Texas"			) { return 32; } 
	if (templeString == "Manhattan New York"	) { return 33; } 
	if (templeString == "Manti Utah"			) { return 34; } 
	if (templeString == "Medford Oregon"		) { return 35; } 
	if (templeString == "Memphis Tennessee"		) { return 36; } 
	if (templeString == "Mesa Arizona"			) { return 37; } 
	if (templeString == "Monticello Utah"		) { return 38; } 
	if (templeString == "Mount Timpanogos Utah"	) { return 39; } 
	if (templeString == "Nashville Tennessee"	) { return 40; } 
	if (templeString == "Nauvoo Illinois"		) { return 41; } 
	if (templeString == "Newport Beach California" ) { return 42; } 
	if (templeString == "Oakland California"	) { return 43; } 
	if (templeString == "Ogden Utah"			) { return 44; } 
	if (templeString == "Oklahoma City Oklahoma" ) { return 45; }
	if (templeString == "Orlando Florida"		 ) { return 46; } 
	if (templeString == "Palmyra New York"		 ) { return 47; } 
	if (templeString == "Portland Oregon"		 ) { return 48; } 
	if (templeString == "Provo Utah"			 ) { return 49; } 
	if (templeString == "Raleigh North Carolina" ) { return 50; } 
	if (templeString == "Redlands California"	 ) { return 51; } 
	if (templeString == "Reno Nevada"			 ) { return 52; } 
	if (templeString == "Rexburg Idaho"			 ) { return 53; } 
	if (templeString == "Sacramento California"	 ) { return 54; } 
	if (templeString == "St. George Utah"		 ) { return 55; } 
	if (templeString == "St. Louis Missouri"	 ) { return 56; } 
	if (templeString == "St. Paul Minnesota"	 ) { return 57; } 
	if (templeString == "Salt Lake"				 ) { return 58; } 
	if (templeString == "San Antonio Texas"		 ) { return 59; } 
	if (templeString == "San Diego California"	 ) { return 60; }
	if (templeString == "Seattle Washington"	 ) { return 61; } 
	if (templeString == "Snowflake Arizona"		 ) { return 62; } 
	if (templeString == "Spokane Washington"	 ) { return 63; } 
	if (templeString == "Twin Falls Idaho"		 ) { return 64; } 
	if (templeString == "Vernal Utah"			 ) { return 65; } 
	if (templeString == "Washington D.C."		 ) { return 66; } 
	if (templeString == "Winter Quarters Nebraska"	) { return 67; } 
	if (templeString == "Other"						) { return 68; } 
	return 60;
}



function refineTempleDistrictList(field) {

	// field.form


}



function refineStakeList(field) {

	// field.form


}



function checkValidEmail(theField) { 
	
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theField.value)) {
		// we're OK
	} else {
		alert("Your email address does not appear to be valid.  Please re-enter.")
		return false;
	}

}



function checkValidPhone(theField) {
	var a = theField.value;
	var n = "";

	for (var i=0; i < a.length; i++) {
		var c = a.charAt(i);
		if ( (c >= '0') && (c <= '9') ) {
			n += c;	
		}
	}

	if (n.length == 10)	{
		theField.value = n.substr(0,3) + '.' + n.substr(3,3) + '.' + n.substr(6,4);
	} else {
		alert("Please enter a 10 digit phone number e.g. 858.555.1212");
	}

}



function makeSureFirstUpper(theField) {
	var pattern = /(\w)(\w*)/;
	var a = theField.value.split(/\s+/g);
	for (var i=0; i < a.length; i++) {
		var parts = a[i].match(pattern);
		var firstLetter = parts[1].toUpperCase();
		var restOfWord  = parts[2].toLowerCase();
		a[i] = firstLetter + restOfWord;
	}
	theField.value = a.join(' ');
}



function makeSureLower(theField) {
	var theValue   = theField.value;
	var lower      = theValue.toLowerCase();
	theField.value = lower;
}



function makeSureValidState(theField) {
	var theValue   = theField.value;
	if (theValue.length > 2) {
		theValue = theValue.substr(0,2);
	}
	var upper      = theValue.toUpperCase();
	theField.value = upper;
}



/**
 * Open a new window in the center of the screen
 * 
 * @argument theurl The URL of the page to be displayed (string)
 * @argument thename The name of the window (note: no spaces allowed) (string)
 */
function popCenteredWindow(theurl, thename) {

    var theAppWindow = null;
    var screen_height;
	var taskbar_height;
 	var taskbar_width;
    var screen_width;
    var top_left_x;
    var top_left_y;
    var window_parameters;
    var window_width  = 640;
    var window_height = 480;
	var fraction = .20;

    screen_width   = screen.availWidth;
	taskbar_width  = screen.width  - screen.availWidth;
    screen_height  = screen.availHeight;
 	taskbar_height = screen.height - screen.availHeight;

    if ( navigator.appName == "Microsoft Internet Explorer" ) {

	  // size per algorithm
      if (screen_width > window_width) {
          window_width += Math.floor( (screen_width - window_width) * fraction);
      }
      if (screen_height > window_height) {
          window_height += Math.floor( (screen_height - window_height) * fraction);
      }
      // center
      top_left_x  = Math.floor( (screen_width -  window_width) / 2 );
      top_left_y  = Math.floor( (screen_height - window_height) / 2 );
   	  // adjust by amount of task bar
      top_left_x  = Math.max(0, top_left_x - taskbar_width);
      top_left_y  = 15; // Math.max(0, top_left_y - taskbar_height);

      //
      window_parameters = 'toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=' + window_width + ',height=' + window_height + ',top=' + top_left_y + ',left=' + top_left_x;
  

      theAppWindow = window.open(theurl, thename, window_parameters);
      
      theAppWindow.focus(); 

	  // alert(window_parameters);

    } else {
  
      // netscape

      window_width  = screen_width;
      window_height = screen_height;
      top_left_x  = screen.availLeft;
      top_left_y  = screen.availTop;


    // thename = thename + " " + window_width + " by " + window_height + " at " + top_left_x + ", " + top_left_y;

    window_parameters = 'toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,outerWidth=' + window_width + ',outerHeight=' + window_height + ',screenx=' + top_left_x + ',screeny=' + top_left_y;

    theAppWindow = window.open(theurl, thename, window_parameters);

    theAppWindow.focus(); 

    // alert(window_parameters);

    }

    // return reference to the window
 	return theAppWindow;

}



/**
 * Open a new window in the location specified
 * 
 * @argument theurl The URL of the page to be displayed (string)
 * @argument thename The name of the window (note: no spaces allowed) (string)
 */
function popSmallWindowAt(theurl, thename, top_left_x, top_left_y ) {

    var theAppWindow = null;
    var screen_height;
	var taskbar_height;
 	var taskbar_width;
    var screen_width;

    var window_parameters = "";
    var window_width  = 480;
    var window_height = 320;

    screen_width   = screen.availWidth;
	taskbar_width  = screen.width  - screen.availWidth;
    screen_height  = screen.availHeight;
 	taskbar_height = screen.height - screen.availHeight;

    if ( navigator.appName == "Microsoft Internet Explorer" ) {

        //
        window_parameters = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=' + window_width + ',height=' + window_height + ',top=' + top_left_y + ',left=' + top_left_x;
  

  		// alert("URL: " + theurl + "\n" + "Name: " + thename + "\n" + "Parameters: " + window_parameters + "\n");

        theAppWindow = window.open(theurl, thename, window_parameters);
        
        theAppWindow.focus(); 


    } else {

	  	// netscape

  		// thename = thename + " " + window_width + " by " + window_height + " at " + top_left_x + ", " + top_left_y;

    	window_parameters = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,outerWidth=' + window_width + ',outerHeight=' + window_height + ',screenx=' + top_left_x + ',screeny=' + top_left_y;
     
     	theAppWindow = window.open(theurl, thename, window_parameters);
    	
    	theAppWindow.focus(); 

  		// alert(window_parameters);

	}

 	// return reference to the window
 	// return theAppWindow;

}



/**
 * Open a new window in the location specified
 * 
 * @argument theurl The URL of the page to be displayed (string)
 * @argument thename The name of the window (note: no spaces allowed) (string)
 */
function popWindowAt(theurl, thename, top_left_x, top_left_y ) {

    var theAppWindow = null;
    var screen_height;
	var taskbar_height;
 	var taskbar_width;
    var screen_width;

    var window_parameters;
    var window_width  = 640;
    var window_height = 480;
	var fraction = .20;

    screen_width   = screen.availWidth;
	taskbar_width  = screen.width  - screen.availWidth;
    screen_height  = screen.availHeight;
 	taskbar_height = screen.height - screen.availHeight;

    if ( navigator.appName == "Microsoft Internet Explorer" ) {

		// size per algorithm
        if (screen_width > window_width) {
          window_width += Math.floor( (screen_width - window_width) * fraction);
        }
        if (screen_height > window_height) {
          window_height += Math.floor( (screen_height - window_height) * fraction);
        }

        //
        window_parameters = 'toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=' + window_width + ',height=' + window_height + ',top=' + top_left_y + ',left=' + top_left_x;
  

        theAppWindow = window.open(theurl, thename, window_parameters);
        
        theAppWindow.focus(); 

  		// alert(window_parameters);

    } else {

	  	// netscape

  		// thename = thename + " " + window_width + " by " + window_height + " at " + top_left_x + ", " + top_left_y;

    	window_parameters = 'toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,outerWidth=' + window_width + ',outerHeight=' + window_height + ',screenx=' + top_left_x + ',screeny=' + top_left_y;
     
     	theAppWindow = window.open(theurl, thename, window_parameters);
    	
    	theAppWindow.focus(); 

  		// alert(window_parameters);

	}

 	// return reference to the window
 	return theAppWindow;

}



function showDirections(oRef) {

	// get cookie data
	// if none, prompt for and save
	var c = new Cookie(document, 'userInfo', 87600, '/', 'sdldssingles.info', false);
	c.load();

	var debugStr = "";
	for (var prop in c) {
		if (typeof c[prop] != "function")	{
			debugStr += "" + prop + ":" + c[prop] + ",";
		}
	}

	// alert(debugStr);

	// pop new window
	var wRef = popCenteredWindow("", "directions");

	var url;

	if (false) {
		// prompt for data
		url = "http://www.sdldssingles.info/cgi-bin/info.pl";		
	} else {
		// have cookie data
		url = "http://maps.yahoo.com/py/ddResults.py?";
		if (c.firstName && c.lastName) {
			oRef.name = "" + c.firstName + " " + c.lastName;
		}
		if (c.street) {
			oRef.addr = "" + c.street;
		}
		if (c.city) {
			oRef.city =	"" + c.city;
		}
		if (c.state) {
			oRef.state = "" + c.state;
		}
		if (c.city && c.state) {
			oRef.csz = "" + c.city + ", " + c.state;
		}
		var first = true;
		for (var prop in oRef) {
			if (first) {
				first = false;
			} else {
				url += '&';
			}
			url += prop + '=' +	oRef[prop];
		}
	}

	wRef.location = url;

}



function getUserInfoCookieData() {

	var c = new Cookie(document, 'userInfo', 87600, '/', '$DOMAIN', false);
	c.First_Name       = "";
	c.Last_Name        = "";
	c.Email            = "";
	c.Stake_Other      = "";

	c.Stake            = "";
	c.State            = "";
	c.Temple_District  = "";

	c.Day_of_Week      = "";
	c.Daily            = "";

	c.load();

	return c;

}





