//  Cloudtoolkit.com JS
//  © 2009 SADA Systems Inc.
//  sadaweb@sadasystems.com
// -------------------------------------------------------------------

// START Password Masking / Keypress actions
function onFocusHandler_p(fld){
	fld.style.display = 'none';
	var fld2 = (document.getElementById) ? document.getElementById('password') : document.all['password'];
	fld2.style.display = 'block';
	fld2.focus();
	return true;
}
function onBlurHandler_p(fld){
	if(fld.value === ''){
		fld.style.display = 'none';
		var fld2 = (document.getElementById) ? document.getElementById('pass') : document.all['pass'];
		fld2.style.display = 'block';
	}
	return true;
}

function submitenter(myfield,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13)
	   {
	   myfield.form.submit();
	   return false;
	   }
	else
	   return true;
}
// END Password Masking / Keypress actions


function getTagText( doc, tagName )
{
    if ( doc.getElementsByTagName( tagName ).length != 0 )
      return doc.getElementsByTagName( tagName )[0].firstChild.data;
    else
      return "";
}

function loadDashboardTab() {
  $( ".inner_content:eq(0)" ).empty();
	$.get( "/CTK/static/Dashboard_TabHeader.xml", function( data ) {
			$( ".inner_content:eq(0)" ).empty();
			var header = "<h1>" + getTagText( data, "Headline" ) + "</h1>";
			$( ".inner_content:eq(0)" ).append( header );
			var icon = "<img src=\"" + getTagText( data, "icon" ) + " \" class=\"general_icon\" />";
			$( ".inner_content:eq(0)" ).append( icon );
			var content = "<p>" + getTagText( data, "Text" ) + "</p>";
			$( ".inner_content:eq(0)" ).append( content );
		}
	);
	
	$( "#dTable" ).load( "/CTK/DashboardHTML.php", function( data ) {
		
    $( "#boxes" ).load( "/CTK/DashboardModals.php", function( data ) {
      activateModalLinks();
    });	
    addZebraStripes();
	});

}

function loadDomainTab( ) {
	$( ".inner_content:eq(1)" ).empty();
	$.get( "/CTK/static/Domain_TabHeader.xml", function( data ) {
      $( ".inner_content:eq(1)" ).empty();
			var header = "<h1>" + getTagText( data, "Headline" ) + "</h1>";
			$( ".inner_content:eq(1)" ).append( header );
			var icon = "<img src=\"" + getTagText( data, "icon" ) + " \" class=\"general_icon\" />";
			$( ".inner_content:eq(1)" ).append( icon );
			var content = "<p>" + getTagText( data, "Text" ) + "</p>";
			$( ".inner_content:eq(1)" ).append( content );
		}
	);

  $( "#domains" ).load( "/CTK/DomainHTML.php", function( data ) { 
    addZebraStripes(); 
    if ( $( "#domains > .stripeMe" ).html().length > 0)
      $( "#dwarning" ).show();
  });	
 
}


function loadDomainAddition() {
	
	$( "#newarea" ).load( "/CTK/DomainAddition.php", $( "#domainname" ).serialize(), function( data ) {
			
	});

}

function loadReportsTab() {
	$.get( "/CTK/static/Reports_TabHeader.xml", function( data ) {
			$( ".inner_content:eq(2)" ).empty();
			var header = "<h1>" + getTagText( data, "Headline" ) + "</h1>";
			$( ".inner_content:eq(2)" ).append( header );
			var icon = "<img src=\"" + getTagText( data, "icon" ) + " \" class=\"general_icon\" />";
			$( ".inner_content:eq(2)" ).append( icon );
			var content = "<p>" + getTagText( data, "Text" ) + "</p>";
			$( ".inner_content:eq(2)" ).append( content );
		}
		
	);
	
	$( "#rTable" ).load( "/CTK/ReportHTMLX.php", function( data ) {
		addZebraStripes();
	});
}

function loadDeploymentTab() {
	$.get( "/CTK/static/Deployment_TabHeader.en.xml", function( data ) {
			$( ".inner_content:eq(3)" ).empty();
			var header = "<h1>" + getTagText( data, "Headline" ) + "</h1>";
			$( ".inner_content:eq(3)" ).append( header );
			var icon = "<img src=\"" + getTagText( data, "icon" ) + " \" class=\"general_icon\" />";
			$( ".inner_content:eq(3)" ).append( icon );
			var content = "<p>" + getTagText( data, "Text" ) + "</p>";
			$( ".inner_content:eq(3)" ).append( content );
			$( ".tabContent:eq(3)" ).empty();
			var innercontent = getTagText( data, "innercontent" );
			$( ".tabContent:eq(3)" ).append( innercontent );
		}
		
	);			
}

function loadTrainingTab() {
	$.get( "/CTK/static/Training_TabHeader.en.xml", function( data ) {
			$( ".inner_content:eq(4)" ).empty();
			var header = "<h1>" + getTagText( data, "Headline" ) + "</h1>";
			$( ".inner_content:eq(4)" ).append( header );
			var icon = "<img src=\"" + getTagText( data, "icon" ) + " \" class=\"general_icon\" />";
			$( ".inner_content:eq(4)" ).append( icon );
			var content = "<p>" + getTagText( data, "Text" ) + "</p>";
			$( ".inner_content:eq(4)" ).append( content );
			$( ".tabContent:eq(4)" ).empty();
			var innercontent = getTagText( data, "innercontent" );
			$( ".tabContent:eq(4)" ).append( innercontent );
		}
	);			
}

function loadResourcesTab() {
	$.get( "/CTK/static/Resources_TabHeader.en.xml", function( data ) {
			$( ".inner_content:eq(5)" ).empty();
			var header = "<h1>" + getTagText( data, "Headline" ) + "</h1>";
			$( ".inner_content:eq(5)" ).append( header );
			var icon = "<img src=\"" + getTagText( data, "icon" ) + " \" class=\"general_icon\" />";
			$( ".inner_content:eq(5)" ).append( icon );
			var content = getTagText( data, "Text" );
			$( ".inner_content:eq(5)" ).append( content );
			$( ".tabContent:eq(5)" ).empty();
			var innercontent = getTagText( data, "innercontent" );
			$( ".tabContent:eq(5)" ).append( innercontent );
		}
		
	);			
}

function configToggle( domain )
{
	$( "#domain_" + domain ).hide(); // delcare this for each domain
	$( "#domain_" + domain + "_c" ).click(function () { // delcare this for each domain
		$(this).toggleClass("domain_link_open").toggleClass("domain_link_closed"); 
		$( "#" + this.id.slice(0, -2) ).toggle("slow"); 
		return true;
	});
}

function addZebraStripes()
{
	$(".stripeMe tr:even").addClass("alt");
/*
	$(".stripeMe tr").mouseover(function() {
		$(this).addClass("over");
	});
	$(".stripeMe tr").mouseout(function() {
		$(this).removeClass("over");
	});
  */
}

function activateModalLinks() 
{
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		//Get the A tag
		var id = $(this).attr('href');
		
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set height and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(1000);
	
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('.window').fadeOut(1000, function() {
   		   $('.window').hide();
   		});

		$('#mask').fadeOut(1000, function() {
   		   $('#mask').hide();
   		});
		
		
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		//$(this).hide();
		//$('.window').hide();
		
		$('.window').fadeOut(1000, function() {
   		   $('.window').hide();
   		});

		$(this).fadeOut(1000, function() {
   		   $(this).hide();
   		});
	});
}


function loginFormSubmit()
{
	var redirect = document.getElementsByName( 'redirect' )[ 0 ];
	//redirect.value = location.href;
	$("#loginForm").submit()
}

// Login widget

$(document).ready(function(){
  uNm = "";
  c_name = "ctk_user";

  if (document.cookie.length>0)
  {
    c_start=document.cookie.indexOf("ctk_user=");
    if (c_start!=-1)
    {
      if ( document.cookie.indexOf( "ctk_pass=" ) != -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;
        uNm = unescape(document.cookie.substring(c_start,c_end));
      }
    }
  }

  if ( uNm != "" )
  {
    htmlStr = "Welcome, " + uNm + "&nbsp;|&nbsp;<a href=\"/CTK/Dashboard\">Dashboard</a>&nbsp;|&nbsp;<a href=\"/CTK/AccountLogin.php?logout=1\">Log Out</a>";
    $( ".login" ).empty();
    $( "#go_btn" ).empty();
    $( ".login" ).append( htmlStr );
    $( ".login" ).css( "color", "#FFF" );
    $( ".login a\\:link, .login a\\:visited" ).css( "text-decoration", "none" );
    $( ".login a\\:hover, .login a\\:active" ).css( "color", "#fec826" );
    $( ".login a\\:hover, .login a\\:active" ).css( "text-decoration", "none" );
  }  
});

// The Tabs
$( function() { 
  $("#dash ul.tabs").tabs("#dash div.panes > div", {
	history: true,
    onClick: function( i ) {    
			var pane = this.getPanes().eq( i );
			var func =  this.getTabs().eq(i).attr( "rel" );
			var funcPair = func.split( ':' );
			eval( funcPair[1] );
    }
	}); 
    
});


// function gets permission info
function getAllPermissionInfo() {
	$.get( "PermissionXML.php", function( data ) {
			
			var addPerm = getTagText( data, 'AddDomain' );
			var permissionList = new Array;
			dNodes = data.getElementsByTagName( 'Domain' );
			for ( var idx = 0; idx < dNodes.length; idx++ )
			{
				var pSet = new Array();
				var attrs = dNodes[ idx ].childNodes;
				for ( var i = 0; i < attrs.length; i++ )
				{
					if ( attrs[i].nodeType == 1 )
					{
						var key = attrs[ i ].localName;
						var value = attrs[ i ].firstChild.data;
						pSet[ key ] = value;
					}
				}
				permissionList.push( pSet );
			}
			
			return [ addPerm, permissionList ];
		
	});
}

// function gets all the tool information
function getAllUtilityInfo() {
	$.get( "UtilityXML.php", function( data ) {
			var utilityList = new Array;
			uNodes = data.getElementsByTagName( 'Utility' );
			for ( var idx = 0; idx < uNodes.length; idx++ )
			{
				var uSet = new Array();
				var attrs = uNodes[ idx ].childNodes;
				for ( var i = 0; i < attrs.length; i++ )
				{
					if ( attrs[i].nodeType == 1 )
					{
						var key = attrs[ i ].localName;
						var value = attrs[ i ].firstChild.data;
						uSet[ key ] = value;
					}
				}
				utilityList.push( uSet );
			}
			
			return utilityList;
		
	});
}


// Jquery UI Rollovers 
//Hovers
$('#dialog_link, ul#icons li, .any_link').hover(
	function() { $(this).addClass('ui-state-hover'); }, 
	function() { $(this).removeClass('ui-state-hover'); }
);