// $Datawitness: jquery.main.js,v 1.6 2010/02/28 20:00:44 byronm Exp $
$(document).ready(function(){
    var bIsFirebugReady = (!!window.console && !!window.console.log);
    //console.log("x defaulted to true")
    markActiveLink();

    ////////////////////////////////////////////////////////////////////////////
    // ROI Calculator                                                         //
    ////////////////////////////////////////////////////////////////////////////
    // first set some dollar values
    $(".dollar").each(function() {
	if (!$(this).val().match(/\$/)) {$(this).attr('value', '$' + $(this).val());}
    });
    $('input[name=sumA]').keyup(function() {$("#totalA").attr('value',($("input[name=sumA]").val() * $("input[name=sumB]").val()));});
    $('input[name=sumB]').keyup(function() {$("#totalA").attr('value',($("input[name=sumA]").val() * $("input[name=sumB]").val()));});
    $('input[name=sumE]').keyup(function() {
	$(".dollar").each(function() {$(this).attr('value',$(this).val().replace(/\$/g,''));});
	$("#totalF").text((Math.round(((parseFloat($("input[name=sumE]").val()) / 260) / 8)*100)/100));
	$(".dollar").each(function() { $(this).attr('value', '$' + $(this).val());});
    });
    $('input[name^=sum]').keyup(function() {
	// remove the dollar signs and operate on the numbers
	$(".dollar").each(function() {$(this).attr('value',$(this).val().replace(/\$/g,''));});

	$("#totalB").attr('value',(($("input[name=totalA]").val() * $("input[name=sumC]").val()) * 260));	
	$("#totalG").text((Math.round(($("input[name=totalB]").val() /60))));	
	$("#totalC").attr('value', ((260 * $("#totalA").val()) * $("#sumD").val()));
	$("#totalH").text((Math.round(($("#totalC").val() /60))));

	var boxTotal = ( parseFloat($("#sumF").val()) * parseFloat($("#sumG").val()) * 12 );
	$("#totalM").attr('value', (roundNumber(boxTotal, 2)));

	var aSum = parseFloat(((((($("input[name=totalA]").val() * $("input[name=sumC]").val())* $("input[name=sumE]").val())/8)/60)*100)/100);
	var bSum = parseFloat($("#totalM").val());
	var finalNum = roundNumber((aSum + bSum), 2);
	$("#totalD").attr('value', finalNum);
	$("#totalI").text(((260 * 8) * $("input[name=sumA]").val()));
	$("#totalJ").attr('value', (Math.round((((($("input[name=totalA]").val() * $("input[name=sumD]").val()) * $("input[name=sumE]").val()) / 8) / 60) *100)/100));
	$("#totalK").text(roundNumber((($("#totalG").text() / $("#totalI").text())*100), 2));
	$("#totalL").attr('value', (Math.round(parseFloat($("#totalD").val() - ($("#totalJ").val()))*100)/100));
	$("#totalP").text(roundNumber((($("#totalH").text() / $("#totalI").text())*100), 2));
	// put the dollar signs back
	$(".dollar").each(function() { $(this).attr('value', '$' + $(this).val());});
   });

    // make reseller code rock by writing it onto every datawitness.net link if it exists
    var rsl_code = $().getUrlParam("rsl_code");
    if (rsl_code) {$.cookie("rsl_code", rsl_code, { expires: 1 });}
    if ($.cookie('rsl_code') != null) {
	$("a[href^='https://www.datawitness.net/register/']").each(function() { 
      	    this.href = this.href.replace(/^https:\/\/www\.datawitness\.net\/register\//, "https://www.datawitness.net/register/index.php?rsl_code=" + $.cookie('rsl_code'));
	});
    }
});

function markActiveLink() {
    //Look through all the links in the main menu
    $("ul#menu li a").filter(function() {
	var currentURL = window.location.toString();
	return $(this).attr("href") == currentURL;    
    }).addClass("active");
    if($("ul#menu li a").hasClass("active") == false) {
	$("div#menu li:nth-child(0) a").addClass("active");
    }

}

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
    return Math.round(rnum * Math.pow(10,rlength))/Math.pow(10,rlength);
}
/* Copyright (c) 2009 Michael Manning (actingthemaggot.com) Dual licensed under the MIT (MIT-LICENSE.txt) and GPL (GPL-LICENSE.txt) licenses.*/
(function(A){A.fn.extend({currency:function(B){var C={s:",",d:".",c:2};C=A.extend({},C,B);return this.each(function(){var D=(C.n||A(this).text());D=(typeof D==="number")?D:((/\./.test(D))?parseFloat(D):parseInt(D)),s=D<0?"-":"",i=parseInt(D=Math.abs(+D||0).toFixed(C.c))+"",j=(j=i.length)>3?j%3:0;A(this).text(s+(j?i.substr(0,j)+C.s:"")+i.substr(j).replace(/(\d{3})(?=\d)/g,"$1"+C.s)+(C.c?C.d+Math.abs(D-i).toFixed(C.c).slice(2):""));return this})}})})(jQuery);jQuery.currency=function(){var A=jQuery("<span>").text(arguments[0]).currency(arguments[1]);return A.text()};

/* Copyright (c) 2006-2007 Mathias Bank (http://www.mathias-bank.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * Version 2.1
 * 
 * Thanks to 
 * Hinnerk Ruemenapf - http://hinnerk.ruemenapf.de/ for bug reporting and fixing.
 * Tom Leonard for some improvements
 * 
 */
jQuery.fn.extend({
/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* To get the document params:
* @example value = $(document).getUrlParam("paramName");
* 
* To get the params of a html-attribut (uses src attribute)
* @example value = $('#imgLink').getUrlParam("paramName");
*/ 
 getUrlParam: function(strParamName){
	  strParamName = escape(unescape(strParamName));
	  
	  var returnVal = new Array();
	  var qString = null;
	  
	  if ($(this).attr("nodeName")=="#document") {
	  	//document-handler
		
		if (window.location.search.search(strParamName) > -1 ){
			
			qString = window.location.search.substr(1,window.location.search.length).split("&");
		}
			
	  } else if ($(this).attr("src")!="undefined") {
	  	
	  	var strHref = $(this).attr("src")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else if ($(this).attr("href")!="undefined") {
	  	
	  	var strHref = $(this).attr("href")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else {
	  	return null;
	  }
	  	
	  
	  if (qString==null) return null;
	  
	  
	  for (var i=0;i<qString.length; i++){
			if (escape(unescape(qString[i].split("=")[0])) == strParamName){
				returnVal.push(qString[i].split("=")[1]);
			}
			
	  }
	  
	  
	  if (returnVal.length==0) return null;
	  else if (returnVal.length==1) return returnVal[0];
	  else return returnVal;
	}
});