//add onCancel handler
//remove the uploading message if there is no more file in the queue
//add onError handler
function disableForm(){
	jQuery.each($('#design-info > *'),function(){
		$(this).attr('disabled',true);
	});
	$('#upload').css('cursor','default');
}

function enableForm(){
	jQuery.each($('#design-info > *'),function(){
		$(this).attr('disabled',false);
	});
	$('#upload').css('cursor','pointer');
}

function completeHandler(event, queueID, fileObj, response, data){

	var id = "#uploadify" + queueID;
	
	var str = '<input type="hidden" name="uploads[]" value="'+response+'" />';
	$(id).empty().append('<span class="filename">'+fileObj.name+' Completed.</span>').append(str);
	
	if(data.fileCount == 0){
		$('#loading-msg').css('display','block').empty().append('<img width="16" src="img/loading.gif" />Submitting Form Data...');
		$('#entryform').submit();
	}
	
	return false;
}

function cancelHandler(event, queueID, fileObj, data) {
	if (data.fileCount == 0) {
		$('#loading-msg').empty().css('display','none');
		$('#upload').attr('disable',false).css('cursor','pointer');
	}
	return true;
}

function formValidation(){
	var errors = new Array();
	var firstname = $('#firstname').val();
	var lastname = $('#lastname').val();
	var address = $('#address').val();
	var city = $('#city').val();
	var country = $('#country').val();
	var phone = $('#phone').val();
	var email = $('#email').val();
	var title = $('#title').val();
	var desc = $('#description').val();
	
	
	if(firstname == '') {
		errors.push('First Name');
	}
	if(lastname == '') {
		errors.push('Last Name');
	}
	if(address == '') {
		errors.push('Address');
	}
	if(city == '') {
		errors.push('City/State/Zip');
	}
	if(country == '') {
		errors.push('Country');
	}
	if(phone == '') {
		errors.push('Phone');
	}
	if(email == '') {
		errors.push('Email');
	}
	if(title == '') {
		errors.push('Product Title');
	}
	if(desc == '') {
		errors.push('Product Description');
	}
	if($('#fileQueue > *').length == 0){
		errors.push('Please at least upload one image');
	}
	
	
	if(errors.length == 0) {
		return true;
	} else {
		return errors;
	}
}

$(document).ready(function() {

	$('#uploadify').uploadify({
		'uploader': 'jquery.uploadify/uploadify.swf',
		'script': 'jquery.uploadify/uploadify.php',
		'queueID':'fileQueue',
		//'folder': '/1000productdesigns/dev/uploads',
		'folder': '/uploads',
		'cancelImg': 'jquery.uploadify/cancel.png',
		'multi': 'true',
		'queueSizeLimit': 3,
		'sizeLimit': 31457280,
		'fileExt': '*.tiff;*.tif;*.jpg;*.eps',
		'fileDesc': 'Image Files (*.tiff;*.tif;*.jpg;*.eps)',
		'onComplete': completeHandler,
		'onCancel':cancelHandler
	});
	
	if($('#chkaccept').is(':checked')){
		if($('#printname').attr('value')!="") {
			$('#printname').attr('readonly',true);
			$('#printname').css('background','#D4D0C8');
			enableForm();
		}else {
			$('#chkaccept').attr('checked',false);
			$('#printname').attr('readonly',false);
			$('#printname').css('background','');
			disableForm();
		}
	}else{
		//disable the following 3 sections
		disableForm();
	}
	
	
	$('#chkaccept').click(function(){
		if ($(this).is(':checked')){
			if($('#printname').attr('value')==""){
				alert("You have to enter the \"Print Name\" before you accept the Grant of Rights.");
				$(this).removeAttr('checked');
			}else {
				$('#printname').attr('readonly',true);
				$('#printname').css('background','#D4D0C8');
				
				//enable the following 3 sections
				enableForm();
			}
		}else {
		
			//disable the following 3 sections
			disableForm();
			$('#printname').attr('readonly',false);
			$('#printname').css('background','');
		}
	});
	
	$('#upload').click(function(){
		//do form validation
		var validResult = formValidation();
		if(validResult === true){
			$('#uploadify').uploadifyUpload();
			$(this).attr('disable',true).css('cursor','default');
			$('#loading-msg').css('display','block').empty().append('<img width="16" src="img/loading.gif" />Uploading Files...');
		} else {
			var length = validResult.length;
			var msg = '<h3>Please check the following fields:</h3><ul>';
			for (i=0;i<length;i++){
				msg = msg + '<li>' + validResult[i] + '</li>';
			}
			msg = msg + '</ul>';
			
			//insert error message into the page
			$('#msg-area').empty().append(msg);
			
			//jump to the error section
			window.location.hash = "error";
		}
	});
});