var numuploads = 0;
var submitafterupload = false;

function uploadfile(upload_id, newfile) {

	HideText('upload_form_'  + newfile );
	ShowText('upload_formwaiting_' + newfile);

	document.getElementById('fileupload_form_' + newfile).submit();
	document.getElementById('fileupload_form_' + newfile).submitted.value = 1;	
	
	document.getElementById('total_status_iframe').style.display = 'block';
	if(numuploads == 0) {
	   document.getElementById('total_status_iframe').contentWindow.location.href = '/managephotoupload/status?totalstatus=1&firstvisit=1';
	}
	numuploads = numuploads + 1;


//	document.getElementById('status_iframe_' + newfile ).style.display = '';
//	document.getElementById('status_iframe_' + newfile ).contentWindow.location.href = 'http://upload' + newfile + '.flingfinder.com.au.vhost.vivitec.com.au/managephotoupload/status?status=1&firstvisit=1&upload_id=' + upload_id;
//	document.getElementById('status_iframe_' + newfile ).contentWindow.location.href = 'http://upload' + newfile + '.flingfinder.com.au/managephotoupload/status?status=1&firstvisit=1&upload_id=' + upload_id;
	
	var addnewupload = Number(newfile) + 1;
//	window.parent.filluploaddiv(addnewupload, '');

	return false;
};


function finishedupload(success, divid) {
	numuploads = numuploads - 1;
	if(success && submitafterupload) {
	  if(saveAndContinue(-1)) {
	    document.forms.finalSubmit.submit();
	    return true;
	  }
	}

	  // if its this one still open.
	  if(document.getElementById('upload_formwaiting_' + divid).style.display == 'block') {
		ReplaceText('thisphotostatus_' + divid, 'Upload Complete.');
	  }
	  if(numuploads > 0) {
	    var uploadsremaininglist = document.getElementsByName('uploadsremaining');
  	    for (var i = 0; i < uploadsremaininglist.length; i++) {
	      uploadsremaininglist[i].innerHTML = '  ' + numuploads + ' photos remain.';
            }

	    var continueButtonsList = document.getElementsByName('upload_formwaiting_continue');
            for (var i = 0; i < continueButtonsList.length; i++) {
              continueButtonsList[i].disabled = true;
	    }
	  } else {
	    var uploadsremaininglist = document.getElementsByName('uploadsremaining');
            for (var i = 0; i < uploadsremaininglist.length; i++) {
              uploadsremaininglist[i].innerHTML = '';
            }

            var continueButtonsList = document.getElementsByName('upload_formwaiting_continue');
            for (var i = 0; i < continueButtonsList.length; i++) {
              continueButtonsList[i].disabled = false;
            }

	  }


	submitafterupload = false;
}

function filluploaddiv(divid, error) {
	// firefox bug!
	var thishttp = createRequestObject();

	thishttp.open('GET', '/managephotoupload/?newfile=' + divid + '&error=' + error);
	thishttp.onreadystatechange = function() { handleFillUploadResponse(thishttp, divid) };
        thishttp.send(null);
}

function handleFillUploadResponse(thishttp, divid) {
     if(thishttp.readyState == 4){
        if(thishttp.status == 200) {
          var response = thishttp.responseText;
          ReplaceText('newfile_' + divid, response);
	  document.getElementById('newfile_' + divid).style.display = 'block';
        } else {
          AJAXError('Status returned ' + thishttp.status);
        }
	thishttp = null;
     }

}


function getexisting(divid, photoid) {
	// firefox bug!
	var thishttp = createRequestObject();
	thishttp.open('GET', '/managephotoupload/?existing=' + photoid + '&divid=' + divid);
	thishttp.onreadystatechange = function() { handleGetExistingResponse(thishttp, divid) };
        thishttp.send(null);
}

function handleGetExistingResponse(thishttp, divid) {
     if(thishttp.readyState == 4){
        if(thishttp.status == 200) {
          var response = thishttp.responseText;
          ReplaceText('existing_' + divid, response);
	  document.getElementById('existing_' + divid).style.display = 'block';
        } else {
          AJAXError('Status returned ' + http.status);
        }
	thishttp = null;
     }

}

function updatetotalStatus() {
	var statushttp = createRequestObject();
	statushttp.open('GET', '/managephotoupload/status?totalstatus=1&rand=' + Math.random());
	statushttp.onreadystatechange = function() { handleTotalStatusResponse(statushttp) };
        statushttp.send(null);
}

function handleTotalStatusResponse(statushttp) {
     if(statushttp.readyState == 4){
        if(statushttp.status == 200) {
          var response = statushttp.responseText;
	  if(response == 'stop') { // fugly.
	     clearInterval(intervalid); 
	  } else {
	        ReplaceText('totalstatusblock', response);
 	  }
        } else {
          AJAXError('Status returned ' + statushttp.status);
        }
     }

}

function updateStatus(upload_id) {
	var statushttp = createRequestObject();
	statushttp.open('GET', '/managephotoupload/status?status=1&upload_id=' + upload_id);
	statushttp.onreadystatechange = function() { handleStatusResponse(statushttp) };
        statushttp.send(null);
}

function handleStatusResponse(statushttp) {
     if(statushttp.readyState == 4){
        if(statushttp.status == 200) {
          var response = statushttp.responseText;
          ReplaceText('statusblock', response);
        } else {
          AJAXError('Status returned ' + statushttp.status);
        }
     }

}

function deletePhoto(photo_id, divid) {
	http = createRequestObject();
	http.open('GET', '/managephotoupload/?delete=' + photo_id);
	http.onreadystatechange = handleModificationResponse;
        http.send(null);	
	return false;
}

function moveUp(photo_id, divid) {
	http = createRequestObject();
	http.open('GET', '/managephotoupload/?moveup=' + photo_id + '&divid=' + divid);
	http.onreadystatechange = handleModificationResponse;
        http.send(null);	
	return false;
}

function moveDown(photo_id, divid) {
	http = createRequestObject();
	http.open('GET', '/managephotoupload/?movedown=' + photo_id + '&divid=' + divid);
	http.onreadystatechange = handleModificationResponse;
        http.send(null);	
	return false;
}

function makeMain(photo_id, divid) {
	http = createRequestObject();
	http.open('GET', '/managephotoupload/?makemain=' + photo_id + '&divid=' + divid);
	http.onreadystatechange = handleModificationResponse;
        http.send(null);	
	return false;
}

function handleModificationResponse() {
     if(http.readyState == 4){
        if(http.status == 200) {
	  // update remaining items
	  var response = http.responseText;
	  eval(response);
        } else {
          AJAXError('Status returned ' + http.status);
        }
     }

}

function saveAndContinue(formid) {
	if(numuploads > 0) {
	   if(!confirm('There are still file uploads active.  These will be cancelled if you leave this page now.  Continue?')) {
	      return false;
	   }
 	} 

  
        var firstUnUploaded = -1;
	
	for (var i = document.forms.length - 1; i >= 0; i--) {
	  var thisform = document.forms[i];
	  if((thisform.name.search('upload_form_') >= 0) && (thisform.submitted.value != 1) && (thisform.image.value != '')) {
	    firstUnUploaded = thisform.orderid.value;
		//alert(thisform.name);  
	}
    	}
	if(firstUnUploaded >= 0) {
	  if(!confirm('You have not yet uploaded photo ' + firstUnUploaded + '. Exit this page anyway?')) {
	    return false;
	  }
	}
	 
	
	  if(formid > 0) {
	  var thisform = document.getElementById('fileupload_form_' + formid);
  	  
	    if(thisform.image.value != '') {
	    if(confirm('You have not yet uploaded the photo "' + thisform.image.value + '".  Do you want upload this file before continuing?')) {
		submitafterupload = true;
		uploadfile(Math.floor(Math.random()*100000), formid);
		return false;
	    }
	  }
	}	  


	var submitstring = '?';
	var anythingtosend = false;
	var submitForm = document.getElementById('finalSubmit');
	for (var i = 1; i < document.forms.length; i++) {
	  var thisform = document.forms[i];
	  if(thisform.name.search('form_existing_') >= 0) {
	   anythingtosend = true;

	   var input = document.createElement("input");
	   input.type="hidden";
	   input.name = 'photo_id_' + i;
	   input.value = thisform.photo_id.value;	
	   submitForm.appendChild(input);
	   
	   input = document.createElement("input");
           input.type="hidden";
           input.name = 'rating_' + i;
		
  
	   if(thisform.rating[0].checked) {
	     input.value = thisform.rating[0].value;
	   } else if(thisform.rating[1].checked) {
	     input.value = thisform.rating[1].value;
	   } else if(thisform.rating[2].checked) {
	     input.value = thisform.rating[2].value;
	   }

           submitForm.appendChild(input);


	   input = document.createElement("input");
           input.type="hidden";
	   input.name = 'private_' + i;

	   if(thisform.private[0].checked) {
	     input.value = thisform.private[0].value;
	   } else if(thisform.private[1].checked) {
	     input.value = thisform.private[1].value;
	   } else if(thisform.private[2].checked) {
	     input.value = thisform.private[2].value;
	   } 

	   submitForm.appendChild(input);

	   input = document.createElement("input");
	   input.type="hidden";
	   input.name = 'comment_' + i;
	   input.value = thisform.comment.value;

	   submitForm.appendChild(input);

	  }
 	  thisform = null;
	}
	return true;	
}

