/* ***************************************
Javascript developed by input 9 interactive
Link: http://www.input9.com

(c) 2010

Author: Bill Hanff

Last Modified: August 4, 2010
*************************************** */


// Global variables
var isFirefox;
var isIE;
var isIE6;

var currentVidPage = 0;
var numberOfVidPages = 0;
var vidLinksPerPage = 4;


function sniff() {
		var ff = "Netscape";
		var ie = "Microsoft Internet Explorer";

		isFirefox = ( navigator.appName == ff );
		isIE = (navigator.appName == ie ); 
		isIE6 = (navigator.userAgent.indexOf("MSIE 6.") != -1) ? true : false;
	}

function fadeIn(objID, opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objID);
		if (opacity < 100) {
			opacity += 10;
			setOpacity(obj, opacity);
			window.setTimeout("fadeIn('"+objID+"',"+opacity+")", 50);
			}
		}
	}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.99:opacity; 
	obj.style.filter = "alpha(opacity:"+opacity+")"; // IE/Win
	obj.style.KHTMLOpacity = opacity/100; // Safari<1.2, Konqueror
	obj.style.MozOpacity = opacity/100; // Older Mozilla and Firefox
	obj.style.opacity = opacity/100;	// Safari 1.2, newer Firefox and Mozilla, CSS3
	}
	
	
function showhide(divID){
		var node = document.getElementById(divID);
		if(node.className == "hide"){
			node.className = "show";
			fadeIn(divID, 0);
		} else {
			node.className = "hide";
		}
	}
	
	
function externalLinks() {
			var links = document.getElementsByTagName('a');// Fetch all the anchor elements in the document.
			for (var i = links.length; i != 0; i--) { // Loop through the a elements
				var a = links[i-1]; // Pull out the element for this iteration.
				if (!a.href) continue; // If the element doesn't have an href, skip it.
				if (a.className && a.className.indexOf('external') != -1){
						a.target = '_blank';
				}
			}
		}

function removeItem(node){
			var ele = document.getElementById(node);
			if(ele.parentNode && ele.nodeType){
				ele.parentNode.removeChild(ele);
				return true;
			}else{
				return false; // not an element or no parent :(
			}
		}

function unsetTB(){ 
		removeItem('tbbg');
		removeItem('tbh');
		}
		
function tbPage(url){
		var winHt = (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight;
		var topVal = Math.round(winHt*0.1);
		var htVal = Math.round(winHt*0.8);
		
		var tbbg = document.createElement('a');
		tbbg.innerHTML = "<span style=\"display:block; width:10px; height:10px;\">&nbsp;</span>";
		tbbg.id = "tbbg";
		tbbg.style.position = (isIE6) ? "absolute" : "fixed";
		tbbg.style.display = "block";
		tbbg.style.zIndex = "5000";
		tbbg.style.top = "0";
		tbbg.style.left = "0";
		tbbg.style.width = "100%";
		tbbg.style.height = (isIE6) ? document.body.offsetHeight+"px" : "100%";
		tbbg.style.backgroundColor = "#000";
		tbbg.style.opacity = 0.75;
		tbbg.style.MozOpacity = 0.75;
		tbbg.style.filter = 'alpha(opacity=75)';

		var tbh = document.createElement('div');
		tbh.id = "tbh";
		tbh.style.position = (isIE6) ? "absolute" : "fixed";
		tbh.style.display = "block";
		tbh.style.zIndex = "5001";
		tbh.style.top = topVal + 'px'; //"160px";
		tbh.style.left = "50%";
		tbh.style.marginLeft = '-351px';
		//tbh.style.marginTop = '-'+ (Math.round((h+50)/2)) +'px';

		tbh.style.width = '702px';
		
		tbh.style.height = htVal + 'px'; //'692px';
		
		tbh.style.backgroundColor = "#FFFFFF";
		tbh.innerHTML = '<a href="#" onclick="unsetTB(); return false;" id="tbCloseButton" title=""><span>close</span></a>'; 
		
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			req.onreadystatechange = processTB;
			req.open("GET", url);
			req.send(null);	
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req) {
				req.onreadystatechange = processTB;
				req.open("GET", url);
				req.send();
			}
		}
		tbbg.onclick = unsetTB;
		window.document.body.appendChild(tbbg);
		window.document.body.appendChild(tbh);
		
		//document.getElementById('tbScroll').style.height = (Math.round(winHt*0.8)-20)+'px';
		//alert(document.getElementById('tbScroll'));
	}

function processTB(){
		if (req.readyState == 4) {
			if (req.status == 200) {
				var winHt = (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight;
				document.getElementById('tbh').innerHTML += req.responseText;
				//alert($('#tbScroll'));
				$('#tbScroll').height((Math.round(winHt*0.8)-155));
				$('#tbScroll').jScrollPane({showArrows:true, scrollbarWidth:16});
				if(document.getElementById('videostage')){
					currentVidPage = 0;
					var vidLinksHTML = document.getElementById('videolinksContent');
					var vidlinks = vidLinksHTML.getElementsByTagName("a"); 
					setupVidPopup(vidlinks[0].href);
					for(var i=0; i<vidlinks.length; i++){
						vidlinks[i].onclick = function(){ setupVidPopup(this.href); return false; };
					}

					var numberOfPages = Math.ceil(vidlinks.length/vidLinksPerPage);
					numberOfVidPages = numberOfPages;
					if(numberOfPages>1){
						for(var i=0; i<numberOfPages; i++){
							if(i==0){
								document.getElementById('videolinksNav').innerHTML = '<a href="#" title="" onclick="slideVidPages('+i+'); return false;" class="active" id="vidpagelink'+i+'">'+(i+1)+'</a>';
							} else {
								document.getElementById('videolinksNav').innerHTML += '<a href="#" title="" onclick="slideVidPages('+i+'); return false;" id="vidpagelink'+i+'">'+(i+1)+'</a>';
							}
						}
						document.getElementById('videolinksNav').innerHTML += '&nbsp;('+vidlinks.length+' videos)';
						document.getElementById('videolinksNav').innerHTML += '<div id="vidnextlastlinks"><a href="#" onclick="prevVidPage(); return false;" title="" id="prevVidPageLink"><span>&lt;</span> Previous</a><a href="#" onclick="nextVidPage(); return false;" title="" id="nextVidPageLink">Next <span>&gt;</span></a></div>';
					}
				}
			} else {
				alert("There was a problem retrieving the information.");
				unsetTB();
			}
		}
	}

function slideVidPages(toPage){
	currentVidPage = toPage;
	for(var i=0; i<numberOfVidPages; i++){
		document.getElementById('vidpagelink'+i).className = 'inactive';
	}
	document.getElementById('vidpagelink'+toPage).className = 'active';
	$('#videolinksContent').animate({ marginLeft:(-632*toPage) }, 250, 'linear');
	}

function nextVidPage(){
	if(currentVidPage+1 < numberOfVidPages){
		currentVidPage++;
		for(var i=0; i<numberOfVidPages; i++){
			document.getElementById('vidpagelink'+i).className = 'inactive';
		}
		document.getElementById('vidpagelink'+currentVidPage).className = 'active';
		$('#videolinksContent').animate({ marginLeft:(-632*currentVidPage) }, 250, 'linear');
	}
}


function prevVidPage(){
	if(currentVidPage != 0){
		currentVidPage--;
		for(var i=0; i<numberOfVidPages; i++){
			document.getElementById('vidpagelink'+i).className = 'inactive';
		}
		document.getElementById('vidpagelink'+currentVidPage).className = 'active';
		$('#videolinksContent').animate({ marginLeft:(-632*currentVidPage) }, 250, 'linear');
	}
}


function setupVidPopup(url){
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			req.onreadystatechange = processVid;
			req.open("GET", url);
			req.send(null);	
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req) {
				req.onreadystatechange = processVid;
				req.open("GET", url);
				req.send();
			}
		}
	}
function processVid(){
		if (req.readyState == 4) {
			if (req.status == 200) {
				var returnText = req.responseText;
				var returnArray = returnText.split('-----------------------------------');
				document.getElementById('tbHeader').innerHTML = returnArray[0];
				document.getElementById('videostage').innerHTML = returnArray[1];
			} else {
				alert("There was a problem retrieving the video.");
			}
		}
	}




function validateEmail(email) {
	//email = email.toString();
	if (! allValidChars(email)) {  // check to make sure all characters are valid
		return false;
		}
	if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
		return false;
	} else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
		return false;
	} else if (email.indexOf("@") == email.length) {  // @ must not be the last character
		return false;
	} else if (email.indexOf("..") >=0) { // two periods in a row is not valid
		return false;
	} else if (email.indexOf(".") == email.length) {  // . must not be the last character
		return false;
		}
	return true;
	}

function addMessage(){
			var errVal = false;
			
			if(document.getElementById('fname').value == ""){
				document.getElementById('lblfname').className = 'error';
				$('#fname').focus(function() { $('#lblfname').removeClass('error'); });
				errVal = true;
			}

			//if(document.getElementById('lname').value == ""){
			//	document.getElementById('lbllname').className = 'error';
			//	$('#lname').focus(function() { $('#lbllname').removeClass('error'); });
			//	errVal = true;
			//}
			
			//if(document.getElementById('position').value == ""){
			//	document.getElementById('lblposition').className = 'error';
			//	$('#position').focus(function() { $('#lblposition').removeClass('error'); });
			//	errVal = true;
			//}
			
			//if(document.getElementById('company').value == ""){
			//	document.getElementById('lblcompany').className = 'error';
			//	$('#company').focus(function() { $('#lblcompany').removeClass('error'); });
			//	errVal = true;
			//}
			
			//if(document.getElementById('city').value == ""){
			//	document.getElementById('lblcity').className = 'error';
			//	$('#city').focus(function() { $('#lblcity').removeClass('error'); });
			//	errVal = true;
			//}
			
			//if(document.getElementById('state').value == ""){
			//	document.getElementById('lblstate').className = 'error';
			//	$('#state').focus(function() { $('#lblstate').removeClass('error'); });
			//	errVal = true;
			//}

			//if(document.getElementById('zip').value == ""){
			//	document.getElementById('lblzip').className = 'error';
			//	$('#zip').focus(function() { $('#lblzip').removeClass('error'); });
			//	errVal = true;
			//}
			
			var mailVal = document.getElementById('emailval').value;
			if(mailVal == "" || validateEmail(mailVal)==false){
				document.getElementById('lblemail').className = 'error';
				$('#emailval').focus(function() { $('#lblemail').removeClass('error'); });
				errVal = true;
			}

			//if(document.getElementById('phone').value == ""){
			//	document.getElementById('lblphone').className = 'error';
			//	$('#phone').focus(function() { $('#lblphone').removeClass('error'); });
			//	errVal = true;
			//}
			
			//if(document.getElementById('fakecommentarea').value == ""){
			//	document.getElementById('lblcomment').className = 'error';
			//	$('#fakecommentarea').focus(function() { $('#lblcomment').removeClass('error'); });
			//	errVal = true;
			//}
			
			if(errVal == false){
				procEntry();
			} else {
				document.getElementById('errormessage').innerHTML = 'Please fix the highlighted fields.';
			}
		}

function allValidChars(email) {
	var parsed = true;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	for (var i=0; i < email.length; i++) {
		var letter = email.charAt(i).toLowerCase();
		if (validchars.indexOf(letter) != -1)
		continue;
		parsed = false;
		break;
	}
	return parsed;
}


function procEntry(){
		/*
		var processFile = $('#contactform').attr('action');
		document.getElementById('headcontactmessage').className = 'successsent';
		document.getElementById('headcontactmessage').innerHTML = '<p><strong>THANK YOU FOR CONTACTING AGAIN.</strong><br />Someone from our team will respond to you as soon as possible.</p>';
		fadeIn('headcontactmessage', 0);
		document.getElementById('contactform').className = 'hide';
		document.getElementById('tbScroll').scrollTo(0);
		*/
		var processFile = $('#contactform').attr('action');
		var sendReason = '';
		if(document.contactform.reason[0].checked){ sendReason = 'Social Media'; }
		if(document.contactform.reason[1].checked){ sendReason = 'Brand Experience'; }
		if(document.contactform.reason[2].checked){ sendReason = 'Direct Response'; }
		if(document.contactform.reason[3].checked){ sendReason = 'Mobile Marketing'; }
		
		
		var params = "fname=" + document.getElementById('fname').value;
		params += "&lname=" + document.getElementById('lname').value;
		params += "&position=" + document.getElementById('position').value;
		params += "&company=" + document.getElementById('company').value;
		params += "&address=" + document.getElementById('address').value;
		params += "&city=" + document.getElementById('city').value;
		params += "&state=" + document.getElementById('state').value;
		params += "&zip=" + document.getElementById('zip').value;
		params += "&email=" + document.getElementById('emailval').value;
		params += "&phone=" + document.getElementById('phone').value;
		params += "&reason=" + sendReason;
		params += "&fakemessage=" + document.getElementById('fakecommentarea').value;
		
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			req.onreadystatechange = processEntryComplete;
			req.open("POST", processFile, true);
			req.setRequestHeader("Content-length", params.length);
			req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			req.send(params);	
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req) {
				req.onreadystatechange = processEntryComplete;
				req.open("POST", processFile, true);
				req.setRequestHeader("Content-length", params.length);
				req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				req.send(params);
			}
		}
		
		document.getElementById('tbContactTitle').className = 'hide';
		document.getElementById('tbPageWrap').innerHTML = '<div id="contactRespnse"><p><strong>THANK YOU FOR CONTACTING AGAIN.</strong><br />Someone from our team will respond to you as soon as possible.</p></div>';
		fadeIn('contactRespnse', 0);

}

function processEntryComplete(){
	if (req.readyState == 4) {
		if (req.status == 200) {
			//document.getElementById('queueItem'+currentProc).className = 'hide';
			//alert('returned!');
		}
	}
}	


function swapFBTwitter(section){
	if(section=='twitter'){
		document.getElementById('sblinkFacebook').className = 'inactive';
		document.getElementById('sblinkTwitter').className = 'active';
		$('#fbplugin').slideUp();
		$('#twitterplugin').slideDown();
	} else {
		document.getElementById('sblinkFacebook').className = 'active';
		document.getElementById('sblinkTwitter').className = 'inactive';
		$('#fbplugin').slideDown();
		$('#twitterplugin').slideUp();
	}
}



function init(){
	sniff();
	externalLinks();
	}
	
window.onload = init;
