// JavaScript Document

var theTimer, flashCounter;
var counter = 0;

// AJAX INIT STUFF -- START
var xmlhttp=null;

try
{
	xmlhttp = new XMLHttpRequest();
}
catch (error)
{
	try
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (error)
	{
		xmlhttp = null;
	}
}
//  END

function echeck(str){
 var filter=/^.+@.+\..{2,4}$/

 if (filter.test(str))
    result=true
 else {
    result=false
	}
 return (result) }

function trim(str){
	var	str = str.replace(/^\s\s*/, ''),
	ws = /\s/,
	i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function clearValue(defaultValue, input){ //alert(defaultValue + '==' + input.value);
	if(defaultValue == input.value) input.value = '';
}

function setDefault(defaultValue, input){
	var currentValue = trim(input.value);
	if(currentValue == '') input.value = defaultValue;
}

function checkContactForm(){
	var passedCheck = true;
	var firstname = document.getElementById('first-name').value;
	var lastname = document.getElementById('last-name').value;
	var email = document.getElementById('email').value;
	var phone = document.getElementById('phone').value;
	var comments = document.getElementById('comments').value;
	
	if(firstname == '' || firstname == 'Enter your first name...'){
		document.getElementById('first-name').style.backgroundColor = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('first-name').style.backgroundColor = '#3587a7';
	}
	
	if(lastname == '' || lastname == '...your last name'){
		document.getElementById('last-name').style.backgroundColor = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('last-name').style.backgroundColor = '#3587a7';
	}
	
	if(email == '...your email address' || !echeck(email)){
		document.getElementById('email').style.backgroundColor = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('email').style.backgroundColor = '#3587a7';
	}
	
	if(phone == '' || phone == '...and your phone number'){
		document.getElementById('phone').style.backgroundColor = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('phone').style.backgroundColor = '#3587a7';
	}
	
	if(comments == '' || comments == '...add your comments'){
		document.getElementById('comments').style.backgroundColor = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('comments').style.backgroundColor = '#3587a7';
	}
	
	return passedCheck; }

function sendcontact(){	
	if(checkContactForm()){
		var querystring = $('#generalContact').serialize(); //alert(querystring);
		xmlhttp.open("GET", "/ajaxHandler.php?action=contact&" + querystring + "&time=" + new  Date().getTime());
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				var response = xmlhttp.responseText; //alert(response);
				document.getElementById('generalContact').innerHTML = '<div id="left"><p class="ajax-contact-thankyou"><br /><br /><em>We have received your message and will respond accordingly!</em></p></div>';
			}
		}
		xmlhttp.send(null);
	}
	
	return false; }

function checkQuickForm(){
	var passedCheck = true;
	var name = document.getElementById('name').value;
	var email = document.getElementById('email').value;
	var phone = document.getElementById('phone').value;
	var comments = document.getElementById('comments').value;
	
	if(name == '' || name == 'Enter your name...'){
		document.getElementById('name').style.color = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('name').style.color = '#515151';
	}
	
	if(email == 'Enter your email address...' || !echeck(email)){
		document.getElementById('email').style.color = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('email').style.color = '#515151';
	}
	
	if(phone == '' || phone == 'Enter your phone number...'){
		document.getElementById('phone').style.color = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('phone').style.color = '#515151';
	}

	
	if(comments == '' || comments == 'Your Comments...'){
		document.getElementById('comments').style.color = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('comments').style.color = '#515151';
	}
	
	return passedCheck; }

function sendServices(){
	if(checkQuickForm()){
		var querystring = $('#quickContact').serialize(); //alert(querystring);
		xmlhttp.open("GET", "/ajaxHandler.php?action=services-contact&" + querystring + "&time=" + new  Date().getTime());
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				var response = xmlhttp.responseText; //alert(response);
				document.getElementById('contactForm').innerHTML = '<p class="ajax-services-thankyou"><br /><br /><em>We have received your message and will respond accordingly!</em></p>';
			}
		}
		xmlhttp.send(null);
	}
	
	return false; }

function checkLeadGenForm(){
	var passedCheck = true;
	var firstname = document.getElementById('first-name').value;
	var lastname = document.getElementById('last-name').value;
	var email = document.getElementById('email').value;
	var phone = document.getElementById('phone').value;
	
	if(firstname == '' || firstname == 'Enter your first name...'){
		document.getElementById('first-name').style.backgroundColor = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('first-name').style.backgroundColor = '#3587a7';
	}
	
	if(lastname == '' || lastname == '...your last name...'){
		document.getElementById('last-name').style.backgroundColor = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('last-name').style.backgroundColor = '#3587a7';
	}
	
	if(email == '...your email address...' || !echeck(email)){
		document.getElementById('email').style.backgroundColor = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('email').style.backgroundColor = '#3587a7';
	}
	
	if(phone == '' || phone == '...and your phone number.'){
		document.getElementById('phone').style.backgroundColor = '#aa0300';
		passedCheck = false;
	}else{
		document.getElementById('phone').style.backgroundColor = '#3587a7';
	}
	
	return passedCheck; }

function sendleadgen(){	
	if(checkLeadGenForm()){
		var querystring = $('#leadGen').serialize(); //alert(querystring);
		xmlhttp.open("GET", "/ajaxHandler.php?action=leadgen&" + querystring + "&time=" + new  Date().getTime());
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				var response = xmlhttp.responseText; //alert(response);
				document.getElementById('leadGen').innerHTML = response;
				$('#downloadText').remove();
			}
		}
		xmlhttp.send(null);
	}
	
	return false; }

function backToTop(){
	$('a.back2top').click(function(){
		$('html, body').animate({
			scrollTop: $("#header").offset().top
		}, 650);
		return false;
	});
}

function toggleLogin(){	
	$('a.login').toggle(function() {
		$('html, body').animate({
			scrollTop: $("#header").offset().top
		}, 650);
		$('#login').slideDown('medium');
		$('#login div.container').animate({top:'0px'},'medium');
	}, function() {
		$('html, body').animate({
			scrollTop: $("#header").offset().top
		}, 650);
		$('#login').slideUp('medium');
		$('#login div.container').animate({top:'-35px'},'medium');
	});
}

function navPosition(){	
	$(window).scroll(function(){
		var scrollTop = $(window).scrollTop();
		$('#position').html(scrollTop);		
		if(scrollTop > 101){
			$('#nav').css({'position':'fixed','top':'0px'});
		}else{
			$('#nav').css({'position':'static','top':'0px'});
		}
	});
}

function portfolioHover(){	
	$('.portfolio div.featured p a').hover(function() {
		$(this).find('span').animate({bottom:'0px'},'medium');
	}, function() {
		$(this).find('span').animate({bottom:'-100px'},'medium');
	});
}

function ajaxTutorial(){
	$('a.tutorial').click(function(){
		counter++;
		var lastIframe = counter - 1;
		if($('.iframe-' + lastIframe).size() > 0){
			$('.iframe-' + lastIframe).slideUp('medium', function (){
				$('.iframe-' + lastIframe).remove();
			});
			$(this).parent().after('<iframe src ="' + $(this).attr('href') + '" width="100%" height="380" frameborder="0" class="iframe-' + counter + '" style="display:none"></iframe>');
			$('.iframe-' + counter + '').slideDown('medium');
		}else{
			$(this).parent().after('<iframe src ="' + $(this).attr('href') + '" width="100%" height="380" frameborder="0" class="iframe-' + counter + '" style="display:none"></iframe>');
			$('.iframe-' + counter + '').slideDown('medium');
		} //alert(counter);
		return false;
	});
}

$(document).ready(function() {  
	 toggleLogin();
	 backToTop();
	 navPosition();
	 portfolioHover();
	 ajaxTutorial();
	 
   // Use the each() method to gain access to each elements attributes
   $('area').each(function()
   {
      $(this).qtip(
      {
         content: { url: '/about-tool-tips.php', data: { 'who': $(this).attr('alt') } }, // Use the ALT attribute of the area map  content: $(this).attr('alt')
         style: {
            //name: 'dark', // Give it the preset dark style
            border: {
               width: 2, 
               radius: 0,
							 color: '#64B82C'
            }, 
						background: '#FFF',
						color: '#000',
            tip: false, // Apply a tip at the default tooltip corner
						classes: { content: 'about-styles' }
         },
				 position: {target: 'mouse'},
				 show: { effect: 'grow' }
      });
   });

	 
});
