/* Used with jquery 1.3.2.min.js and jquery.simplemodal-1.2.3.min.js to handle contact form submit and jquery effects on product-detail template. */

// this needs to occur before document is ready
$('head').append('<script type="text/javascript" src="/global-scripts/jquery.bt.min.js"></' + 'script>');
if(navigator.appName === 'Microsoft Internet Explorer'){// IE requires excanvas.js for beautytip
	$('head').append('<script type="text/javascript" src="/global-scripts/excanvas.js"></' + 'script>');
}

$(document).ready(function() {
	// only execute if form element realname exists on the page
	if(typeof(document.getElementsByName('realname')[0]) !== 'undefined') {
		var expFormName = document.getElementsByName('realname')[0].value;
		document.getElementById('productContactForm').name = expFormName;
		// set contact form name attribute for SiteCat tracking
		$('input#LeadSource').val(document.URL); // Sets #LeadSource input value
		
		if(location.search.match(/send=yes$/g)){
			$("#productContactForm").hide();
			$('#formConfirmation').modal();
			s.sendFormEvent('s', s.pageName, expFormName);
			// send success event for form
		}
		
		if ($("#productContactForm").hasClass('enableValidation')){
			$('#submitButton').hover(function(){
				$(this).css({'cursor':'pointer','cursor':'hand'});
			},function(){
				$(this).css({'cursor':'auto'});
			});
			$('#submitButton').bind('click',function(){
				submitForm();
			});
			startValidation(); // starts form validation
		}
	}
});

function submitForm (){
	if($(".btFail").length>0){	
		finalValidation();
	}
	else{
		if(location.search.match(/^\?cat1?2?=/g)){ // checks for cat1 or cat2 req params before appending send param
			$("#formRedirect")[0].value = document.URL + "&send=yes"; // redirects page to display confirmation message
		}
		else {
			$("#formRedirect")[0].value = document.URL + "?send=yes"; // redirects page to display confirmation message
		}
		$("#productContactForm").submit(); // submits form
	}
}

// form validation functions begin
function startValidation(){	
	$('.productFormLabel').each(function(){
		if($(this).hasClass('productFormLabelRequired')){
			var requiredFieldName = $(this).attr('for');
			var jThis = $('.productTextField[name='+requiredFieldName+']');
			setValues(jThis,requiredFieldName); // set values
			
			jThis.bind('keyup',function(){
				setValues(jThis,requiredFieldName); // reset values at keyup
				callBT(jThis);
			});
		}	
		else {
			var labelText = $(this).text();
			$(this).text(labelText + " (Optional)");
		}
	});
}

function finalValidation(){
	$('.productFormLabel').each(function(){
		if($(this).hasClass('productFormLabelRequired')){
			var requiredFieldName = $(this).attr('for');
			var jThis = $('.productTextField[name='+requiredFieldName+']');
			
			setValues(jThis,requiredFieldName);
			
			if($('.productTextField[name='+requiredFieldName+']').hasClass('btFail')){
				callBT(jThis);
			}
		}	
	});
}

function setValues(j,req){
	var fieldValue = j.val();
	var btMessage = testValue(req,fieldValue)[0];
	var btTestClass = testValue(req,fieldValue)[1];
	
	if (j.hasClass('btFail')){
		j.removeClass('btFail');
	}			
	if (j.hasClass('btSuccess')){
		j.removeClass('btSuccess');
	}
	
	j.addClass(btTestClass);
	
	j.parent('li').children('.btText').remove(); // removes existing btText element
	
	if (j.hasClass('btSuccess')){
		j.parent('li').append('<ul class="btText"><li class="btTextPass">'+btMessage+'</li></ul>');
	}
	
	else if (j.hasClass('btFail')){
		j.parent('li').append('<ul class="btText"><li class="btTextFail">'+btMessage+'</li></ul>');
	}
	
	else {
		j.parent('li').append('<ul class="btText"><li>'+btMessage+'</li></ul>');
	}
	
	return;
}

function testValue(fieldName,fieldValue){
	var email='Email';
	var emailTest = /\b[\w._%+-]+@(?:[\w-]+\.)+[\w]{2,4}\b/;
	var phone='Phone';
	var phoneTest = /^(\([2-9]|[2-9])(\d{2}|\d{2}\))(-|.|\s)?\d{3}(-|.|\s)?\d{4}$/;
	var textTest = /^\s*[a-zA-Z,\s]+\s*$/;
	var btMessage, btTestClass;
	switch(fieldName){
		case email:
			if(emailTest.test(fieldValue)){
				btMessage="Thank you.";
				btTestClass="btSuccess";
			}
			else {
				btMessage="Please use a valid email.";
				btTestClass="btFail";
			}
			break;
		case phone:
			if(phoneTest.test(fieldValue)){
				btMessage="Thank you.";
				btTestClass="btSuccess";
			}
			else {
				btMessage="Please use a valid phone number.";
				btTestClass="btFail";
			}
			break;
		default:
			if(textTest.test(fieldValue)){
				btMessage="Thank you.";
				btTestClass="btSuccess";
			}
			else {
				btMessage="Please do not leave blank.";
				btTestClass="btFail";
			}
			break;
	}
	return [btMessage,btTestClass];
}

function callBT(j){	
	if(j.hasClass('btFail')){
		j.bt({trigger: 'none',
			contentSelector: "$(this).next('.btText')",
			clickAnywhereToClose: false,
			fill: '#ff0000', 
			strokeStyle: '#015cae', 
			spikeLength: 15, 
			spikeGirth: 15,
			overlap: 0,
			strokeWidth: 0,     
			padding: 5, 
			cornerRadius: 0, 
			cssStyles: {
			fontFamily: 'arial,sans-serif', 
			fontSize: '11px',
			color: '#595959',
			width: '150px',
			positions: ['right']
			}
		});
		j.btOn();
	}	
	
	if(j.hasClass('btSuccess')){
		j.bt({trigger: 'none',
			contentSelector: "$(this).next('.btText')",
			clickAnywhereToClose: true,
			fill: '#015cae', 
			strokeStyle: '#015cae', 
			spikeLength: 15, 
			spikeGirth: 15,
			overlap: 0,
			strokeWidth: 0,     
			padding: 5, 
			cornerRadius: 0, 
			cssStyles: {
			fontFamily: 'arial,sans-serif', 
			fontSize: '11px',
			color: '#595959',
			width: '150px',
			positions: ['right']
			}
		});
		j.btOn();
	}
}
// form validation functions end
