﻿/*
The SoftAd Group
Copyright (c) 2000-2004 The SoftAd Group, Inc.  All rights reserved
*/
// Generic Javascript Library for ChannelNet processing
// Copyright © 1999-2000 SoftAd Group, Inc.

//for variables used in place of text alerts (i.e. CNVALIDERROR2, see en-us.js, de-de.js, etc.

function toChannelNetSKU (f){

	var attCount = 0;
	var attString = '';
	
	// more than 1 attribute
	if(f._Attribute && f._Attribute[0] && f._Attribute[0].options) {
		var attLength = f._Attribute.length;
		for(i=0; i<attLength; i++) {
			var currentAtt = f._Attribute[i];
			
			if(currentAtt.options[currentAtt.selectedIndex].value && currentAtt.options[currentAtt.selectedIndex].value!='') {
				attCount++;
				if(attCount > 1) {
					attString += "&Attribute" + attCount + "=";
				}
				attString += currentAtt.options[currentAtt.selectedIndex].value;
			}
			
		}
	
	// only one attribute
	} else if(f._Attribute) {
		var currentAtt = f._Attribute;
		
		if(currentAtt.options[currentAtt.selectedIndex].value && currentAtt.options[currentAtt.selectedIndex].value!='') {
			attString +=  currentAtt.options[currentAtt.selectedIndex].value;
		}
	}
	
	if(f.Attribute1) {
	
		f.Attribute1.value = attString;
		
	}
	
} 

function updateOpener(style){
  var force = true;
  var oWin = window.opener;
  
  if(!style) {
	style = '';
  }
  
if (!oWin.membershome)
  oWin.location.href="channelnet.aspx?cn=Cart&act=getStoredCart&crt=&Style=" + style + "&debug=0";
  
  document.CN_returnValue = true;
}

// -------------------------------------------------------------------------------
// Function         : validateNumeric(formElement, lowerBound, upperBound)
// -------------------------------------------------------------------------------
// Author           : Allan Charlton
// Created on       : Wednesday, October 4, 2000
// 
// This function validates numeric data entered into a form element and returns 
// an error if the value of the element is not in the correct range
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME			TYPE        DESCRIPTION
// formElement	element		The form element which will have its value validated.
// lowerBound	number		The lower bound for the numeric range
// upperBound	number		The upper bound for the numeric range
// -------------------------------------------------------------------------------
// Last Updated     : Wednesday, November 22, 2000
// Updated by       : George Pollard
// -------------------------------------------------------------------------------

function validateNumeric(formElement, lowerBound, lowerInclusive, upperBound, upperInclusive) {
	var regExpObj = new RegExp('^\\-?\\d*\\.?\\d*$','');
	var value = formElement.value;
	
	if (formElement.value == '') {
		return true;
	}
	if (regExpObj.test(value)) {
		var aboveLowerBound = (lowerInclusive ? value >= lowerBound : value > lowerBound); 
		var belowUpperBound = (upperInclusive ? value <= upperBound : value < upperBound); 

		if (upperBound >= lowerBound) {
			if (aboveLowerBound && belowUpperBound) {
				return true;
			}
			else {
				var sLower = (lowerInclusive ? CNINCLUSIVE :  CNEXCLUSIVE);
				var sUpper = (upperInclusive ?  CNINCLUSIVE :  CNEXCLUSIVE);
				alert(CNVALIDERROR2 + '\n' + CNVALIDERROR3 + '\n' + CNVALIDERROR4  + lowerBound + sLower +  CNVALIDERROR6  + upperBound + sUpper);
			}
		} else {
			if (aboveLowerBound || belowUpperBound) {
				return true;
			}
			else {
				var sLower = (lowerInclusive ?  CNEXCLUSIVE :  CNINCLUSIVE);
				var sUpper = (upperInclusive ?  CNEXCLUSIVE :  CNINCLUSIVE);
				alert(CNVALIDERROR2 + '\n' + CNVALIDERROR3 + '\n' + CNVALIDERROR5  + upperBound + sUpper +  CNVALIDERROR6  + lowerBound + sLower);
			}
		}
	} else {
		alert(CNVALIDERROR2 + '\n' + CNVALIDERROR7);
	}

	// selects the field value
	formElement.focus();
	formElement.select();
	return false;
}

// -------------------------------------------------------------------------------
// Function         : validateDateFormat(ctrl)
// -------------------------------------------------------------------------------
// Author           : George Pollard
// Created on       : Wednesday, October 25, 2000
// 
// Returns true if the string in the specified control is a valid date and displays 
// appropriate error messages as a side effect.
// Todo: add support for non-US date formats.
// Cloned from channelnet_cmc.js/validateDate().
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME			TYPE        DESCRIPTION
// ctrl		element		The form element which will have its value validated.
// quiet		boolean		Indicates whether alerts are to be suppressed or not.
// -------------------------------------------------------------------------------
// Last Updated     : Tuesday, October 31, 2000
// Updated by       : George Pollard
// -------------------------------------------------------------------------------

function validateDateFormat(ctrl, quiet) {
	var regExpObj = new RegExp("^(\\d{1,2})/(\\d{1,2})/((\\d{2})|(\\d{4}))$")
	if (!regExpObj.test(ctrl.value)) {
		if (!quiet)
			alert("'" + ctrl.value + CNDATEERROR1 + '\n' + CNDATEERROR2);
		return false;
	}
						
	regExpObj.exec(ctrl.value);
	var month = RegExp.$1;
	var day = RegExp.$2;
	var year = RegExp.$3;
	var err = isValidDate(day,month,year);
	if (err!=null) {
		if (!quiet)
			alert(month + "/" + day + "/" + year + CNDATEERROR1 + '\n'+err);
		return false;
	}
	return true;
}

// -------------------------------------------------------------------------------
// Function         : minutesToDate(minutes)
// -------------------------------------------------------------------------------
// Author           : George Pollard
// Created on       : Wednesday, October 25, 2000
// 
// Returns the equivalent date string .
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME			TYPE        DESCRIPTION
// minutes		integer		The number of minutes elapesed since midnight 01/01/1970.
// -------------------------------------------------------------------------------
// Last Updated     : Wednesday, October 25, 2000
// Updated by       : George Pollard
// -------------------------------------------------------------------------------

function minutesToDate(minutes) {
	if (minutes == "")
		return "";
	else {
		var date = new Date(minutes * 60000);
		return '' + (date.getUTCMonth()+1) + '/' + date.getUTCDate() + '/' + date.getUTCFullYear();
	}
}

// -------------------------------------------------------------------------------
// Function         : setResetValue(sReset)
// -------------------------------------------------------------------------------
// This function sets the value of the Reset element on a form and submits the form 
// -------------------------------------------------------------------------------
// Returns true.
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// formObj          formObj     The form to validate and submit
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME        DESCRIPTION
// 02/06/2001   Chad Peck   Created

function setResetValue(formObj) {
	
	if(formObj && formObj.Reset && formObj._Reset) {
		if(formObj._Reset.checked) {
			formObj.Reset.value = 'F';
		} else {
			formObj.Reset.value = 'T';
		}
	}
	
	return true;
	
}

// -------------------------------------------------------------------------------
// Function         : removeButtonPressed(bo, style)
// -------------------------------------------------------------------------------
// This function is used by Build and Assess to Remove a Session from the 
// list of available sessions for a user. 
// -------------------------------------------------------------------------------
// Returns true if the item can be deleted and false otherwise.
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// bo               string      The name of the bo (Assess or Build)
// style            string      The name of the stylesheet
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME        DESCRIPTION
// 02/06/2001   Chad Peck   Created

function removeButtonPressed(bo, style)
{
	var list = document.forms[0].sessionid;
	var returnValue = false;
	
	if(!style) {
		style = '';
	}
	
	if (list.onchange && list.onchange()) {
		if(canDelete()) {
			var option = list.options[list.selectedIndex];
			var sessionid = option.value;
			document.location.replace("channelnet.aspx?cn=" + bo + "&act=delete&crt=sessionid="+sessionid+"&Style=" + style);
			//alert(sessionid);
			returnValue = true;
		}
	}
					
	return returnValue;
}

// -------------------------------------------------------------------------------
// Function         : setSelection(row)
// -------------------------------------------------------------------------------
// This function is used by Build and Assess to set which row is selected when
// the user clicks on it.  It changes the background color of the selected row.
// -------------------------------------------------------------------------------
// Returns null
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// row              table row   The table row object
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME        DESCRIPTION
// 02/06/2001   Chad Peck   Created

function setSelection(row)
{
	var table = row.parentElement;
	var rowLength = table.children.length;
	
	// loops through all of the rows
	for(i=1; i!=rowLength; i++)
	{
		var currentChild = table.children[i];
			
		// sets the color of the selected row
//		if (currentChild==row)
//			currentChild.bgColor = '#EEEEFF';
//		else
//			currentChild.bgColor = '#FFFFFF';
	}
}
			
// -------------------------------------------------------------------------------
// Function         : setProductSelection(row, bo)
// -------------------------------------------------------------------------------
// This function is used by Build and Assess to set which row is selected when
// the user clicks on it
// -------------------------------------------------------------------------------
// Returns null
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// row              table row   The table row object
// bo               string      The name of the bo (Assess or Build)
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME        DESCRIPTION
// 02/06/2001   Chad Peck   Created

function setProductSelection(row, bo)
{
	// sets the color of the selected row
	setSelection(row);
	
	document.selectedRow = row;
	if(bo && bo.toUpperCase() == 'BUILD') {
		selectedProductId = Trim(document.selectedRow.id);
		updatePictures(selectedProductId);
	}
}
			
// -------------------------------------------------------------------------------
// Function         : setQuestionSelection(row)
// -------------------------------------------------------------------------------
// This function is used by Build and Assess to set which row is selected when
// the user clicks on it
// -------------------------------------------------------------------------------
// Returns null
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// row              table row   The table row object
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME        DESCRIPTION
// 02/06/2001   Chad Peck   Created

function setQuestionSelection(row)
{
	//setSelection(row);
	document.selectedQuestionRow = row;
}

// -------------------------------------------------------------------------------
// Function         : addToCartButtonPressed(bo, style)
// -------------------------------------------------------------------------------
// This function is used by Build and Assess to add a configured product to the cart
// -------------------------------------------------------------------------------
// Returns null
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// bo               string      The name of the bo (Assess or Build)
// style            string      The name of the stylesheet
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME        DESCRIPTION
// 02/06/2001   Chad Peck   Created

function addToCartButtonPressed(bo, style)
{
	if(!style) {
		style = '';
	}
	
	// adds 
	if(bo && bo.toUpperCase() == 'BUILD') {
		cartInfo += getCartStateString();
	}
	
	if (cartInfo == "") {
		handleCNError(CNADDTOCARTERROR);
		return false;
	} else {		
		var productLabel = window.prompt(CNCUSTOMENAMEPROMPT, "");
		if (productLabel) {	
			document.location.href = "/cart/channelnet.aspx?cn=Cart&act=addCustomProduct&crt=Price=0%26Qty=1%26ProductLabel=" + urlencode_field(productLabel) + "%26ATTRIBUTES=" + urlencode_field(cartInfo) + "&Style=" + style;
		}
	}
}
	
// -------------------------------------------------------------------------------
// Function         : saveButtonPressed(bo, style)
// -------------------------------------------------------------------------------
// This function is used by Build and Assess to save a users configuation
// -------------------------------------------------------------------------------
// Returns null
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// bo               string      The name of the bo (Assess or Build)
// style            string      The name of the stylesheet
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME        DESCRIPTION
// 02/06/2001   Chad Peck   Created

function saveButtonPressed(bo, style, language)
{
	var sessionname = window.prompt(CNSAVEBUTTONPROMPT, "");
	if (sessionname)
	{
		var sessionname = urlencode_field(sessionname);
		if(!style) {
			style = '';
		}
	
		if(!language) {
			language = '';
		}
	
			
		document.location.href = "/" + bo + "/channelnet.aspx?cn=" + bo + "&act=save&crt=sessionname=" + sessionname + "%26pagesize=5%26" + getStateString() + "&Style=" + style + "&Lang=" + language;
	} 
}

// -------------------------------------------------------------------------------
// Function         : appendStateString(formObj)
// -------------------------------------------------------------------------------
// This function is used by Build and Assess to append the state string to the 
// form element containing the category id.
// -------------------------------------------------------------------------------
// Returns null
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// formObj          form        
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME        DESCRIPTION
// 02/06/2001   Chad Peck   Created

function appendStateString(formObj) {
			
	if (formObj && formObj.categoryid) {
		formObj.categoryid.value += '&' + unescape(getStateString());
	}
			
}
			
// -------------------------------------------------------------------------------
// Function         : printButtonPressed(style)
// -------------------------------------------------------------------------------
// This function is used by Build and Assess to display a page formatted for
// printing.
// -------------------------------------------------------------------------------
// Returns null
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// style            string      The name of the stylesheet
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME        DESCRIPTION
// 02/06/2001   Chad Peck   Created

function printButtonPressed(style)
{
	if(!style) {
		style = '';
	}

	document.location.href = "channelnet.aspx?cn=Build&act=print&crt=pagesize=5%26" + getStateString() + "&Style=" + style;
}


// -------------------------------------------------------------------------------
// Function         : checkViewMode()
// -------------------------------------------------------------------------------
// This function is used by Build and Assess to display a page formatted for
// printing.
// -------------------------------------------------------------------------------
// Returns null
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// style            string      The name of the stylesheet
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME        DESCRIPTION
// 02/27/2001   Chad Peck   Moved from sitebuilderpublic.js

function checkViewMode() {

	var returnValue = true;
	
	if (parent && parent.leftnav && parent.leftnav.document && parent.leftnav.document.formViewMode && parent.leftnav.document.formViewMode.ViewMode && parent.leftnav.document.formViewMode.ViewMode.value == 'Edit') {
		returnValue =  false;
	}

	return returnValue;
}

// -------------------------------------------------------------------------------
// Function         : getProductsForCompare()
// -------------------------------------------------------------------------------
// This function is used create a list of product IDs to be compared.
// -------------------------------------------------------------------------------
// Returns a comma-delimited list of product IDs
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME           DESCRIPTION
// 06/26/2001  George Pollard   Created

function getProductsForCompare() {
	var products = "";
	for (var ix = 0; ix < document.forms.length; ix++) {
		var form = document.forms[ix];
		var currentElement;
		var formLength = form.elements.length;
		for(var elementIndex=0; elementIndex < formLength; elementIndex++) {
			currentElement = form.elements[elementIndex];
			if (currentElement.name=="_comparebox" && currentElement.checked==true) {
				products+=","+currentElement.value;
			}
		}
	}
	if (products!="")
		products=products.substring(1);
	return products;
}

// -------------------------------------------------------------------------------
// Function         : compareButtonPressed(action, style, language)
// -------------------------------------------------------------------------------
// This function is used request a Compare BO action
// -------------------------------------------------------------------------------
// Returns null
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// action           string      The name of the compare action to perform
// style            string      The name of the stylesheet
// language         string      The id of the language
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME           DESCRIPTION
// 06/26/2001  George Pollard   Created

function compareButtonPressed(action, style, language) {
	compareButtonPressedCommon(action, getProductsForCompare(), style, language);
}


// -------------------------------------------------------------------------------
// Function         : addToCompareSetSingle(id, style, language)
// -------------------------------------------------------------------------------
// This function is used add a single product to the compare set
// -------------------------------------------------------------------------------
// Returns null
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// id		           int			  The ID of the product to be added
// style            string      The name of the stylesheet
// language         string      The id of the language
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME           DESCRIPTION
// 06/26/2001  George Pollard   Created

function addToCompareSetSingle(id, style, language) {
	compareButtonPressedCommon("addProductsToCompareSet", id, style, language);
}

// -------------------------------------------------------------------------------
// Function         : compareButtonPressedCommon(action, productlist, style, language)
// -------------------------------------------------------------------------------
// This function is used request a Compare BO action
// -------------------------------------------------------------------------------
// Returns null
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// action           string      The name of the compare action to perform
// productlist      string      The comma-delimimited list of prodic IDs
// style            string      The name of the stylesheet
// language         string      The id of the language
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME           DESCRIPTION
// 06/26/2001  George Pollard   Created

function compareButtonPressedCommon(action, productlist, style, language) {
	if(!language) 
		language = '';

	var form = document.forms[0];
	var criteria =  "productlist%3D" + productlist;
	if (form.dealerid)
		criteria += "%26dealerid%3D" + form.dealerid.value;
		
	document.location.href = "/Compare/channelnet.aspx?cn=Compare&act=" + action + "&crt=" + criteria + "&Style=" + style + "&Lang=" + language;
}

// -------------------------------------------------------------------------------
// Function         : addCustomProductToCompareSet(bo, style)
// -------------------------------------------------------------------------------
// This function is used by Build and Assess to add a configured product to the compare set
// -------------------------------------------------------------------------------
// Returns null
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// bo               string      The name of the bo (Assess or Build)
// style            string      The name of the stylesheet
// -------------------------------------------------------------------------------
// History
// -------------------------------------------------------------------------------
// DATE			NAME        DESCRIPTION
// 07/02/2001  George Pollard   Created

function addCustomProductToCompareSet(bo, style)
{
	if(!style) {
		style = '';
	}
	
	document.location.href = "/Compare/channelnet.aspx?cn=Compare&act=addCustomProductToCompareSet&crt=sessiontype=" + bo + "&Style=" + style;
}
	

