/**
	Update Build Info after test & upgrading 
	new JQuery versions
	----------------------------------------
	Built off of JQuery framework: v 1.2.3
*/

function getBlurScript () {
	return "if(this.blur)this.blur();";	
}

// Removes focus from a link after it's clicked.
function removeLinkBorders () {
	$("a").focus(
		function () {
			$(this).blur();
		}
	);
}
function removeTabBorders () {
	$("ul[@class=TabbedPanelsTabGroup]").children().attr("onfocus", getBlurScript());
}

function addHomeLink () {
	$("#approva-logo").click(
		function () {
			window.location = "/";	
		}
	);
}

function addTargets () {
	jQuery.each($("a"), function () {
		if (
			($(this).attr("href").indexOf("http://") != -1 || $(this).attr("href").indexOf("https://") != -1) && $(this).attr("href").indexOf("approva.net") == -1) 
		{
			$(this).attr("target","newWin");	
		}
	});
}

function replaceNavHeaders () {
	$("#secondary-nav > dd > h5").addClass($("#secondary-nav > dd > h5").text());
}

function loadNews () {
	if (count == limit) {count = 0;}
	//$('blogticker').innerHTML = 'testing';
	$("#feedTicker > p").children().hide();
	// <a href="#"><span class="date">Feb 22, 2008</span> What is Your Controls Monitoring IQ?</a>
	$("#feedTicker > p").html('<a href="'+rssFeed[count].href+'"><span class="date">'+rssFeed[count].date+'</span> '+ rssFeed[count++].title + '</a>');
	
	$("#feedTicker > p").children().fadeIn();
	//$("#feedTicker > p").children().show();
	count++;
	//var t=setTimeout("alert('5 seconds!')",5000)
	//var f = setTimeout('$(#feedTicker > p").children().hide()',10000);
	var f = setTimeout('$("#feedTicker > p").children().fadeOut()',10000);
	var f = setTimeout("Effect.Fade('blog-entry-item')",6000);
	var a = setTimeout("loadNews()",11000);
}

function applySlideToggles () {
	$(".capabilities > dd > h5").click(
		function () {
			if ($(this).next().is(":hidden")) {
				$(this).parent().css("backgroundImage", "url(/themes/site_themes/net.approva/assets/images/icons/icon_collapse.gif)");
				$(this).parent().css("backgroundColor", "#ebf3f6");
			} else {
				$(this).parent().css("backgroundImage", "url(/themes/site_themes/net.approva/assets/images/icons/icon_expand.gif)");
				$(this).parent().css("backgroundColor", "#f7fafa");
			}
			$(this).next().toggle(); //slideToggle("slow");
		}
	);
}

// function to validate email id
function isEmail(sEmail) {
    if (sEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		if(sEmail.search(/^(\s*)$/) < 1) {
			if ((sEmail.length)-(sEmail.lastIndexOf('.')) >= 3) {
		        return true;
			} else {
				return false;
			}
		}
	} else {
        return false;
	}
}

// function to validate phone id
function isPhone (sPhone) {
	if (sPhone.length < 10) return false;
	
	var sTrm = '';
	var i = 0;
	
	while (i < sPhone.length) {
		if (isNaN(Number(sPhone.substr(i,1))) == false && sPhone.substr(i,1) != " ") {
			sTrm += sPhone.substr(i,1);
		}
		i++;
	}
	
	if (sTrm.length < 10) { 
		return false;
	} 
	return true;
}

// function to validate text id
function isValid (s) {
	return $.trim(s).length > 0;
}

/** 
 	Form Validation
	This new validator implements the jQuery framework (see jquery.com).
	@version 2.0.0.0
*/

// function to validate id (calls isEmail, isPhone, and isValid) 
function validateForm (id, formNum, dontSendEmail) {
	var bool = true;
	var error = "red";
	var color = "#d1d1d1";
	
	var qstr = "&";
	
	var num = formNum;
	
	// Check all text fields for values
	$("input[@type=text][@class=inputText]", $(id)).each( function () {
		if (!isValid($(this).val())) {
			$(this).css("border-color",error);
			//$(this).parent().addClass("error");
			$(this).prev().css("color",error);
			bool = false;
		} else {
			$(this).css("border-color",color);
			
			qstr += $(this).attr("id") + "=" + $(this).val() + "&";
			//$(this).parent().removeClass("error");
			$(this).prev().css("color","white");
		}
	});
	
	$("textarea[@class=inputTextArea]", $(id)).each( function () {
		if (!isValid($(this).val())) {
			$(this).css("border-color",error);
			//$(this).parent().addClass("error");
			$(this).prev().css("color",error);
			bool = false;
		} else {
			$(this).css("border-color",color);
			
			qstr += $(this).attr("id") + "=" + $(this).val() + "&";
			//$(this).parent().removeClass("error");
			$(this).prev().css("color","white");
		}
	});
	
	// Check all phone fields for proper format
	$("input[@type=text][@id*=phone]", $(id)).each( function () {
		if (!isPhone($(this).val())) {
			$(this).css("border-color",error);
			$(this).prev().css("color", error);
			bool = false;
		} else {
			$(this).css("border-color",color);
			$(this).prev().css("color","white");
			
			qstr += $(this).attr("id") + "=" + $(this).val() + "&";
		}
	});
	
	// Check all email addresses for proper format
	$("input[@type=text][@id*=email]", $(id)).each( function () {
		if (!isEmail($(this).val())) {
			$(this).css("border-color",error);
			$(this).prev().css("color",error);
			bool = false
		} else {
			$(this).css("border-color",color);
			$(this).prev().css("color","white");
			
			qstr += $(this).attr("id") + "=" + $(this).val() + "&";
		}
	});
	
	$("input[@type=hidden]", $(id)).each( function () {
	  if(dontSendEmail == 'undefined' || typeof dontSendEmail == false)
	  {
		if (!isValid($(this).val())) {
			bool = false;
		} else {
			qstr += $(this).attr("id") + "=" + $(this).val() + "&";
			//$(this).parent().removeClass("error");
		}
	  }
	});
	
	$("select[@class=selectOne]", $(id)).each( function () {
		if (!isValid($(this).val())) {
			$(this).css("border-color",error);
			$(this).prev().css("color",error);
		} else {
			$(this).css("border-color",color);
			$(this).prev().css("color","white");
			
			qstr += $(this).attr("id") + "=" + $(this).val() + "&";
		}
	});
	
	/*if (bool) {
		bool = setRegCookie();	
	}*/
	

	if (bool && (typeof dontSendEmail == 'undefined' || typeof dontSendEmail == false)) {
		reg = true;
		/*unloadForm($(id));
		getFile(reg);*/
		
		sendEmail(qstr, num);
	}
	if(dontSendEmail == true)
		
	  return bool;
	
	return false;
	
}

function loadContactForm () {
	loadForm($("form[@id=inquiryForm]"));
	
	return false;
}

function sendEmail (s,num) {
	var mailerURL;

	if (num == 1) {
		mailerURL = "/system/php/generalMailer.php";
	} else if (num == 2) {
		mailerURL = "/system/php/salesMailer.php";
	} else if (num == 3) {
		mailerURL = "/system/php/serviceMailer.php";
	} else if (num == 4) {
		mailerURL = "/system/php/rndMailer.php";
	} else if (num == 5) {
		mailerURL = "/system/php/mediaMailer.php";
	} else if (num == 6) {
		mailerURL = "/system/php/feedbackMailer.php";
	} else if (num == 7) {
		document.callForm.submit(); return false; // mailerURL = "/system/php/callMailer.php";
	} else if (num == 8) {
		document.demoForm.submit(); return false;// mailerURL = "/system/php/demoMailer.php";
	} else if (num == 9) {
		mailerURL = "/system/php/appMailer.php";
	}
	else {
		mailerURL = "/system/php/emailer.php";
	}
	$.ajax({
		type: "POST",
		url: mailerURL,
		/* url: "/system/php/emailer.php",*/
		data: s,
		success: function (msg) {
			/*window.location = msg;*/
			alert('Thank you. Your message has been sent.');
			//unloadForm($(id));
		}
	});
}

function setRegCookie () {
	var ex = new Date();
	var expireAt = ex.getTime() + (90 * 24 * 60 * 60 * 1000);
	var bool = true;
	
	ex.setTime(expireAt);
	document.cookie = "userStatus=isReg; path=/; expires=" + ex.toGMTString();
	
	if (!document.cookie) {
		bool = false;
		alert("Please make sure your browser is able to store cookies so that you can access the Resource Center.");
	}
	
	return bool;
}

// Float Form Script
/**
	@param id (string) The action id
	@param ohidden (object) Hidden key|value params.
*/
function loadForm (form,ohidden) {
	var top = (($(window).height() - $(form).parent().height())/2 ) + $(window).scrollTop() + "px";
	var left = (($(window).width() - $(form).parent().width())/2) + 125 + "px";
	
	$(form).parent().hide();
	
	$(form).parent().css("top",top);
	$(form).parent().css("left",left);
	
	$(form).parent().fadeIn(500);
	// add window resize event
	$(window).resize (function ()
								{	
								positionForm($(form));
								});
}

function unloadForm (form) {
	$(form).parent().fadeOut(500);	
}

function positionForm(form) {
	$(form).parent().css("top", ( ($(window).height() - $(form).parent().height())/2 ) + $(window).scrollTop() + "px");
	$(form).parent().css("left", (($(window).width() - $(form).parent().width())/2) + 125 + "px");
}

function closeForm () {
	document.getElementById("floatingForm").style.visibility = "hidden";
}
function openForm (id,str) {
	if (document.getElementById("floatingForm").style.visibility != "visible") {
		document.getElementById("floatingForm").style.visibility = "visible";
		document.getElementById("floatingForm").style.top = getScrollTop() + 60 + "px";
		document.getElementById("floatingForm").style.left = Math.floor((getClientWidth() - 400)/2) + "px";
		
		if (id) { 
			document.getElementById('00N30000000fSmp').setAttribute('value',id);
		}
		if (str) {
			document.getElementById('retURL').setAttribute('value',str);	
		}
	}
}
function checkForCookie () {
	
	var ex = new Date();
	var bValid = true;
	var expireAt = ex.getTime() + (90 * 24 * 60 * 60 * 1000);
	ex.setTime(expireAt);
	document.cookie = "userInfo=isGathered; path=/resourcecenter/; expires=" + ex.toGMTString();
	document.cookie = "userInfo=isGathered; path=/; expires=" + ex.toGMTString();
	
	if (!document.cookie) {
		bValid = false;
		alert("Please make sure your browser is able to store cookies so that you can access the Resource Center.");
	}
	
	return bValid;
}
