$(document).ready(function () {
    // loading..
    $('#loading').ajaxStart(function(){
		$(this).fadeIn();
	}).ajaxStop(function(){
		$(this).fadeOut();
	});
    $('#buttonUpload').ajaxStart(function(){
		$(this).attr('disabled', true);
	}).ajaxStop(function(){
		$(this).attr('disabled', false);
	});
    $('.button-submit').ajaxStart(function(){
		$(this).attr('disabled', true);
	}).ajaxStop(function(){
		$(this).attr('disabled', false);
	});

	$('#formQuote').submit(function(){
        var frmFirstName = $('#frmFirstName').val();
        var frmLastName = $("#frmLastName").val();
        var frmContact = $("#frmContact").val();
        var frmRecipient = $("#frmContact option:selected").attr("class");
        var frmProductType = $("#frmProductType").val();
        var frmCompany = $("#frmCompany").val();
        var frmCity = $("#frmCity").val();
        var frmState = $("#frmState").val();
        var frmCountry = $("#frmCountry").val();
        var frmPhone = $("#frmPhone").val();
        var frmEmail = $("#frmEmail").val();
        var frmProjectDesc = $("#frmProjectDesc").val();
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var attachment = '';
        $(".attached").each(function(){
            var filename = $(this).val();
            attachment += "&attached[]=" + filename;
        });

        var dataString = 'frmFirstName='+frmFirstName+'&frmLastName='+frmLastName+'&frmContact='+frmContact+'&frmRecipient='+frmRecipient+'&frmProductType='+frmProductType+'&frmCompany='+frmCompany+'&frmCity='+frmCity+'&frmState='+frmState+'&frmCountry='+frmCountry+'&frmPhone='+frmPhone+'&frmEmail='+frmEmail+'&frmProjectDesc='+frmProjectDesc+attachment;

        if(frmFirstName == '' || frmFirstName == 'undefined') {
            alert('Please enter your first name');
            $("#frmFirstName").focus();
		}
        else if(frmLastName == '' || frmLastName == 'undefined') {
            alert('Please enter your last name');
            $("#frmLastName").focus();
		}
        else if(frmCompany == '' || frmCompany == 'undefined') {
            alert('Please enter company name');
            $("#frmCompany").focus();
		}
        else if(frmPhone == '' || frmPhone == 'undefined') {
            alert('Please enter phone number');
            $("#frmPhone").focus();
		}
        else if(frmEmail == '' || frmEmail == 'undefined') {
            alert('Please enter email address');
            $("#frmEmail").focus();
            return false;
		}
		else if(!emailReg.test(frmEmail)) {
            alert('Please enter valid email address');
            $("#frmEmail").focus();
            return false;
		}
		else if(frmProductType == 0) {
            alert('Please select product type');
            $("#frmProductType").focus();
            return false;
		}
		else if(frmContact == 0) {
            alert('Please select an office to contact');
            $("#frmContact").focus();
            return false;
		}
		else if(frmProjectDesc == '' || frmProjectDesc == 'undefined') {
            alert('Please enter project description');
            $("#frmProjectDesc").focus();
            return false;
		}
        else{
            //alert(dataString);
            //return false;
            $.ajax({
                type: 'POST',
                url: 'bin/contact.php',
                data: dataString,
                success: function(data) {
                    if(data == 'sent'){
                        //$('#formQuote').hide();
                        //$('#form-sent').show();
                        //$(window).scrollTo('.column-left', 1);
                        self.location = 'thanks.php';
                    }
                    else{
                        $('#form-error').show();
                        $('#form-error').find('span').html(data);
                    }
                }
            })
        }
        return false;
	});

	var removeFile = function() {
        var thelist = $(this).parent();;
        var rel = $(this).attr('rel');
        var dataString = 'rel='+rel;
        if(rel == '' || rel == 'undefined') {
            alert('Error deleting file');
            return false;
        }
        else{
            $.ajax({
                type: 'POST',
                url: 'bin/dodeletefile.php',
                data: dataString,
                success: function(data) {
                    if(data == 'deleted'){
                        $(thelist).html('');
                        $(thelist).fadeOut(200);
                    }
                    else{
                        alert('Error deleting file');
                    }
                }
            })
            return false;
        }
	};
    $('.remove-file').livequery('click', removeFile);

	$('#upload-trigger').click(function(){
        if(!($(this).hasClass('opened'))){
            $(this).find('img').attr('src', 'images/arrow_yellow_down.gif');
            $(this).addClass('opened').find('img').css({marginTop: '3px'});
        }
        else{
            $(this).find('img').attr('src', 'images/arrow_yellow_right.gif');
            $(this).removeClass('opened').find('img').css({marginTop: '1px'});;
        }
        $('.upload-area').toggle(120);
        return false;
	});

	$('#buttonUpload').click(function(){
        ajaxFileUpload();
        return false;
	});

});

function ajaxFileUpload(){
    $.ajaxFileUpload({
	    url:'bin/doajaxfileupload.php',
		secureuri:false,
		fileElementId:'fileToUpload',
		dataType: 'json',
		success: function (data, status){
		    if(typeof(data.error) != 'undefined'){
			    if(data.error != ''){
				    alert(data.error);
				}
                else{
				    //alert(data.msg);
                    $("#uploaded-files ul li.last-list").before("<li class='newly-added'>"+data.msg+"</li>");
                    $("#fileToUpload").val("");
				}
			}
		},
		error: function (data, status, e){
		    alert(e);
		}
	})
    return false;
}
