// Set Base Path Of Contact Module - Make it relative to the contact page & site root

var basepath		= "";





/***************************************************************************/

/***************** Loads CSS ***********************************************/

var contactModulePath = basepath + "../contact_module_1.3/";



var is_ie6 = (window.external && typeof window.XMLHttpRequest == "undefined");



var contactformPath = contactModulePath+"assets/contactform.css";

loadjscssfile(contactformPath,'css');



var isIE	= isIE();

function isIE() {

	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);

}

if(is_ie6) {

	var contactformIEPath = contactModulePath+"assets/contactformIE.css";

	loadjscssfile(contactformIEPath,'css');

}





function loadjscssfile(filename, filetype){   

	if (filetype == "js"){ //if filename is a external JavaScript file   

		var fileref = document.createElement("script");   

		fileref.setAttribute("type","text/javascript");   

		fileref.setAttribute("src", filename);   

	}   

	else if (filetype == "css"){ //if filename is an external CSS file   

		var fileref = document.createElement("link");  

		fileref.setAttribute("rel", "stylesheet"); 

		fileref.setAttribute("type", "text/css");  

		fileref.setAttribute("href", filename);  

	}  

	if (typeof fileref != "undefined") {

		document.getElementsByTagName("head")[0].appendChild(fileref);

	}

} 

/***************************************************************************/

/************* jQuery ******************************************************/

$(function(){

	

	var bubbleHtml = "";	

	var bubblePath = contactModulePath+"htmlvalidatebubble.php";

	

	$.get(bubblePath, function(data){

  		bubbleHtml = data;

	}, "text");



	

	var htmlformPath = contactModulePath+"htmlform.php";

	$("#contactForm").load(htmlformPath, "", loadValidation);

	function loadValidation(){

		var boxNum			= 0;

		var runValidation	= true;

		var formStatus		= true;

		var formData		= "";

		var scrollAnimation	= false;

		//////////////////////////////////

		$('#contactSubmit').click(function(){

			$('.fc-tbx').remove();

			formStatus			= true;

			runValidation		= true;

			var inputObjects 	= $(".validate");

			inputObjects.each(function(){

				boxNum++;

				validationChecker($(this), boxNum);

			});

			if(formStatus) {

				var ajaxLoadPath = contactModulePath + "images/ajax-loader.gif";

				

				var ajaxDiv			= "<div class='formReturn'><img src='"+ajaxLoadPath+"' /></div>";

				$("#contactForm").html(ajaxDiv);



				

				var ajax_sendPath = contactModulePath+"includes/ajax_send.php";

				$.ajax({

					type: "POST",

					url: ajax_sendPath,

					data: formData,

					success: function(msg){

						$("#contactForm").fadeOut("slow", function(){

							$("#contactForm").html("<div class='formReturn'>"+msg+"</div>");

							$("#contactForm").fadeIn("slow");			

						});

					}	

				});

				return false;

			} else {

				return false;	

			}

		});

		//////////////////////////////////

		function createBox(posLeft, posTop, input, boxNum, msg){

						

			var bubbleHtml2 = bubbleHtml.replace("{{boxnum}}", boxNum);

			var bubbleHtml3 = bubbleHtml2.replace("{{posLeft}}", posLeft);

			var bubbleHtml4 = bubbleHtml3.replace("{{posTop}}", posTop);

			var bubbleHtml5 = bubbleHtml4.replace("{{msg}}", msg);			



			//var validationElement = "<div id='input"+boxNum+"' class='fc-tbx' style='display:none;visibility: visible; opacity: 1; position: absolute; float: left; left: "+posLeft+"px; top: "+posTop+"px;'><table cellspacing='0' cellpadding='0' border='0'><tbody><tr><td class='tl'/><td class='t'/><td class='tr'/></tr><tr><td class='l'/><td class='c'><div class='err'><p>"+msg+"</p></div><a class='close'/></td><td class='r'/></tr><tr><td class='bl'/><td class='b'/><td class='br'/></tr></tbody></table></div>";	 

			var validationElement = bubbleHtml5;

			$("#contactForm").append(validationElement);

			

			if(isIE) {

				$('#input'+boxNum).css('display', 'block');

			} else {

				$('#input'+boxNum).fadeIn('slow');

			}



			if(scrollAnimation == false) {

				scrollAnimation = true;			

				$.scrollTo( '#input'+boxNum, 900, {easing:'easeOutQuad', onAfter:function(){scrollAnimation = false;}} );

			}

			$('#input'+boxNum).click(function(){

			

			if(isIE) {

				$('#input'+boxNum).css('display', 'none');

			} else {

				$('#input'+boxNum).fadeOut('slow');

			}





			});

			input.focus();

			input.change(function(){

				if(isIE) {

					$('#input'+boxNum).css('display', 'none');

				} else {

					$('#input'+boxNum).fadeOut('slow');

				}

			});

			

			runValidation	= false;

			formStatus		= false;

		}

		//////////////////////////////////

		function validationChecker(input, boxNum){

			var inputClass	= $(input).attr("class");

			var inputCheck	= $(input).val();

			var inputTitle	= $(input).attr("title");

			

			var pos			= $(input).position();

			var posLeft		= pos.left + 280;

			var posTop		= pos.top + -55;

					

			if(inputClass.indexOf("dropdown") != -1) {

				posLeft		= posLeft - 40;

			}

			if(inputClass.indexOf("state") != -1) {

				posLeft		= posLeft - 140;

			}



			if(inputClass.indexOf("email") != -1) {

				if(runValidation){

					if(!echeck(inputCheck)){

						msg = "Invalid Email Address";

						createBox(posLeft, posTop, input, boxNum, msg);

						return false;

					}

				}

			}

			if(inputClass.indexOf("phone") != -1) {

				if(runValidation){

					if(!validatePhone(inputCheck)){

						msg = "Please enter a valid phone number";

						createBox(posLeft, posTop, input, boxNum, msg);

						return false;

					}

				}			

			}

			

			checkEmpty	= stripBlanks(inputCheck);

			

			if(checkEmpty == "" || inputCheck == "blank") {

				if(runValidation) {

					if(inputTitle != "") {

						msg = inputTitle;

					} else {

						msg = "This field is required.";

					}

					createBox(posLeft, posTop, input, boxNum, msg);

					return false;

				}

			}

			var inputName	= $(input).attr("name");

			formData		+= inputName + "=" + inputCheck + "&";

		}

	}

});

/**************************** Check Input Blank ****************************/

function stripBlanks(fld) {

	var result = "";

	var c = 0;

	for (i=0; i<fld.length; i++) {

		if (fld.charAt(i) != " " || c > 0) {

			result += fld.charAt(i);

			if (fld.charAt(i) != " ") c = result.length;

		}

	}

	return result.substr(0,c);

} 

/**************************** Phone Validation ***********************/

function validatePhone(inputCheck) {

	var num = inputCheck.replace(/[^\d]/g,'');

	if(num.length < 10) {

		return false;

	} else {

		return true;

	}

}

/**********************************************************************/

function echeck(str) {

	var at="@";

	var dot=".";

	var lat=str.indexOf(at);

	var lstr=str.length;

	var ldot=str.indexOf(dot);

	if (str.indexOf(at)==-1){

		return false;

	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){

		return false;

	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){

		return false;

	}

	if (str.indexOf(at,(lat+1))!=-1){

		return false;

	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){

		return false;

	}

	if (str.indexOf(dot,(lat+2))==-1){

		return false;

	}

	if (str.indexOf(" ")!=-1){

		return false;

	}

	return true;			

}

/***************************************************************************/

/*************************************************** ScrollTo *********/

;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

/***************************************************************************/

/*************************************************** Easing ************/

jQuery.easing['jswing'] = jQuery.easing['swing'];



jQuery.extend( jQuery.easing,

{

	def: 'easeOutQuad',

	swing: function (x, t, b, c, d) {

		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);

	},

	easeInQuad: function (x, t, b, c, d) {

		return c*(t/=d)*t + b;

	},

	easeOutQuad: function (x, t, b, c, d) {

		return -c *(t/=d)*(t-2) + b;

	},

	easeInOutQuad: function (x, t, b, c, d) {

		if ((t/=d/2) < 1) return c/2*t*t + b;

		return -c/2 * ((--t)*(t-2) - 1) + b;

	},

	easeInCubic: function (x, t, b, c, d) {

		return c*(t/=d)*t*t + b;

	},

	easeOutCubic: function (x, t, b, c, d) {

		return c*((t=t/d-1)*t*t + 1) + b;

	},

	easeInOutCubic: function (x, t, b, c, d) {

		if ((t/=d/2) < 1) return c/2*t*t*t + b;

		return c/2*((t-=2)*t*t + 2) + b;

	},

	easeInQuart: function (x, t, b, c, d) {

		return c*(t/=d)*t*t*t + b;

	},

	easeOutQuart: function (x, t, b, c, d) {

		return -c * ((t=t/d-1)*t*t*t - 1) + b;

	},

	easeInOutQuart: function (x, t, b, c, d) {

		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;

		return -c/2 * ((t-=2)*t*t*t - 2) + b;

	},

	easeInQuint: function (x, t, b, c, d) {

		return c*(t/=d)*t*t*t*t + b;

	},

	easeOutQuint: function (x, t, b, c, d) {

		return c*((t=t/d-1)*t*t*t*t + 1) + b;

	},

	easeInOutQuint: function (x, t, b, c, d) {

		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;

		return c/2*((t-=2)*t*t*t*t + 2) + b;

	},

	easeInSine: function (x, t, b, c, d) {

		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;

	},

	easeOutSine: function (x, t, b, c, d) {

		return c * Math.sin(t/d * (Math.PI/2)) + b;

	},

	easeInOutSine: function (x, t, b, c, d) {

		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;

	},

	easeInExpo: function (x, t, b, c, d) {

		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;

	},

	easeOutExpo: function (x, t, b, c, d) {

		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;

	},

	easeInOutExpo: function (x, t, b, c, d) {

		if (t==0) return b;

		if (t==d) return b+c;

		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;

		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;

	},

	easeInCirc: function (x, t, b, c, d) {

		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;

	},

	easeOutCirc: function (x, t, b, c, d) {

		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;

	},

	easeInOutCirc: function (x, t, b, c, d) {

		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;

		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;

	},

	easeInElastic: function (x, t, b, c, d) {

		var s=1.70158;var p=0;var a=c;

		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;

		if (a < Math.abs(c)) { a=c; var s=p/4; }

		else var s = p/(2*Math.PI) * Math.asin (c/a);

		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;

	},

	easeOutElastic: function (x, t, b, c, d) {

		var s=1.70158;var p=0;var a=c;

		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;

		if (a < Math.abs(c)) { a=c; var s=p/4; }

		else var s = p/(2*Math.PI) * Math.asin (c/a);

		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;

	},

	easeInOutElastic: function (x, t, b, c, d) {

		var s=1.70158;var p=0;var a=c;

		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);

		if (a < Math.abs(c)) { a=c; var s=p/4; }

		else var s = p/(2*Math.PI) * Math.asin (c/a);

		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;

		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;

	},

	easeInBack: function (x, t, b, c, d, s) {

		if (s == undefined) s = 1.70158;

		return c*(t/=d)*t*((s+1)*t - s) + b;

	},

	easeOutBack: function (x, t, b, c, d, s) {

		if (s == undefined) s = 1.70158;

		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;

	},

	easeInOutBack: function (x, t, b, c, d, s) {

		if (s == undefined) s = 1.70158; 

		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;

		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;

	},

	easeInBounce: function (x, t, b, c, d) {

		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;

	},

	easeOutBounce: function (x, t, b, c, d) {

		if ((t/=d) < (1/2.75)) {

			return c*(7.5625*t*t) + b;

		} else if (t < (2/2.75)) {

			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;

		} else if (t < (2.5/2.75)) {

			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;

		} else {

			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;

		}

	},

	easeInOutBounce: function (x, t, b, c, d) {

		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;

		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;

	}

});
