

var Upload={

	services_count:null,
	file_label_highlight_on:'#FFFFE0',
	file_label_highlight_off:'#F9F9F9',
	enable_upload:null,
	using_cgi:null,
	path_to_link_script:null,
	check_allow_extensions_on_client:null,
	check_disallow_extensions_on_client:null,
	allow_extensions:null,
	disallow_extensions:null,
	check_file_name_format:null,
	check_file_name_regex:null,
	check_file_name_error_message:null,
	max_file_name_chars:null,
	max_upload_size:null,
	min_file_name_chars:null,
	check_null_file_count:null,
	check_duplicate_file_count:null,
	block_ui_enabled:null,
	show_files_uploaded:null,
	remote_table_class_changer:0,
	form_count:1,
	current_upload_form_name:null,
	current_upload_form_number:0,
	current_upload_file_name:null,
	max_fetching_file:null,
	max_upload_slots:null,
	progressbar_images:[],
	current_pr_num:3,
	http_host:null,
	path_to_upload:null,
	limit_services_error:50,
	enable_remote_upload:null,
	flash_support:false,
	post_params:{},		
	getFileName:function(slot_value){
		var index_of_last_slash = slot_value.lastIndexOf("\\");

		if(index_of_last_slash < 1){ index_of_last_slash = slot_value.lastIndexOf("/"); }

		var file_name = slot_value.slice(index_of_last_slash + 1, slot_value.length);

		return file_name;
	},

	getFileExtension:function(slot_value){
		if(slot_value.lastIndexOf('.') == -1)
		{
			return '';
		}	
		var file_extension = slot_value.substring(slot_value.lastIndexOf('.') + 1, slot_value.length).toLowerCase();

		return file_extension;
	},

	highlightFileLabel:function(file_label, color){ JQ("#"+file_label).css({background:color}); },

	clearFileLabels:function(){
		JQ(":file").each(function(){
			Upload.highlightFileLabel(JQ(this).attr('id')+"_label", Upload.file_label_highlight_off);
		});
	},

	// Check the file format before uploading
	checkFileNameFormat:function(){
		if(!Upload.check_file_name_format){ return false; }

		var found_error = false;

		JQ(":file").each(function(){
			if(JQ(this).val() !== ""){
				var file_name = Upload.getFileName(JQ(this).val());

				if(file_name.length > Upload.max_file_name_chars){
					Upload.highlightFileLabel(JQ(this).attr('id')+"_label", Upload.file_label_highlight_on);
					Upload.showAlert("Error, file name cannot be more than " + Upload.max_file_name_chars + " characters.", 500, 85, Upload.block_ui_enabled);
					found_error = true;
				}

				if(file_name.length < Upload.min_file_name_chars){
					Upload.highlightFileLabel(JQ(this).attr('id')+"_label", Upload.file_label_highlight_on);
					Upload.showAlert("Error, file name cannot be less than " + Upload.min_file_name_chars + " characters.", 500, 85, Upload.block_ui_enabled);
					found_error = true;
				}

				if(!Upload.check_file_name_regex.test(file_name)){
					Upload.highlightFileLabel(JQ(this).attr('id')+"_label", Upload.file_label_highlight_on);
					Upload.showAlert(Upload.check_file_name_error_message, 500, 85, Upload.block_ui_enabled);
					found_error = true;
				}
			}
		});

		return found_error;
	},

	// Check for legal file extentions
	checkAllowFileExtensions:function(){
		if(!Upload.check_allow_extensions_on_client){ return false; }

		var found_error = false;

		JQ(":file").each(function(){
			if(JQ(this).val() !== ""){
				var file_extension = Upload.getFileExtension(Upload.getFileName(JQ(this).val()));

				if(!file_extension.match(Upload.allow_extensions)){
					Upload.highlightFileLabel(JQ(this).attr('id')+"_label", Upload.file_label_highlight_on);
					Upload.showAlert('Sorry, uploading a file with the extension "' + file_extension + '" is not allowed.', 500, 85, Upload.block_ui_enabled);
					found_error = true;
				}
			}
		});

		return found_error;
	},
	// Check for legal file extentions
	checkEmptyFileExtensions:function(){
		var found_error = false;

		JQ(":file").each(function(){
			if(JQ(this).val() !== ""){
				var file_extension = Upload.getFileExtension(Upload.getFileName(JQ(this).val()));
				
				if(file_extension == '')
				{
					Upload.showAlert('Sorry, uploading a file without extension is not allowed.', 500, 85, Upload.block_ui_enabled);
					found_error = true;
				}
			}
		});

		return found_error;
	},
	// Check for illegal file extentions
	checkDisallowFileExtensions:function(){
		if(!Upload.check_disallow_extensions_on_client){ return false; }

		var found_error = false;

		JQ(":file").each(function(){
			if(JQ(this).val() !== ""){
				var file_extension = Upload.getFileExtension(Upload.getFileName(JQ(this).val()));

				if(file_extension.match(Upload.disallow_extensions)){
					Upload.highlightFileLabel(JQ(this).attr('id')+"_label", Upload.file_label_highlight_on);
					Upload.showAlert('Sorry, uploading a file with the extension "' + file_extension + '" is not allowed.', 500, 85, Upload.block_ui_enabled);
					found_error = true;
				}
			}
		});

		return found_error;
	},

	// Make sure the user selected at least one file
	checkNullFileCount:function(){
		if(!Upload.check_null_file_count){ return false; }

		var found_file = false;

		JQ(":file").each(function(){
			if(JQ(this).val() !== ""){ found_file = true; }
		});

		if(!found_file){
			Upload.showAlert("Please Choose A File To Upload.", 400, 80, Upload.block_ui_enabled);
			return true;
		}
		else{ return false; }
	},

	// Make sure the user is not uploading duplicate files
	checkDuplicateFileCount:function(){
		if(!Upload.check_duplicate_file_count){ return false; }
		var found_duplicate_file_name = false;
		var found_duplicate = false;
		var file_count = 0;
		var file_name_array = [];

		JQ(":file").each(function(){
			if(JQ(this).val() !== ""){
				var obj = {};
				obj.file_name = Upload.getFileName(JQ(this).val());
				obj.label_name = JQ(this).attr('id')+"_label";
				file_name_array[file_count] = obj;
				file_count++;
			}
		});

		for(var i = 0; i < file_name_array.length; i++){
			var obj_1 = file_name_array[i];

			for(var j = 0; j < file_name_array.length; j++){
				var obj_2 = file_name_array[j];

				if(obj_1.file_name === obj_2.file_name && i !== j){
					found_duplicate = true;
					found_duplicate_file_name = obj_1.file_name;
				}
			}
		}

		if(found_duplicate){
			Upload.showAlert("Duplicate upload files detected.<br>"+found_duplicate_file_name, 400, 80, Upload.block_ui_enabled);
			return true;
		}
		else{ return false; }
	},
	// Make sure the user is not uploading duplicate remote url file
	checkRemoteURLUpload:function(){
		var notfound_http = false;
		var found_duplicate = false;
		var found_duplicate_url_name = false;
		var url_count = 0;
		var url_name_array = JQ('#file_url').val().split('\n');
		
		for(var i = 0; i < url_name_array.length; i++)
		{
			var obj_1 = url_name_array[i];
			var httpPosition=obj_1.indexOf('http://'); 
			if(httpPosition != 0  && obj_1 !=="")
			{
				notfound_http = true;
			}
			if(obj_1.length>1)
				url_count++;
			
			for(var j = 0; j < url_name_array.length; j++)
			{
				var obj_2 = url_name_array[j];

				if(obj_1 === obj_2 && i !== j && obj_1 !=="" )
				{
					found_duplicate_url_name =  obj_1;
					found_duplicate = true;
				}
			}
		}

		if(found_duplicate){
			Upload.showAlert("Duplicate URL detected.<br>"+found_duplicate_url_name, 400, 80, Upload.block_ui_enabled);
			return true;
		}
		if(notfound_http){
			Upload.showAlert("All URL`s Should Strat With http://", 400, 80, Upload.block_ui_enabled);
			return true;
		}
		if(url_count > Upload.max_fetching_file)
		{
			Upload.showAlert("You Can Enter "+Upload.max_fetching_file+" URL`s only", 400, 110, Upload.block_ui_enabled);
			return true;
		}
		//check enter 1 url
		if(url_count==0)
		{
			Upload.showAlert("Please Enter A URL To Upload.", 400, 80, Upload.block_ui_enabled);
			return true;
		}		
		
		return false;
	},
	showAlert:function(alert_message, alert_width, alert_height, block_ui_enabled){
		if(!block_ui_enabled){ alert(alert_message); }
		else{
			alert_message = '<br>' + alert_message + "<br><br><input style='width:75px;' type='button' id='ok_btn' name='ok' value='OK' onClick='JQ.unblockUI();'>";

			JQ.blockUI({
				message:alert_message,
				css:{
					width:alert_width+'px',
					height:alert_height+'px',
					top:(JQ(window).height() / 3) - (alert_height / 2) + 'px',
					left:(JQ(window).width() / 2) - (alert_width / 2) + 'px',
					textAlign:'center',
					cursor:'default',
					backgroundColor:'#EDEDED',
					borderColor:'#D9D9D9',
					color:'black',
					font:'14px Arial',
					fontWeight:'bold',
					padding:'2px',
					opacity:'1',
					'-webkit-border-radius':'2px',
					'-moz-border-radius':'2px'
				},
				overlayCSS:{
					cursor:'default',
					applyPlatformOpacityRules:true
				}
			});
		}
	},
	// check file descripion length
	checkFileDescripionLength:function(){
		var descripionStr = JQ("#descripion").val();

		if(descripionStr.length>255)
		{
			Upload.showAlert('Sorry,file description should not be more than 255 characters ', 500, 85, Upload.block_ui_enabled);
			return true;
		}
		else
			return false;

	},
	// check service
	checkService:function(){
		
		for (var x = 1; x <= Upload.services_count; x++)
		{
			if(JQ("#service"+x).is(':checked')==true)
				return false;
		}
		
		Upload.showAlert('Sorry,you should choose at least one service ', 500, 85, Upload.block_ui_enabled);
		return true;

	},
	setServicesCookie:function(){
		var cookieName = 'SelectedServices';
		var cookieValue = '';
		for (var x = 1; x <= Upload.services_count; x++)
		{
			if(JQ("#service"+x).is(':checked')==true)
			{
				cookieValue = cookieValue + JQ("#service"+x).attr("name").toLowerCase() + ',';
				
				//***set account detail in cookie
				if(JQ("#"+JQ("#service"+x).attr("name")+"_login_remember").is(':checked')==true)
				{
					if(JQ("#"+JQ("#service"+x).attr("name")+"_login_username").val() != '' && JQ("#"+JQ("#service"+x).attr("name")+"_login_password").val() != '')
					{
						var login_username_cookieName = JQ("#service"+x).attr("name")+"_login_username";
						var login_username_cookieValue = JQ("#"+JQ("#service"+x).attr("name")+"_login_username").val();
						JQ.cookie(login_username_cookieName,login_username_cookieValue, { expires: 90 });
						var login_password_cookieName = JQ("#service"+x).attr("name")+"_login_password";
						var login_password_cookieValue = JQ("#"+JQ("#service"+x).attr("name")+"_login_password").val();
						JQ.cookie(login_password_cookieName,login_password_cookieValue, { expires: 90 });
					}
				}
				else
				{
					var login_username_cookieName = JQ("#service"+x).attr("name")+"_login_username";
					JQ.cookie(login_username_cookieName,null);
					var login_password_cookieName = JQ("#service"+x).attr("name")+"_login_password";
					JQ.cookie(login_password_cookieName,null);
				}
			}
		}
		if(cookieValue != '')
		{
			JQ.cookie(cookieName,cookieValue, { expires: 90 });
			return false;
		}
		else
		{
			Upload.showAlert('Cookie error ', 500, 85, Upload.block_ui_enabled);
			return true;
		}

	},
	// check service
	checkWebSite:function(){
		var siteStr = JQ("#site").val();
		if(siteStr == "")
			return false;
		if(siteStr == "http://")
			return false;
		if((siteStr.length > 40) || (siteStr.indexOf(".") <0))
		{
			Upload.showAlert('Sorry,your site address is invalid ', 500, 85, Upload.block_ui_enabled);
			return true;
		}
		else
		{
			return false;
		}
	},
	checkEmail:function(){
		var emailStr = JQ("#email").val();
		var filter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		if(emailStr == "")
			return false;
		if((emailStr.length > 50) || !filter.test(emailStr))
		{
			Upload.showAlert('Sorry,your email address is invalid ', 500, 85, Upload.block_ui_enabled);
			return true;
		}
		else
		{
			return false;
		}
	},	
	checkEnableUpload:function(){
		if(Upload.enable_upload==0)
		{
			Upload.showAlert('Sorry,Upload file temporarily unavailable ', 500, 85, Upload.block_ui_enabled);
			return true;
		}
		else
		{
			return false;
		}
	},
	checkEnableRemoteUpload:function(){
		if(Upload.enable_remote_upload==0)
		{
			Upload.showAlert('Sorry,Remote upload temporarily unavailable ', 500, 85, Upload.block_ui_enabled);
			return true;
		}
		else
		{
			return false;
		}
	},	
	checkFileSelected:function(){
		if(swfu.getStats().files_queued == 0)
		{
			Upload.showAlert("Please Choose A File To Upload.", 400, 80, Upload.block_ui_enabled);
			return true;
		}
		else
		{
			return false;
		}
	},		
	// change the file upload page
	changeFileUploadPage:function(way){
		if(way=='remote')
		{
			JQ("#upload_button_statues").val('remote');
			JQ("#upload_button_statues").html('<a id="upload_button" class="ovalbutton" href="#" OnClick="return Upload.linkRemoteUpload();"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Upload&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></a>');	
			JQ("#upload_form_file_text").html('URL :');
			JQ("#upload_form_file_input").html('Enter a URL on each line. Up to 3 URL\'s ONLY.<br><textarea id="file_url" name="urls" class="textfield" cols="60" rows="6"></textarea>');
			//JQ("#upload_form_site_input").fadeOut("slow");
			//JQ("#upload_form_site_text").fadeOut("slow");
			JQ("#upload_multiple_files").fadeOut("slow");
			JQ("#new_upload_form").html('');
			JQ("#upload_form_email_text").fadeOut("slow");
			JQ("#upload_form_email_input").fadeOut("slow");	
			
			JQ("#upload_form_file_ButtonPlaceHolder").fadeOut("slow");
		}
		else if(way=='file')
		{
			JQ("#upload_button_statues").val('file');
			JQ("#upload_button_statues").html('<a id="upload_button" class="ovalbutton" href="#" OnClick="return Upload.linkUpload();"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Upload&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></a>');				
			JQ("#upload_form_file_text").html('file:');
			JQ("#upload_form_file_input").html('<input type="file" name="file_1" class="textfield" size="30">');
			//JQ("#upload_form_site_input").fadeIn("slow");
			//JQ("#upload_form_site_text").fadeIn("slow");			
			JQ("#upload_multiple_files").fadeIn("slow");	
			JQ("#upload_form_email_text").fadeOut("slow");
			JQ("#upload_form_email_input").fadeOut("slow");	
			
			JQ("#upload_form_file_ButtonPlaceHolder").fadeOut("slow");
		}
		else if(way=='flash')
		{
			if (!Upload.flash_support) 
			{
				Upload.showAlert('You need the Flash Player 9.028 or above to use flash uploader(advanced uploader).', 650, 85, Upload.block_ui_enabled);
			}
			else
			{
				JQ("#upload_button_statues").val('flash');
				JQ("#upload_button_statues").html('<a id="upload_button" class="ovalbutton" href="#" OnClick="return Upload.linkFlashUpload();"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Upload&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></a>');				
				JQ("#upload_form_file_text").html('Select file(s):');
				JQ("#upload_form_file_input").html('');
				JQ("#upload_multiple_files").fadeOut("slow");
				JQ("#upload_form_email_text").fadeIn("slow");
				JQ("#upload_form_email_input").fadeIn("slow");
				
				JQ("#upload_form_file_ButtonPlaceHolder").fadeIn("slow");
			}
			
		}			
	},	
	// Link the upload
	linkRemoteUpload:function(){
		if(Upload.checkEnableUpload()){ return false; }
		if(Upload.checkEnableRemoteUpload()){ return false; }
		if(Upload.checkRemoteURLUpload()){ return false; }
		if(Upload.checkFileDescripionLength()){ return false; }
		if(Upload.checkWebSite()){ return false; }
		if(Upload.checkService()){ return false; }
		if(Upload.setServicesCookie()){ return false; }
		
		
		JQ("#upload_button").fadeOut("slow");
		JQ.getScript("lib/GenerateUnqid.php", function(){});
	},
	// Link the upload
	linkFlashUpload:function(){
		if(Upload.checkEnableUpload()){ return false; }
		if(Upload.checkFileSelected()){ return false; }
		if(Upload.checkFileDescripionLength()){ return false; }
		if(Upload.checkWebSite()){ return false; }
		if(Upload.checkService()){ return false; }
		if(Upload.setServicesCookie()){ return false; }
		if(Upload.checkEmail()){ return false; }
		var test={};
		JQ.each(JQ('#upload_form_1').serializeArray(), function(i, field) {
			Upload.post_params[field.name] = field.value;
		});
		
		//alert('inja');
		//JQ.each(Upload.post_params, function(i, field) {
			//alert(i+'='+field);
		//});
		
		swfu.setPostParams(Upload.post_params);		
		
		JQ("#upload_button").fadeOut("slow");
		JQ.getScript("lib/GenerateUnqid.php", function(){});
		return false;
	},		
	// Link the upload
	linkUpload:function(){
		if(Upload.checkEnableUpload()){ return false; }
		if(Upload.check_file_name_format || Upload.check_allow_extensions_on_client || Upload.check_disallow_extensions_on_client || Upload.check_duplicate_file_count){ Upload.clearFileLabels(); }
		if(Upload.checkFileNameFormat()){ return false; }
		if(Upload.checkEmptyFileExtensions()){ return false; }
		if(Upload.checkAllowFileExtensions()){ return false; }
		if(Upload.checkDisallowFileExtensions()){ return false; }
		if(Upload.checkNullFileCount()){ return false; }
		if(Upload.checkDuplicateFileCount()){ return false; }
		if(Upload.checkFileDescripionLength()){ return false; }
		if(Upload.checkWebSite()){ return false; }
		if(Upload.checkService()){ return false; }
		if(Upload.setServicesCookie()){ return false; }
		if(Upload.set_current_upload_form_name()){ return false; }
		
		JQ("#upload_button").fadeOut("slow");
		JQ.getScript("lib/GenerateUnqidAndAction.php", function(){});

		return false;
	},
	// Link the upload
	set_current_upload_form_name:function(){
		
		var check_exist_form_number = Upload.current_upload_form_number + 1;	
		
		//****change progress image****************
		if(check_exist_form_number>1)
		{
			Upload.current_pr_num = Math.floor(Upload.progressbar_images.length * (Math.random() % 1));
		}
		//*************************************
		if(JQ('#upload_form_'+check_exist_form_number).html() != null)
		{
			//check choose a file to upload
			if(JQ('#upload_form_'+check_exist_form_number+' :file').val()!='')
			{
				//set file name************************
				Upload.current_upload_file_name = Upload.getFileName(JQ('#upload_form_'+check_exist_form_number+' :file').val());
				//************************************
				Upload.current_upload_form_number++;
				Upload.current_upload_form_name = 'upload_form_'+Upload.current_upload_form_number;
				//alert(Upload.current_upload_form_number+Upload.current_upload_form_name);
				return false
			}
			else
			{
				Upload.current_upload_form_number++;
				Upload.current_upload_form_name = 'upload_form_'+Upload.current_upload_form_number;
				//**trigger upload for next form
				Upload.linkUpload();
				return true;
			}
		}
		else
		{
			//Upload.showAlert('error,set currnent upload from name', 500, 85, Upload.block_ui_enabled);
			return true;
		}
	},
	add_new_form:function(){
		if((Upload.form_count+1)>Upload.max_upload_slots)
		{
			Upload.showAlert("Up to "+Upload.max_upload_slots+" File's ONLY", 400, 80, Upload.block_ui_enabled);
		}
		else
		{
			Upload.form_count++;
			var form_name = 'upload_form_'+Upload.form_count;
		
			JQ('#new_upload_form').append('<form id="'+form_name+'" name="'+form_name+'" method="post" enctype="multipart/form-data" class="categories"></form>');
			JQ('#'+form_name).append('<div id="'+form_name+'_services"></div>');
	
			if(jQuery.browser.msie==true)
			{
				JQ('#'+form_name+'_services').append(JQ('#service_should_add_new_form').html());
			}
			else
			{
				var a = document.getElementById('service_should_add_new_form').cloneNode(1);
				JQ('#'+form_name+'_services').append(a)
			}
			
			JQ('#'+form_name).append(JQ('#should_add_new_form').html());
		}
	},
		//Submit the upload form
	startUpload:function(upload_id){
		//Upload.resetUploadDiv();
		var upload_button_statues = JQ("#upload_button_statues").attr('value');
		
		if(upload_button_statues=='file')
		{
			//JQ("#upload_button_statues").val('Stop');
			JQ("#upload_button").html("<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Stop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>");
			
			var iframe_name = "upload_iframe_" + upload_id;
			JQ("#upload_container").html("<iframe name='"+iframe_name+"' id=\"iframe_upload\" height=\"0\" width=\"0\" style=\"visibility:hidden\" frameborder='0' scrolling='no'></iframe>");
			JQ("#"+Upload.current_upload_form_name).attr("target", iframe_name);
			
			if(Upload.using_cgi==1)
			{
				JQ("#"+Upload.current_upload_form_name).attr("action", Upload.http_host + Upload.path_to_upload + "?upload_id=" + upload_id);
			}
			else
			{
				JQ("#"+Upload.current_upload_form_name).attr("action", "lib/upload.php");
				JQ("#UNIQ_ID").html("<input type=\"hidden\" name=\"APC_UPLOAD_PROGRESS\" id=\"progress_key\" value=\"\" ><input type=\"hidden\" name=\"APC_UPLOAD_PROGRESS\" id=\"progress_key\" value=\"\" ><input type=\"hidden\" name=\"APC_UPLOAD_PROGRESS\" id=\"progress_key\" value=\"\" ><input type=\"hidden\" name=\"APC_UPLOAD_PROGRESS\" id=\"progress_key\" value=\""+upload_id+"\" >");
			}
			
			//*** hide all upload form*****************************
			JQ("#new_upload_form").fadeOut("slow");
			JQ("#upload_form_1").fadeOut("slow");	
			//*****************************************************
			
			JQ("#progress_text").html('<br><br>Uploading <font style="font-size:20px;font-style:italic;color:red;">'+Upload.current_upload_file_name+'</font><br><br>');
			
			JQ("#pb4").attr('style','visibility:show;');
			JQ("#pb4").progressBar(0, { showText: true, barImage: 'images/'+Upload.progressbar_images[Upload.current_pr_num]} );
							
			JQ("#"+Upload.current_upload_form_name).submit();
			
			//if(jQuery.browser.msie==true)
			//{
				//JQ("#upload_button").fadeIn("fast");
			//}
			setTimeout("Upload.getProgressStatus('"+upload_id+"');",2000);
		}
		else if(upload_button_statues=='remote')
		{
			var iframe_name = "upload_iframe_" + upload_id;
			JQ("#upload_container").html("<iframe name='"+iframe_name+"' id=\"iframe_upload\" height=\"0\" width=\"0\" style=\"visibility:hidden\" frameborder='0' scrolling='no'></iframe>");
			JQ("#upload_form_1").attr("target", iframe_name);
			
			JQ("#upload_form_1").attr("action", "lib/remote.php?upload_id=" + upload_id);
			
			JQ("#upload_form_1").fadeOut("slow");
			
			JQ("#remote_table").attr('style','visibility:show;');
			//JQ("#pb4").progressBar(0, { showText: true, barImage: 'images/'+Upload.progressbar_images[Upload.current_pr_num]} );
			
			JQ("#upload_form_1").submit();
			
			//setTimeout("Upload.getProgressStatus_remote('"+upload_id+"');",2000);
			
		}
		else if(upload_button_statues=='flash')
		{
			JQ("#distribute_text").fadeOut("slow");
			JQ("#service_should_add_new_form").fadeOut("slow");
			JQ("#switch_upload_way").fadeOut("slow");
			JQ("#upload_multiple_files").fadeOut("slow");
			JQ("#upload_form_file_text").fadeOut("slow");
			JQ("#upload_form_file_input").fadeOut("slow");
			JQ("#upload_form_site_text").fadeOut("slow");
			JQ("#upload_form_site_input").fadeOut("slow");
			JQ("#upload_form_descripion_text").fadeOut("slow");
			JQ("#upload_form_descripion_input").fadeOut("slow");
			JQ("#upload_form_email_text").fadeOut("slow");
			JQ("#upload_form_email_input").fadeOut("slow");
			JQ("#upload_form_size_text1").fadeOut("slow");
			JQ("#upload_form_size_text2").fadeOut("slow");

			JQ("#upload_form_file_ButtonPlaceHolder").attr('style','visibility:hidden;');
			//swfu.setButtonDisabled(true);
			
			JQ('#pb4_detail').attr('style','visibility:show;');
			JQ('#pb4_detail').attr('style','display:block;');
			
			swfu.post_params = Upload.post_params;
			swfu.startUpload();
			
		}
	},

	// Stop the upload
	stopUpload:function(){
	
		JQ("#upload_button_statues").val('Upload');
		JQ("#upload_button").html("<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Upload&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>");
		
		try{ document.execCommand('Stop'); }
		catch(e){}
		try{ window.stop(); }
		catch(e){}
		
		JQ("#upload_container").html('');
		
		//*** show all upload form*****************************
		JQ("#new_upload_form").fadeIn("slow");
		JQ("#upload_form_1").fadeIn("slow");	
		//*****************************************************
		JQ("#progress_text").html('');
		
		JQ("#pb4").progressBar(0, { showText: true, barImage: 'images/'+Upload.progressbar_images[Upload.current_pr_num]} );
		JQ("#pb4").attr('style','visibility:hidden;');
		
		JQ("#upload_button").fadeIn("fast");
		
	},
	
	// Get the progress of the upload
	getProgressStatus:function(upload_id){
		if(Upload.using_cgi==1)
		{
			JQ.getScript(Upload.http_host + "lib/getprogress_cgi.php?upload_id="+upload_id + "&rand=" + Math.floor(Math.random()*99999), function(){});
		}
		else
		{
			JQ.getScript(Upload.http_host + "lib/getprogress_apc.php?upload_id="+upload_id + "&rand=" + Math.floor(Math.random()*99999), function(){});
		}
	},
	setProgressStatus_cgi:function(SR,total,upload_id,mylink){		
		SR=Math.round(SR);
		
		if(total==0 && SR==0)
		{
			setTimeout("Upload.getProgressStatus('"+upload_id+"');",2500);
		}
		else if(total>Upload.max_upload_size)
		{	
			total=((Upload.max_upload_size/1024)/1024);
			Upload.showAlert('Sorry,your file size should be less than '+total, 500, 85, Upload.block_ui_enabled);
			Upload.stopUpload();
		}
		else
		{

			JQ("#pb4").progressBar(SR, { showText: true, barImage: 'images/'+Upload.progressbar_images[Upload.current_pr_num]} );
			if(SR<100)
			{
				setTimeout("Upload.getProgressStatus('"+upload_id+"');",2500);
			}
			else if(SR==100)
			{		
				setTimeout("Upload.endUpload_cgi('"+mylink+"');",2000);
			}

		}
	},
	setProgressStatus_apc:function(SR,total,upload_id,cancel_upload){
		SR=Math.round(SR);
		
		if(total>Upload.max_upload_size)
		{	
			total=((Upload.max_upload_size/1024)/1024);
			Upload.showAlert('Sorry,your file size should be less than '+total, 500, 85, Upload.block_ui_enabled);
			Upload.stopUpload();
		}
		else
		{
			if(cancel_upload==1)
			{	
				alert('Upload Cancelled');
				
			}
			else
			{
				JQ("#pb4").progressBar(SR, { showText: true, barImage: 'images/'+Upload.progressbar_images[Upload.current_pr_num]} );
				if(SR<100 && cancel_upload==0)
				{
					setTimeout("Upload.getProgressStatus('"+upload_id+"');",1500);
				}
				else if(SR==100)
				{	
					
					setTimeout("Upload.endUpload_apc();",2000);
				}
			}
		}
		
	},
	endUpload_cgi:function(mylink){	
				var msg='Upload Successful (to our server) Upload to the following services can take several minutes, depending on the size of the file.(minimum 5 minutes)';
				var link='http://www.embedupload.com/?d='+mylink;
				var uniqid = new Date().getTime();
				JQ('#mylink').append("<table width=\"100%\" height=\"70\"><tr><td align=\"left\" valign=\"center\">Here is<font style=\"font-size:20px;font-style:italic;color:red;\">   "+Upload.current_upload_file_name+"   </font> download link: [<a href=\"javascript:copyText(document.mylinkform_"+uniqid+".downloadLink_"+uniqid+")\">copy</a>]</td></tr><tr align=\"center\"><td><form name=\"mylinkform_"+uniqid+"\" id=\"mylinkform\"><input type=\"hidden\" name=\"downloadLink_"+uniqid+"\" value=\""+link+"\"><b><a href=\""+link+"\" style=\"font-size:25px;font-style:italic\" target=\"_blank\" >"+link+"</a></b></form></td></tr></table>");
				
				JQ("#upload_button").fadeOut("slow");
				JQ("#pb4").progressBar(0, { showText: true, barImage: 'images/'+Upload.progressbar_images[Upload.current_pr_num]} );
				JQ("#pb4").attr('style','visibility:hidden;');
				JQ("#progress_text").html('<br><br>');
				//Upload.showAlert(msg, 500, 110, Upload.block_ui_enabled);
				Upload.linkUpload();
		
	},
	endUpload_apc:function(){
				//alert(JQ('#iframe_upload').contents().find('#msg').html());
				msq=JQ('#iframe_upload').contents().find('#msg').html();
				link=jQuery.trim(JQ('#iframe_upload').contents().find('#link').html());
				
				if(msq==null || link==null || link=='')
				{
					setTimeout("Upload.endUpload_apc();",2000);
				}
				else
				{
					var link=jQuery.trim(JQ('#iframe_upload').contents().find('#link').html());
					JQ('#mylink').html("<table width=\"100%\"><tr><td align=\"left\" valign=\"center\">Here is your download link: [<a href=\"javascript:copyText(document.mylinkform.downloadLink)\">copy</a>]</td></tr><tr align=\"center\"><td><form name=\"mylinkform\" id=\"mylinkform\"><input type=\"hidden\" name=\"downloadLink\" value=\""+link+"\"><b><a href=\""+link+"\" style=\"font-size:25px;font-style:italic\">"+link+"</b></form></td></tr></table>");
					
					JQ("#upload_button").fadeOut("slow");
					JQ("#pb4").attr('style','visibility:hidden;');
					Upload.showAlert(msq, 500, 110, Upload.block_ui_enabled);
				}
		
	},
	endUpload_remote:function(mylink,upload_id){
		JQ('#status_'+upload_id).html('Completed');
		var download_link = 'http://www.embedupload.com/?d='+mylink;
		JQ('#mylink_'+upload_id).html("<a href=\""+download_link+"\" target=\"_blank\"><b>"+download_link+"</b></a>");		
	},
	add_remote_table_row:function(link,upload_id){
		if(Upload.remote_table_class_changer % 2==0)
		{	
			JQ("#remote_table").append('<tr><td scope="row">'+link+'</td><td><span class="progressBar" id="status_'+upload_id+'">pending</span></td><td class="spec"><div id="mylink_'+upload_id+'">...</div></td></tr>');
		}
		else
		{	
			JQ("#remote_table").append('<tr><td scope="row" class="alt">'+link+'</td><td class="alt"><span class="progressBar" id="status_'+upload_id+'">pending</span></td><td class="specalt"><div id="mylink_'+upload_id+'">...</div></td></tr>');
		}
		Upload.remote_table_class_changer++;
		
		//setTimeout("Upload.getProgressStatus_remote('"+upload_id+"');",2000);

	},
	initialize_remote:function(json){
		//JQ("#remote_table").attr('style','visibility:show;');
		var upload_id ='';
		var uploadresult	 = eval(" (" + json + ") ");
		var i=0;
		
		while(i < uploadresult.length)
		{
			Upload.add_remote_table_row(uploadresult[i].link,uploadresult[i].upload_id);
			upload_id += uploadresult[i].upload_id + ';';
			i++;
		}
		
		setTimeout("Upload.getProgressStatus_remote('"+upload_id+"');",2000);
	
	},
	setProgressStatus_remote:function(json){		
		
		var progress	 = eval(" (" + json + ") ");
		var stillinprogress = false;
		var upload_id ='';
		for(i=0;i < progress.length; i++) 
		{	
			if(progress[i].error!=0)
			{
				JQ('#status_'+progress[i].upload_id).html(progress[i].error);
			}
			else if(progress[i].total==0)
			{
				stillinprogress = true;
			}
			else if(progress[i].total>Upload.max_upload_size)
			{	
				JQ('#status_'+progress[i].upload_id).html('Sorry,your file size should be less than');			
			}
			else
			{
				progress[i].percent=Math.round(progress[i].percent);
				
				JQ('#status_'+progress[i].upload_id).progressBar(progress[i].percent, { showText: true, barImage: 'images/'+Upload.progressbar_images[Upload.current_pr_num]} );
				if(progress[i].percent<100)
				{
					stillinprogress = true;
				}
				else if(progress[i].percent==100)
				{		
					JQ('#status_'+progress[i].upload_id).html('Completed');
					var download_link = 'http://www.embedupload.com/?d='+progress[i].mylink;
					JQ('#mylink_'+progress[i].upload_id).html("<a href=\""+download_link+"\" ><b>"+download_link+"</b></a>");
				}
	
			}
		
			upload_id += progress[i].upload_id + ';';
		}
		if (stillinprogress) 
		{  
			setTimeout("Upload.getProgressStatus_remote('"+upload_id+"');",2000);  
		}
		else
		{
			//alert('end alii');
		}
	},
	// Get the progress of the remote upload
	getProgressStatus_remote:function(upload_id){

		JQ.getScript(Upload.http_host + "lib/getprogress_remote.php?upload_id="+upload_id + "&rand=" + Math.floor(Math.random()*99999), function(){});

	},
	// Get the progress of the services
	getProgressStatus_services:function(upload_id,services){
		JQ.getScript(Upload.http_host + "lib/getprogress_services.php?upload_id="+upload_id + "&services="+services+"&rand=" + Math.floor(Math.random()*99999), function(){});
	},
	setProgressStatus_services:function(json){
		var progress	 = eval(" (" + json + ") ");
		var stillinprogress = false;
		var services ='';
		for(i=0;i < progress.length; i++) 
		{	
			if(progress[i].error!=0)
			{
				Upload.limit_services_error--;
				if(Upload.limit_services_error>0)
				{
					stillinprogress = true;
					services += progress[i].service + ';';
				}				
				//JQ('#'+progress[i].service+'_sr').html(progress[i].error);
			}
			else if(progress[i].total==0)
			{
				stillinprogress = true;
				services += progress[i].service + ';';
			}
			else
			{
				progress[i].percent=Math.round(progress[i].percent);
				
				JQ('#'+progress[i].service+'_sr').html(progress[i].percent+'%');
				if(progress[i].percent<100)
				{
					stillinprogress = true;
					services += progress[i].service + ';';
				}
				else if(progress[i].percent==100)
				{		
					JQ('#'+progress[i].service.toLowerCase()).html('<a href=\"http://www.embedupload.com/?'+JQ('#'+progress[i].service+'_acronym').val()+'='+progress[i].upload_id+'\" target=\"_blank\">Download Now</a>');
				}
	
			}
			
			
		}
		
		if (stillinprogress) 
		{  
			setTimeout("Upload.getProgressStatus_services('"+progress[i-1].upload_id+"','"+services+"');",2000);  
		}
		else
		{
			//alert('end all');
		}
	}
};