var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;

if (isIE) {
	
	var appVersion_str = navigator.appVersion;
	var beginSlice = appVersion_str.indexOf("MSIE");
	var browserVersion = appVersion_str.slice( beginSlice+5, beginSlice+6 );
	
	//alert( "browserVersion is " + browserVersion ); 
}
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

/**
 *  Perform tasks for the onclick event
 *
 *  @type (function)
 *  @param (String) _incomingStr	 a string for the ID of a tip DIV.
 *  @param (String) _stateStr			 a string to show or hide tip.
 *
**/
function showTip( _incomingStr, _stateStr ) {

	var tipObj = document.getElementById( _incomingStr );
	
	var state = _stateStr;
	
	if ( state == "show" ) {
	
		tipObj.style.display = "block";
	}
	else if ( state == "hide" ) {
	
		tipObj.style.display = "none";
	}
}

/**
 *  Called by doBorderFramesLayout
 *	Determines window height
 *
 *  @type Function
 *  @param Object _div	DIV object to resize
 *  @param String _height	Height to perform scaling
 */
function setDivHeight( _div, _height ) {

	try {
	
	  var divObj = _div // Get div element
	  // var dh = d.offsetHeight // div height
	  var newHeight = _height; 
	  
	  divObj.style.height = newHeight + "px"; // Set div height to window height
	}
    catch (err)
    {
        //alert( "Exception inside setDivHeight() " + err);
    }
}

/**
 *  Called by doBorderFramesLayout
 *	Determines window height
 *
 *  @type Function
 */

function getWindowHeight() {

	try {
	
		var windowHeight = 0;
		
		if (typeof(window.innerHeight) == 'number')
		windowHeight = window.innerHeight;
		
		else {
			
		if (document.documentElement && document.documentElement.clientHeight)
		  windowHeight = document.documentElement.clientHeight;
			
		else {
		  if (document.body && document.body.clientHeight)
			windowHeight = document.body.clientHeight; }; };
					
		return windowHeight;
	}
    catch (err)
    {
        //alert( "Exception inside getWindowHeight() " + err);
    }
};

/**
 *  Called by doBorderFramesLayout
 *	Determines window height
 *
 *  @type Function
 *	@return String 		Returns the y value for scrolling offset
 */
function scrollingDetector() {

	try {
	
		var topY = 0;
	
		if ( isIE ) {
		
			topY = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		}
		else {
		
			topY = window.pageYOffset;
		} 
	
		//alert ("You're at " + topY + " pixels.");
		
		return topY;
	}
    catch (err)
    {
        //alert( "Exception inside scrollingDetector() " + err);
    }
}


/**
 *  Called by frame or tab layouts, handles movements with ease and memory
 *	Sizes and positions borderframe objects
 *
 *  @type Function
 *	@param String elem			Element to move
 *	@param String startPos		Starting point
 *	@param String endPos		Destination point
 *	@param String steps			Similar to total number of frames
 *	@param String intervals		How often to move
 *	@param String powr			> 1 produces ease-in, < 1 produces ease-out, = 1 produces linear animations
 */
function doPosChangeMem(elem,startPos,endPos,steps,intervals,powr) {

	try {
	
	//Position changer with Memory by www.hesido.com
		if (elem.posChangeMemInt) window.clearInterval(elem.posChangeMemInt);
		var actStep = 0;
		elem.posChangeMemInt = window.setInterval(
			function() {
				elem.currentPos = [
					easeInOut(startPos[0],endPos[0],steps,actStep,powr),
					easeInOut(startPos[1],endPos[1],steps,actStep,powr)
					];
				elem.style.left = elem.currentPos[0]+"px";
				elem.style.top = elem.currentPos[1]+"px";
				actStep++;
				if (actStep > steps) window.clearInterval(elem.posChangeMemInt);
			}
			,intervals)
	}
    catch (err)
    {
        //alert( "Exception inside doPosChangeMem() " + err);
    }
}


/**
 *  Called by doPosChangeMem, handles movements with ease
 *	Sizes and positions borderframe objects
 *
 *  @type Function
 *	@param String minValue		starting point
 *	@param String maxValue		destination point
 *	@param String totalSteps	Similar to total number of frames
 *	@param String actualStep	A specific frame reference
 *	@param String powr			> 1 produces ease-in, < 1 produces ease-out, = 1 produces linear animations
 */
function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {

	try {
	
		//Generic Animation Step Value Generator By www.hesido.com
		var delta = maxValue - minValue;
		var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
		return Math.ceil(stepp)
	}
    catch (err)
    {
        //alert( "Exception inside easeInOut() " + err);
    }
}

var OVERLAY_ID = "overlay";

var TAB_BOTTOM_HIDDEN = "tab_bottom_hidden";
var TAB_BOTTOM_VISIBLE = "tab_bottom_visible";

var TAB_CONTENT_HIDDEN = "tab_content_hidden";
var TAB_CONTENT_VISIBLE = "tab_content_visible";

var NEWS_TAB_ID ="news_tab";
var NEWS_TAB_CONTROL_ID = "news_tab_control";
var NEWS_TAB_CONTROL_HIDDEN = "news_tab_control_hidden";
var NEWS_TAB_CONTROL_VISIBLE = "news_tab_control_visible";
var NEWS_TAB_CONTENT_ID = "news_content";

var TESTIMONIAL_TAB_ID ="testimonial_tab";
var TESTIMONIAL_TAB_CONTROL_ID = "testimonial_tab_control";
var TESTIMONIAL_TAB_CONTROL_HIDDEN = "testimonial_tab_control_hidden";
var TESTIMONIAL_TAB_CONTROL_VISIBLE = "testimonial_tab_control_visible";
var TESTIMONIAL_TAB_CONTENT_ID = "testimonial_content";

var VOIP_TAB_ID ="voip_tab";
var VOIP_TAB_CONTROL_ID = "voip_tab_control";
var VOIP_TAB_CONTROL_HIDDEN = "voip_tab_control_hidden";
var VOIP_TAB_CONTROL_VISIBLE = "voip_tab_control_visible";
var VOIP_TAB_CONTENT_ID = "voip_content";

var lastTabVisible;
var tabClickCount = 0;

var imageProxy = new Image();
/**
 *  Called by tab onclick events, initiates active tab display
 *
 *  @type Function
 *	@param String _tabID		the tab ID container to activate
 */
function displayTab( _tabID ) {

	var activeTab_ID = _tabID;
	
	//alert( "activeTab_ID is " + activeTab_ID );
	
	imageProxy.src = "https://order.ucontrol.com/click_track?page_id=" + activeTab_ID;
	
	hideTabs( activeTab_ID );
	
	showTab( activeTab_ID );
	//imageProxy.src = "";
}

/**
 *  Called by displayTab(), establishes inactive tab settings
 *
 *  @type Function
 */
function hideTabs( _clickedTabID ) {

	var clickedTab_ID, hideTab_ID, hideTab_obj, tabControl_obj, tabContent_obj;

	var clickedTab_ID = _clickedTabID;

	if ( clickedTab_ID == null ) {
	
		clickedTab_ID = "";
	}
	
	hideTab_ID = NEWS_TAB_ID;
	
	//alert( "hideTab_ID is " + hideTab_ID );
	
	hideTab_obj = document.getElementById( hideTab_ID );
	
	if ( clickedTab_ID == NEWS_TAB_ID ) {
	
		//if ( hideTab_obj.className == TAB_BOTTOM_VISIBLE ) lastTabVisible = ""; 

		hideTab_obj.className = TAB_BOTTOM_HIDDEN;
	
		tabControl_obj = document.getElementById( NEWS_TAB_CONTROL_ID );
	
		tabControl_obj.className = NEWS_TAB_CONTROL_HIDDEN;
		
		tabContent_obj = document.getElementById( NEWS_TAB_CONTENT_ID );
		
		tabContent_obj.className = TAB_CONTENT_HIDDEN;
	}			
	

	hideTab_ID = TESTIMONIAL_TAB_ID;
	
	//alert( "hideTab_ID is " + hideTab_ID );
	
	hideTab_obj = document.getElementById( hideTab_ID );
	
	if ( clickedTab_ID == TESTIMONIAL_TAB_ID ) {
	
		//if ( hideTab_obj.className == TAB_BOTTOM_VISIBLE ) lastTabVisible = ""; 

		hideTab_obj.className = TAB_BOTTOM_HIDDEN;
		
		tabControl_obj = document.getElementById( TESTIMONIAL_TAB_CONTROL_ID );
	
		tabControl_obj.className = TESTIMONIAL_TAB_CONTROL_HIDDEN;
		
		tabContent_obj = document.getElementById( TESTIMONIAL_TAB_CONTENT_ID );
		
		tabContent_obj.className = TAB_CONTENT_HIDDEN;
	}		

		
	hideTab_ID = VOIP_TAB_ID;
	
	//alert( "hideTab_ID is " + hideTab_ID );
	
	hideTab_obj = document.getElementById( hideTab_ID );
	
	if ( clickedTab_ID == VOIP_TAB_ID ) {
	
		//if ( hideTab_obj.className == TAB_BOTTOM_VISIBLE ) lastTabVisible = hideTab_ID; 

		hideTab_obj.className = TAB_BOTTOM_HIDDEN;
	
		tabControl_obj = document.getElementById( VOIP_TAB_CONTROL_ID );
	
		tabControl_obj.className = VOIP_TAB_CONTROL_HIDDEN;
		
		tabContent_obj = document.getElementById( VOIP_TAB_CONTENT_ID );
		
		tabContent_obj.className = TAB_CONTENT_HIDDEN;
	}
}

/**
 *  Called by displayTab(), establishes active tab settings
 *
 *  @type Function
 *	@param String _tabID		the tab ID container to activate
 */
function showTab( _tabID ) {

	var showTab_ID = _tabID;
	var overlay_obj, tabControl_obj, tabContent_obj;
	
	//alert( "showTab_ID is " + showTab_ID );
	
	var showTab_obj = document.getElementById( showTab_ID );

	overlay_obj = document.getElementById( OVERLAY_ID );

	if ( (lastTabVisible == showTab_ID) && (tabClickCount > 0) ) {
	
		overlay_obj.style.display = "none";
		
		tabClickCount = 0;
	
		return;
	}

	if ( showTab_ID == NEWS_TAB_ID ) {
	
		overlay_obj.style.display = "inline";

		tabControl_obj = document.getElementById( NEWS_TAB_CONTROL_ID );
	
		tabControl_obj.className = NEWS_TAB_CONTROL_VISIBLE;
		
		tabContent_obj = document.getElementById( NEWS_TAB_CONTENT_ID );
		
	}
	else if ( showTab_ID == TESTIMONIAL_TAB_ID ) {
	
		overlay_obj.style.display = "inline";

		tabControl_obj = document.getElementById( TESTIMONIAL_TAB_CONTROL_ID );
	
		tabControl_obj.className = TESTIMONIAL_TAB_CONTROL_VISIBLE;
		
		tabContent_obj = document.getElementById( TESTIMONIAL_TAB_CONTENT_ID );
		
	}
	else if ( showTab_ID == VOIP_TAB_ID ) {
	
		overlay_obj.style.display = "inline";

		tabControl_obj = document.getElementById( VOIP_TAB_CONTROL_ID );
	
		tabControl_obj.className = VOIP_TAB_CONTROL_VISIBLE;
		
		tabContent_obj = document.getElementById( VOIP_TAB_CONTENT_ID );
		
	}
	
	tabContent_obj.className = TAB_CONTENT_VISIBLE;
	
	showTab_obj.className = TAB_BOTTOM_VISIBLE;

	lastTabVisible = showTab_ID;
	
	tabClickCount++;
}

/**
 *  Called by login form, passes secure login to OSM
 *
 *  @type Function
 *	@param String _tabID	the tab ID container to activate
 */
function doLogin() {

	var loginForm_obj = document.getElementById( "loginForm" );
	
	loginForm_obj.submit();
}

/**
 *  Called by login form, handles Enter keypress and submits form
 *
 *  @type Function
 *	@param Object evt		the keypress event
 */
function handleEnter(evt) {
    evt = (evt) ? evt : event;
    
    var charCode = (evt.charCode) ? evt.charCode :
        ((evt.which) ? evt.which : evt.keyCode);
    
    if (charCode == 13 || charCode == 3) {
        
        doLogin();
    } 
}

/**
 *  Called by pages to get the ucontrol.com cookie for pricing plan offer code
 *
 *  @type Function
 *	@param String c_name		the cookie name
 */
function getCookie(c_name) {
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=")
	  if (c_start!=-1)
		{ 
		c_start=c_start + c_name.length+1 
		c_end=document.cookie.indexOf(";",c_start)
		if (c_end==-1) c_end=document.cookie.length
		return unescape(document.cookie.substring(c_start,c_end))
		} 
	  }
	return ""
}

/***********************************************
* Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

/**
 *  Called by pages to get the ucontrol.com cookie for pricing plan offer code
 *
 *  @type Function
 *	@param Object _incomingField		incoming field to validate
 */
function checkEmail( _incomingField ) {

	try {
		var x = _incomingField.value;
		
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(x)) {
			
			//alert('YES! Correct email address');
		}	
		else { 
			
			alert( "Please type a correct email address." );
			_incomingField.focus();
		}
	}
    catch (err)
    {
        //alert( "Exception inside checkEmail() " + err);
    }
}
function validateContactForm()
{
	if(document.feedback_form.firstname.value == "")
	{
		alert("Please enter your first name.");
		document.feedback_form.firstname.focus();
		return false;
	}
	if(document.feedback_form.lastname.value == "")
	{
		alert("Please enter your last name.");
		document.feedback_form.lastname.focus();
		return false;
	}
	if(document.feedback_form.email.value == "")
	{
		alert("Please enter your email address.");
		document.feedback_form.email.focus();
		return false;
	}
	if(document.feedback_form.email.value.indexOf("@") == -1 ||  document.feedback_form.email.value.indexOf(".") == -1)
	{
		alert("Please enter your valid email address.");
		document.feedback_form.email.focus();
		return false;
	}
	if(document.feedback_form.comments.value == "")
	{
		alert("Please enter message/comments.");
		document.feedback_form.comments.focus();
		return false;
	}
	return true;
			
}
	
	
function wantFormValidate()
{
	if(document.info_form.firstname.value == "")
	{
		alert("Please enter your first name.");
		document.info_form.firstname.focus();
		return false;
	}
	if(document.info_form.lastname.value == "")
	{
		alert("Please enter your last name.");
		document.info_form.lastname.focus();
		return false;
	}
	if(document.info_form.email.value == "")
	{
		alert("Please enter your email address.");
		document.info_form.email.focus();
		return false;
	}
	if(document.info_form.email.value.indexOf("@") == -1 ||  document.info_form.email.value.indexOf(".") == -1)
	{
		alert("Please enter your valid email address.");
		document.info_form.email.focus();
		return false;
	}
	return true;
			
}
// doFieldForward global setting
var gIsFoward_bool = true;

/**
 *  Validates the length of a field and forwards cursor @ max length
 *
 *  @type (function)
 *  @param (Object) _incomingField		 object for the current field to validate.
 *  @param (String) _type				 a string for the type of data required in this field.
 *  @param (Number) _maxLength			 a number for the max length of this field.
 *  @param (String) _forwardingField	 a string for the field to forward cursor.
 *
**/
function doFieldForward( _incomingField, _type, _maxLength, _forwardingField ) {

	try {	

		var currentField = _incomingField;
		var currentValue = currentField.value;
		var currentLength = currentValue.length;

		// ensure the field is not blank
		if ( currentLength > 0 ) {
			var regExp;
			
			// validate type of data in field
			if ( _type == "number" ) {
	
				var regExp = /^\d+$/;
				
				//alert( "About to regExp currentValue as " + currentValue );
			
				if ( currentValue.search(regExp) == -1 ) {
				
					alert( "Please type a number from 0-9 in this field." );
					currentField.value = currentValue.slice(0, currentLength-1);
					currentField.focus();
					return;
				}
			}
		
			if ( gIsFoward_bool ) {		
				
				//alert( "currentLength is " +  currentLength );
				
				// validate current length versus _maxlength
				if ( currentLength == _maxLength  ) {
				
					// forward the focus
					var tempObj = document.getElementById( _forwardingField );
					tempObj.focus();
				}
			}
		}
	}
	catch (err) {
	
		//alert( "Error inside doFieldForward() " + err );
	}
}
