//////////////////////////////////////////////////////////////////
// wind.js v1.0
// 
// Company: New West Technologies, LLC
//          3451 Garden City Drive
//          Suite 600
//          Landover MD, 20785
//          301.429.1180
//
// Author: Kevin King
//////////////////////////////////////////////////////////////////
// max federal tax credit available
var max_tax_credit = 0;

//////////////////////////////////////////////////////////////////
///////////////DO NOT EDIT BELOW THIS LINE////////////////////////
//////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////
//DEBUG AND TESTING/////DEBUG AND TESTING/////DEBUG AND TESTING/////DEBUG AND TESTING//
///////////////////////////////////////////////////////////////////////////////////////

//DEBUG SETTINGS
var DEBUG = false;
var LEVEL = 1;

//TESTING SETTINGS
var TESTING = false;

///////////////////////////////////////////////////////////////////////////////////////
//DEBUG AND TESTING/////DEBUG AND TESTING/////DEBUG AND TESTING/////DEBUG AND TESTING//
///////////////////////////////////////////////////////////////////////////////////////

function turbine() { //turbine object
	this.company;
	this.type;
	this.name;
	this.power;
	this.cost;
	this.options_list;
	this.diameter;
	this.direct_drive;
	this.o_and_m;
	this.lifetime;
}

function tower() { //tower object
	this.id;
	this.height;
	this.tower_cost;
	this.other_cost;
	this.cost;
}

function tcr() { //tower cross reference object
	this.id;
	this.t1;
	this.t2;
	this.t3;
	this.t4;
	this.t5;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Building the GET variable
// Use when you pass things to the url through httpGET
///////////////////////////////////////////////////////////////////////////////////////////////////////////

var get_vars = document.URL; //retrieve the url
var GET = new Array(); //create our array of values

if(get_vars.indexOf("?") > 0) { //check to see if there are any variables passed by GET
	get_vars = get_vars.substr(get_vars.indexOf("?") + 1) + "&";
} else {
	get_vars = "";
}

//array index (was used in debug, not used in function)
i=0;
while(get_vars.length > 0) { //create the GET array
	name = get_vars.substr(0,get_vars.indexOf("="));
	value = get_vars.substr(get_vars.indexOf("=") + 1, get_vars.indexOf("&") - get_vars.indexOf("=") - 1);
	GET[name] = value;
	while(GET[name].indexOf("+") > 0) {
		GET[name] = GET[name].replace("+"," ");
	}
	get_vars = get_vars.substr(get_vars.indexOf("&") + 1);
	i++;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////

//declare arrays
var turbines = new Array();
var towers = new Array();
var cross_reference = new Array();
var xmlurl = new Array("common/turbines.xml", "common/towers.xml", "common/tower_type.xml");
var x;

//check browser for XML Compatibility
if (window.ActiveXObject) {// code for IE
	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
} else if (document.implementation.createDocument) {// code for Firefox, Mozilla, Opera, etc.
	xmlDoc=document.implementation.createDocument("","",null);
} else {
	alert('Your browser cannot handle this script');
}

//retrieve XML documents
for(j=0;j<3;j++) {
	xmlDoc.async=false;
	xmlDoc.load(xmlurl[j]);
	switch(j) {
		case 0: //turbines
			x=xmlDoc.getElementsByTagName("turbine");
			turbines = new Array(x.length);
			
			for(i=0;i<x.length;i++) {
				turbines[i] = new turbine();
				turbines[i].company = x[i].getElementsByTagName("company")[0].childNodes[0].nodeValue;
				turbines[i].type = x[i].getElementsByTagName("type")[0].childNodes[0].nodeValue;
				turbines[i].name = x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
				turbines[i].power = x[i].getElementsByTagName("power")[0].childNodes[0].nodeValue;
				turbines[i].cost = x[i].getElementsByTagName("cost")[0].childNodes[0].nodeValue;
				turbines[i].options_list = x[i].getElementsByTagName("options_list")[0].childNodes[0].nodeValue;
				turbines[i].diameter = x[i].getElementsByTagName("diameter")[0].childNodes[0].nodeValue;
				turbines[i].direct_drive = x[i].getElementsByTagName("direct_drive")[0].childNodes[0].nodeValue;
				turbines[i].o_and_m = x[i].getElementsByTagName("o_and_m")[0].childNodes[0].nodeValue;
				turbines[i].lifetime = x[i].getElementsByTagName("lifetime")[0].childNodes[0].nodeValue;
//				alert(turbines[i].name);	//debug
 			}
			break;
		case 1: //towers
			x=null;
			x=xmlDoc.getElementsByTagName("tower_type");
			towers = new Array(x.length + 1);
			towers[0] = new tower();
			
			for(i=0;i<x.length;i++) {
				tid = x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
				towers[tid] = new tower();
				towers[tid].id = x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
				towers[tid].height = x[i].getElementsByTagName("height")[0].childNodes[0].nodeValue;
				towers[tid].tower_cost = x[i].getElementsByTagName("tower_cost")[0].childNodes[0].nodeValue;
				towers[tid].other_cost = x[i].getElementsByTagName("other_cost")[0].childNodes[0].nodeValue;
				towers[tid].cost = x[i].getElementsByTagName("cost")[0].childNodes[0].nodeValue;
			}
			break;
		case 2: //turbine-tower cross reference
			x=null;
			x=xmlDoc.getElementsByTagName("index");
			cross_reference = new Array(x.length);
			
			for(i=0;i<x.length;i++) {
				cross_reference[i] = new tcr();
				cross_reference[i].id = x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue; 
				cross_reference[i].t1 = x[i].getElementsByTagName("t1")[0].childNodes[0].nodeValue;
				cross_reference[i].t2 = x[i].getElementsByTagName("t2")[0].childNodes[0].nodeValue;
				cross_reference[i].t3 = x[i].getElementsByTagName("t3")[0].childNodes[0].nodeValue;
				cross_reference[i].t4 = x[i].getElementsByTagName("t4")[0].childNodes[0].nodeValue;
				cross_reference[i].t5 = x[i].getElementsByTagName("t5")[0].childNodes[0].nodeValue;
				cross_reference[i].t6 = x[i].getElementsByTagName("t6")[0].childNodes[0].nodeValue;
			}
			break;
	}
}


//////////
//IsNumeric(str)
// checks to see if a string is numeric
//////////
function IsNumeric(str) {
	if(DEBUG && LEVEL == 0)
		alert("IsNumeric(" + str + ")");

//	var ValidChars = "0123456789.,";
	var ValidChars = "0123456789.";
	var IsNumber = true;
	var Char;

	try {
		for (z = 0; z < str.length && IsNumber == true; z++) { //checks each character against the vaild characters string
			Char = str.charAt(z); 
			
			if (ValidChars.indexOf(Char) == -1) {
				if(z == 0 && (Char == "-" || Char == "+"))	//check to see if there is a direction assigned to number
					continue;
				else	//character was not in ValidChars
					IsNumber = false;
			}
		}
	} catch (err) {	//added while looking for an error, probably not necessary
		alert("Error in IsNumeric(str): " + err);
		return false;
	}
	
	return IsNumber;
}

//////////
//calc_pmt(APR,months_in_loan,principal_balance)
// calculates the monthly payment necessary to pay of PBAL in NPER given RATE of interest
//////////
function calc_pmt(rate,nper,pbal) {
	if(DEBUG && LEVEL == 0)
		alert("calc_pmt");

	rate = rate/100; //convert percent to number
	z = 1 + rate;
	g = Math.pow(z,nper);
	
	pmt = pbal * rate * g/(g - 1);

	return(pmt);
}

//////////
//precise_round(n [,p])
// rounds N to P places
// P can be any integer value
// P = 0 is alias for Math.round()
// default value of P is 2
//////////
function precise_round() {
	var p = 2;	//precision (default is 2)
	var n;	//number to round
	var m;	//output number

	var str;	//for debug

	if(precise_round.arguments.length == 0) {	//called w/o args
		return null;
	} else if(precise_round.arguments.length == 1) { //called w/ one arg, assume default precision
		n = precise_round.arguments[0];
		str = n;
	} else {	//called with 2 or more args, ignores all args past the second
		if(Math.round(precise_round.arguments[1]) != precise_round.arguments[1]) {	//check if second arg is not an integer
			return null;
		}
		n = precise_round.arguments[0];
		p = precise_round.arguments[1];
		str = n + ", " + p;
	}

	if(DEBUG && LEVEL == 0)
		alert("precise_round(" + str + ")");

	if(!IsNumeric(p) || !IsNumeric(n)) {	//check to see if p and n are numeric (IsNumeric is a custom function)
		return null;
	}

	if(p == 0) {	//default to math.round if p=0
		return Math.round(n);
	} else if(p > 0) {	//round to decimal places
		m = n*Math.pow(10,p);
		m = Math.round(m);
		m = m/Math.pow(10,p);
	} else {	//round to significant integer places
		m = n/Math.pow(10,p);
		m = Math.round(m);
		m = m*Math.pow(10,p);
	}
	return m;
}

function fill_form_GET(name) {	//fill the form from the GET vars
	if(DEBUG)
		alert("fill_form_GET(" + name + ")");
	
	var form = document.getElementById(name); //retrieve form
	for(i=0;i<form.length;i++) {	//for all form entries
		try {	//try and find a GET[] object entry that applies to the form 
			if(GET[form.elements[i].id]) {
				if(form.elements[i].selectedIndex >= 0) {	//need to treat <select></select> objects special
					form.elements[i].selectedIndex = GET[form.elements[i].id];
					if(form.elements[i].id.indexOf("turbine") >= 0)	//this is custom - needed to reload the tower height list
						towers_list(form.elements[i].value, form.elements[i].id.substr(form.elements[i].id.length - 1) );
				} else {	//other form objects
					form.elements[i].value = GET[form.elements[i].id];
				}
			}
		} catch (e) {	//means GET[form.elements[i].id] did not exist
		}
	}
}

function more_info(loc) {
	if(DEBUG && LEVEL == 0)
		alert("more_info(" + loc + ")");
	
	dest = "common\\more_info.asp#" + loc;
	w = window.open(dest,"window1","width=540,height=520"); //toolbar=0,scrollbars=0,
//	alert(w.location.href);
	w.focus();
}





//////////////////////////////////////////////////////////////////
// These functions are not used in the final program. They were //
// intended for use, but were never properly debugged before    //
// deployment.  All functions are known to have issues. These   //
// may or may not be noted before the function.                 //
//////////////////////////////////////////////////////////////////


//never tested - unknown if useful
function printer() {	//print this page
	document.getElementById("error").style.visibility= "hidden";
	document.getElementById("buttons").style.visibility= "hidden";
	si1 = document.getElementById("turbine1").selectedIndex;
	si2 = document.getElementById("turbine2").selectedIndex;
	document.getElementById("turbine1").size=1;
	document.getElementById("turbine2").size=1;
	document.getElementById("turbine1").selectedIndex = si1;
	document.getElementById("turbine2").selectedIndex = si2;
	window.print();
	document.getElementById("error").style.visibility= "";	
	document.getElementById("buttons").style.visibility= "visible";
	document.getElementById("turbine1").size=4;
	document.getElementById("turbine2").size=4;
	document.getElementById("turbine1").selectedIndex = si1;
	document.getElementById("turbine2").selectedIndex = si2;
}

// never tested - unknown if functional
// supporting files missing
function send_email() {
	e=window.open("email.html","email","menubar=0,toolbar=0,scrollbars=0,width=500,height=360"); //
}

// is expected to work for basic calculator, but
// not advanced calculator - no final testing done
// does not pass all necessary vars to GET

// bookmark function variables
//var book_loc = location.href;
var book_name = "MD Wind Resource Calculator";
function bookmark() {
	if(DEBUG && LEVEL == 0)
		alert("bookmark()");

	for(i=0;i<bookmark.arguments.length && i<2;i++) {	//check if bookmark was passed args
		switch(i) {
			case 0: //first arg is location
				book_loc = bookmark.arguments[i].value;
				break;
			case 1: //second arg is name
				book_name = bookmark.arguments[i].value;
				break;
		}
	}

	try {	//see if book_loc exists
		if(book_loc != "")
			loc_missing = false;
		else
			loc_missing = true;
	} catch (err) {
		loc_missing = true;
	}

	try {	//see if book_name exists
		if(book_name != "")
			name_missing = false;
		else
			name_missing = true;
	} catch (err) {
		name_missing = true;
	}
	

	if(loc_missing) { 	//check if location is set
		book_loc = location.href;

		if(book_loc.indexOf("advanced=") > 0) {
			if(book_loc.indexOf("lat=") > 0)
				book_loc = book_loc.substr(0,book_loc.indexOf("lat=") - 1);
			else if(book_loc.indexOf("turbine") > 0)
				book_loc = book_loc.substr(0,book_loc.indexOf("turbine") - 1);

			custom = document.getElementById("location_data");

			for(i=0;i<custom.length;i++) {
				if(custom.elements[i].id != "" && custom.elements[i].value != "" && custom.elements[i].id.indexOf("submit") == -1 && custom.elements[i].id.indexOf("calculate") == -1) {
					if(custom.elements[i].selectedIndex >= 0)
						book_loc = book_loc + "&" + custom.elements[i].id + "=" + custom.elements[i].selectedIndex;
					else
						book_loc = book_loc + "&" + custom.elements[i].id + "=" + custom.elements[i].value;
				}
			}

			custom = document.getElementById("user_data");
			
		} else {
			custom = document.getElementById("cost_inputs");

			if(book_loc.indexOf("cpkwh1") > 0)
				book_loc = book_loc.substr(0,book_loc.indexOf("cpkwh1") - 1);
		}			
		

		for(i=0;i<custom.length;i++) {
				if(custom.elements[i].id != "" && custom.elements[i].value != "" && custom.elements[i].id.indexOf("submit") == -1 && custom.elements[i].id.indexOf("calculate") == -1) {
					if(custom.elements[i].selectedIndex >= 0)
						book_loc = book_loc + "&" + custom.elements[i].id + "=" + custom.elements[i].selectedIndex;
					else
						book_loc = book_loc + "&" + custom.elements[i].id + "=" + custom.elements[i].value;
				}
		}
		
		alert(book_loc.indexOf("units=") + " " + units);
		if(book_loc.indexOf("units=") > 0) {
			book_loc = book_loc.replace(/feet/i,units);
			alert(book_loc);
			book_loc = book_loc.replace(/meters/i,units);
			alert(book_loc);
		} else {
			book_loc.concat("&units=",units);
		}
		
	}
	

	if(name_missing) { 	//check if name is set
		book_name = document.title;
	}
	
	//create favorite
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(book_name, book_loc,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( book_loc, book_name); 
	}

}

