//Set text to titlecase
function toTitleCase(original)
{
	var results = "";
	var words = original.split(" ");
	for(keyvar in words)
	{
		results += ' ' + words[keyvar].substr(0,1).toUpperCase() + words[keyvar].substr(1,words[keyvar].length);
	}
	return results;
}

//Global string replacement function
function ReplaceAll(Source,stringToFind,stringToReplace)
{
	var temp = Source;
	var index = temp.indexOf(stringToFind);

	while(index != -1)
	{
		temp = temp.replace(stringToFind,stringToReplace);
		index = temp.indexOf(stringToFind);
	}
	return temp;
}


function preload(arrayOfImages) 
{
	cycle = setInterval(function() 
	{
	    var image_loaded = true;
	    var images = arrayOfImages;
		var img;
	 
		for (var i = 0; i < images.length; i++) 
		{
			//var img = images[i];
			
			img = new Image();
			img.src = images[i];
	
			if (img.width == undefined || img.width == 0)
			{
				image_loaded = false;
			}
		}
		
		//alert(img + ":" + img.width);
		if (image_loaded) 
		{
		//alert(img + ":" + img.width);
			jQuery('#div_message').hide();
			jQuery('#div_content').show();
			//LoadVespa('bronze');
			//alert("COMPLETE");
			clearInterval(cycle);
		}
	 

	}, 1000);
}



//=======================================================
// METHOD - VALIDATEEMAIL()
//=======================================================
function ValidateEmail(email_address)
{
	//Check for empty field and correct syntax of email field
	if(email_address == "")
	{
	   alert('Please enter a value in the Email Address field.');
	   return(false);
	}
	else
	{
	   //Get position of @ symbol
	   var atPosition = email_address.indexOf('@');
	
	   //Get position of . symbol
	   var dotPosition = email_address.lastIndexOf('.');
	
	   //Check for incorrect positioning or non-existence of symbols
	   if(atPosition < 1 || dotPosition - atPosition < 2)
	   {
	      alert('Please enter a valid email address.');
	      return(false);
	   }
	}

	//If all goes well then return true
	return(true);
}


//=======================================================
// METHOD - VALIDATEPHONE()
//=======================================================
function ValidatePhone(phone_number) 
{
	var stripped = phone_number.replace(/[\(\)\.\-\ ]/g, '');   
	stripped = stripped.replace(/^1/g, '');  


	if (phone_number == "") 
	{
		alert("Please enter a phone number.\n\nExample: (555) 555-5555");
		return(false);
    } 
	else if(isNaN(stripped)) 
	{
		alert("The phone number entered contains invalid characters.\nPlease enter a valid number.\n\nExample: (555) 555-5555");     
		return(false);
	} 
	else if (!(stripped.length == 10)) 
	{
		alert("The phone number entered is not the correct length.\nMake sure you included an area code.\n\nExample: (555) 555-5555");     
		return(false); 
   	}

	//If all goes well then return true
	return(true);
}


//=======================================================
// METHOD - VALIDATEZIPCODE()
//=======================================================
function ValidateZipCode(zip_code) 
{ 

	if (zip_code == "") 
	{
		alert("Please enter a zip code.\n\nExample: 55555");
		return(false);
      } 
	else if(isNaN(zip_code)) 
	{
		alert("The zip code entered contains illegal characters.\nPlease enter a valid zip code.\n\nExample: 55555");     
		return(false);
	} 
	else if (!(zip_code.length == 5)) 
	{
		alert("The zip code entered is not the correct length.\nPlease enter a valid zip code.\n\nExample: 55555");     
		return(false); 
   	}

	//If all goes well then return true
	return(true);
}


//=======================================================
// METHOD - ValidateDate()
//=======================================================
function ValidateDate(date) 
{ 
	//set pattern to search by
	var pattern = /^\d{2}[\/]?\d{2}[\/]?\d{4}/ig; //MATCHES 05/05/1999

	if(date.match(pattern)) 
	{
		return(true);
	}

	return(false)
}


//=======================================================
// METHOD - ValidateIllegalCharacters()
//=======================================================
function ValidateIllegalCharacters(source) 
{ 
	//Searches for illegal characters and returns false if fails
	if(source.match(/[\<\>!@#\$%^&\*,]+/i) ) 
	{
		return(false);
	}

	return(true);
}



//=======================================================
// METHOD - ValidateLength()
//=======================================================
function ValidateLength(source, minimum, maximum)
{ 
	//Searches for length
	if(source.length > maximum || source.length < minimum) 
	{
		return(false);
	}

	return(true);
}


//=======================================================
// METHOD - TrimTrailingString(source, text)
//=======================================================
function TrimTrailingString(source, text)
{
	var regex = new RegExp(text + "$","gi");
	return (source.replace(regex, ""));
}



