// PHP FORM VALIDATION

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter your ' + fieldLabel +'.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function isPhone(str)
{
	return inValidCharSet(str,"0123456789-()+.");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}

function validPhone(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!isPhone(formField.value))
 		{
 			alert('Please enter valid characters for your ' + fieldLabel +' (0123456789.-()+).');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}

function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}

function validateForm(theForm)
{
	// Customize these calls for your form

	// Start ------->
	if (!validRequired(theForm.name,"Name"))
		return false;
		
	if (!validEmail(theForm.email,"Email Address",true))
		return false;
		
	if (!validRequired(theForm.comments,"Comments"))
		return false;
	// <--------- End
	
	return true;
}




// ROLLOVERS

function change_it(the_name)
{
 if (document.images)
 {
 document.images[the_name].src= eval(the_name + "_new.src");
 }
}
function change_back(the_name)
{
 if (document.images)
 {
  document.images[the_name].src= eval(the_name + "_init.src");
 }
}
 if (document.images)
{
 // Preload original image (to show when page loads)
 var biolink_init= new Image();
 biolink_init.src="http://kellyjojohnson.com/images/menu-bio.gif";
 var gigslink_init= new Image();
 gigslink_init.src="http://kellyjojohnson.com/images/menu-gigs.gif";
 var musiclink_init= new Image();
 musiclink_init.src="http://kellyjojohnson.com/images/menu-music.gif";
 var videolink_init= new Image();
 videolink_init.src="http://kellyjojohnson.com/images/menu-video.gif";
 var photoslink_init= new Image();
 photoslink_init.src="http://kellyjojohnson.com/images/menu-photos.gif";
 var shoplink_init= new Image();
 shoplink_init.src="http://kellyjojohnson.com/images/menu-shop.gif";
 var contactlink_init= new Image();
 contactlink_init.src="http://kellyjojohnson.com/images/menu-contact.gif";
 var bloglink_init= new Image();
 bloglink_init.src="http://kellyjojohnson.com/images/menu-blog.gif";
 // Preload image that will show on the mouseover event
 var biolink_new= new Image();
 biolink_new.src="http://kellyjojohnson.com/images/menu-bio_ov.gif";
 var gigslink_new= new Image();
 gigslink_new.src="http://kellyjojohnson.com/images/menu-gigs_ov.gif";
 var musiclink_new= new Image();
 musiclink_new.src="http://kellyjojohnson.com/images/menu-music_ov.gif";
 var videolink_new= new Image();
 videolink_new.src="http://kellyjojohnson.com/images/menu-video_ov.gif";
 var photoslink_new= new Image();
 photoslink_new.src="http://kellyjojohnson.com/images/menu-photos_ov.gif";
 var shoplink_new= new Image();
 shoplink_new.src="http://kellyjojohnson.com/images/menu-shop_ov.gif";
 var contactlink_new= new Image();
 contactlink_new.src="http://kellyjojohnson.com/images/menu-contact_ov.gif";
 var bloglink_new= new Image();
 bloglink_new.src="http://kellyjojohnson.com/images/menu-blog_ov.gif";
}