// JavaScript Document



// commands to set the default navigation bar image based on the file name.
// The nav image files are based on webpage file names.
function navImage () {
	var url = window.location.pathname;
	var filename = url.substring(url.lastIndexOf('/')+1, url.lastIndexOf("."));
	
	// this is to take into account if a user goes to the root level domain without
	// typing in index.php, it defaults to the home image.
	if((url.lastIndexOf('/')+2) > url.length)
		filename = "index";

	document.getElementById(filename).style.backgroundImage = 'url(media/buttons/active.png)';
	document.getElementById(filename).style.color = '#a39a80';
	
	new Image().src = "media/buttons/active.png";
	new Image().src = "media/buttons/joinBtnRoll.png";
	new Image().src = "media/buttons/downloadBtnRoll.png";
	new Image().src = "media/buttons/viewecoroll.png";
} // End of function navImage 


//Submit event to manage the AJAX subscription to mailing list
jQuery(function(){
	navImage();
	
	// start jqtweet!
	JQTWEET.loadTweets();
	
	jQuery.ajax({
		type: "GET",
		url: "custquotes.xml",
		dataType: "xml",
		success: function(xml) {
			x = xml.getElementsByTagName("statement");
			y = xml.getElementsByTagName("customer");
			var randomnum=Math.floor(Math.random()*x.length);
			var quote = x[randomnum].childNodes[0].nodeValue;
			var custname = y[randomnum].childNodes[0].nodeValue;
			jQuery('#custquote').html(quote);
			jQuery('#custname').html(custname);
		} //end of success: function(xml) {
	}); //end of $.ajax({
	
    jQuery("#mailer").submit(function(e){
       e.preventDefault();
        jQuery.post("emaillist.php", jQuery("#mailer").serialize(),

        function(data){
        	if (data.flag == 1) {
					alert("Please enter a valid email address");
					shake("email");
				}
			else if (data.flag == 2) 
				alert("There was a problem sending your request. Please try again or contact us by phone.");
			else if (data.flag == 3) {
					alert("The email field is required and cannot be empty");
					shake("email");
				}
			else {
				document.getElementById("email").value = "Enter email to join mailing list";
				jQuery("#emailthanks").fadeIn('slow');
			}
        }, "json"); //end $.post
    }); //end $("#mailer").submit(function(e)
}); // end $(function()


//If user clicks on email input field clear it if the user has not changed the default value.
//A return is done user clicks on field and field is not blank or the default value.
function checkfield(frm) 
{
	var strDefault = "Enter email to join mailing list";
	var strEmail = frm.email.value;
	
	if (strEmail == strDefault)
		frm.email.value = "";
	else if (strEmail == "")
		frm.email.value = strDefault;
		
} //end function checkfield(frm)


//validate email address
function validate_email(id, form)
{
	if (form == 'contactForm' && document.getElementById('custcontact').value == 'phone' && document.getElementById(id).value == '') {
		document.getElementById('contactForm').submit();
		return true;
	}
	else if (form == 'contactForm' && document.getElementById('custcontact').value == 'email' && document.getElementById(id).value == '') {
		document.getElementById('contactForm').submit();
		return true;
	}
		
	var x = document.getElementById(id).value;
	var atpos = x.indexOf("@") ;
	var dotpos = x.lastIndexOf(".");
	var full = x.length;

	//alert(x + " " + atpos + " " + dotpos + " " + full);
  if (atpos<1 || dotpos<atpos+2 || dotpos+2>=full) { 
		alert("Please enter a valid email address");
		shake(id);	 
		return false;
  }
	
	return true;
} //end function validate_email(field)


//roll image when mouse is over or out
function imgroll(roll, id, page)
{
	document.getElementById(id).src = "media/" + page + "/" + roll;
}

function shake(e, oncomplete, distance, time) {
	
	// Handle arguments
	if (typeof e === "string") e = document.getElementById(e);
	if (!time) time = 500;
	if (!distance) distance = 5;
	
	var originalStyle = e.style.cssText;	// Save the original style of e
	e.style.position = "relative";			// Make e relatively positioned
	var start = (new Date()).getTime();		// Note the animation start time
	animate();								// Start the animation
	
	// This function checks the elapsed time and updates the position of e.
	// If the animation is complete, it restores e to its original state.
	// Otherwise, it updates e's position and schedules itself to run again.
	function animate() {
		var now = (new Date()).getTime();	// Get current time
		var elapsed = now-start;			// How long since we started
		var fraction = elapsed / time;		// What fraction of total time?
		
		if (fraction < 1) {	// If the animation is not yet complete
			// Compute the x position of e as a function of animation
			// completion fraction. We use a sinusoidal function, and multiply
			// the completion fraction by 4pi, so that it shakes back and
			// forth twice.
			var x = distance * Math.sin(fraction*4*Math.PI);
			e.style.left = x + "px";
			
			// Try to run again in 25ms or at the end of the total time.
			// We're aiming for a smooth 40 frames/second animation.
			setTimeout(animate, Math.min(25, time-elapsed));
			
			} // End of if (fraction < 1)
			
		else {	// Otherwise, the animation is complete
			e.style.cssText = originalStyle // Restore the original style 
			if (oncomplete) oncomplete(e);	// Invoke completion callback
		}
	} // End of function animate()
} // End of function shake(e, oncomplete, distance, time)
	

//Google analytics
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-25966462-1']);
  _gaq.push(['_trackPageview']);
 
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
