function BusinessCard()
{
	this.oAjaxThreads = new Array();
}

BusinessCard.prototype.getNextThread = function()
{
	var i = 0;
	
	if (this.oAjaxThreads.length == 0)
	{
		var oAjax = new AJAX();
		this.oAjaxThreads.push(oAjax);		
	}

	while (this.oAjaxThreads[i] && this.oAjaxThreads[i].working && i < this.oAjaxThreads.length )
	{
		i++;
	}
	
	if (!this.oAjaxThreads[i])
	{
		var oAjax = new AJAX();
		this.oAjaxThreads.push(oAjax);		
	}
	
	return this.oAjaxThreads[i];
}

BusinessCard.prototype.setFieldValue = function(object)
{
	if(object)
	{
		var div = document.getElementById("fieldValue_"+object.id);
	
		if(div)
		{
			if(object.value!="")
				div.innerHTML = object.value;
			else
				div.innerHTML = "&nbsp;";
		}
	}
}

BusinessCard.prototype.selectTemplate = function(id, ext, orientation)
{
	var cardTemplate = document.getElementById("cardTemplate");
	var cardTemplateBack = document.getElementById("cardTemplateBack");
	cardTemplate.style.backgroundImage = "url('/images/templates/"+id+"_preview."+ext+"')";
	//cardTemplateBack.style.backgroundImage = "url('/images/templates/"+id+"_preview."+ext+"')";
	//cardTemplateBack.style.backgroundImage = "url('/images/businessCardBack/"+id+"_preview.jpg"+"')";
	
	if(orientation=="right")
	{
		cardTemplate.style.textAlign = "right";
	}
	else if(orientation=="center")
	{
		cardTemplate.style.textAlign = "center";
	}
	else
	{
		cardTemplate.style.textAlign = "left";
		orientation = "left";
	}
	
	//align center fix
	if(oBrowser.isIE())
	{
		for(var i=1; i<=12; i++)
		{
			document.getElementById("fieldValue_fieldType_"+i).style.textAlign = orientation;
		}
	}
	
	if(document.getElementById("businessCardTemplateId"))
		document.getElementById("businessCardTemplateId").value = id;
		
	return;
}

BusinessCard.prototype.setColor = function(color)
{
	if(color!="")
	{
		for(var i=1; i<=12; i++)
		{
			document.getElementById("fieldValue_fieldType_"+i).style.color = "#"+color;
		}
	}
}

BusinessCard.prototype.cardSubmit = function(type)
{
	var form = document.getElementById("formCard");
	return true;
}

BusinessCard.prototype.setFontSize = function(fontSize)
{
	if(fontSize=="big")
	{
		var cardTemplate = document.getElementById("cardTemplate");
		cardTemplate.style.fontSize = "16px";
		
		var fieldValue_fieldType_1 = document.getElementById("fieldValue_fieldType_1");
		fieldValue_fieldType_1.style.fontSize = "28px";
		
		for(var i=2; i<=12; i++)
		{
			document.getElementById("fieldValue_fieldType_"+i).style.fontSize = "16px";
		}
	}
	else if(fontSize=="medium")
	{
		var cardTemplate = document.getElementById("cardTemplate");
		cardTemplate.style.fontSize = "13px";
		
		var fieldValue_fieldType_1 = document.getElementById("fieldValue_fieldType_1");
		fieldValue_fieldType_1.style.fontSize = "26px";
		
		for(var i=2; i<=12; i++)
		{
			document.getElementById("fieldValue_fieldType_"+i).style.fontSize = "13px";
		}
	}
	else if(fontSize=="small")
	{
		var cardTemplate = document.getElementById("cardTemplate");
		cardTemplate.style.fontSize = "12px";
		
		var fieldValue_fieldType_1 = document.getElementById("fieldValue_fieldType_1");
		fieldValue_fieldType_1.style.fontSize = "24px";
		
		for(var i=2; i<=12; i++)
		{
			document.getElementById("fieldValue_fieldType_"+i).style.fontSize = "12px";
		}
	}
	else if(fontSize=="tiny")
	{
		var cardTemplate = document.getElementById("cardTemplate");
		cardTemplate.style.fontSize = "11px";
		
		var fieldValue_fieldType_1 = document.getElementById("fieldValue_fieldType_1");
		fieldValue_fieldType_1.style.fontSize = "22px";
		
		for(var i=2; i<=12; i++)
		{
			document.getElementById("fieldValue_fieldType_"+i).style.fontSize = "11px";
		}
	}
}

BusinessCard.prototype.setOptions = function(fontColor, fontSize)
{
	this.setColor(fontColor);
	this.setFontSize(fontSize);
}

BusinessCard.prototype.setBackImage = function()
{
	document.getElementById("cardTemplateBack").innerHTML = "Loading...";
	setTimeout("document.getElementById(\"cardTemplateBack\").style.backgroundImage = \"url('/images/tmp/"+document.getElementById("uploaderId").value+"_preview.jpg')\";document.getElementById(\"cardTemplateBack\").innerHTML = \"\";", 3000);
	setTimeout("oBusinessCard.checkQuality();", 3100);
}

BusinessCard.prototype.setTemplateType = function(templateId)
{
	if(templateId)
	{
		templateId = templateId;
	}
	else
	{
		templateId = "";
	}
	document.getElementById('formCard').action = "/card/view/"+templateId;
	document.getElementById('formCard').submit();
}

BusinessCard.prototype.submit = function(freeCard)
{
	var approve = document.getElementById("approve");
	var validated = true;
	
	if(!approve.checked)
	{
		alert("You must to accept the terms.");
		validated = false;
	}
	
	if(validated)
	{
		if(freeCard)
		{
			window.location.href='/shop/details/22';
		}
		else
		{
			window.location.href='/shop/details/25';	
		}
			
	}
}

BusinessCard.prototype.checkQuality = function()
{
	var oAjax =  this.getNextThread();
	oAjax.url = '/card/checkQuality/';
	oAjax.data = 'image=';
	oAjax.method = 'post';
	oAjax.addRequestListener(this, this.checkQualityFinished, false);
	oAjax.open();
}

BusinessCard.prototype.checkQualityFinished = function(xml, text)
{
	var divQuality = document.getElementById("imageQuality");
	var textQuality = "";
	if(text=="1")
	{
		divQuality.style.color = "red";
		textQuality = "Poor";
	}
	else if(text=="5")
	{
		divQuality.style.color = "#5F5506";
		textQuality = "Regular";
	}
	else if(text=="10")
	{
		divQuality.style.color = "green";
		textQuality = "Good";
	}
	divQuality.style.textAlign = "center";
	divQuality.style.fontSize = "13px";
	divQuality.style.fontWeight = "bold";
	divQuality.style.margin = "10px";
	divQuality.style.width = "530px";
	divQuality.style.padding = "5px 0";
	divQuality.style.background = "#FFB27F none repeat scroll 0 0";
	divQuality.style.border = "1px solid #FF781F";
	divQuality.innerHTML = "Image Quality: "+textQuality;
}

BusinessCard.prototype.cardOnload = function()
{
	setTimeout('document.getElementById("pLoading").style.display = "none"', 1000);
	setTimeout('document.getElementById("tableCard").style.display = ""', 1000);
}

var oBusinessCard = new BusinessCard();
