var lastupload="";
var suberror=0;
jQuery(function(){
var ul = jQuery('#upload_ul ul');
jQuery('#drop a').click(function(){
// Simulate a click on the file input button
// to show the file browser dialog
if(document.getElementById("adlink").value =="")
{
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Please enter a URL for a website or page !');
jQuery('#adlink').focus();
});
return false;
}
jQuery('#upload_error').css( "display", "none" );
jQuery('#upload_error').html(' ');
if(document.getElementById("adlink").value != "")
{
var checkurl = RegExp(/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!-\/]))?/);
var adlink = jQuery('#adlink').val();
if (!checkurl.test(adlink) ){
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Please provide a valid URL, http://www.example.com !');
jQuery('#adlink').focus();
});
return false;
}
jQuery('#upload').find('#upl').click();
}
});
jQuery('#ms_instructions a').click(function(){
jQuery("#upload_form").fadeIn(500);
});
// Initialize the jQuery File Upload plugin
jQuery('#upload').fileupload({
///This element will accept file drag/drop uploading
dropZone: jQuery(''),
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
maxFileSize: 5000000, // 5 MB
// This function is called when a file is added to the queue;
// either via the browse button, or via drag/drop:
add: function (e, data) {
// kill any error messages
jQuery('#upload_error').css( "display", "none" );
jQuery('#upload_error').html(' ');
// kill display of previously uploaded ad
//if(jQuery('#display_ad_container').length !=0){
jQuery("#ms_ad_preview").fadeOut(10, function() { jQuery("#ms_ad_preview").html(); });
//}
// website link input empty //////////////
var input = jQuery('#adlink');
data.formData = {adlink: input.val()};
if (!data.formData.adlink) {
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Please provide a link to a website !');
input.focus();
suberror=1;
});
return false;
}
// website link input contains invalid URL //////////////////
var checkurl = RegExp(/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!-\/]))?/);
var adlink = jQuery('#adlink').val();
if (!checkurl.test(adlink) ){
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Please provide a valid URL, http://www.example.com !');
input.focus();
});
return false;
}
var tpl = jQuery('
');
// tplerror not used
var tplerror=jQuery('
');
// regex for gif png jpe?g
var thefile=data.files[0].name.match(/(\.|\/)(gif|jpe?g|png)$/i);
var doerror=false;
// not gif jpg or png file
if(null == thefile)
{
tplerror.find('p').text(data.files[0].name) // png jpg gif files only
.append('' + formatFileSize(data.files[0].size) + 'Error: file type not allowed!');
doerror=true;
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Error: image must be png, jpg, or gif ');
});
return false;
}
// file exceeds 5MB
if(parseFloat(data.files[0].size) > 5000000) // file too big
{
doerror=true;
tplerror.find('p').text(data.files[0].name)
.append('' + formatFileSize(data.files[0].size) + 'Error: file too big!');
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Error: file too big!');
});
return false;
}
if(doerror) // should not be called any longer
{
data.context = tplerror.appendTo(ul);
tplerror.find('span').click(function(){
tplerror.fadeOut('slow',function(){
tplerror.remove();
});
});
return false;
}
// Append the file name and file size
tpl.find('p').text(data.files[0].name)
.append('' + formatFileSize(data.files[0].size) + '');
// Add the HTML to the UL element
data.context = tpl.appendTo(ul);//context depreciate;
// Initialize the knob plugin
tpl.find('input').knob();
// Listen for clicks on the cancel icon
tpl.find('span').click(function(){
if(tpl.hasClass('working')){
jqXHR.abort();
}
tpl.fadeOut(function(){
tpl.remove();
});
});
// Automatically upload the file once it is added to the queue
var jqXHR = data.submit();
jQuery('#adlink').val("");
},
progress: function(e, data){
// Calculate the completion percentage of the upload
var progress = parseInt(data.loaded / data.total * 100, 10);
// Update the hidden input field and trigger a change
// so that the jQuery knob plugin knows to update the dial
data.context.find('input').val(progress).change();
if(progress == 100){
data.context.removeClass('working');
}
},
fail:function(e, data){
// Something has gone wrong!
data.context.addClass('error');
},
complete: function(e,data){
//alert(data);
},
done: function (e, data) {
var is_json = true;try {var obj = jQuery.parseJSON(data.result);} catch(err) {is_json = false;}
//alert(data.result);
if(is_json){
var obj = jQuery.parseJSON(data.result);
if(obj.status=="success")
{
var iurl=obj.imgurl;
var iwidth=obj.iwidth;
var iheight=obj.iheight;
var upadid=obj.adid;
var iimage='';
var viewimage='
This is what your ad will look like.
'+iimage+'
';
jQuery('#ms_ad_preview').html(viewimage).hide().fadeIn(1000);
//jQuery(viewimage)
//.appendTo('#member_services')
//.hide().fadeIn(1000);
jQuery('#userads_tab tbody').prepend(obj.userad);
document.getElementById('userads').style.display='block';
if(obj.badurl==1)
{
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html("Upload Successful. Warning: Please double check the url you provided to make sure it's not broken and correct as required.");
});
}
} //end status eq success
else
{
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html(obj.status);
});
jQuery('#upload_ul ul li:first').remove();
}//end of status not eq success
} //end of is_json;
else
{
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Error processing request. Please try again. If the error persists, please contact bateworld customer satisfaction !');
});
jQuery('#upload_ul ul li:first').remove();
}
data.jqXHR;
} // end of done
});
// Prevent the default action when a file is dropped on the window
jQuery(document).on('drop dragover', function (e) {
e.preventDefault();
});
// Helper function that formats the file sizes
function formatFileSize(bytes) {
if (typeof bytes !== 'number') {
return '';
}
if (bytes >= 1000000000) {
return (bytes / 1000000000).toFixed(2) + ' GB';
}
if (bytes >= 1000000) {
return (bytes / 1000000).toFixed(2) + ' MB';
}
return (bytes / 1000).toFixed(2) + ' KB';
}
});