//Form Validation.
var popupcalendar;
var popupcalendar2;

window.onload = function ()	{
	popupcalendar = new Epoch('cal','popup',document.getElementById('dBirthDate'),false);	
	popupcalendar2 = new Epoch('cal','popup',document.getElementById('dDeathDate'),false);	
}
function initpopup()	{
	
	popupcalendar = new Epoch('cal','popup',document.getElementById('dBirthDate'),false);	
	popupcalendar2 = new Epoch('cal','popup',document.getElementById('dDeathDate'),false);		
}

function changeShow(objname)	{
	//alert("TEST " + document.getElementById(objname).backgroundImage);
	
	var backDown = "url(../images/downbullet.gif)";
	var backUp = "url(../images/upbullet.gif)";
	
	myObj = document.getElementById(objname);
	
	//myObj.style.backgroundImage = "url(../images/candle.gif)"
	
	if(myObj.style.backgroundImage == backUp){
		//we are going from normal, to slide down, so lets put our up arrow in
		myObj.style.backgroundImage = backDown;
	} //end if
	else	{
		// Reset to normal
    	myObj.style.backgroundImage = backUp;
  	} // end else
	
} // end changeShow(obj) function

function doSlide(slide, show)	{
	toggleSlide(slide);
	changeShow(show);
}

function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}

function showReply(divid, memId)	{
	var myDiv = document.getElementById(divid);
	var parentId = divid.substr(6);
	
	if (myDiv.innerHTML != "")	{
		myDiv.innerHTML = "";
		document.getElementById('reply_' + parentId).innerHTML = "Reply";
	}
	else	{
		document.getElementById('reply_' + parentId).innerHTML = "Hide";
		myDiv.innerHTML = "<form id='add-condolence' name='journal' action='#' method='post' onSubmit='return checkForm(this);'><BR><fieldset><label for='message'>Journal Entry</label><textarea name='message' id='message'></textarea><br /><input type='hidden' name='parentId' id='parentId' value='" + parentId + "'><input type='hidden' name='iMemberId' id='iMemberId' value='" + memId + "'><input type='hidden' name='title' id='title' value='RE:'><input type='hidden' name='manageJournal' id='manageJournal' value='yes'><input type='image' src='images/save.gif' alt='Add Journal Entry' class='submit' /></fieldset></form>";
	}
	
}


function checkForm(theform)	{
	
	var returnval;
	var returnError;
	var errorArray = new Array();
	//var emailRegex = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
	var regexstring = "";
	//var requiredField = ("vTitle", "vFirstName", "vLastName", "vEmail", "vPassword", "vRePassword") ("no", "alpha", "alpha", "email", "password");
	div = document.getElementById("register-msg");
	//reset the error message output
	div.innerHTML = "";
	
	for (var i=0; i < theform.elements.length; i++) {
		
		var element = theform.elements[i].name;
		var myValue = theform.elements[i].value;
		//if (document.getElementById(element))	{} //FNISH THIS
		switch(element)	{
			
			
			case ("vFirstName"):
				//This allows you to validate first names and last names in seperate fields. Instead of validating a full name in one field.
				regexstring = "^[a-zA-Z]+(([\'\,\.\-][a-zA-Z])?[a-zA-Z]*)*$"; 		
				returnError = "Enter a valid First Name";
				break;
			case ("vLastName"):
				//This allows you to validate first names and last names in seperate fields. Instead of validating a full name in one field.
				regexstring = "^[a-zA-Z]+(([\'\,\.\-][a-zA-Z])?[a-zA-Z]*)*$"; //ANY alphanumeric string with spaces, commas, dashes.
				returnError = "Enter a valid Last Name";
				break;
			case ("vEmail"):
				//regexstring = "(\w+?@\w+?\x2E.+)"; //Validates an email address
				regexstring = "^[a-zA-Z0-9\_\-]+[a-zA-Z0-9\.\_\-]*@([a-zA-Z0-9\_\-]+\.)+([a-zA-Z]{2,4}|travel|museum)$";
				returnError = "Not a valid email address";
				break;
				
			case ("vPassword"):
				//alert("first " + myValue + " then " + document.getElementById("vRePassword").value);
				if (myValue == document.getElementById("vRePassword").value)	{
					//return a valid match if between 4 and 20 characters
					//alert ("true");
					regexstring = "^.{4,20}$";
					returnError = "Password must be between 4 and 20 characters";	
				}
				else	{
					regexstring ="error";
					returnError = "Your Passwords Do Not Match";
				}
				break;
			case ("vRePassword2"):
			
				if (document.getElementById("vRePassword").value != myValue)	{
					regexstring = "FAIL!";
					returnError = "Your Passwords Do Not Match!";				
				}
				else	{
					regexstring = "[a-z0-9/s]{3,30}";
					returnError = "Enter only letters and numbers. 3-30 characters allowed.";
				}
				
				break;
			case ("vEmail"):
				//regexstring = "(\w+?@\w+?\x2E.+)"; //Validates an email address
				regexstring = "^[a-zA-Z0-9\_\-]+[a-zA-Z0-9\.\_\-]*@([a-zA-Z0-9\_\-]+\.)+([a-zA-Z]{2,4}|travel|museum)$";
				returnError = "Not a valid email address";
				break;
			
			case("dBirthDate"):
				//PHP/Perl based regular expression: find aplhanumeric only(alpha and numeric only) (string and numeric only) but it will not find the empty string as it has to find string between 1 to 11 length.
				regexstring = "[a-z0-9/s]{1,30}";
				returnError = "Not a valid Birth Date";
				break;
				
			case("dDeathDate"):
				//PHP/Perl based regular expression: find aplhanumeric only(alpha and numeric only) (string and numeric only) but it will not find the empty string as it has to find string between 1 to 30 length.
				regexstring = "[a-z0-9/s]{1,30}";
				returnError = "Not a valid Death Date";
				break;	
			
			case("imageName"):
				//PHP/Perl based regular expression: find aplhanumeric only(alpha and numeric only) (string and numeric only) but it will not find the empty string as it has to find string between 1 to 30 length.
				if (myValue != "")	{
					if (document.getElementById("vImage").value == "")	{
						regexstring = "ERROR!";
						returnError = "Please select the appropriate image";
					}
				}
				else	{
					if (document.getElementById("vImage").value != "")	{
						regexstring = "ERROR!";
						returnError = "Please enter an image name";
					}
				}
				break;
			case("vImage"):
				if (document.getElementById("vImage").value != "")	{
					regexstring = "^([a-zA-Z].*|[1-9].*)\.(((j|J)(p|P)(g|G))|((g|G)(i|I)(f|F))|((p|P)(n|N)(g|G)))$";
					returnError = "Must upload a JPG, GIF or PNG file";
				}
			break;
			case("donation"):
				
				regexstring = "^[-]?([1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|\.[0-9]{1,2})$";
				returnError = "Not Valid!";
				
			break;
				
			case("vComment"):
				regexstring = "^(.|[ \t\r\n\v\f])+$";
				returnError = "Invalid condolence message";
			break;
			
			default:
				regexstring = "";
				break;			
		} //end switch
	
		
		//We have a set regex, therefore, our string needs to be "matched"
		
		if (regexstring != "")	{	
			if (!myValue.match(regexstring))	{
				errorArray[errorArray.length] = returnError;
			}
			returnval = false;
		}//end if
		
	}//end for	
	
div = document.getElementById("register-msg");

if (errorArray < 1)	{
	returnval = true;
}
else	{
	
	//for (var key in errorArray)	{
	for (key=0; key < errorArray.length; key++)	{
		//alert(errorArray[key]);
		div.innerHTML += errorArray[key] + "<BR>";
	}
}
scroll(0,0);
return returnval;	
} // end function


function checkDelete()	{
	if (confirm("Are you sure you want to delete the selected messages?"))
		return true;	
	else
		return false;
}

//*********************************
//
// Ajax type stuff
//
//********************************

// JavaScript Document

// Get base url
url = document.location.href;
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);

function ajax_do (url) {
        // Does URL begin with http?
        if (url.substring(0, 4) != 'http') {
                url = base_url + url;
        }

        // Create new JS element
        var jsel = document.createElement('SCRIPT');
        jsel.type = 'text/javascript';
        jsel.src = url;

        // Append JS element (therefore executing the 'AJAX' call)
        document.body.appendChild (jsel);
}
