function swap(image, over) {
	if (over) {
		document.images[image].src = document.images[image].src.replace(/_off/gi, '_over');
	} else {
		document.images[image].src = document.images[image].src.replace(/_over/gi, '_off');
	}
}

function preload() {
	args = preload.arguments;
	var preloaded = new Array();
	for (i=0;i<args.length;i++) {
		preloaded[i] = new Image();
		preloaded[i].src = args[i];
	}
}

// open up the e-support dialog
function openESupport() {
	myWidth  = 400;
	myHeight = 350;
	myTop  = (screen.height / 2) - Math.round((myHeight/2));
	myLeft = (screen.width / 2) - Math.round((myWidth/2));
	props = 'width='+myWidth+',height='+myHeight+',top='+myTop+',left='+myLeft;
	url = '/resources/queue_disclaimer.html';
	var newwin = window.open(url,'newwin',props);
	return false;
}

// open up the solution tour
function openTour() {
	flashWin = window.open('/directpointe.html','NewWin','width=720,height=480,scrollbars=0,locationbar=0,resizeable=0,menubar=0');
	flashWin.focus();
}

function calculateROI(calcForm) {
	// set assumed global vars
	var salary_burden, term_subscription, extend_lifecycle, hours_month, hours_year, mins_hour, days_month, total_cost, monthly_cost, months_year, total_savings, monthly_savings;
	var growth_factor, growth_cost, growth_savings;
	salary_burden = 0.3;
	term_subscription = 48;
	extend_lifecycle = 6;
	hours_month = 173;
	hours_year = 2080;
	mins_hour = 60;
	days_month = 20;
	months_year = 12;
	
	// set all assumed hardware vars
	var unit_server, unit_network, unit_pc, unit_notebook, rate_virus, rate_exchange, rate_imap, rate_other_msg, rate_msg;
	var total_server, total_network, total_pc, total_notebook, total_virus, total_msg, total_savings_hardware, total_savings_extra;
	var rate_savings_hardware, rate_savings_additional;
	unit_server = 6000;     //8000;
	unit_network = 3000;    //4000;
	unit_pc = 1000;         //1300;
	unit_notebook = 2300;   //2700;
	rate_virus = 6;         //8;
	rate_exchange = 25;   
	rate_imap = 5;
	rate_other_msg = 10;
	rate_savings_hardware = 0.1;
	rate_savings_extra = extend_lifecycle / term_subscription;
	
	// set all service costs
	var total_it, total_assistance, total_external, total_training, avg_salary_hour, total_service, total_savings_service, rate_savings_service;
	cost_training = 15;
	rate_savings_service = 0.2;
	
	// set all productivity costs
	var hours_downtime, mins_spam, hours_internet;
	var total_downtime, total_design, total_mgmt, total_workflow, total_spam, total_internet, total_savings_productivity;
	var rate_design, rate_mgmt, rate_workflow, rate_savings_productivity;
	hours_downtime = 2;
	rate_design = 0.25;    
	rate_mgmt = 0.1;
	rate_workflow = 0.2;
	mins_spam = 15;
	hours_internet = 2;
	rate_savings_productivity = 0.5;	
	
	// next set all vars passed from form
	var theForm = document.getElementById(calcForm);
	var qty_server, qty_pc, qty_notebook, qty_network, avg_salary, full_time, avg_salary_it, hours_external, rate_external, hours_employees, rate_growth;
	qty_server = parseInt(theForm['qty_server'].value);
	qty_network = parseInt(theForm['qty_network'].value);
	qty_pc = parseInt(theForm['qty_pc'].value);
	qty_notebook = parseInt(theForm['qty_notebook'].value);
	avg_salary = parseFloat(theForm['avg_salary'].value);
	full_time = parseInt(theForm['full_time'].value);
	avg_salary_it = parseFloat(theForm['avg_salary_it'].value);
	hours_external = parseInt(theForm['hours_external'].value);
	rate_external = parseFloat(theForm['rate_external'].value);
	hours_assistance = parseInt(theForm['hours_assistance'].value);
	rate_growth = parseFloat(theForm['rate_growth'].value);
	
	switch(theForm['email'].value) {
		case "Outlook (Exchange)":
			rate_msg = rate_exchange;
			break;
		case "IMAP":
			rate_msg = rate_imap;
			break;
		case "Other":
			rate_msg = rate_other_msg;
			break;
		default:
		    rate_msg = 0.0;
			//rate_email = 0.0;
			break;
	}
	
	// hard code this to match the spreadsheet.
	//rate_msg = 20;
	
	// calculate all hardware/software costs
	total_server = unit_server * qty_server;
	total_network = unit_network * qty_network;
	total_pc = unit_pc * qty_pc;
	total_notebook = unit_notebook * qty_notebook;
	total_virus = rate_virus * (qty_pc + qty_notebook) * term_subscription;	
	total_msg = rate_msg * (qty_pc + qty_notebook) * term_subscription;
	total_hardware = total_server + total_network + total_pc + total_notebook + total_virus + total_msg;
	total_savings_extra = rate_savings_extra * (total_server + total_network + total_pc + total_notebook);
	total_savings_hardware = (rate_savings_hardware * total_hardware) + total_savings_extra;
	
	
	// calculate all service costs
	avg_salary_hour = (avg_salary / hours_year) * (1 + salary_burden);
	total_it = (hours_month * full_time) * ((avg_salary_it / hours_year) * (1 + salary_burden)) * term_subscription;
	total_assistance = hours_assistance * avg_salary_hour * term_subscription;
	total_external = hours_external * rate_external * term_subscription;
	total_training = (qty_pc + qty_notebook) * cost_training * term_subscription;
	total_service = total_it + total_assistance + total_external + total_training;
	total_savings_service = rate_savings_service * total_service;

	// calculate all productivity costs
	total_downtime = ((qty_pc + qty_notebook) * hours_downtime) * avg_salary_hour * term_subscription;
	total_design = (roundUp((qty_pc + qty_notebook) * rate_design)) * avg_salary_hour * term_subscription;
	total_mgmt = (roundUp((qty_pc + qty_notebook) * rate_mgmt)) * avg_salary_hour * term_subscription;
	total_workflow = (roundUp((qty_pc + qty_notebook) * rate_workflow)) * avg_salary_hour * term_subscription;
	total_spam = (((qty_pc + qty_notebook) * mins_spam) / mins_hour) * avg_salary_hour * days_month * term_subscription;
	total_internet = (qty_pc + qty_notebook) * hours_internet * avg_salary_hour * term_subscription * (1 + salary_burden);
	total_productivity = total_downtime + total_design + total_mgmt + total_workflow + total_spam + total_internet;
	total_savings_productivity = (rate_savings_productivity * total_productivity);

	
	// old code total_cost = total_hardware + total_service + total_productivity ;
	total_cost = total_hardware + total_service + (total_productivity * rate_savings_productivity);
	monthly_cost = total_cost / term_subscription;
	total_savings = total_savings_hardware + total_savings_service + total_savings_productivity;
	monthly_savings = total_savings / term_subscription;
	
	// rate_growth is a whole number, e.g. 10; divide by 100 to get the real rate
	rate_growth = (rate_growth != 0) ? rate_growth / 100 : 0;
	growth_factor = 1 + rate_growth;
	total_cost = (monthly_cost * months_year) * (1 + growth_factor + Math.pow(growth_factor, 2) + Math.pow(growth_factor, 3));
	total_savings = (monthly_savings * months_year) * (1 + growth_factor + Math.pow(growth_factor, 2) + Math.pow(growth_factor, 3));
	
	var yearlyCosts = [];
	for (i=0;i<4;i++) {
	    yearlyCosts[i] = (monthly_cost * months_year) * Math.pow(growth_factor, i);
	}
	
	var yearlySavings = [];
	for (i=0;i<4;i++) {
	    yearlySavings[i] = (monthly_savings * months_year) * Math.pow(growth_factor, i);
	}
	
	if (isNaN(total_cost) || isNaN(monthly_cost) || isNaN(total_savings) || isNaN(monthly_savings)) {
		alert("The calculator will work only with numeric values. Please complete all fields and remove all punctuation.");
	} else {
		document.getElementById('monthly_cost').innerHTML = "$ " + addCommas(monthly_cost.toFixed(0));
		document.getElementById('total_cost').innerHTML = "$ " + addCommas(total_cost.toFixed(0));
		document.getElementById('monthly_savings').innerHTML = "$ " + addCommas(monthly_savings.toFixed(0));
		document.getElementById('total_savings').innerHTML = "$ " + addCommas(total_savings.toFixed(0));
		
		// Load up the corda chart
		getRoiChart(yearlyCosts, yearlySavings);
	}
		
	return false;
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;

	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}

	return x1 + x2;
}

function roundUp(number) {
	if (parseInt(number) == number) {
		number = number;
	} else if (Math.round(number) > number) {
		number = Math.round(number);
	} else {
		number = Math.round(number) + 1;
	}
	return number;
}

function emailCheck (emailStr)
    {
		//var emailStr = document.inputform.email.value;
        var emailPat=/(.+)@(.+)/;
        var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
        var validChars="\[^\\s" + specialChars + "\]"
        var quotedUser="(\"[^\"]*\")";
        var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
        var atom=validChars + '+';
        var word="(" + atom + "|" + quotedUser + ")";
        var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
        var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
        var matchArray=emailStr.match(emailPat);
        if (matchArray==null)
            {
             alert("Email address seems invalid. Please check the value and resubmit.");
          //   return false;
             return false;
            }
        var user=matchArray[1]
        var domain=matchArray[2]

        if (user.match(userPat)==null)
            {
                alert("The email username doesn't seem to be valid.");
                return false;
            }

        var IPArray=domain.match(ipDomainPat);
        if (IPArray!=null)
            {
             for (var i=1;i<=4;i++)
                 {
                     if (IPArray[i]>255)
                         {
                             alert("Email Destination IP address is invalid!");

                          return false;
                         }
                    }
                //return true;
            }

        var domainArray=domain.match(domainPat)
        if (domainArray==null)
            {
             alert("The email domain name doesn't seem to be valid.");
             return false;
            }

        var atomPat=new RegExp(atom,"g");
        var domArr=domain.match(atomPat);
        var len=domArr.length;
        if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
            {
                alert("The email address must end in a three-letter domain, or two letter country.");
                return false;
            }

        if (len<2)
            {
                var errStr="This email address is missing a hostname!";
                alert(errStr);
		        return false;
            }
		//emailPassed = true;
        //alert("test here");
        return true;
    }