/*
 *
 * attachments.js
 *
 * Copyright 2007 Mediaspectrum, Inc. All Rights Reserved.
 *
 * This software is the proprietary information of Mediaspectrum, Inc.
 *
 */

var fileind = 5;

function resetFileCount() {
	for (var i = 6; i <= fileind; i++) {
		var f = document.getElementById('f' + i);
		var tr = f.parentNode;
		tr.removeChild(f);
	}
	fileind = 5;
	setCookie("fileind", "5", 1);
}

function checkCountFiles() {
	var cFiles = readCookie("fileind");
	if (cFiles > 5) {
		createFilesFields(cFiles - 5);
	}
}

function createFilesFields(count) {
	if(count) {
		for(i = 0; i < count; i++) addFileInput("container2");
	}
}

function addFileInput(containerId) {
	var container = document.getElementById(containerId);
	var tr = document.getElementById("addfile");
	if(container && tr) {
		fileind++;
//		setCookie("fileind", fileind, 1);
		var newtr = document.createElement('tr');
		newtr.setAttribute('id', 'f' + fileind);
		var labeltd = document.createElement('td');
		labeltd.setAttribute('align', 'right');
		var label = document.createTextNode('Ad File ' + fileind + ': ');
		var inputtd = document.createElement('td');
		var input;
		if(window.navigator.userAgent.indexOf("MSIE") != -1) {
			input = document.createElement('<input type="file" name="file_attached' + fileind + '">');
		} else {
			input = document.createElement('input');
			input.type='file';
			input.name='file_attached';
		}
		input.setAttribute('id', 'file_attachedId' + fileind);
		input.className = 'AdInput';
		inputtd.className = 'AdInput';
		inputtd.appendChild(input);
		labeltd.appendChild(label);
		labeltd.className = 'AdInfoLabels';
		newtr.appendChild(labeltd);
		newtr.appendChild(inputtd);
		container.insertBefore(newtr, tr);
//		testHTML(container.innerHTML);//!!!!
	}
}

function testHTML(strHTML) {
  var prop="width=" + 500 + "," + "height=" + 500 + ","+"resizable=1,status=yes,toolbar=no,menubar=no,scrollbars=1,fullscreen=0;"
  prev = window.open("", "Preview", prop);
  prev.document.open();

  prev.document.write(strHTML);
  prev.document.close();
  prev.focus();
}
