//******************************************************************************************//
//     	 			   File: 	html_javascript
//
//			 		Version:	1.0 
//					   Date:	Thu Jun 8 2006
//				  Copyright:	¨2006 PureBlend New Media Design Group <info@pureblend.com>
//
//******************************************************************************************//
function OpenPopupWindow(aURL,aWidth,aHeight) {

	// mozilla & safari DOM
	theParentTop 		= parent.screenY;
	theParentLeft 		= parent.screenX;
	theParentHeight 	= parent.innerHeight;
	theParentWidth 		= parent.innerWidth;
	
	// winIE DOM
	 if (navigator.appName.indexOf("Microsoft")!=-1) {
		theParentTop 		= parent.screenTop - 60; // cheat the top a bit
		theParentLeft 		= parent.screenLeft;
		theParentHeight 	= document.body.offsetHeight;
		theParentWidth 		= document.body.offsetWidth;
	}
	
	// common code
	theTop 				= Math.round((theParentHeight / 2) - (aHeight / 2) + theParentTop);
	theLeft				= Math.round((theParentWidth  / 2) - (aWidth  / 2) + theParentLeft);
	//windowName_suffix  	= Math.round(Math.random()*4);
	
	theWindow = window.open(aURL, 'popWindow', 'width=' + aWidth + ',height=' + aHeight + ',top=' + theTop + ',left=' + theLeft + ',scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes');
}

// Show or Hide an object

function ToggleVisibility(anID) {
	theItem	= document.getElementById(anID);
	
	if (theItem) {
		if (theItem.style.display == "block") {
			theItem.style.display = "none";
		} else {
			theItem.style.display = "block";
		}
	}
}

function ShowAnObject(elt) {
	elt.style.display = "block";
}

function HideAnObject(elt) {
	elt.style.display = "none";
}

// Swap an Image (used for star and balloon mouseovers)

function SwapImage(anID,aSrc) {
	if (aSrc) {
		if (aSrc.src) {
			theStarIMG 			= document.getElementById(anID);
			if (theStarIMG) {
				theStarIMG.src	= aSrc.src;
			}
		}
	}
	return true;
}

// Used to restore star rating images onmouseout

function RestoreStarImage(anID) {

	theStarIMG 					= document.getElementById(anID);
	
	if (theStarIMG) {
		if (theStarIMG.alt) {
			theStarIMG.src		= theStarIMG.alt;			// original src is stored in alt attribute
		}													// user-readible alts are stored in the star's imagemap
	}
	return true;											
}

// Set the Star Rating

function SetRating(anID,aRating) {
	theStarIMG 			= document.getElementById(anID);	// the star IMG tag
	theSrcStr			= "" + theStarIMG.alt;				// alt attribute contains original src attribute
	theNewSrcStr		= "";
	
	// determine background color, then build new gold star URL
	
	if (theSrcStr.indexOf("white") > -1) {
		theNewSrcStr	= "/images/stars/star_white_gold_" + aRating + "0.jpg";
	} else if (theSrcStr.indexOf("gray") > 0) {
		theNewSrcStr	= "/images/stars/star_gray_gold_"  + aRating + "0.jpg";
	} else if (theSrcStr.indexOf("beige") > 0) {
		theNewSrcStr	= "/images/stars/star_beige_gold_" + aRating + "0.jpg";
	}
	
	if (theNewSrcStr) {
		theStarIMG.src		= theNewSrcStr;					// save new src URL
		theStarIMG.alt		= theNewSrcStr;					// save a copy in the alt attribute for onmouseout events
	}
	
	var workerBee = document.getElementById('workerBee');
	workerBee.setAttribute('src', '/resources_setRating.php?rid='+anID+'&rating='+aRating);

	return true;
}

// Add Comments
// Opens popup window where users may add their own comments and view comments from others

function AddComment(anID) {
	theURL = "/comments.php?layoutStyle=verbose&resID=" + anID;
	OpenPopupWindow(theURL,450,640);
}

function AddCommentBrief(anID) {
	theURL = "/comments.php?layoutStyle=brief&resID=" + anID;
	OpenPopupWindow(theURL,450,330);
}

function OpenSlideShowWindow(anID) {
	theURL = "/slideshow.php?resID=" + anID;
	OpenPopupWindow(theURL,800,900);
}

function OpenSlideShowImage(path,caption,height,width) {
	theURL = "/slideshow.php?path=" + path + "&caption=" + caption + "&height=" + height + "&width=" + width;
	OpenPopupWindow(theURL,800,800);
}

function OpenFileUpload(anID) {
	theURL = "/file_upload.php?clientID=" + anID;
	OpenPopupWindow(theURL,600,500);
}

function calculateSubtotal(objCheckbox, amount) {
	
	if (arguments[2] != undefined) {
		var previousRadioVal	= arguments[2];
	} else {
		var previousRadioVal	= 0;
	}
	
	
	var subtotal_price		= document.getElementById('subtotal_price');

	// Clean up the current value
	var split				= subtotal_price.value.split("$");
	var split_sans_commas	= split[1].replace(/,/, '');
	var currentSubtotal		= Number(split_sans_commas); 

	if (objCheckbox.checked == false) {
		// was checked, now subtract
		newSubtotal				= currentSubtotal - amount;
	} else {
		// was not checked, now add
		newSubtotal				= currentSubtotal + amount;
	}
	
	subtotal_price.value	= '$'+addCommas(newSubtotal.toFixed(2));
}

// Add commas to thousands in numbers
function addCommas(nStr)
{
	// From http://www.mredkj.com/javascript/numberFormat.html
	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 ToggleItemToPurchase(item) {
	
	
	/**
	1. Show or Hide Options block according to Construction/Estimate selection.
	2. If Hide, then clear all currently selected Options
	3. Reset Subtotal.
	4. Create a new input element on mainform that is styles
	
	**/
	var style_el_name		= null;
	var mainform 			= item.form.name;
	var options_float		= document.getElementById('options_float');
	var styles				= document.getElementsByTagName('input');
	var subtotal_price		= document.getElementById('subtotal_price');
	var thePrice 			= document.getElementById('basePrice_value').innerHTML.replace(/\$/, '');
	var arrDisabled 		= new Array(4,15); // Disabled options when electronic is selected.
	// #2 
	for (i = 0; i <= styles.length; i++) {
		if (typeof styles[i] != 'undefined' && styles[i].name.search(/styles/) >= 0) {
			styles[i].checked 	= false;
			style_el_name		= styles[i].name;
		} 
	}

	//#4

	
	
	// #1
	if (item.id == 'item_estimate') {
		
		
		HideAnObject(options_float);
		var estimateSetPrice	= document.getElementById('estimateSetPrice_value').innerHTML.replace(/\$/, '');
		//thePrice = parseFloat(thePrice) - 1500;
		estimateSetPrice = estimateSetPrice.replace(',', '');
		thePrice = parseFloat(estimateSetPrice);
		
		// #4
		addHiddenOption(style_el_name, 14, mainform);
	
	} else {
		var targetStyleName = style_el_name;
		
		ShowAnObject(options_float);
		try {
			style_el_name	= document.getElementById(style_el_name);
			style_el_name.parentNode.removeChild(style_el_name);
		} catch(e) { /**alert(e);**/ }
		
		if (item.id == 'item_elect_down' || item.id == 'item_elect_cd') {
			
			disableOptions(styles, arrDisabled, boolDisable = true);
			
			try {
				
				var theMissingStyles = document.getElementById('missingStyles');
				theMissingStyles.parentNode.removeChild(theMissingStyles);
				
			} catch(e) { }
			
			var substyleID;
			if (item.id == 'item_elect_down') {
				var electronicSetPrice_value	= document.getElementById('electronicDownloadSetPrice_value').innerHTML.replace(/\$/, '');
				substyleID = 1;
			} else {
				var electronicSetPrice_value			= document.getElementById('electronicCDSetPrice_value').innerHTML.replace(/\$/, '');
				substyleID = 5;
			}
			addHiddenOption(targetStyleName, substyleID, mainform);
			
			electronicSetPrice_value = electronicSetPrice_value.replace(',', '');
			thePrice = parseFloat(electronicSetPrice_value);
			
			
			
		} else {
			disableOptions(styles, arrDisabled, boolDisable = false);
		}
	}

	// #3
	subtotal_price.value		= '$'+addCommas(cent(thePrice));
	
}

function addHiddenOption(optionFieldName, optionValue, formName) {
	var newHidden_style_ele	= document.createElement('input');
		newHidden_style_ele.setAttribute('type', 'hidden');
		newHidden_style_ele.setAttribute('name', optionFieldName);
		newHidden_style_ele.setAttribute('id', optionFieldName);
		newHidden_style_ele.setAttribute('value', optionValue);
		newHidden_style_ele.setAttribute('checked', true);
		document.getElementById(formName).appendChild(newHidden_style_ele);
}

function disableOptions(arrOptions, arrOptionValues, boolDisable) {
	var style_el_name;

	if (typeof arrOptionValues == 'object') {
		for (i = 0; i <= arrOptions.length; i++) {
			
			if (typeof arrOptions[i] != 'undefined' && arrOptions[i].name.search(/styles/) >= 0) {
				if (arrOptions[i].value == 4 || arrOptions[i].value == 15) {
					arrOptions[i].checked 	= false;
					arrOptions[i].disabled 	= boolDisable;
					style_el_name			= arrOptions[i].name;
				}
			} 
		}
		return style_el_name;
	}
}

function cent(amount) {
// returns the amount in the .99 format
    amount -= 0;
    return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}


