// JavaScript Document
var current_set = 0;
var photoSets = Array();
photoSets[0] = true;

function processForm(thisform){
	
	for(var i in photoSets){
		if(!photoSets[i]){continue};
		
		var category = document.getElementById('category'+i);
		var image = document.getElementById('image'+i);
		var title = document.getElementById('title'+i);
		var tags = document.getElementById('tags'+i);
		var description = document.getElementById('description'+i);
		
		if(!category.value){
			alert("Please select the category of your photo!");
			category.focus();
			return false;
		}
		if(!image.value){
			alert("You must select a picture!");
			image.focus();
			return false;
		}
		if(!checkImageExtention(image.value)){
			alert("Your picture must have one of the following extensions:\n.jpg .gif .png");
			image.focus();
			return false;
		}
		if(!title.value){
			alert("Please enter a title for your photo!");
			title.focus();
			return false;
		}
		if(!description.value){
			alert("Please enter a description for your photo!");
			description.focus();
			return false;
		}
		if(!tags.value){
			alert("Please enter the tags for your photo!");
			tags.focus();
			return false;
		}
	}
	
	if(!current_loc){
		alert("Please select the location!");
		return false;
	}
	
	if(!document.getElementById('copy1_y').checked){
		alert("We regret that WorldViewer cannot accept a photography that is not owned by you!");
		return false;
	}
	
	if(!document.getElementById('copy2_y').checked){
		alert("To submit a photo you must agree to our copyright policy!");
		return false;
	}
	
	return true;
}

function checkImageExtention(file){
	var temp = file.lastIndexOf('.')+1;
	temp = file.substring(temp, file.length);
	temp = temp.toLowerCase();
	if(!(temp=='jpg' || temp=='gif' || temp=='png')){
		return false;
	}
	return true;
}


function createNewPhotoFieldSet(){
	current_set++;
	photoSets[current_set] = true;
	var photo_det_placeholder = document.getElementById('photo_det_placeholder');
	
	var new_table = document.createElement('table');
	
	var spacer_row = document.createElement('tr');
	var spacer_cell = document.createElement('td');
	spacer_cell.height = 30;
	spacer_row.appendChild(spacer_cell);
	new_table.appendChild(spacer_row);
	
	var cat_row = document.createElement('tr');
	var cat_caption = document.createElement('td');
	cat_caption.align = 'right';
	cat_caption.innerHTML = 'Category:';
	cat_row.appendChild(cat_caption);
	var cat_value = document.createElement('td');
	var cat_select = document.createElement('select');
	cat_select.name = 'category[]';
	cat_select.id = 'category'+current_set;
	for(cat in categories){
		cat_select.innerHTML += '<option value="'+categories[cat]['url_name']+'">'+categories[cat]['name']+'</option>';
	}
	cat_value.appendChild(cat_select);
	cat_row.appendChild(cat_value);
	new_table.appendChild(cat_row);
	
	var photo_row = document.createElement('tr');
	var photo_caption = document.createElement('td');
	photo_caption.align = 'right';
	photo_caption.innerHTML = 'Photo:';
	photo_row.appendChild(photo_caption);
	var photo_value = document.createElement('td');
	var photo_file = document.createElement('input');
	photo_file.type = 'file';
	photo_file.name = 'image[]';
	photo_file.id = 'image'+current_set;
	photo_value.appendChild(photo_file);
	photo_row.appendChild(photo_value);
	new_table.appendChild(photo_row);
	
	var title_row = document.createElement('tr');
	var title_caption = document.createElement('td');
	title_caption.align = 'right';
	title_caption.innerHTML = 'Title:';
	title_row.appendChild(title_caption);
	var title_value = document.createElement('td');
	var title_file = document.createElement('input');
	title_file.type = 'text';
	title_file.name = 'title[]';
	title_file.id = 'title'+current_set;
	title_file.className = 'text_input';
	title_file.style.width = '100%';
	title_value.appendChild(title_file);
	title_row.appendChild(title_value);
	new_table.appendChild(title_row);
	
	var desc_row = document.createElement('tr');
	var desc_caption = document.createElement('td');
	desc_caption.align = 'right';
	desc_caption.innerHTML = 'Description:';
	desc_row.appendChild(desc_caption);
	var desc_value = document.createElement('td');
	var desc_area = document.createElement('textarea');
	desc_area.name = 'description[]';
	desc_area.id = 'description'+current_set;
	desc_area.className = 'textarea_input';
	desc_area.cols = 60;
	desc_area.rows = 1;
	desc_value.appendChild(desc_area);
	desc_row.appendChild(desc_value);
	new_table.appendChild(desc_row);
	
	var tags_row = document.createElement('tr');
	var tags_caption = document.createElement('td');
	tags_caption.align = 'right';
	tags_caption.innerHTML = 'Tags:';
	tags_row.appendChild(tags_caption);
	var tags_value = document.createElement('td');
	var tags_area = document.createElement('textarea');
	tags_area.name = 'tags[]';
	tags_area.id = 'tags'+current_set;
	tags_area.className = 'textarea_input';
	tags_area.cols = 60;
	tags_area.rows = 1;
	tags_value.appendChild(tags_area);
	tags_row.appendChild(tags_value);
	new_table.appendChild(tags_row);
	
	var rem_row = document.createElement('tr');
	var rem_caption = document.createElement('td');
	rem_row.appendChild(rem_caption);
	var rem_value = document.createElement('td');
	rem_value.align = 'right';
	var rem_but = document.createElement('input');
	rem_but.type = 'button';
	rem_but.name = 'rem_photo';
	rem_but.value = 'Remove';
	rem_but.id = 'remove_but_'+current_set;
	rem_but.className = 'button_input';
	rem_but.onclick = function(){
		removePhoto(this);
	}
	rem_value.appendChild(rem_but);
	rem_row.appendChild(rem_value);
	new_table.appendChild(rem_row);
	
	photo_det_placeholder.appendChild(new_table);
}


function removePhoto(unitPlcaholderChild){
	var but_id = unitPlcaholderChild.id;
	var photo_id = parseInt(but_id.substr(but_id.lastIndexOf('_')+1));
	photoSets[photo_id] = false;
	
	var temp = unitPlcaholderChild.parentNode;
	var temp = temp.parentNode;
	var unitPlcaholder = temp.parentNode;
	unitPlcaholder.parentNode.removeChild(unitPlcaholder);
}
