

// function to show alt images
function switchImages(TheImageSource,obj) {		
	//Set the source image (Image clicked) information
	var SourceImage = TheImageSource;
	var SourceImageAlt = obj.alt;
	var SourceImageTitle = obj.title;
	
	//Set the destination detail image to be the clicked alt image
	document.getElementById("MainProdImage").src = SourceImage;		
	document.getElementById("MainProdImage").alt = SourceImageAlt;	
	document.getElementById("MainProdImage").title = SourceImageTitle;	
			
	document.getElementById("EnlargeLink").href = SourceImage.replace('dt','en');
	document.getElementById("EnlargeLink2").href = SourceImage.replace('dt','en');
}


//function to check the product fields
function CheckProductFields() {
	//Make sure all the options are selected
	
	if(document.DetailForm.AttributesList.value != '') {
		var AttrList = document.DetailForm.AttributesList.value;
		
		var Attrs = AttrList.split(",");
		for(i=0;i<=Attrs.length-1;i++) {	
			
			var AttName = Attrs[i];
			var AttName = AttName.replace(' ','');
			
			if(document.getElementById("AttributeName"+AttName).value == '') {
				alert("The "+Attrs[i].toLowerCase()+" option must be selected.");
				return false;
			}
		}			
	}	
	
	
	//Check the quantity field
	var qty = document.DetailForm.Quantity.value;
	if(qty <= 0 || isNaN(qty)) {
		alert("The quantity value must be a numeric value greater than 0.");
		return false;
	}
}


//Function to calculate the price
function CalculatePrice() {
	var prodPrice = document.DetailForm.ProductPrice.value;
	var qty = document.DetailForm.Quantity.value;
	
	if(isNaN(qty)) {
		qty = 1;
	}
	else if(qty == '') {
		qty = 1;
		document.DetailForm.Quantity.value = 1;
	}
	else if(qty == 0) {
		qty = 1;
		document.DetailForm.Quantity.value = 1;
	}
		
	var TotalOptionPrice = 0;
	
	var prodPrice = ((prodPrice*1)*(qty*1));
	
	if(document.DetailForm.AttributeIDList.value != '') {
		var AttrList = document.DetailForm.AttributeIDList.value;
		var Attrs = AttrList.split(",");
		
		for(i=0;i<=Attrs.length-1;i++) {				
			
			if(eval("document.DetailForm.AddCost"+Attrs[i]+".value") != '') {
				var OptionCost = eval("document.DetailForm.AddCost"+Attrs[i]+".value");
				
				TotalOptionPrice= (TotalOptionPrice*1) + (OptionCost*1);
				
			}
			else {
				TotalOptionPrice = (TotalOptionPrice*1) + (0*1);					
			}
		}			
	}		
	
	prodPrice = ((prodPrice*1)+(TotalOptionPrice*1));
			
	
	document.getElementById('ProductDspPrice').innerHTML = "Our Price: $"+cent(prodPrice);
}

//function to set the product attributes
function setProductAttribute(Attribute, AttributeValue, AttributeCost, CalculateCost) {		
	var FindSep = AttributeValue.indexOf("~",1);
		
	if(FindSep != -1) {
		var AttRealValue = AttributeValue.split("~");
				
		document.getElementById('AddCost'+Attribute).value = AttRealValue[1];	
	}
	else {
		document.getElementById('AddCost'+Attribute).value = AttributeCost;	
	}
	
	if(CalculateCost == 1) {
		CalculatePrice();
	}
}


//Function to change a numeric value to dollar format
function cent(amount) {
	// returns the amount in the .99 format 
	amount -= 0;
	amount = (Math.round(amount*100))/100;
	return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}


//Return the max char size based on the selectd font and font size
function GetMaxChars(LineNum,CheckHeight) {	
	var FontID = eval("document.DetailForm.SelectedFont"+LineNum+".value");
	
	if(eval('document.DetailForm.SelectedFontSize'+LineNum+'.length') == 1) {
		var TemplateID = document.DetailForm.TemplateID.value;
		
		var f = new Font();
		f.setCallbackHandler(getSizeResult);	
		f.getFontSizes(TemplateID,FontID,LineNum);	
	}
	else {
		
		
		var FontSize = eval("document.DetailForm.SelectedFontSize"+LineNum+".value");
		
		if(CheckHeight == 1) {
			var totalLines = document.getElementById('MaxLinesVal').value;
			
			var totalHeight = 0;
			for(i=1;i<=totalLines;i++) {
				var LineHeight = eval("document.DetailForm.SelectedFontSize"+i+".value");
				totalHeight = eval(totalHeight+(LineHeight*1));
			}
						
			if(totalHeight > eval(document.DetailForm.MaxCharHeight.value*1)) {
				var HeightError = 1;
			}
			
		}
		
		if(HeightError) {
			eval("document.DetailForm.SelectedFontSize"+LineNum+".selectedIndex=0");
			alert("Sorry, the height of your combined lines is greater than the height of this stamp. Please go back and adjust your text so it fits onto the stamp.");
		}
		else {
			
			if(FontSize > 0 && FontID > 0) {	
				//Call the function to return the max chars
				var f = new Font();
				f.setCallbackHandler(getFontResult);	
				f.GetMaxChars(FontID,FontSize,LineNum);	
			}
		}
	}
}


function getFontResult(MaxChars) {		
	var avgSize = MaxChars[1];
	var LineNum = MaxChars[0];
	var FontValue = MaxChars[2];
	
	var MaxCharWidth = document.DetailForm.MaxCharWidth.value;
	var MaxCharHeight = document.DetailForm.MaxCharHeight.value;
	
	var MaxCharLen = Math.floor(eval((MaxCharWidth/avgSize)*1));
	
	document.getElementById('MaxCharText'+LineNum).innerHTML = "( "+MaxCharLen+" character maximum)";
	document.getElementById('LineText'+LineNum).disabled = false;
	
	if(document.getElementById('CartItem'+LineNum).value == 0) {
		document.getElementById('LineText'+LineNum).value = '';
		document.getElementById('TextOutput'+LineNum).innerHTML = '';
	}
	else {
		LineTextVal = document.getElementById('LineText'+LineNum).value;		
		if(LineTextVal.length > MaxCharLen) {
			document.getElementById('LineText'+LineNum).value = '';
			document.getElementById('TextOutput'+LineNum).innerHTML = '';
		}
	}
	
	document.getElementById('LineText'+LineNum).maxLength = MaxCharLen;
	
	var CurrFontValue = FontValue;
	while ( CurrFontValue.indexOf(' ') != -1)
    {
        CurrFontValue = CurrFontValue.replace(' ','-');
    }
	
	
	document.getElementById('SelectedFontValue'+LineNum).value = CurrFontValue.toLowerCase();
}


function getSizeResult(Sizes) {
	
	if(Sizes.length > 0 ) {
		var LineNumVal = Sizes[0];
		
		var SelectedSize = document.getElementById('SelectedFontSizeVal'+LineNumVal).value;
		
		with(document.DetailForm){		
			//Need to remove all options
			var option = new Option();					
				option.text='- Font Size -';
				option.value='';
				eval('SelectedFontSize'+LineNumVal).options[0] = option;
				
			for(i=1;i<Sizes.length;i++){			
				var option = new Option();					
					
					option.text=Sizes[i]+'pt';
					option.value=Sizes[i];
					eval('SelectedFontSize'+LineNumVal).options[i] = option;	
					if(Sizes[i] == SelectedSize) {
						eval('SelectedFontSize'+LineNumVal).options[i].selected = true;
						GetMaxChars(LineNumVal,1);
					}
			}
			
			eval('SelectedFontSize'+LineNumVal).disabled = false;	
			
		}	
		
	}
}


function WriteLetters(LineNum) {
	document.getElementById('TextOutput'+LineNum).className = document.getElementById('SelectedFontValue'+LineNum).value;
	document.getElementById('TextOutput'+LineNum).innerHTML = document.getElementById('LineText'+LineNum).value;
	do_sIFR(document.getElementById('SelectedFontValue'+LineNum).value,'',LineNum);
	document.getElementById('LineText'+LineNum).focus();
}


function do_sIFR(face, color, LineNum) { 
	var face = "/sIFR/"+face+".swf"; 		
	var color = "#000000"; 
	if ( typeof sIFR == 'function' ) { sIFR.replaceElement(named({sSelector:"h4", sFlashSrc: face, sColor: color, sLinkColor:"#cccccc", sBgColor:"#000000", sHoverColor:"#CCCCCC",sWmode:"transparent", sCase:"", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0&offsetLeft=0&offsetRight=0&offsetBottom=0"})); } 
}


function ResetLines(TotalLines) {
	document.DetailForm.TotalHeight.value = 0;
	
	for(i=1;i<=TotalLines;i++) {
		if(eval("document.DetailForm.CartItem"+i+".value") == 0) { 
			eval("document.DetailForm.SelectedFont"+i+".selectedIndex=0");
			eval("document.DetailForm.SelectedFontSize"+i+".selectedIndex=0");
			document.getElementById('LineText'+i).disabled = true;
		}
	}
}
