var imageId = 0;
var counter = 0.0;
var imageNames = new Array('slim', 'classic', 'mini');
var step = Math.PI / 85;
var interval;

$(document).ready(
	function()
	{
		$('#submitNew').click(
			function()
			{
				$.ajax({
					type: "POST",
					url: "scripts/form-script.php",
					data:
						"name=" + $('#name').val() + 
						"&lname=" + $('#lname').val() +
						"&mid=" + $('#mid').val() + 
						"&mail=" + $('#mail').val() + 
						"&pass=" + $('#pass').val() + 
						"&cpass=" + $('#cpass').val() +
						"&company=" + $('#company').val() +
						"&address=" + $('#address').val() +
						"&country=" + $('#country').val() +
						"&state=" + $('#state').val() +
						"&city=" + $('#city').val() +
						"&zip=" + $('#zip').val() +
						"&phone=" + $('#phone').val() +
						"&altphone=" + $('#altphone').val() +
						"&fax=" + $('#fax').val() + 
						"&letter=" + $('#letter').val(),
					success:
						function(response)
						{
							$('#formResponse').html(response);
						}
				});
			}
		);
		
		$('#country').change(
			function()
			{
				if($(this).val() != 'USA')
					$('#state').attr('disabled', 'disabled');
				else
					$('#state').attr('disabled', '');
			}
		);
		
		$('#loginForm').submit(
			function()
			{
				$.ajax({
					type: "POST",
					url: "scripts/login-script.php",
					data: "mail=" + $('#logMail').val() + "&pass=" + $('#logPass').val(),
					async: false,
					success: 
						function(response)
						{
							//$('#loginResponse').html(response);
							htmlResponse = ''; /*'&nbsp;&nbsp;&nbsp;&nbsp;Login successful.'*/
							switch(response)
							{
								case '0': htmlResponse = '&nbsp;&nbsp;&nbsp;&nbsp;The e-mail address is not<br />&nbsp;&nbsp;&nbsp;&nbsp;registered !<br /><br />'; break;
								case '1': htmlResponse = '&nbsp;&nbsp;&nbsp;&nbsp;The password does not<br />&nbsp;&nbsp;&nbsp;&nbsp;match the e-mail !<br /><br />'; break;
							}
							$('#loginResponse').html(htmlResponse);
							if(response == 2)
								location.reload(true);
						}
				});
				return false;
			} //submitLogin click
		);
		
		$('.productPage').click(
			function()
			{
				id = $(this).attr('id');
				for(i = 1; i <= 8; i++)
					if(id == i)
						$('#productPage' + i).css('display', 'block');
					else
						$('#productPage' + i).css('display', 'none');
				
			}
		);
		
		$('.pageNav').click(
			function()
			{
				id = $(this).attr('id').substr(7);
				if(id != -1)
				{
					$('.productSinglePage').css('display', 'none');
					$('#page' + id).css('display', 'block');
				}
			}
		);
		
		$('.categoryItem').hover(
			function()
			{
				id = $(this).attr('id').substr(12);
				$(this).css('cursor', 'pointer');
				$('#modelDomains' + id).show();
			},
			function()
			{
				id = $(this).attr('id').substr(12);
				$('#modelDomains' + id).hide();
			}
		);
		
		$('.categoryItem').click(
			function()
			{
			}
		);
		
		$('.productSecondImage').click(
			function()
			{
				//$path = $(this).attr('src');
				imageName = $(this).attr('src').substr(15);
				//alert(imageName);
				$('#productLargeImage').attr('src', 'uploads/' + imageName);
			}
		);
		
		$("#quantity").keyup(
			function()
			{
				getNewPrice( $("#capacity").val(), $(this).val() );
			}
		);
		
		$("#capacity").change(
			function()
			{
				getNewPrice( $(this).val(), $("#quantity").val() );
			}
		);
		
		$("#addField").click(
			function()
			{
				$.ajax({
					type: "POST",
					url: "scripts/rma-script.php",
					//data: "mail=" + $('#logMail').val() + "&pass=" + $('#logPass').val(),
					async: true,
					success: 
						function(response)
						{
							//$('#rmaTable').html(response);
						}
				});
				numFields = $("#rmaTable").attr('rel');
				numFields++;
				$("#rmaTable").append('<tr><td><input type="text" name="piProd' + numFields + '" style="width:100px;" /></td><td><input type="text" name="piQuant' + numFields + '" style="width:100px;" /></td><td><input type="text" name="piCap' + numFields + '" style="width:100px;" /></td><td><input type="text" name="piInvoice' + numFields + '" style="width:100px;" /></td><td><input type="text" name="piDesc' + numFields + '" style="width:100px;" /></td></tr>');
				$("#rmaTable").attr('rel', numFields);
			}
		);
		
		$("#popup").popup({
			width:700,
			height:680
		});
	}
);

function fadeImage(fadeIn)
{
	opacity = Math.cos(counter);
	$('#galleryImage').css('opacity', opacity);
	$('#galleryImage').css('filter', 'alpha(opacity=' + (opacity * 100) + ')');
	if(fadeIn)
	{
		counter += step;
		if(counter >= Math.PI / 2)
		{
			clearInterval(interval);
			interval = setInterval("fadeImage(false)", 50);
			imageId++;
			if(imageId == 3)
				imageId = 0;
			$('#galleryImage').attr('src','img/new/pici-'+ imageNames[imageId] +'.jpg');
		}
	}
	else
	{
		counter -= step;
		if(counter <= 0.0)
		{
			clearInterval(interval);
			setTimeout("interval = setInterval(\"fadeImage(true)\", 30);", 2000);
		}
	}
	
}

function sendOrderData(capacity, price, quantity, productId, domain)
{
	quant = quantity
	switch(quantity)
	{
		case '1K': quant = 1000; break;
		case '2.5K': quant = 2500; break;
		case '5K': quant = 5000; break;
		case '10K': quant = 10000; break;
		default: break;
	}
	$.ajax({
		type: "POST",
		url: "scripts/order-data.php",
		data: "capacity=" + capacity + "&price=" + price + "&quantity=" + quant + "&productId=" + productId + "&domain=" + domain,
		success:
			function()
			{
				window.location = 'index.php?id=order';
			}
	});
}

function getNewPrice(capacity, quantity)
{
	$.ajax({
		type: "POST",
		url: "scripts/price-script.php",
		data: "capacity=" + capacity + "&quantity=" + quantity,
		success:
			function(response)
			{
				$("#itemPrice").html(response);
			}
	});
}

function validateRMAForm()
{
	if($("#region").val() == '')
	{
		$("#rmaResponse").html("Please select your region !");
		$("#region").focus();
		return false;
	}
	
	if($("#cname").val() == '')
	{
		$("#rmaResponse").html("Please enter your company's name !");
		$("#cname").focus();
		return false;
	}

	if($("#cpers").val() == '')
	{
		$("#rmaResponse").html("Please the contact person's name !");
		$("#cpers").focus();
		return false;
	}

	if($("#raddr").val() == '')
	{
		$("#rmaResponse").html("Please enter a return address !");
		$("#raddr").focus();
		return false;
	}

	if($("#city").val() == '')
	{
		$("#rmaResponse").html("Please enter a city !");
		$("#city").focus();
		return false;
	}

	if($("#state").val() == '')
	{
		$("#rmaResponse").html("Please enter your state/country !");
		$("#state").focus();
		return false;
	}
	
	if($("#zip").val() == '')
	{
		$("#rmaResponse").html("Please enter your zip code!");
		$("#zip").focus();
		return false;
	}

	if($("#mail").val() == '')
	{
		$("#rmaResponse").html("Please enter your email address !");
		$("#mail").focus();
		return false;
	}
	
	return true;
}
