	$(function(){		
		var btnUpload=$('#upload');
		var status=$('#status');
		new AjaxUpload(btnUpload, {
			action: 'ajax.upload-file.php',
			name: 'uploadfile',
			onSubmit: function(file, ext){
				//alert(ext);
				 if (! (ext && /^(jpg|png|jpeg|gif|pdf|txt|doc|docx|eps|ai)$/.test(ext))){ 
                    // extension is not allowed 
					status.text('Only JPG, PNG, GIF, TXT, PDF, DOC files are allowed');
					return false;
				}					
				status.text('Uploading...');
				$('#loader').show();
			},
			onComplete: function(file, response){
				//alert(response);
				//On completion clear the status
				status.text('');
				$('#loader').hide();
				//Add uploaded file to list
				if(response != "error"){
					//alert(response);
					myArray = response.split("|");
					var fileName = myArray[0];
					var altFileName = myArray[2];
					var imageNumber = myArray[1];
					if(imageNumber >= 4){
						$('#upload').hide();
					}
					if (LimitAttach(fileName) == false ){ 
						
						$('<li></li>').appendTo('#files').html('<a target="_blank" href="./contestImage/'+response+'">'+altFileName+'</a><br /><a href="javascript:void(0)" onclick="javascript:ajax_remove('+imageNumber+')">Remove</a>').addClass('successJQ');
					
					} else {
						$('<li></li>').appendTo('#files').html('<img src="./contestImage/thumb/'+fileName+'" alt="" /><br /><a href="javascript:void(0)" onclick="javascript:ajax_remove('+imageNumber+')">Remove</a>').addClass('successJQ');
					}				
				} else{
					$('<li></li>').appendTo('#files').text("Can Not Upload More Then 4 Images").addClass('errorJQ');
				}
			}
		});
		
	});
	
	
	

function LimitAttach(file) {
    extArray = new Array(".jpg", ".png", ".eps", ".gif", ".jpeg", ".ai");
	allowSubmit = false;
    if (!file) return;
    while (file.indexOf("\\") != -1)
	    file = file.slice(file.indexOf("\\") + 1);
    
	ext = file.slice(file.indexOf(".")).toLowerCase();
    for (var i = 0; i < extArray.length; i++) {
		if (extArray[i] == ext) { 
			allowSubmit = true; break; 
		} 
    }
	return allowSubmit;   
}	

	


