$(document).ready(function(){ var fieldname = $('#ia').val(); /* Load the previously uploaded files */ var filecount = window.parent.window.$('#'+fieldname+'_filecount').val(); $('#'+fieldname+'_filecount').val(filecount); var image_extensions = new Array("gif", "jpeg", "jpg", "png", "swf", "psd", "bmp", "tiff", "jp2", "iff", "bmp", "xbm", "ico"); if (filecount > 0) { var jsontext = window.parent.window.$('#'+fieldname).val(); var json = eval('(' + jsontext + ')'); var i; $('#'+fieldname+'_licount').val(filecount); for (i = 1; i <= filecount; i++) { var previewblock = "
| ";
if (isValueInArray(image_extensions, json[i-1].ext.toLowerCase()))
previewblock += " "+decodeURIComponent(json[i-1].name); previewblock += " | ";
if ($('#'+fieldname+'_show_title').val() == 1 && $('#'+fieldname+'_show_comment').val() == 1)
previewblock += ""; else if ($('#'+fieldname+'_show_comment').val() == 1) previewblock += " | "; previewblock += " | ![]() |
'+translt.errorNoMoreFiles+'
'); return false; } /* If the file being uploaded is not allowed, * do not upload the file and display an error message ! */ var allowSubmit = false; for (var i = 0; i < allowed_filetypes.length; i++) { //check to see if it's the proper extension if (jQuery.trim(allowed_filetypes[i].toLowerCase()) == jQuery.trim(ext.toLowerCase()) ) { //it's the proper extension allowSubmit = true; break; } } if (allowSubmit == false) { $('#notice').html(''+translt.errorOnlyAllowed.replace('%s',$('#'+fieldname+'_allowed_filetypes').val())+'
'); return false; } // change button text, when user selects file button.text(translt.uploading); // If you want to allow uploading only 1 file at time, // you can disable upload button this.disable(); // Uploding -> Uploading. -> Uploading... interval = window.setInterval(function(){ var text = button.text(); if (text.length < 13){ button.text(text + '.'); } else { button.text(translt.uploading); } }, 400); }, onComplete: function(file, response){ button.text(translt.selectfile); window.clearInterval(interval); // enable upload button this.enable(); // Once the file has been uploaded via AJAX, // the preview is appended to the list of files var metadata = eval('(' + response + ')'); var count = parseInt($('#'+fieldname+'_licount').val()); count++; $('#'+fieldname+'_licount').val(count); var image_extensions = new Array("gif", "jpeg", "jpg", "png", "swf", "psd", "bmp", "tiff", "jp2", "iff", "bmp", "xbm", "ico"); if (metadata.success) { $('#notice').html(''+metadata.msg+'
'); var previewblock = "";
// If the file is not an image, use a placeholder
if (isValueInArray(image_extensions, metadata.ext.toLowerCase()))
previewblock += " ";
previewblock += ""+decodeURIComponent(metadata.name)+" | ";
if ($("#"+fieldname+"_show_title").val() == 1 && $("#"+fieldname+"_show_comment").val() == 1)
previewblock += ""; else if ($("#"+fieldname+"_show_comment").val() == 1) previewblock += " | "; previewblock += " | ![]() | "+
"
'+translt.errorTooMuch+'
'); } else { $('#notice').html(''+metadata.msg+'
'); } } }); // if it has been jst opened, the upload button should be automatically clicked ! // TODO: auto open using click() not working at all ! :( }); function isValueInArray(arr, val) { inArray = false; for (i = 0; i < arr.length; i++) if (val.toLowerCase() == arr[i].toLowerCase()) inArray = true; return inArray; } // pass the JSON data from the iframe to the main survey page function passJSON(fieldname, show_title, show_comment, pos) { var json = "["; var filecount = 0; var licount = parseInt($('#'+fieldname+'_licount').val()); var i = 0; while (i <= licount) { if ($("#"+fieldname+"_li_"+i).is(':visible')) { if (filecount > 0) json += ","; json += '{'; if ($("#"+fieldname+"_show_title").val() == 1) json += '"title":"' +$("#"+fieldname+"_title_" +i).val().replace(/"/g, '\\"')+'",'; if ($("#"+fieldname+"_show_comment").val() == 1) json += '"comment":"'+$("#"+fieldname+"_comment_"+i).val().replace(/"/g, '\\"')+'",'; json += '"size":"' +$("#"+fieldname+"_size_" +i).val()+'",'+ '"name":"' +$("#"+fieldname+"_name_" +i).val()+'",'+ '"filename":"' +$("#"+fieldname+"_filename_" +i).val()+'",'+ '"ext":"' +$("#"+fieldname+"_ext_" +i).val()+'"}'; filecount += 1; } i += 1; } json += "]"; window.parent.window.copyJSON(json, filecount, fieldname, show_title, show_comment, pos); } function saveAndExit(fieldname, show_title, show_comment, pos) { var filecount = parseInt($('#'+fieldname+'_filecount').val()); var minfiles = parseInt($('#'+fieldname+'_minfiles').val()); if (minfiles != 0 && filecount < minfiles) { var confirmans = confirm(translt.errorNeedMore.replace('%s', (minfiles - filecount))) if (confirmans) { passJSON(fieldname, show_title, show_comment, pos); return true } else return false; } else { passJSON(fieldname, show_title, show_comment, pos); return true; } } function deletefile(fieldname, count) { var file_index; var xmlhttp; if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); var filecount = parseInt($('#'+fieldname+'_filecount').val()); var licount = parseInt($('#'+fieldname+'_licount').val()); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { $('#notice').html(''+xmlhttp.responseText+'
'); setTimeout(function() { $(".success").remove(); }, 5000); $("#"+fieldname+"_li_"+count).hide(); filecount--; $('#'+fieldname+'_filecount').val(filecount); // rearrange the file indexes // i.e move the files below i to one step up file_index = $("#"+fieldname+"_file_index_"+count).val(); for (j = count; j <= licount; j++) { if ($('#'+fieldname+'_li_'+j).is(":visible")) { $('#'+fieldname+'_file_index_'+j).val(file_index); file_index++; } } var minfiles = parseInt($('#'+fieldname+'_minfiles').val()); var maxfiles = parseInt($('#'+fieldname+'_maxfiles').val()); if (filecount < minfiles) $('#uploadstatus').html(translt.errorNeedMore.replace('%s',(minfiles - filecount))); else $('#uploadstatus').html(translt.errorMoreAllowed.replace('%s',(maxfiles - filecount))); } } filename=$("#"+fieldname+"_filename_"+count).val(); name=$("#"+fieldname+"_name_"+count).val(); xmlhttp.open('GET','delete.php?sid='+surveyid+'&fieldname='+fieldname+'&filename='+filename+'&name='+encodeURI(name), true); xmlhttp.send(); } function escapeHtml(unsafe) { return unsafe .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }