var proofText = "&nbsp;";
var proofBox = "";
var proofBoxWed = "";
var divs=document.getElementsByTagName('div');
function matchHeight(){
	contDivs=[];
	// initialize maximum height value
	maxHeight=0;
	// iterate over all <div> elements in the document
	for(var i=0;i<divs.length;i++){
		// make collection with <div> elements with class attribute 'container'
		if(/\bhtcontainer\b/.test(divs[i].className)){
			d=divs[i];
			contDivs[contDivs.length]=d;
			// determine height for <div> element
			if(d.offsetHeight)
			 divHeight=d.offsetHeight;
			else if(d.style.pixelHeight)
			 divHeight=d.style.pixelHeight;
			// calculate maximum height
			maxHeight=Math.max(maxHeight,divHeight);
		}
		if(/\bcontent-body\b/.test(divs[i].className))
			subDiv = divs[i];
	}
	// assign maximum height value to all of container <div> elements
	for(var i=0;i<contDivs.length;i++){
		if(document.all)
			contDivs[i].style.height = maxHeight;
		else
			contDivs[i].style.minHeight = maxHeight;
	}
	if(document.all)
		subDiv.style.height = maxHeight - 72;
	else
		subDiv.style.minHeight = maxHeight - 72;	
}
// execute function when page loads
window.onresize=window.onload=function(){
	if(document.getElementsByTagName)
		matchHeight();
}

//Changes to 2 decimal
function formatAsMoney(amount) {
    amount -= 0;
    amount = (Math.round(amount*100))/100;
    return (amount == Math.floor(amount)) ? amount + '.00' 
              : ( (amount*10 == Math.floor(amount*10)) ? 
                       amount + '0' : amount);
}
//Used to change price
function changePrice(thisform,thisobj){
	if(thisobj){ 
		var txtarea = thisform.getElementsByTagName("textarea")
		if(thisobj.options[thisobj.selectedIndex].value.indexOf("Yes") > -1)
			txtarea[0].style.display = "block";
		else
			txtarea[0].style.display = "none";
	}
	var checkpricetype;
	var price = document.getElementById("baseprice").value;
	var modPrice = "";
	if(price != "")
		price = parseFloat(price);
	else
		price = 0;

	for(i=0;i<thisform.elements.length;i++){
		if(thisform.elements[i].type == "select-one" && thisform.elements[i].selectedIndex >= 0){
			priceval = thisform.elements[i].options[thisform.elements[i].selectedIndex].value;
			if(priceval.indexOf("-$") > 0){
					priceval = priceval.split("-$");
					checkpricetype = "negative";
			}else{
				if(priceval.indexOf("(-") > 0){
						priceval = priceval.split("(-");
						checkpricetype = "negative";
				}else{
					if(priceval.indexOf("+$") > 0)
						priceval = priceval.split("+$");
					else
						priceval = priceval.split("(+");
				}
			}
			
			if(priceval.length > 1){
				priceval = priceval[1].split(")");
				if(checkpricetype == "negative"){
					priceval = parseFloat("-" + priceval[0]);
					checkpricetype = "positive";
				}
				else
					priceval = parseFloat(priceval[0]);
			}else
					priceval = 0;
			
			price = price + priceval
		
		}
	}
	modPrice = modPrice + "$" + formatAsMoney(price);

  document.getElementById("totalprice").innerHTML = modPrice;
}

function writePers(pos,firstload){
	var readDiv;
	var findid = "personalize-" + pos;
	var subcontentid = "sub-content-box-" + pos;
	var writeDiv = document.getElementById("personalize-elements");
	proofBoxObj = document.getElementById("proof-box");
	proofBoxWedObj = document.getElementById("proof-box-wedding");

	if(proofBox == "")
		proofBox = proofBoxObj.innerHTML;
	if(proofBoxWedObj){
		if(proofBoxWed == "")
			proofBoxWed = proofBoxWedObj.innerHTML;
	}
	
	if(document.getElementById(findid)){
		readDiv = document.getElementById(findid).innerHTML;
		if(readDiv != ""){
			if(readDiv.indexOf("writeProof") > -1){
				proofBoxWedObj.innerHTML = "";
				proofBoxWedObj.style.display = "none";
				proofBoxObj.innerHTML = proofBox;
				proofBoxObj.style.display = "block";
			}else{
					proofBoxObj.innerHTML = "";
					proofBoxObj.style.display = "none";
				if(proofBoxWedObj){
					proofBoxWedObj.innerHTML = proofBoxWed;
					proofBoxWedObj.style.display = "block";
				}
			}
				
			writeDiv.parentNode.style.display = "block";
			writeDiv.innerHTML = readDiv;
		}else{
			proofBoxObj.innerHTML = "";
			proofBoxObj.style.display = "none";
			writeDiv.innerHTML = "";
			writeDiv.parentNode.style.display = "none";
		}
	}
	if(document.getElementById(subcontentid))
		readDiv = document.getElementById(subcontentid).innerHTML;
	else
		readDiv = "";
		
	document.getElementById("sub-content-box").innerHTML = readDiv;
	proofText = "&nbsp;"
	writeProof();
	if(firstload){
		var parentForm = document.getElementById("orderform").getElementsByTagName("form");
		parentForm = parentForm[0];
		changePrice(parentForm);
	}
}

function writeProof(obj){
	proofText = "&nbsp;";
	var readDiv = document.getElementById("personalize-elements");
	var allInput = readDiv.getElementsByTagName("input");
	var writeDiv = document.getElementById("proof-text");
	for(i=0; i < allInput.length; i++){
		if(allInput[i].type != "button"){
			if(proofText == "&nbsp;" && allInput[i].value != "")
				proofText = allInput[i].value;
			else{
				if(allInput[i].size > 1 && allInput[i].value != "")
					proofText = proofText + "<br>" + allInput[i].value;
				else{
					if(allInput[i].value != "")
						proofText = proofText + "&nbsp;" + allInput[i].value;
				}
			}
		}
	}
	if(writeDiv)
		writeDiv.innerHTML = proofText;
}

function activateRest(){
	for(var i=0;i<divs.length;i++){
		if(/\bhide-box\b/.test(divs[i].className))
			divs[i].style.display = "block";
	}
	
	/*if(location.href.indexOf("#4") > -1)
		location.href = location.href;
	else
		location.href = location.href + "#4";*/
	gotoloc(3);
}

function gotoloc(num){
	var url = location.href;
	if(url.indexOf("#") > -1){
		url = url.substr(0,url.indexOf("#"));
	}
	location.href = url + "#" + num;
}

function showScript(url,ww,wh){
		scriptWindow = window.open(url,'scriptWindow','width='+ww+',height='+wh+',status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
		scriptWindow.focus();
}

function makeWin(imsrc, ww, wh) {
	ww=ww+25; wh=wh+50;
	w1=window.open('','w1','width='+ww+',height='+wh+',status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
	w1.document.open();
	w1.document.write("<html><head><title>Image Zoom<\/title><META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\"><META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\"><\/head>");
	w1.document.write("<body bgcolor='#FFFFFF' text='#000000' leftmargin=2 topmargin=10><p align=center>");
	w1.document.write("<img name=theimg src=" + imsrc + "><br>");
	w1.document.write("<a href=\"javascript:window.close();\"><font size=1 face=tahoma color=#000000>close window<\/font><\/a><\/body><\/html>");
	w1.document.focus();
}