// JavaScript Document

function addRow(table_id){
	var tbl = document.getElementById(table_id);
	var text = "Photo:";
	var input = '<input type="file" name="photos[]" align="left" onchange="checkExtensions( this );" />';
	var remove = '<input type="button" name="remove" id="remove" value="Remove" onclick="removeRow(\'tbl_input\',this.parentNode.parentNode)" />';
	var rows_count = tbl.rows.length; 
	try{
		var newRow = tbl.insertRow(rows_count-3);
		var newCell = newRow.insertCell(0); 
		newCell.innerHTML = text;
		newCell.setAttribute('align', "right");
		var newCell = newRow.insertCell(1); 
	    newCell.innerHTML = input + remove; 
		newCell.setAttribute('align', "left");
	} catch(ex) {
		alert(ex);
	}
}

function addNewPhotos(table_id){
	var tbl = document.getElementById(table_id);
	var input = '<input class="file" type="file" name="newphotos[]" align="left" onchange="checkExtensions( this );" />';
	var remove = '<input type="button" name="remove" id="remove" value=" X " onclick="removeRow(\'tbl_image\',this.parentNode.parentNode)" />';
	var rows_count = tbl.rows.length; 
	try{
		var newRow = tbl.insertRow(rows_count-1);
		var newCell = newRow.insertCell(0); 
	    newCell.innerHTML = input + remove; 
		newCell.setAttribute('align', "left");
	} catch(ex) {
		alert(ex);
	}
}

function removeRow(tbl,row) 
{ 
  var table = document.getElementById(tbl); 
  try { 
	table.deleteRow(row.rowIndex); 
  } catch (ex) { 
	alert(ex); 
  } 
}

function checkExtensions( uploadFile ) {
	var extension = new Array();
	//Suppot formats				
	extension[0] = ".png";
	extension[1] = ".gif";
	extension[2] = ".jpg";
	extension[3] = ".jpeg";
	extension[4] = ".PNG";
	extension[5] = ".GIF";
	extension[6] = ".JPG";
	extension[7] = ".JPEG";
	
	var fileName = uploadFile.value;
	
	//get extension
	var ext = fileName.substr(fileName.lastIndexOf('.'));
	
	for(var i = 0; i < extension.length; i++) {
		if(ext == extension[i]) { 
			return true;
		}
	}
			
	alert("Picture Services only supports JPEG(.jpg), GIF(.gif) and PNG(.png) image formats. Try again using a version of your picture saved in one of those formats.");
	uploadFile.parentNode.innerHTML = uploadFile.parentNode.innerHTML;

	return false;
}
