$("document").ready(function() {

// HIGHLIGHT NEW PROJECTS
$(".new").prepend('<img class="new_project" src="../images/icon/new.png" width="63" height="63" alt="new project" title="new project" />');

// SITE FORMS
var regEmail = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
var regPhone = /^((\+\d{1,3}(-| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?\d{2,6}\)?))(-| |.)?(\d{3,4})(-| |.)?(\d{4})(( x| ext)\d{1,5}){0,1}$/;

var inputAlert = "Please provide "; //Alert message
var notValid = {"background-image":"url(http://www.chrispond.com/images/icon/not_valid.gif)", "color":"#660000"}; //Not valid text color
var valid = {"background-image":"url(http://www.chrispond.com/images/icon/valid.gif)", "color":"#006600"} //Valid text color

$("input").mouseover(inputValidation);
$("textarea").mouseover(inputValidation);

// VALIDATE NEWSLETTER SIGN-UP
$("#newsletter input[id=subscribe_button]").click(function () {
if(!$("#sign_up_input").val().match(regEmail)){
		$("#sign_up_input").val(inputAlert + "EMAIL");
		$("#sign_up_input").css(notValid);
		return false;
	}
	return true;
});

// VALIDATE UNSUBSCRIBE FORM
$("#unsubscribe input[id=unsubscribe_button]").click(function () {
if(!$("#email").val().match(regEmail)){
		$("#email").val(inputAlert + "EMAIL");
		$("#email").css(notValid);
		return false;
	}
	return true;
});

// VALIDATE CONTACT FORM
$("#contact input[id=submit_inquiry]").click(function () {
	if($("#your_name").val() == "" || $("#your_name").val() == inputAlert + "your name"){
		$("#your_name").val(inputAlert + "your name");
		$("#your_name").css(notValid);
		return false;
	}
	else if(!$("#your_email").val().match(regEmail)){
		$("#your_email").val(inputAlert + "your email");
		$("#your_email").css(notValid);
		return false;
	}
	else if($("#comment").val() == "" || $("#comment").val() == inputAlert + "your comment"){
		$("#comment").val(inputAlert + "your comment");
		$("#comment").css(notValid);
		return false;
	}
	return true;
});

// VALIDATE REFER A FRIEND NEWSLETTER FORM
$("#refer_a_friend [id=send_referral]").click(function () {
if($("#your_name").val() == "" || $("#your_name").val() == "Please provide your name"){
	$("#your_name").val("Please provide your name");
	$("#your_name").css(notValid);
	return false;
}
else if(!$("#your_email").val().match(regEmail)){
	$("#your_email").val("Please provide your email");
	$("#your_email").css(notValid);
	return false;
}
else if(!$("#friend_email").val().match(regEmail)){
	$("#friend_email").val("Please provide friends email");
	$("#friend_email").css(notValid);
	return false;
}
});

// VALIDATE 404 ERROR FORM
$("#error input[id=submit_error]").click(function () {
if($("#error_ticket").val() == "" || $("#error_ticket").val() == inputAlert + "error ticket"){
		$("#error_ticket").val(inputAlert + "error ticket");
		$("#error_ticket").css(notValid);
		return false;
	}
	return true;
});

//Hover Blur Validation
function inputValidation(){
$("input").focus(function(evt) {var inputName = $(this).attr("name").replace("_", " ");var inputId = $(this).attr("id");var startValue = $(this).val();if($(this).attr("type") == "submit"){}else if($(this).val() == "" || $(this).val() == "your email"){$(this).attr({value: ''});$(this).blur(function validateForm() {if ($(this).attr("name").indexOf("email") != -1 || $(this).attr("name").indexOf("EMAIL") != -1 ) {if ($(this).val().match(regEmail)){$(this).css(valid);}else{$(this).css(notValid);$(this).val(inputAlert + inputName);}}else if ($(this).attr("name").indexOf("phone") != -1 ) {if ($(this).val().match(regPhone)){$(this).css(valid);}else{$(this).css(notValid);$(this).val(inputAlert + inputName);}}else {if ($(this).val() == "" || $(this).val() == inputName || $(this).val() == inputAlert + inputName ){$(this).css(notValid);$(this).val(inputAlert + inputName);}else{$(this).css(valid);}}});}else if($(this).val() == inputAlert + inputName){inputValue = $(this).val().replace(inputAlert, "");$(this).attr({value: ''});$(this).blur(function() {if($(this).val() == "" || $(this).val() == inputAlert || $(this).val() == inputAlert + inputName ){$(this).val(inputAlert + inputName);$(this).css(notValid);}else{$(this).css(valid);}});}});
$("textarea").focus(function(evt) {var inputName = $(this).attr("name").replace("_", " ");var inputId = $(this).attr("id");if($(this).val() == inputName || $(this).val() == ""){$(this).attr({value: ''});$(this).blur(function() {if ($(this).val() == "" || $(this).val() == inputName || $(this).val() == inputAlert + inputName ){$(this).css(notValid);$(this).val(inputAlert + inputName);}else{$(this).css(valid);}});}else if($(this).val() == inputAlert + inputName){inputValue = $(this).val().replace(inputAlert, "");$(this).attr({value: ''});$(this).blur(function() {if($(this).val() == "" || $(this).val() == inputName || $(this).val() == inputAlert + inputName ){$(this).val(inputAlert + inputValue);$(this).css(notValid);}else{$(this).css("color", valid);$(this).css(valid);}});}});
}
});
