function leadingZeros(x){
	if (x < 10)
		return '000' + x;
	else if (x < 100) 
		return '00' + x;
	else if (x < 1000)
		return '0' + x;
	
	return x;
}

function OpenFontPreview(font) {
	if (font == 0) {
		alert('Custom text preview is not available for custom fonts.');
		return;
	}
	window.open("/item/font_preview/"+font, "FontPreview", "width=700,height=150,top=0,left=0,resizable=1,scrollbars=1");
}

function CheckForm(form) {
	if (form.paper_id.value == -1) {
		alert('You did not select a paper type. Please click the image for the paper design you would like to use.');
		return false;
	}
	else if (form.font_id.value == -1) {
		alert('You did not select a font. Please click the image for the font you would like to use. Alternatively, you may click the last option to upload your own font.');
		return false;
	}
	else if (form.frame_id.value == -1) {
		alert('You did not select a frame for the certificate. Please click the image for the frame you would like to use, or click the "No frame" option.');
		return false;
	}
	
	return true;
}

var values = new Array();
var lastObj = new Array();
lastObj["paperSelection"] = null;
values["paperSelection"] = 'paper';
lastObj["frameSelection"] = null;
values["frameSelection"] = 'frame';
lastObj["fontSelection"] = null;
values["fontSelection"] = 'font';

function OpenFontPreview(font) {
	if (font == 0) {
		alert('Custom text preview is not available for custom fonts.');
		return;
	}
	window.open("/item/font_preview/"+font, "FontPreview", "width=700,height=150,top=0,left=0,resizable=1,scrollbars=1");
}

function SetDefaultSelections() {
	var obj = document.all ? document.all['p0001'] : document.getElementById ? document.getElementById('p0001') : "";
	SetSelection(obj);
	obj = document.all ? document.all['t0001'] : document.getElementById ? document.getElementById('t0001') : "";
	SetSelection(obj);
	obj = document.all ? document.all['f0000'] : document.getElementById ? document.getElementById('f0000') : "";
	SetSelection(obj);
}
function SetSelection(obj) {
	AddClass(obj.parentNode.parentNode, 'selected');
	
	itemSection = obj.parentNode.parentNode.parentNode.title;
	
	if (lastObj[itemSection] != obj.parentNode.parentNode) {
		RemoveClass(lastObj[itemSection], 'selected');
		lastObj[itemSection] = obj.parentNode.parentNode;
	}
	
	var infoObj = document.all ? document.all[itemSection] : document.getElementById ? document.getElementById(itemSection) : ""
	
	var itemId = obj.title.substring(1,5);
	if (itemSection == "fontSelection")
		infoObj.innerHTML = obj.title+" - <a href=\"javascript:void(0)\" onClick=\"OpenFontPreview("+itemId+")\">Custom preview</a>";
	else
		infoObj.innerHTML = obj.title+" - <a href=\"graphics/"+values[itemSection]+"s/"+itemId+".jpg\" target=\"_blank\">View larger preview</a>";
	
	var itemObj = document.all ? document.all[values[itemSection]+"_id"] : document.getElementById ? document.getElementById(values[itemSection]+"_id") : ""
	itemObj.value = obj.id.substring(1,5);
	
	return false;
}