// JavaScript Document
var timer;

function resizeIMG()
{
	var img = document.getElementById('bg');
	img.onload = function() 
	{
		var ratio = document.body.offsetWidth / img.width;
		if (ratio < 1) 
		{
			img.height = img.height * ratio;
			img.width = img.width * ratio;
		}
	}
}

function getURL(url)
{
	window.location = url;	
}

function validateSetInfo()
{
	var error = false;
	var errortxt = '';

	if ( $('#date').val() == '')
	{
		error = true;
		errortxt += '- Enter a date please.\n';
	}

	if ( $('#setname').val() == '')
	{
		error = true;
		errortxt += '- Enter a setname please.\n';
	}
	
	if (error)
	{
		alert(errortxt);
		return false;
	}
	else
	{
		return true;
	}
}

function validatePhotos()
{
	var error = false;
	var errortxt = '';
	
	if ($('#wtrmrk').val() == '')
	{
		error = true;
		errortxt += '- Select the watermark option you want\n';		
	}
	
	if ( $('#divStatus').html() == 'Upload Queue')
	{
		error = true;
		errortxt += '- You did not select any pictures yet.\n';
	}
	
	if (error)
	{
		alert(errortxt);
		return false;
	}
	else
	{
		return true;
	}
}

function validateLink()
{
	var error = false;
	var errortxt = '';
	
	if ($('#name').val() == '')
	{
		error = true;
		errortxt += '- Give a name for this link\n';		
	}
	
	if ( $('#address').val() == '')
	{
		error = true;
		errortxt += '- Give an address for this link.\n';
	}
	
	if (error)
	{
		alert(errortxt);
		return false;
	}
	else
		return true;
}

function deleteSet(id)
{
	if (confirm("Are you sure you want to delete this entire image set?"))
	{
		window.location = 'sets_delete.php?id=' + id;
	}
}

function checkboxcount(fmobj)
{
	var cbs = $('[type=checkbox]').serializeArray();
	
	if ( cbs.length > 0)
	{
		$('#deletall').css('display', 'inline-block');	
	}
	else
	{
		$('#deletall').css('display', 'none');
	}
}

function deleteChecked()
{
	var cbs = $('input:checkbox').serializeArray();
	
	if ( cbs.length > 0)
	{
		return true;
	}
	else
	{
		alert("Please select at least one checkbox to use this function");
		return false;
	}		
}

function validateLogin()
{
	var error = false;
	var errortxt = '';
	
	if ( $('#username').val() == '')
	{
		error = true;
		errortxt += '- Username is required\n';
	}
	
	if ( $('#password').val() == '')
	{
		error = true;
		errortxt += '- Password is required\n';
	}
	
	if (error)
	{
		alert(errortxt);
		return false;
	}
	else
		return true;
	
}

function validateContact()
{
	var error = false;
	var errortxt = '';
	var reg	= /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var cssgood = '';
	var cssbad = '';	
	
	if ( $('#name').val() == '' )
	{
		error = true;
		errortxt += '- Name is a required field\n';
	}
	
	if ( $('#email').val() == '' )
	{
		error = true;
		errortxt += '- E-mail is a required field\n';
	}
	else
	{
		if (reg.test( $('#email').val() ) == false )
		{
			error = true;
			errortxt += '- Incorrect e-mail address\n';
		}
	}
	
	if ( $('#question').val() == '' )
	{
		error = true;
		errortxt += '- Question is a required field\n';
	}
	
	if ( $('#captcha').val() == '' )
	{
		error = true;
		errortxt += '- Anti-Spam is a required field\n';
	}
	
	if ( $('#captcha').val() != '' )
	{
		// ajax the correct answer
		$.post('inc/ajax.inc.php', { type: 'contact', answer: $('#captcha').val() }, function(data) 
			{
				if (data == -1)
				{
					error = true;
					errortxt += '- Anti-Spam anwser is incorrect!\n';
				}
				
				if (error)
				{
					alert(errortxt);
					return false;
				}
				else
					return true;
			}
		);
	}
	else
	{
		if (error)
		{
			alert(errortxt);
			return false;
		}
		else
			return true;
	}
}

function purchase_tab()
{
	$('#p_opt').slideToggle();
	clearTimeout(timer);
}

function add2cart()
{
	var imageID = $('#ID').val();
	var size = $('input[name*="po_r"]:checked').val();
	
	// send data to cartHandler
	$.post('inc/ajax.inc.php', { type: 'cart', ID: imageID, imageSize: size }, function(data) 
		{
			// update cart status
			$('#cart_total_items').html(data);
			$('.cart_options').hide();
			$('#addtocart').hide();
			$('#incart').show();
			$('#cart_details_top').slideDown();
			timer = setTimeout ( "purchase_tab()", 3000 ); 
		}
	);
}

function validatePaypal()
{
	var error = false;
	var errortxt = '';
	var reg	= /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if ( $('#firstname').val() == '')
	{
		error = true;
		errortxt += '- First name is required\n';
	}
	if ( $('#lastname').val() == '')
	{
		error = true;
		errortxt += '- Last name is required\n';
	}
	if ( $('#email').val() == '')
	{
		error = true;
		errortxt += '- E-mail is required\n';
	}
	else
	{
		if (reg.test( $('#email').val() ) == false )
		{
			error = true;
			errortxt += '- Incorrect e-mail address\n';
		}
	}
	
	if (error)
	{
		alert(errortxt);
		return false;
	}
	else
		return true;
	
}

function deleteLink(id)
{
	if ( confirm("Are you sure you want to delete this link ?") )
	{
		getURL('links_delete.php?id=' + id);
	}
}

function enablePaypal()
{
	$('#API_USERNAME').attr('disabled', false);
	$('#API_SIGNATURE').attr('disabled', false);
	$('#API_PASSWORD').attr('disabled', false);
}
