/*function countCaps() {
 var text = document.getElementById("galname10").value;
 if (text.length<=0) 
  return false;
 var count = 0;
 for(i=0; i < text.length; i++) {
  if(text[i].charCodeAt(text[i]) >= 65 && text[i].charCodeAt(text[i]) <= 90)
   count++;
 }
 if( count/text.length*100 > 70  && text.length>4) {
  alert("Too much UPPERCASE in title!");
  return false;
 } 
 
 return true;
}
*/
function flashUpload(){	
  //if (!countCaps())
   //return false;
		var customPostFields;
		customPostFields = "galname10;" + document.getElementById("galname10").value;
		tags = document.forms["test"].elements["tags[]"];
		for (i = 0; i < tags.length; i++) {
			if (tags[i].checked) {
				customPostFields = customPostFields + "|tags[" + i + "];" + tags[i].value;
			}
		}
		customPostFields = customPostFields + "|token;" + document.getElementById("token_control").value;
		customPostFields = customPostFields + "|uid;" + document.getElementById("uid").value;
		customPostFields = customPostFields + "|thumbs;" + document.getElementById("thumbs").value;
		document.MultiPowUpload.setParameter("customPostFields",customPostFields);
		document.MultiPowUpload.uploadAll();
}
	
var refreshId = [];	
var IId;
var totalFiles = 0;
var totalFailed = 0;
var uploadedFiles = [];
	
function externalUpload()
{
		totalFiles = $("input[id^='_fileURL']").length;
        $("input[id^='_fileURL']").each(function(i, elem){
			$.post("ajax_upload.php", { type: "START_UPLOAD", file: $(this).val(), _index: i  },
				function(data){
					var t = jQuery.parseJSON(data);
					var _id = $("div[id^='_output']").get(i);
					
					if (t[0] == false){ // Error
						totalFailed++;
						clearInterval(refreshId[i]);
						$("div[id^='pbar']").get(i).style.background = "red";
						$("div[id^='pbar']").get(i).style.width = "100%"
						$(_id).css("color","red");
					}
					if (t[0] == true){ // Success
					    var  _fa = [i,t[2]];
					    uploadedFiles.push(_fa);
						$(_id).css("color","green");
						
					}	
					
					$(_id).html(t[1]);
					
						if ((uploadedFiles.length+totalFailed) == totalFiles && (uploadedFiles.length > 0)){
							processUpload(0); //Process first file
						}
					
				});
				IId = i;
				
				refreshId[i] = setInterval(function(IId){
					$.post("ajax_upload.php", { type: "GET_PROGRESS", _index: i },
						function(pdata){
							var z = jQuery.parseJSON(pdata);
							$("div[id^='pbar']").get(i).style.width = z + '%';
							if (z >= 100){
								clearInterval(refreshId[i]);
							}
					});
				},1000);
		
  		 });
}

function processUpload(iii){
tags = document.forms["test"].elements["tags[]"];
var _filename = [];
var _tags = [];

_filename = uploadedFiles[iii][1];
_filename = _filename.split("/")
_filename = _filename.pop();

	for (i = 0; i < tags.length; i++) {
		if (tags[i].checked) {
			_tags.push(tags[i].value);
		}
	}
	
	$.post("externalupload.php", { tags: _tags, thumbs: document.getElementById("thumbs").value, uid: document.getElementById("uid").value, token: document.getElementById("token_control").value, galname10: document.getElementById("galname10").value, gid: '', filename: _filename, location: uploadedFiles[iii][1], current: iii, count: uploadedFiles.length },
	function(data){
		var t = jQuery.parseJSON(data);
		var _id = $("div[id^='_output']").get(uploadedFiles[iii][0]);
		$(_id).html(t[1]);
		if (t[0] < uploadedFiles.length-1){
			var _t = parseInt(t[0]) + 1;
			processUpload(_t);
		}
		//<?php if($_GET['c'] == '') {echo SITE_URL.'uploaded.php?id='.$uniqueid;} else echo SITE_URL.'gallery.php?id='.$_GET['c']; ?>",
		if (t[0] == uploadedFiles.length-1){ // Finished
			setTimeout(function(){window.location = redirectLocation}, 2000)
		}
	});
}

function checkProgress(i){
	$.post("ajax_upload.php", { type: "GET_PROGRESS", _index: i },
		function(pdata){
			var z = jQuery.parseJSON(pdata);
			$("div[id^='pbar']").get(i).style.width = z + '%';
			if (z >= 100){
				clearInterval(refreshId[i]);
			}
	});
}

var checkCount=0
var maxChecks=3
function setChecks(obj) {
  //gay or transsex don't allow another checks
   if ($('#test input[value=27]').attr('checked')  && obj.checked && obj.value!=27) {
     obj.checked=false;
     return;
   }  

   if ($('#test input[value=44]').attr('checked')  && obj.checked && obj.value!=44) {
     obj.checked=false;
     return;
   }  

   if ( obj.checked && obj.value==27) {
     $('#test input[type="checkbox"][name="tags[]"]').attr('checked',false);
     $('td[id^="tag"]').css('color','#01B3CD');
     checkCount=0;
     obj.checked=true;
   }  

   if ( obj.checked && obj.value==44) {
     $('#test input[type="checkbox"][name="tags[]"]').attr('checked',false);
     $('td[id^="tag"]').css('color','#01B3CD');
     checkCount=0;
     obj.checked=true;
   }  

   if(obj.checked) {
     checkCount=checkCount+1;
         document.getElementById('tag'+obj.value).style.color='#FF003A';
   } else {
     checkCount=checkCount-1
     if (checkCount<0)
       	checkCount=0;
               	document.getElementById('tag'+obj.value).style.color='#01B3CD';
   }
   if (checkCount>maxChecks) {
     obj.checked=false
     checkCount=checkCount-1
     alert("You may only select up to 3 tags.")
   }
   if(checkCount==0)
   {
           document.getElementById('submitbu').disabled=true;
   }
   else
   {
           enable_submit(document.getElementById('terms'));
   }
}

function enable_submit (obj)
{
	if(obj.checked == true && checkCount != 0)
	document.getElementById('submitbu').disabled=false;
	else
	document.getElementById('submitbu').disabled=true;
}

