var processMove = false;
var processFMove = false;
var processSMove = false;
var actNav = "usBut";
var curFeat = 1;
var timer = 0;
var curScreen = 0;
var alphaRunning = '';
var alphaRunningTar = 0;
var fetWidth = 900;
nH = 0;
times = 0;
//debugInterval = window.setInterval("startDebug()", 440);

var rel_fetch = false;
try {
  rel_fetch = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    rel_fetch = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      rel_fetch = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      rel_fetch = false;
	  window.location = 'logout.php';
    }
  }
}

function startTimer(newFet){
	mouse = false;
	moveTimer = window.setInterval("changeMoveElement('+','"+newFet+"',625);",6000);
}

function startDebug(){
	holder = document.getElementById('debug').innerHTML;
	document.getElementById('debug').innerHTML = "Phil		= " + processMove +"<br />" +"Feature		= " + processFMove +"<br />" +"Screens		= " + processSMove +"<br />" +holder;
}

function moveElement(tar,h,butName){
	if (timer == true && mouse == true){
		timer = false;
		window.clearInterval('moveTimer');
	}
	if (butName != curFeat){
		if (!h){
			if(document.getElementById){
				h = document.getElementById(tar).style.width;
			}else if (document.layers){
				h = document.layers[tar].clip.width;
			}
			h += 70;
		}
		if (processMove == false){
			//move dot
			for(d=1;d<=3;d++){
				if (d == butName){
					document.getElementById('dot'+d).className = 'feature_dot_feature';
				}else{
					document.getElementById('dot'+d).className = 'feature_dot';
				}
			}
			curFeat = butName;
			myInterval = window.setInterval("changeHeight('"+tar+"','"+h+"','marginLeft','processMove')", 40);
			processMove = true;
		}
	}
}

function changeHeight(tar,h,el,pr){
	// GET TARGET CURRENT HEIGHT //
	curH = eval('document.getElementById(tar).style.'+el);
	curH = parseFloat(curH);
	if (checkClose(curH,h) == false){
		//if (curH < 0){ curH = curH * -1; }
		dif = h-curH;
		mov = dif/5;
		if (nH != curH+mov){ nH = curH+mov; times = 0; }else{ times = times+1; }
		if (mov < 1 && mov > -1){
			changeTo = h+"px";
		}else{
			changeTo = curH+mov+"px";
		}
		finTar = 'document.getElementById("'+tar+'").style.'+el;
		eval(finTar +"='"+changeTo+"';");
	}else{
		lastMov = new Date();
		clearInterval(myInterval);
		//document.getElementById('debug').innerHTML = "PR: " + pr + "<br />" + document.getElementById('debug').innerHTML;
		processMove = false;
	}
}

function checkClose(curH,h){
	if (Math.round(curH) != h){
		return false;
	}else{
		return true;
	}
}

function moveFeature(tar){
	if (processFMove == false){
		if (tar == "usBut" || tar == 1){
			th = 0;
			curFeat = 1;
		}else if (tar == "afBut" || tar == 2){
			th = -190;
			curFeat = 2;
		}else if (tar == "cuBut" || tar == 3){
			th = -382;
			curFeat = 3;
		}
		h = 0;
		featureInterval = window.setInterval("changeHeight('featureRotator1','"+th+"','marginTop','processFMove')", 40);
		processFMove = true;
	}
}

function navButChange(tar,act,tarName){
	if (actNav != tar){
		if (act == 'over'){
			document.getElementById(tar).className = tarName+'ButAct';
		}else{
			document.getElementById(tar).className = tarName+'But';
		}
	}
}

function changeMoveElement(dir,tar,w){

	if (curFeat < tot && dir == '+'){
		newFeat = curFeat+1;
	}else if (curFeat == tot && dir == '+'){
		newFeat = 1;
	}else if (curFeat > 1 && dir == '-'){
		newFeat = curFeat-1;
	}else if (curFeat == 1 && dir == '-'){
		newFeat = tot;
	}else if (dir == '+'){
		newFeat = curFeat++;
	}else if (dir == '-'){
		newFeat = curFeat--;
	}
	if (newFeat == 1){
		width = 1;
		moveElement(tar,width,newFeat);
	}else{
		width = -((newFeat-1)*fetWidth);
		moveElement(tar,width,newFeat);
	}
}

function showSST(tar){
	obj = document.getElementById('ssT'+tar);
	if (obj.style.display == 'none' || obj.style.display == ''){
		obj.style.display = 'block';
	}else{
		obj.style.display = 'none';
	}
}

function moveScreen(dir,max){
	if (processSMove == false){
		if (dir == '+'){
			curScreen = curScreen+1;
		}else if (dir == '-'){
			curScreen = curScreen-1;
		}
		if (curScreen > max-2){
			curScreen = 0;
		}else if (curScreen < 0){
			curScreen = max-2;
		}
		th = curScreen*303;
		th = th *-1;
		h = 0;
		screenInterval = window.setInterval("changeHeight('screenShots','"+th+"','marginLeft','processSMove')", 40);
		processSMove = true;
	}
}

function chAlpa(al,tar,img){
	//alert(al + " " + img);
	if (img){
		document.getElementById(tar).innerHTML = '<br /><br /><a href="javascript:void(0);" onclick="chAlpa(0,\'contentHolder\');"><img src="'+img+'" border="0" /></a>';
		document.getElementById(tar).style.display = "block";
		/*w = alertSize('w');
		h = alertSize('h');
		document.getElementById('contentHolder').style.width = w + "px";
		document.getElementById('contentHolder').style.height = h + "px";*/
	}
	if (al != 0){
		opacStart = 0;
	}else{
		opacStart = 100;
	}
	
	
	opacity(tar,opacStart, al, 500);
	alphaRunning = tar;
	alphaRunningTar = al;
}


function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
			if (i == 0){
				document.getElementById(id).style.display = "none";
				alphaRunning = "";
			}
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	//document.getElementById('featureStatus').innerHTML += '<br /> obj: '+id;
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function alertSize(wh) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  tH = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
  alert(myHeight + " " + tH);
  if (wh == 'w'){
  	return myWidth;
  }else{
  	return myHeight;
  }
}

function moveContact(h){
	myInterval = window.setInterval("changeHeight('contactForm','"+h+"','height','processMove')", 40);
	processMove = true;
}

function checkContact(){
	val = '';
	if (document.getElementById('yName').value == ''){
		alert('Please provide your name so we know who we are talking with.');
		document.getElementById('yName').focus();
		return false;
	}else{
		val += '&name='+document.getElementById('yName').value;
	}
	if (isEmail(document.getElementById('yEmail').value) == false){
		alert('Please provide your name so we know who we are talking with.');
		document.getElementById('yEmail').focus();
		return false;
	}else{
		val += '&email='+document.getElementById('yEmail').value;
	}
	if (document.getElementById('yWebsite').value == ''){
		alert('Please provide your website so we can better understand your current needs.');
		document.getElementById('yWebsite').focus();
		return false;
	}else{
		val += '&website='+document.getElementById('yWebsite').value;
	}
	if (document.getElementById('bestTime').value == ''){
		alert('Please provide the best time to contact you with more information.');
		document.getElementById('bestTime').focus();
		return false;
	}else{
		val += '&time='+document.getElementById('bestTime').value;
	}
	val += '&phone='+document.getElementById('yPhone').value;
	val += '&situation='+document.getElementById('situation').value;
	
	if (val != ''){
		procForm(val);
	}
}

function procForm(f){
	document.getElementById('contactForm').style.display = 'none';
	document.getElementById('contactProcess').style.display = 'block';
	if (f != ''){
		rel_fetch.open('GET', 'proc_form.php?id=1'+f, true);
		rel_fetch.send(null);
		rel_fetch.onreadystatechange = function(){ 
			if(rel_fetch.readyState  == 4){
				if(rel_fetch.status  == 200){
					if (rel_fetch.responseText == 'failed'){
						showStatus('Sorry, we couldn\'t process the form at this time. Please try again later.', 'r');
						document.getElementById('contactForm').style.display = 'block';
						document.getElementById('contactProcess').style.display = 'none';
					}else{
						document.getElementById("contactForm").style.display = 'none';
						document.getElementById('contactProcess').style.display = 'none';
						document.getElementById("contactThanks").style.display = 'block';
					}
				}
			}
		}
	}		
}


function showHide(tar,but,img){
	tar = document.getElementById(tar);
	if (tar.style.display == 'none' || tar.style.display == '' || !tar.style.display){	
		tar.style.display = 'block';
		if (img){ document[but].src = 'images/'+img+"open.gif"; }
	}else{	
		tar.style.display = 'none';	
		if (img){ document[but].src = 'images/'+img+"close.gif"; }
	}
}

function submitQuestion(){
	if (document.getElementById('fname').value == ""){
		alert("Please let us know your name so we know who to contact.");
		return false;
	}
	if (isEmail(document.getElementById('email').value) == false){
		alert('Please provide a valid email address so we respond with an answer.');
		return false;
	}
	document.forms['questionForm'].submit();
}


function isEmail(str) {
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported)
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function chAlphaV(al,tar,vid,w,h){
	if (vid && vid != "em"){
		document.getElementById(tar).innerHTML = '<object width="'+w+'" height="'+h+'"><param name="movie" value="'+vid+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+vid+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+w+'" height="'+h+'"></embed></object><div style="padding-top:10px;text-align:left;padding-left:10px;text-align:center;width:800px;margin:0px auto;"><a href="javascript:chAlpa(0,\'contentHolderContent\',\'\');">Back to Site</a></div>';
		document.getElementById(tar).style.display = "block";
	}else if (vid && vid == "em"){ 
		document.getElementById(tar).innerHTML = '<iframe title="YouTube video player" width="800" height="648" src="http://www.youtube.com/embed/eMtf3ebCdOQ" frameborder="0" allowfullscreen></iframe><div style="padding-top:10px;text-align:left;padding-left:10px;text-align:center;width:'+w+'px;margin:0px auto;background:#ffffff;"><a href="javascript:chAlpa(0,\'contentHolderContent\',\'\');">[X] Back to Site</a></div>';
		document.getElementById(tar).style.display = "block";
	}else{
		document.getElementById(tar).style.innerHTML = "";
	}
	if (al != 0){
		opacStart = 0;
	}else{
		opacStart = 100;
	}
	
	
	opacity(tar,opacStart, al, 500);
	alphaRunning = tar;
	alphaRunningTar = al;
	
}

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
			if (i == 0){
				document.getElementById(id).style.display = "none";
				alphaRunning = "";
			}
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	//document.getElementById('featureStatus').innerHTML += '<br /> obj: '+id;
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function checkTime(){
	ret = false;
	if (lastMov != 0){
		curDate = new Date();
		curMin = curDate.getSeconds();
		lastMin = lastMov.getSeconds();
		document.getElementById('debug').innerHTML = "Cur: " + curMin + "<br />" + "Last: " + lastMin + "<br />" + document.getElementById('debug').innerHTML;
		if (curMin > (lastMin+30)){
			document.getElementById('debug').innerHTML = "30: true<br />" + document.getElementById('debug').innerHTML;
			ret = true;
		}else if (lastMin > 31 && curMin != lastMin){
			chMin = lastMin-30;
			if (curMin > chMin){
				document.getElementById('debug').innerHTML = "chMin: "+ chMin + " - " + curMin +"<br />" + document.getElementById('debug').innerHTML;
				ret = true;
			}else{
				ret = false;
			}
		}else{
			ret = false;
		}
	}else{
		ret = true;
	}
	document.getElementById('debug').innerHTML = "Last 30: " + curMin + " - " + (lastMin+30) + "<br />" + "ret: " + ret + "<br />" + document.getElementById('debug').innerHTML;
	return ret;
}
