/* --------------------------------------------------------------------------------------------*/
/* --------------------------------->>> PREC FRAMEWORK V 1.0 <<<-------------------------------*/
/* --------------------------------------------------------------------------------------------*/
var Application = {}

Application.utils = {
	addLoadEvent: function(func){	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
			}
		}
	}
}
var PRECFRAMEWORK = {};
/* -----------------------------------*/
/* ------------>>> LOAD <<<-----------*/
/* -----------------------------------*/
PRECFRAMEWORK.baseLoad = {};


/* -----------------------------------*/
/* ----------->>> EVENTS <<<----------*/
/* -----------------------------------*/
PRECFRAMEWORK.events = {
	attachValidateEvent : function() {	 
      var validInputs = document.getElementsByClassName('valField');
      validInputs.each(function(vid) {
         Event.observe(vid, 'blur', PRECFRAMEWORK.listeners.valHandler.bindAsEventListener(PRECFRAMEWORK.listeners, {
            id : vid.id}
         )); }
      );
   }
, attachCloseForm : function() {
	 
      var closeBtns = document.getElementsByClassName('closeBtn');
      closeBtns.each(function(vid) {
         Event.observe(vid, 'click', PRECFRAMEWORK.listeners.closeBtnHandler.bindAsEventListener(PRECFRAMEWORK.listeners, {
            id : vid.id}
         )); }
      );
   }
 , fromSubmitEvent: function(){
		     var formBtn = $('processForm');
			 if(formBtn){	
				 Event.observe(formBtn, 'click', PRECFRAMEWORK.listeners.formHandler.bindAsEventListener(PRECFRAMEWORK.listeners, {id : formBtn.id}));
			}	
		}
 , fromSubmit: function(){
		     var formP = $('contactFormProcess');
			 if(formP){		 
				 Event.observe(formP, 'submit', PRECFRAMEWORK.listeners.formHandler.bindAsEventListener(PRECFRAMEWORK.listeners, {id : formP.id}));
			}	
		}
};
/* -----------------------------------*/
/* --------->>> LISTENERS <<<---------*/
/* -----------------------------------*/
PRECFRAMEWORK.listeners = {
	 formValid:null
	 
	, valHandler : function(e, args) {
      var ev;
	  ev = Event.element(e);
	  var id = ev.id;	  
      var valType = ev.getAttribute('rel');
      PRECFRAMEWORK.utils.highlightField(id, valType);
      }
	
	, formHandler: function(e, args){

	  Event.stop(e);
	  var validInputs = document.getElementsByClassName('valField');
      validInputs.each(function(vid){
		 var valType = vid.getAttribute('rel');
		
		 PRECFRAMEWORK.utils.isValid = true;
		 if(PRECFRAMEWORK.utils.validateField(vid.id, valType)==true){
		 }else{			 	
			 PRECFRAMEWORK.utils.isValid = false;
			  var field = document.getElementById(vid.id);
			  var lable = document.getElementById(vid.id + '_lbl');
				lable.className = 'err';		
				
			  lable.style.display = 'block';
		 	  field.style.background ='#fb5553';
			  field.style.color = '#fff';
		 }
		 });
		
	
		var statusLine = document.getElementById('statusLine');
		
		if(PRECFRAMEWORK.utils.isValid == true){
			$('contactFormProcess').submit();
			//PRECFRAMEWORK.XMLHTTP.processForm();		
		}else{
			statusLine.style.color = '#ff0000';
			statusLine.style.fontWeight = 'bold';
			statusLine.style.display = 'block';
			statusLine.innerHTML = '<h6>Oops, please enter or correct the information in the red.</h6>';
		}
	
	}
, closeBtnHandler: function(e, args){
		var loginLI = document.getElementById('login');
	    loginLI.className = "";
	}
};
/* -----------------------------------*/
/* ---------->>> XMLHTTP <<<----------*/
/* -----------------------------------*/
PRECFRAMEWORK.XMLHTTP = {
	loadingImage: '<img src="/images/layout/ajax-loader.gif" alt="processing" border="0" align="absmiddle"/>',
	

	loadPage:function(var1){
	var innerTab = $('sub-content');

	 var date = new Date();
      var d = date.getSeconds();
		var url = '/includes/pages/'+var1+'.html?nocache=' + d;
		new Ajax.Request(url, {
			method: 'get',	
			onSuccess: function(t) {
				var contact = '<div id="contactTab">For more information about our products<br />or our other services, call us at<br />888-PreCheck (773-2432) or <br /><a href="/PreCheck/contact-us/">click here</a> to contact a sales representitive.</div>'
				var response = (t.responseText + contact);
				PRECFRAMEWORK.utils.scalePage = true;
				 var innerTab = $('sub-content');
				 innerTab.style.height = 'auto';
     		 	innerTab.update(response);
				 PRECFRAMEWORK.events.fromSubmitEvent();
				PRECFRAMEWORK.utils.scalePage = true;
				PRECFRAMEWORK.utils.createCookie('precheckProduct',var1,1)
				PRECFRAMEWORK.utils.updateRegion();	
				
			},	
			onFailure: function(t){
			innerTab.innerHTML = 'There has been a network error.'			
			}
		});
		//loading animation
		innerTab.innerHTML = '<img src="/images/layout/ajax-loader.gif" alt="processing" border="0" />   LOADING';
		
	}
	
, loadForm:function(var1){
	var innerTab = $('sub-content');

	 var date = new Date();
      var d = date.getSeconds();
		var url = '/includes/forms/'+var1+'.html?nocache=' + d;
		new Ajax.Request(url, {
			method: 'get',	
			onSuccess: function(t) {
				var contact = '';
				var response = (t.responseText + contact);
				PRECFRAMEWORK.utils.scalePage = true;
				 var innerTab = $('sub-content');
				 innerTab.style.height = 'auto';
     		 	innerTab.update(response);
				PRECFRAMEWORK.utils.scalePage = true;				
				PRECFRAMEWORK.utils.updateRegion();	
				PRECFRAMEWORK.events.attachValidateEvent();
				PRECFRAMEWORK.events.fromSubmitEvent();
				PRECFRAMEWORK.utils.setSectionCookie();
			},	
			onFailure: function(t){
			innerTab.innerHTML = 'There has been a network error.'			
			}
		});
		//loading animation
		innerTab.innerHTML = '<img src="/images/layout/ajax-loader.gif" alt="processing" border="0" />   LOADING';
		
	}
	
	
	
	
, loadSub:function(var1){
	var tab = $('sub-content');
	 var date = new Date();
      var d = date.getSeconds();
		var url = '/includes/subs/'+var1+'.html?nocache=' + d;
		new Ajax.Request(url, {
			method: 'get',	
			onSuccess: function(t) {				
				var response = t.responseText;
				 var innerTab = $('sub-content');
				PRECFRAMEWORK.utils.createCookie('precheckProduct',var1,1)
     			 innerTab.update(response);
				PRECFRAMEWORK.utils.updateRegion();
					
			},	
			onFailure: function(t){
			tab.innerHTML = 'There has been a network error.'
			}
		});
		//loading animation
		tab.innerHTML = '<img src="/images/layout/ajax-loader.gif" alt="processing" border="0" />   LOADING';
		
	}
	
, processForm:function(){	
	var statusBtn = $('formBtn');	
	var serializedForm = Form.serialize('contactFormProcess');
	var url = '/response/salesforce/';
			new Ajax.Request(url, {
			parameters: serializedForm,
			method: 'post',		
			onSuccess: function(t) {		
				var statusBtn2 = $('formBtn');	
				statusBtn2.src = '/images/btn/contact.gif';
				var result = t.responseText
			
				var responseHolder = document.getElementById('statusLine');
				var responseBlock = document.getElementById('enterInfo');
				
				if(result.Valid == true){
					responseBlock.innerHTML = result;
				}else{
					responseHolder.innerHTML = result;
					responseHolder.style.display = 'block';
					responseHolder.sytle.color='#00ff00'
				}
			},	
			onFailure: function(t){
				var stausLine = document.getElementById('stausLine');
				stausLine.style.display = 'block';
				stausLine.innerHTML = 'There has been a network error.';
				stausLine.style.color = '#ff0000';
				statusBtn.src = '/images/btn/contact.gif';
			}
		});			
		statusBtn.src = '/images/layout/ajax-loader.gif';
		//loading animation
	}
	
, loadFeed:function(){
	var innerTab = document.getElementById('newsfeed');
	if(innerTab){
	 var date = new Date();
      var d = date.getSeconds();
		var url = '/manager/feeds/news/?type=ul&nocache=' + d;
		new Ajax.Request(url, {
			method: 'get',	
			onSuccess: function(t) {
				var response = (t.responseText);
				innerTab.innerHTML = response;	
			},	
			onFailure: function(t){
			innerTab.update('There has been a network error.');			
			}
		});
		//loading animation
		innerTab.innerHTML = '<img src="/images/layout/ajax-loader.gif" style="margin-left:25px;" alt="processing" border="0" />   LOADING';
		}
	}
};
/* -----------------------------------*/
/* ------------>>> UTILS <<<----------*/
/* -----------------------------------*/
PRECFRAMEWORK.utils = {
	isValid: false,
	scaleToNew : null,
	collapsed: false,
	scalePage: null,
	initPage: function(){
	var inner = document.getElementById("inner");
	if (inner)
	{
		inner.onmouseover = function () 
		{
			this.className += " hover";
		}
		inner.onmouseout = function ()
		{
			this.className = this.className.replace(" hover", "");
		}
	}
}
, highlightField: function(id, valType){ 
		var field = document.getElementById(id);
		var lable = document.getElementById(id + '_lbl');
		var errSpan = document.getElementById(id + '_err');
		
		 if(PRECFRAMEWORK.utils.validateField(id, valType)){
			  lable.style.display = 'block';
			  lable.className = 'suc';		
			  field.style.background ='#fff';
		 	  field.style.color='#000'; 
			  if(errSpan){
				errSpan.style.display = 'none';
				}
		 	  }else{ 
			    lable.className = 'err';
			    lable.style.display = 'block';
		 	  	field.style.background ='#fb5553';
				field.style.color = '#000';
				if(errSpan){
					errSpan.style.display = 'inline';
				}
		 	  } 
	}

, validateField: function(id, valType){ 
		var field = document.getElementById(id); 
		var str;
		str = field.value; 
		var type; 
		var regExp = new RegExp;
		
		
		switch(valType){ 
			case 'number':
				 regExp = /^[-+]?[0-9]+(\.[0-9]+)?$/;
				  break; 
			case 'currency':
				 regExp = /^\$?[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/;
				  break;
			case 'zip': regExp = /^[-+]?[0-9]+(\.[0-9]+)?$/; 
				break; 
			case 'email':
				regExp = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
				break; 
			case 'phone':
				regExp = /((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/;
				break; 
			case 'required': 
				val = true; 
				break;
			default: 
				val = true;
		 }
		 if(!str){ 
		 	return false;
		  }else{ 
		  	if(str.match(regExp)){
		  		return true;
		  	}else{
		  		return false;
		  	}
		  	 }
		}
, updateRegion: function(){
	if(PRECFRAMEWORK.utils.scalePage == true){
		PRECFRAMEWORK.utils.setFlashHeight(177);
	}else{
		
  	var outerTab = $('sub-content-holder');
	var padding = parseInt(outerTab.getStyle('padding-top')) + parseInt(outerTab.getStyle('padding-bottom'));
    var height = parseInt(outerTab.getHeight() - padding);
     outerTab.style.height = height + 'px';
      //replace contents
	  var innerTab = {};
	  var innerTab = $('sub-content');
      var newHeight = (innerTab.getHeight()+50);
	  
	  if((height - padding) !== newHeight) {
         var pct = (newHeight / height * 100);
         if(PRECFRAMEWORK.utils.scaleToNew) {
            //if the effect is already running cancel it.
		   PRECFRAMEWORK.utils.scaleToNew.cancel();
           }
				
         	PRECFRAMEWORK.utils.scaleToNew = new Effect.Scale(outerTab, pct, {
            scaleX : false, scaleContent : false, duration : 0.8, afterFinish : function() {
               PRECFRAMEWORK.utils.scaleToNew = null;
			   if(PRECFRAMEWORK.utils.scalePage){				  
			   			
			  	 }			   
			   }
            }
         	);
      	}
	}
}

, closePage: function(){
		var flashVid = $("home");
		
		// get current height and width
		var hCur = Element.getHeight(flashVid);
		var newHeight = 299;
		// scalars based on change from old to new
		var yScale = (newHeight / hCur) * 100;
		new Effect.Scale(flashVid, yScale, {scaleX: false, duration: 0.7, delay:.5, scaleContent: false});
		PRECFRAMEWORK.utils.collapsed = false;
		//objBody.className = "viddivstylenh"
	
}

 , setSection: function(args) {
		
    PRECFRAMEWORK.utils.thisMovie("productsFlash").goSwap(args.tabid);	
	if(PRECFRAMEWORK.utils.collapsed){
		PRECFRAMEWORK.XMLHTTP.loadPage(args.pname);
	}else{
		PRECFRAMEWORK.XMLHTTP.loadSub(args.pname);
	}
}

 , thisMovie: function(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}	

, setFlashHeight: function(theHeight, response) {
	
		var flashVid = $("home");
		
		// get current height and width
		var hCur = Element.getHeight(flashVid);
		var newHeight = theHeight;

		// scalars based on change from old to new
		var yScale = (newHeight / hCur) * 100;
		if(PRECFRAMEWORK.utils.scalePage == true){
			PRECFRAMEWORK.utils.scalePage = false;
			new Effect.Scale(flashVid, yScale, {scaleX: false, duration: 0.7, delay:0, scaleContent: false, afterFinish:PRECFRAMEWORK.utils.updateRegion()});
		}else{
			new Effect.Scale(flashVid, yScale, {scaleX: false, duration: 0.7, delay:.5, scaleContent: false});
		}
		PRECFRAMEWORK.utils.collapsed = true;
		//objBody.className = "viddivstylenh"
		
		
}

, restoreFlashHeight: function () {
		var flashVid = $("home");
		
		// get current height and width
		var hCur = Element.getHeight(flashVid);
		var newHeight = 250;

		// scalars based on change from old to new
		var yScale = (newHeight / hCur) * 100;
		new Effect.Scale(flashVid, yScale, {scaleX: false, duration: 0.7, delay:1.5, scaleContent: false});
		//objBody.className = "viddivstyle"	
		PRECFRAMEWORK.utils.collapsed = false;
}


, setFlex: function(){
	var page = document.getElementById("pagewidth");
	page.className = 'flex';
	var theHeight;
	if (window.innerHeight) {
		theHeight=window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
	theHeight=document.documentElement.clientHeight;
	}
	else if (document.body) {
	theHeight=document.body.clientHeight;
	}
	var contentExp = document.getElementById('header-operations');
		theHeight = theHeight - 250;
	if(contentExp){
	contentExp.style.height = theHeight + "px";
	}
	fixLayout();
	
}
, setFixed: function(){
	var page = document.getElementById('pagewidth');
	page.className = 'fixed';
}

, createCookie: function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

 , readCookie: function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

 , eraseCookie: function(name) {
	createCookie(name,"",-1);
}


, openTabs: function(args){	
	var tabNav = document.getElementById('tab-btn');		
	var tabBtn = document.getElementById('tabA');
	if(args.type == 'hide'){
		tabBtn.innerHTML = args.title;
		tabNav.style.display = 'none';
	}else{
		tabBtn.innerHTML = args.title;
		tabNav.style.display = 'block';
		PRECFRAMEWORK.XMLHTTP.loadPage(args.title);
	}
	
	var tabO = document.getElementById(args.openTab);
	tabO.style.display = 'block';
	var tabC = document.getElementById(args.closeTab);
	tabC.style.display = 'none';
	PRECFRAMEWORK.events.attachSetBackTabEvent();
}

, setSectionCookie: function(){
 var productType = $('productType');
 if(productType){
	productType.value = PRECFRAMEWORK.utils.readCookie('precheckProduct');
 }
}
, breadcrumbs: function(){
  sURL = new String;
  bits = new Object;
  var x = 0;
  var stop = 0;
  var output = '';
  sURL = location.href;
  sURL = sURL.slice(8,sURL.length);
  chunkStart = sURL.indexOf("/");
  chunkStart = sURL.indexOf("/");
  sURL = sURL.slice(chunkStart+1,sURL.length)
  while(!stop){
    chunkStart = sURL.indexOf("/");
    if (chunkStart != -1){
      bits[x] = sURL.slice(0,chunkStart)
      sURL = sURL.slice(chunkStart+1,sURL.length);
    }else{
      stop = 1;
    }
    x++;
  }

var check = 0;
	
if(sURL.length > 0 && x > 0)
{
	bits[x-2] = bits[x-2] + "/" + sURL;
check = 1;
}
	
  for(var i in bits){
    output += "<a href=\"";
    for(y=1;y<x-i;y++){
      output += "../";
    }
	if(check == 1 && bits[i] == bits[x-2])
{
qString = new String;
qString = bits[i];

output += qString;

chunkStart = qString.indexOf("/");
 bits[i] = qString.slice(0,chunkStart)
output += "\">" + ((bits[i].replace('pre-check','PreCheck')).replace('-','&nbsp;')).replace('Check', '<span class="red">Check</span>') + "</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;";
}
else{	
    output += bits[i] + "/\">" + ((bits[i].replace('pre-check','PreCheck')).replace('-','&nbsp;')).replace('Check', '<span class="red">Check</span>') + "</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;";
}
  }
  if($("breadCrumbs")){ 
 	 $("breadCrumbs").innerHTML = (output);
  }
}
, initMenu: function(){

 var nodes = document.getElementsByClassName("submenu");
 for (var i=0; i<nodes.length; i++)
 
 {
  nodes[i].onclick = function ()
  {
  if (this.parentNode.tagName == "LI"){
   if (this.parentNode.className.indexOf("active") !=-1)
   {
    this.parentNode.className = this.parentNode.className.replace("active", "");
   
   }
   else
   {
    this.parentNode.className += " active";
   }
   return false;
   }
  }
 }
}

   
}

Event.onDOMReady(function() {	  
	
	 PRECFRAMEWORK.utils.breadcrumbs();
	 PRECFRAMEWORK.events.attachCloseForm();
	 PRECFRAMEWORK.utils.setSectionCookie();
	
	
});

	
if (window.addEventListener) {		// W3C
	window.addEventListener("load",PRECFRAMEWORK.XMLHTTP.loadFeed,false);
	window.addEventListener("load",PRECFRAMEWORK.events.fromSubmitEvent,false);
	window.addEventListener("load", PRECFRAMEWORK.events.fromSubmit,false);
	window.addEventListener("load", PRECFRAMEWORK.events.attachValidateEvent,false);
} else if (window.attachEvent) {	// Exploder
	window.attachEvent("onload",PRECFRAMEWORK.XMLHTTP.loadFeed);
	window.attachEvent("onload",PRECFRAMEWORK.XMLHTTP.loadFeed);
	window.attachEvent("onload",PRECFRAMEWORK.events.fromSubmitEvent);
	window.attachEvent("onload", PRECFRAMEWORK.events.fromSubmit);
	window.attachEvent("onload", PRECFRAMEWORK.events.attachValidateEvent);
} else {							// Old skool
	window.onload = function() {PRECFRAMEWORK.XMLHTTP.loadFeed();}
}
