// ============================================================================
// Default Variables
//
// browser constants
var BRWR_NAME = navigator.appName;
var BRWR_PFRM = navigator.platform;
var BRWR_VERS = parseFloat(navigator.appVersion);

// ============================================================================
// Render Appropriate Stylesheet

// ============================================================================
// Default Variables
//
// constants
var ary_PRODUCTS = new Array("F500","F5AL","F100","F1AL","G500","G5AL","FBCS","FGCS", "F1G5","F1GA","F5G5","AF5G","DTSC");		// product list
var PRODPRICES = new conf_PricesClass();

// these are the costs of the individual bundles with all options in the DTSC
var DTSC_F100 = 999;
var DTSC_G500 = 499;
var DTSC_FGC = 845;

// global variables
var gLastProduct = "";
var gCurrProduct = "";
var gCost = 0;
var gSave = 0;
var gValue3 = "";


//alert('In config.js Currprod ' + gCurrProduct + 'done');
// ============================================================================
// Pricing Class
//
function conf_PricesClass()
{
this.F500 = new conf_ProdClass("FORTUNE 500",599);
this.F5AL = new conf_ProdClass("FORTUNE 500 w/executive contact names",999);
this.F100 = new conf_ProdClass("FORTUNE 1000",999);
this.F1AL = new conf_ProdClass("FORTUNE 1000 w/executive contact names",1799);
this.G500 = new conf_ProdClass("GLOBAL 500",599);
this.G5AL = new conf_ProdClass("GLOBAL 500 w/executive contact names",499);
this.FBCS  = new conf_ProdClass("2008 Fortune 100 Best Companies to Work For", 250); 
this.FGCS  = new conf_ProdClass("Fastest-Growing Companies w/executive contact" + "names ", 499); 
this.F1G5 = new conf_ProdClass("FORTUNE 1000 + Global 500",1498);
this.F1GA  = new conf_ProdClass("FORTUNE 1000 + Global 500 w/executive contact names ",2644);
this.F5G5 = new conf_ProdClass("FORTUNE 500 + Global 500 ",1098);
this.AF5G  = new conf_ProdClass("FORTUNE 500 + Global 500 w/executive contact names ",1844);
this.DTSC = new conf_ProdClass("The DATASTORE Collection",2897);

}


function conf_ProdClass(myName, myCost)
{
	this.NAME = myName;
	this.COST = myCost;
	
}



// ============================================================================
// Event handler for clicking on a Product Radio
//
function conf_chooseProduct(obj_selectedItem)
{
	// all browsers but not netscape 6
	if (!((BRWR_NAME.indexOf("Netscape") != -1) && (BRWR_VERS >= 5))) {
	//	alert(' In Netscape5');
		conf_chooseProductCore(obj_selectedItem)
	}
}
function conf_chooseProduct_N6(obj_selectedItem)
{
	// only for Netscape Navigator 6 because it doesn't handle the onClick
	// correctly.  Instead it requires the onChange event.
	// only netscape 6 and above (keep an eye on this to see if netscape
	// changes how they do this down the line.
	if ((BRWR_NAME.indexOf("Netscape") != -1) && (BRWR_VERS >= 5)) {
         //      alert('IN config in N6');
		conf_chooseProductCore(obj_selectedItem);
	}
}
// core functionality so it doesn't have to be in here twice.
function conf_chooseProductCore(obj_selectedItem)
{
	// get the name of the current product selected
	gCurrProduct = obj_selectedItem.value;
       // alert('DEBUG: In chooseProduct' + gCurrProduct + 'done');
	// set this product as the last product for next time an option is clicked
	gLastProduct = gCurrProduct;
	// recalculate the prices
	conf_calcPrices();
	setBold();
}

function setBold() {
	//document.getElementById(gCurrProduct).style.textDecoration = "bold";
	for (i=0; i < gForm.PROD.length; i++) {
		if (gForm.PROD[i].checked == true) {
			whichID = gForm.PROD[i].value;
			document.getElementById(whichID).style.fontWeight = "bold";
			whichID_price = whichID + "_price";
			document.getElementById(whichID_price).style.visibility = "visible";
			gForm.final_total.value = gForm.elements[whichID_price].value;
		}
		else {
			whichID = gForm.PROD[i].value;
			document.getElementById(whichID).style.fontWeight = "normal";
			whichID_price = whichID + "_price";
			document.getElementById(whichID_price).style.visibility = "hidden";
		}
	}
}


// ============================================================================
// Return the index number for the form element with passed value
//
function conf_getIndex(str_value)
{
	var int_rtrn = 0;
	for (i=0; i < gForm.elements.length; i++) {
		if (gForm.elements[i].value == str_value) {
			int_rtrn = i;
			break;
		}
	}
	return int_rtrn;
}

// ============================================================================
// Calculate the Cost and set the Product Values
//
function conf_calcPrices()
{
	var _opts = 0;
	var _optstring = "";
	var _summary = "";
	var _value1 = "";
	var _value2 = "";
	gValue3 = "";	
	var _value_list = "";
	
	
	if (gCurrProduct == "DTSC") {
		//alert(' In Prodprice');
		gCost = PRODPRICES["DTSC"].COST;
	
		//gSave = PRODPRICES["DTSC"].SAVE;
		_summary += PRODPRICES["F100"].NAME + "$" + DTSC_F100 + ".00\n";
	_summary += PRODPRICES["G500"].NAME + " $" + DTSC_G500 + ".00\n";
		_summary += PRODPRICES["FBCS"].NAME + " $" + DTSC_FGC + ".00";
	_value1 = "F1000FTCH"; _value2 = "G500_F10FTCH"; gValue3 = "FBCS";
	_value_list = "F1000FTCH,G500_F10FTCH,FBCS_COLL";
	}
	else if (gCurrProduct == "") {
		if (gForm.PROD.value = "FGCS") {
		//if (gForm.elements["FGCS"].checked) {
			gCost = PRODPRICES["FGCS"].COST;
			_summary += PRODPRICES["FGCS"].NAME + " $" + PRODPRICES["FGCS"].COST + ".00";
			_value1 = "FGCS"; _value2 = ""; gValue3 = "";
			_value_list = "FGCS";
		}
	}
	else if (gCurrProduct == "F1G5") {
		_summary += conf_getCosts("F100");
		_value1 = "F1000";
		_summary += conf_getCosts("G500");
		_value2 = "G500_F10";
		_summary += conf_getSummary();
		// subtract additional savings for ordering a bundle
		//gSave += PRODPRICES[gCurrProduct].SAVE;
		_value_list = _value1 +","+ _value2;
		if (gValue3 != "")
			 _value_list += "," + gValue3;
	}
        else if (gCurrProduct == "F1GA") {
              _summary += conf_getCosts("F1AL");
              _value1 = "F1AL";
              _summary += conf_getCosts("G5AL");
              _value2 = "G5AL";
	      _summary += conf_getSummary();
              _value_list = _value1 +","+ _value2;
              if (gValue3 != "")
	      _value_list += "," + gValue3;
	        }
           else if (gCurrProduct == "AF5G") {
	          var _valueOpts;
		 _summary += conf_getCosts("F5AL",0);
	          _value1 = "F5AL" ;
                  _summary += conf_getCosts("G5AL",100);
		  _value2 = "G5AL";
	         _summary += conf_getSummary();
	         // subtract additional savings for ordering a bundle
	        //gSave += PRODPRICES[gCurrProduct].SAVE;
	        _value_list = _value1 +","+ _value2;
	         if (gValue3 != "")
	         _value_list += "," + gValue3;
	       }

	else if (gCurrProduct == "F5G5") {
		var _valueOpts;
		_summary += conf_getCosts("F500",0);
		_value1 = "F500" ;
		_summary += conf_getCosts("G500",100);
		_value2 = "G500_F5";
		_summary += conf_getSummary();
		// subtract additional savings for ordering a bundle
		//gSave += PRODPRICES[gCurrProduct].SAVE;	
		_value_list = _value1 +","+ _value2;
		if (gValue3 != "")
			 _value_list += "," + gValue3;
	}
	else {
		_prodname = gCurrProduct;
		// correct the product name for F1000
		if (_prodname == "F100") _prodname = "F1000";
		_value1 = _prodname ;
		_summary += conf_getCosts(gCurrProduct);
		_summary += conf_getSummary();
		_value_list = _value1;
		if (gValue3 != "") {
			_value2 = gValue3;	// bump it down one
			gValue3 = "";		// erase value
			_value_list += "," + _value2;
		}
	}
	// print the cost
	/*
	if (gCost == 0) {
		gForm.elements["TOTAL_a"].value = " -";
		gForm.elements["TOTAL_b"].value = " -";
	}
	else {
		gForm.elements["TOTAL_a"].value = "$" + gCost + ".00";
		gForm.elements["TOTAL_b"].value = "$" + gCost + ".00";
	}
	*/
	// print the savings
	//if (gSave == 0) {	
	//	gForm.elements["USAVE_a"].value = " -";
	//	gForm.elements["USAVE_b"].value = " -";	
	//}
	//else {
	//	gForm.elements["USAVE_a"].value = "$" + gSave + ".00";
	//	gForm.elements["USAVE_b"].value = "$" + gSave + ".00";
	//}
	// display the order summary
//	gForm.elements["OrderSummary"].value = _summary;
	// clear the values
	gCost = 0;
	gSave = 0;
	
	// save the cookie
  	conf_setCookie();
	
	// set the hidden fields with the new values
	gForm.product1.value = _value1;
	gForm.product2.value = _value2;
	gForm.product3.value = gValue3;
	gForm.product_list.value = _value_list;
	gForm.refer.value =	refer_cookie;
	// gForm.refer.value = the_REFERER;
	
/*	
	var msg = "";
	msg += "gForm.product1.value = " + gForm.product1.value + "\n";
	msg += "gForm.product2.value = " + gForm.product2.value + "\n";
	msg += "gForm.product3.value = " + gForm.product3.value + "\n";
	msg += "gForm.product_list.value = " + gForm.product_list.value + "\n";
	alert(msg);
*/
}

function conf_getCosts(myProduct)
{
	var _cost = 0;
	var _opts = 0;
	var _optstring = " ";
	var _optionCounter = 0;
	
	// get the base price
	_cost += PRODPRICES[myProduct].COST;	
			
	// build summary
	return PRODPRICES[myProduct].NAME +  "$" + _cost + ".00\n";
}

function conf_getSummary(myVersion)
{
	var _discount = 0;
	var _collection = "";
	
	// check for the implicit DTSC bundle
	if (gCost == DTSC_F100 + DTSC_G500) {
		_discount = 50;
		_collection = "_COLL";
	}
	else if ((gCurrProduct != "") && (gCurrProduct != "DTSC")) {
		_collection = "_DSCNT";
	}

	// check for fastest growing companies
/*
	if (gForm.elements["FGCS"].checked) {
		gCost += PRODPRICES["FGCS"].DISCOUNT - _discount;
	//	gSave += PRODPRICES["FGCS"].SAVE + _discount;
		gValue3 = "FGCS" + _collection;
		return PRODPRICES["FGCS"].NAME + " $" ;			
	}
	else {
		return "";
	}
	*/
}

// ============================================================================
// Read In Cookies

function conf_readCookie()
{
	var _start = 0;
	var _end = 0;
	var _items;
	gCurrProduct = "";
	
	// check to see if there is a cookie to read
	if (data_cookie != "") 
	{
		_start = data_cookie.indexOf("=")+1;
		_end = data_cookie.indexOf("=")+5;
		// walk through the cookie string biting off pieces
		// first check the products
		//for ( _items=0; _items<ary_PRODUCTS.length; _items++ )
		//{
			// take a bite
			label = data_cookie.substr(_start,4);
			value = data_cookie.substr(_end,1);
			indexNum = conf_getIndex(label);
			//alert("label:"+label);
			//alert("value:"+value);
			//alert("indexNum"+indexNum);
			
			// Load the values into the form
			if (value == 1) {
				gForm.elements[indexNum].checked = true;
				gCurrProduct = label;
			}
			// mark next piece
			_start += 5;
			_end += 5;
		//} // End of For Loop
		// next check the options so long as a product was selected
		/*
		for ( _items=0; _items<ary_OPTIONS.length; _items++ )
		{
			// take a bite
			label = data_cookie.substr(_start,4);
			value = data_cookie.substr(_end,1);
			// Load the values into the form
			if ((gCurrProduct != "") && (value == 1)) {
				gForm.elements[gCurrProduct+"_"+label].checked = true;
			}
			// mark next piece
			_start += 5;
			_end += 5;
		} // End of For Loop
		*/
		// check the datastore collection
		label = data_cookie.substr(_start,4);
		value = data_cookie.substr(_end,1);
		// Load the values into the form
		

	if ((label == "DTSC") && (value == 1)) {
		gForm.elements[conf_getIndex("DTSC")].checked = true;
		gCurrProduct = "DTSC";
		}
		// mark next piece
		_start += 5;
		_end += 5;
		// check the FGC
	label = data_cookie.substr(_start,4);
	value = data_cookie.substr(_end,1);
		// Load the values into the form
	if ((label == "FGCS") && (value == 1)) {
		gForm.elements[conf_getIndex("FGC")].checked = true;
	}
		// change the total text box to reflect current prices
	//	conf_createOptsString();
		conf_calcPrices();
		gLastProduct = gCurrProduct;
	} // End of Reading the cookie
	// store the REFER in a hidden field
	gForm.refer.value = refer_cookie;


}

// ============================================================================
// Save Data to Cookies
//
function conf_setCookie()
{
	var _counter;
	var _cookie = "DATA=";
	// Make a long string of the elements for the cookie
	// make a consistent code, e.g., 4-digit code
	for (_counter=0; _counter<ary_PRODUCTS.length; _counter++) 
	{
		_cookie += ary_PRODUCTS[_counter];
		_cookie += (gForm.elements[conf_getIndex(ary_PRODUCTS[_counter])].checked) ? "1" : "0";
	}
	write_Cookie(_cookie);
}

// ============================================================================
// Reset the form
//
function conf_resetForm()
{
	gCurrProduct = "";
	gForm.reset();
	gForm.refer.value = refer_cookie;
}

// ============================================================================
// Validate the form
//
function conf_validateBeforeFormSubmit()
{
	for (i=0; i < gForm.PROD.length; i++) 
	{
		if (gForm.PROD[i].checked == true) 
		{
			return true;
		}
	}
	document.getElementById('errorMsg').style.display='block';
	return false;
}



