// JavaScript Document

/***********************************
  PRICING FUNCTIONS
***********************************/

function updatePrice() {


	var size 		= document.getElementById('sizeChoice');
	var finish 		= document.getElementById('finishChoice');
	var display		= document.getElementById('displayChoice');
	var priceDiv	= document.getElementById('priceDivID'); 
	var total		= -1;

	total = getSizePrice(size.value);

	// exclude bookmarks
	if(size.value != "2x8" && size.value != "2.5x8") {
		total = total * getFinishAdjustment(finish.value);
	}
	
	//display addition
	total = total + getDisplayPrice(size.value, display.value);

	// specific pricing for large patina pieces
	if(size.value == "36x48" && finish.value == "Patina" && display.value == "Floating") {
		total = 850;
	}
	
	priceDiv.innerHTML = '<span>Price: $' + Math.round(total) + '</span>';
	
	return Math.round(total);
	
}

function getFinishAdjustment(finish) {
	var adjustment = -1;
	
	switch(finish) {
	case "Natural":
		adjustment = 1.25;
		break;
	case "Driftwood":
	case "OceanMist":
	case "Forest":
	case "Clover":
		adjustment = 1.2;
		break;
	case "Heirloom":
	case "Patina":
	case "Azul":
	case "Verde":
		adjustment = 1;
		break;
	}
	
	return adjustment;
}

function getSizePrice(size) {
	var sizePrices 		= new Array();
	sizePrices["2x2"]	= 5;
	sizePrices["Bookmark"]		= 10;
	sizePrices["Bookmark Wide"]	= 10;
	sizePrices["4x4"]	= 10;
	sizePrices["4x6"]	= 25;
	sizePrices["5x7"]	= 35;
	sizePrices["6x12"]	= 75;
	sizePrices["8x8"]	= 50;
	sizePrices["8x10"]	= 85;
	sizePrices["9x12"]	= 100;
	sizePrices["10x10"]	= 100;
	sizePrices["11x14"]	= 150;
	sizePrices["12x12"]	= 140;
	sizePrices["12x24"]	= 195;
	sizePrices["16x20"]	= 250;
	sizePrices["18x24"]	= 325;
	sizePrices["24x24"]	= 400;
	sizePrices["24x36"]	= 500;
	sizePrices["36x48"]	= 1250;
	
	sizePrices["Bookend"]		= 100;
	sizePrices["Bookend Wide"]	= 125;
	
	return sizePrices[size];
}

function getDisplayPrice(size, display) {
	var displayPrices 		= new Array();
	
	displayPrices['4x4_Leather']	= 2;
	displayPrices['4x6_Leather']	= 2;
	displayPrices['5x7_Leather']	= 2;
	displayPrices['6x12_Leather']	= 5;
	displayPrices['8x8_Leather']	= 5;
	displayPrices['8x10_Leather']	= 5;
	displayPrices['9x12_Leather']	= 5;
	displayPrices['10x10_Leather']	= 5;
	displayPrices['11x14_Leather']	= 7;
	displayPrices['12x12_Leather']	= 7;
			
	displayPrices['4x4_Easel']	= 10;
	displayPrices['4x6_Easel']	= 10;
	displayPrices['5x7_Easel']	= 10;
	displayPrices['6x12_Easel']	= 10;
	displayPrices['8x8_Easel']	= 10;
	displayPrices['8x10_Easel']	= 10;
	displayPrices['9x12_Easel']	= 10;
	displayPrices['10x10_Easel']	= 10;
	displayPrices['11x14_Easel']	= 10;
	displayPrices['12x12_Easel']	= 10;
	
	displayPrices['4x4_Floating']	= 10;
	displayPrices['4x6_Floating']	= 10;
	displayPrices['5x7_Floating']	= 10;
	displayPrices['6x12_Floating']	= 20;
	displayPrices['8x8_Floating']	= 20;
	displayPrices['8x10_Floating']	= 20;
	displayPrices['9x12_Floating']	= 20;
	displayPrices['10x10_Floating']	= 20;
	displayPrices['11x14_Floating']	= 35;
	displayPrices['12x12_Floating']	= 35;
	displayPrices['12x24_Floating']	= 35;
	displayPrices['16x20_Floating']	= 35;
	displayPrices['18x24_Floating']	= 55;
	displayPrices['24x24_Floating']	= 55;
	displayPrices['24x36_Floating']	= 55;
	displayPrices['36x48_Floating']	= 55;
		
	var returnPrice = 0;
	
	switch(display) {
		case 'Leather':
		case 'Hemp':
			returnPrice = displayPrices[size + '_' + 'Leather'];
			break;
		case 'Easel':
		case 'Floating':
			returnPrice = displayPrices[size + '_' + display];
			break;
		case 'Standard':
		default:
			returnPrice = 0;
	}
	
	if(returnPrice == null) {
		returnPrice = 0;	
	}
	
	return returnPrice;
}

function updateLettersAndPrice() {
	var size 		= document.getElementById('sizeChoice');
	var finish 		= document.getElementById('finishChoice');
	var display		= document.getElementById('displayChoice');
	var priceDiv	= document.getElementById('priceDivID'); 
	var total		= -1;
	var feedback 	= document.getElementById("LettersFeedback");
	var letters 	= document.getElementById('lettersID');

	cleanLetters = letters.value.match(/[a-zA-Z ]/g);
	
	if(cleanLetters == null) {		
		feedback.innerHTML = "0 letters chosen";
		letters.value = "";
		
		total = 0;
		priceDiv.innerHTML = '<span>Price: $' + Math.round(total) + '</span>';
		
	} else {
		var counter = 0;
		// count white space
		for (var x = 0; x < cleanLetters.length; ++x) {
			if(cleanLetters[x] == " ") {
				++counter;
		 	}
		}
		finalLength = cleanLetters.length - counter;
		feedback.innerHTML = finalLength + " letters chosen";
		letters.value = cleanLetters.join("");
		
		// size price
		total = getSizePrice(size.value);
		// finish adjustment
		total = total * getFinishAdjustment(finish.value);
		//display addition
		total = total + getDisplayPrice(size.value, display.value);
		
		total = total * finalLength;
		priceDiv.innerHTML = '<span>Price: $' + Math.round(total) + '</span>';
	}
}

/***********************************
  VALIDATION FUNCTIONS
***********************************/

/* Validate the provided field and display provided message if invalid 
 * 	@param 	field		Field to be checked
 *	@param	alerttxt	Message to display if field is invalid
 *	@return	retVal		false if invalid; true if valid
 */
function validate_required(field,alerttxt) {
	var retVal = false;
	with (field) {
	  if (value==null || 
	      value==""   ||
		  value=="--") {	  	
	  	style.border = '2px dashed #00CCFF';
	  	retVal = false;
	  } else {
	  	style.border = '1px solid #00CCFF';
	  	retVal = true;
	  }
	}
	return retVal;
}

/* Validate the size of provided field and display provided message if invalid 
 * 	@param 	field		Field to be checked
 *	@param	fieldSize	Size to be checked against
 *	@param	operator	How to be tested. "equal", "less", etc
 *	@return	retVal		false if invalid; true if valid
 */
function validate_size(field, fieldSize, operator) {
	var retVal = false;
	with (field) {
		switch(operator) {
			case "less":
				if (value.length > fieldSize) {	  	
					style.border = '2px dashed #00CCFF';
					retVal = false;
				} else {
					style.border = '1px solid #ccc';
					retVal = true;
				}
				break;
			case "less nonempty":
				if (value.length > fieldSize || value.length == 0) {	  	
					style.border = '2px dashed #00CCFF';
					retVal = false;
				} else {
					style.border = '1px solid #ccc';
					retVal = true;
				}
				break;
			case "equal":
				if (value.length != fieldSize || value.length == 0) {	  	
					style.border = '2px dashed #00CCFF';
					retVal = false;
				} else {
					style.border = '1px solid #ccc';
					retVal = true;
				}
				break;
		}
	}
	return retVal;
}

/***********************************
  FORM FUNCTIONS
***********************************/

/* Assigns the new value to the input variable provided
 * 	@param 	varIDString		Input variable's ID
 *	@param	newValue		Input variable's new value
 *	@return	none
 */
function setVariableValue(varIDString) {
	var varToSet = document.getElementById(varIDString);

	if( varToSet.value == "true" ) {
		varToSet.value = "false";
	} else if( varToSet.value == "false" ) {
		varToSet.value = "true";
	}
}

/* Set hidden option values 
 * 	@param 	sizeValue		As shown value of size option
 * 	@param 	finishValue		As shown value of finish option
 * 	@param 	displayValue	As shown value of display option
 *	@return	none
 */
function setHiddenVars(sizeValue, finishValue, displayValue) {
	var size 	= document.getElementById('sizeChoice');
	var finish 	= document.getElementById('finishChoice');
	var display	= document.getElementById('displayChoice');
	
	size.value 		= sizeValue;
	finish.value 	= finishValue;
	display.value 	= displayValue;
	
}


/***********************************
  STYLING FUNCTIONS
***********************************/

/* 
collapseExpand - show/hides the items on the left-hand navigation
*/
function collapseExpand(category) {
	var obj = document.getElementById(category);
	
	if(obj.style.display =='block') {
		obj.style.display='none';
	} else {
		obj.style.display='block';
	}
}

/* 
collapse - hide the item
*/
function collapse(itemID) {
	var obj = document.getElementById(itemID);
	
	obj.style.display='none';
}

/* 
expand - show the item
*/
function expand(itemID) {
	var obj = document.getElementById(itemID);
	
	obj.style.display='block';
}

// 
// Dreamweaver image swap functions
// --------------------------------
//
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}