/* Form Validation Functions */

/*Validates the following provided they are named and :
	First Name
	Last Name
	Phone Number
	Email Address
*/
jQuery.noConflict();
function isEmail(email_address){
	if (email_address.match(/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/) != null)
		return true;
	return false;	
}

function isName(name){
	if (name.match(/^((?:[A-Za-z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.))(?:([ -])((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.)))?$/) != null)
		return true;
	return false;
}

function addErrorDiv(divId,message, showTriangle){
	divId = "#" + divId;
	if (showTriangle == undefined)
		showTriangle = false;
	var errorDiv = document.createElement('div');
	var errorDivArrow = document.createElement('div');
	var errorDivContent = document.createElement('div');
	
	jQuery(errorDiv).addClass('errorDiv');
	jQuery(errorDivContent).addClass("errorDivContent");
	jQuery(errorDivArrow).addClass("errorDivArrow");
	
	jQuery("body").append(errorDiv);
	jQuery(errorDiv).append(errorDivContent);
	jQuery(errorDiv).append(errorDivArrow);
	
	if(showTriangle == true)
		jQuery(errorDivArrow).html('<div class="line10"></div><div class="line9"></div><div class="line8"></div><div class="line7"></div><div class="line6"></div><div class="line5"></div><div class="line4"></div><div class="line3"></div><div class="line2"></div><div class="line1"></div>');
	jQuery(errorDivContent).html(message);
	
	var callerTopPosition = jQuery(divId).offset().top;
	var callerleftPosition = jQuery(divId).offset().left;
	var callerWidth =  jQuery(divId).width();
	var callerHeight =  jQuery(divId).height();
	var inputHeight = jQuery(errorDivContent).height();

	callerleftPosition = callerleftPosition + callerWidth+20;
	callerTopPosition = callerTopPosition-5;

	jQuery(errorDiv).css({
		top:callerTopPosition,
		left:callerleftPosition,
		opacity:0
	});
	
	jQuery(errorDiv).fadeTo("fast",0.8);
}

function noToEmails(){
	addErrorDiv('to_emails',"At least one recipient required")
}

function invalidToEmails(){
	addErrorDiv('to_emails',"One or more email addresses are invalid");
}

function noFromEmail(){
	addErrorDiv('from_email',"Please enter your email address");
}

function invalidFromEmail(){
	addErrorDiv('from_email',"Invalid email address");
}

function noFirstName(){
	addErrorDiv('first_name',"Please enter your First Name");
}

function invalidFirstName(){
	addErrorDiv('first_name',"Please check your First Name");
}

function noLastName(){
	addErrorDiv('last_name',"Please enter your Last Name");
}

function invalidLastName(){
	addErrorDiv('last_name',"Please check your Last Name");
}

function validate(frm){
	jQuery(".errorDiv").remove();
	var valid = true;
	var to_emails = frm.to_emails.value;
	if (to_emails == ''){
		noToEmails();
		valid = false;
	}else{
		to_emails = to_emails.split('\n');
		for(var i = 0; i < to_emails.length; i++){
			if (!isEmail(to_emails[i])){
				valid = false;
				break;
			}
		}
		if (!valid)
			invalidToEmails();
	}
	if (frm.from_email.value == ''){
		noFromEmail();
		valid = false;
	}else if (!isEmail(frm.from_email.value)){
		invalidFromEmail();
		valid = false;
	}
	if (frm.first_name.value == ''){
		noFirstName();
		valid = false;
	}else if (!isName(frm.first_name.value)){
		invalidFirstName();
		valid = false;
	}
	if (frm.last_name.value == ''){
		noLastName();
		valid = false;
	}else if (!isName(frm.last_name.value)){
		invalidLastName();
		valid = false;
	}
	return valid;
}