// tabledeleterow.js version 1.2 2006-02-21
// mredkj.com

// CONFIG notes. Below are some comments that point to where this script can be customized.
// Note: Make sure to include a <tbody></tbody> in your table's HTML

var INPUT_NAME_PREFIX = 'inputName'; // this is being set via script
var RADIO_NAME = 'totallyrad'; // this is being set via script
var TABLE_NAME = 'tblWishList'; // this should be named in the HTML
var ROW_BASE = 1; // first number (for display)
var CART_COUNT = 1; // first number (for display)
var hasLoaded = true;

window.onload=fillInRows;

function fillInRows()
{
	hasLoaded = true;
}

// CONFIG:
// myRowObject is an object for storing information about the table rows
//function myRowObject(one, two, three, four)
function myRowObject(one, two, three)
{
	this.one = one; // text object
	this.two = two; // td object
	this.three = three; // input button object
//	this.four = four; // input radio object
}

/*
 * insertRowToTable
 * Insert and reorder
 */
function insertRowToTable()
{
	if (hasLoaded) {
		var tbl = document.getElementById(TABLE_NAME);
		var rowToInsertAt = tbl.tBodies[0].rows.length;
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
			if (tbl.tBodies[0].rows[i].myRow && tbl.tBodies[0].rows[i].myRow.four.getAttribute('type') == 'radio' && tbl.tBodies[0].rows[i].myRow.four.checked) {
				rowToInsertAt = i;
				break;
			}
		}
		addRowToTable(rowToInsertAt);
		reorderRows(tbl, rowToInsertAt);
	}
}

/*
 * addRowToTable
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
//function addRowToTable(num)
function addRowToTable(fid,fname,investment_text,country_text)
{
	if (hasLoaded) { 
		var tbl = document.getElementById(TABLE_NAME);
		var nextRow = tbl.tBodies[0].rows.length;
		var iteration = nextRow + ROW_BASE;
		var cookItemCount;
		var cookArr;
		var cookVal = "";
		var addElement = true;
	
		if(CART_COUNT == 1){
			document.getElementById("trCartEmpty").style.display='none';
			document.getElementById("trSubmit").style.display=document.getElementById("trBlank").style.display;
		}
		num = nextRow-1;
		
		cookVal = readCookie("countItems");

		if(cookVal != "" || cookVal != null){
			cookArr = cookVal.split("<break>");
			cookItemCount = cookArr.length;
		}else{
			cookItemCount=0;
		}
		
		if(cookItemCount <= 9){
			
			if(cookVal != ""){
				cookArr = cookVal.split("<break>");
				var findRes = findInArray(fid, cookArr);
//				alert("'"+cookVal+"'-----------"+cookArr.indexOf(fid));
				if(findRes)
					addElement=false;
			}
			
			if(addElement){
				ajax_savecart(iteration, fname, fid, investment_text, country_text, 'add');
				
				if(cookVal == "")
					createCookie("countItems",fid,1);
				else
					createCookie("countItems",cookVal+"<break>"+fid,1);
				
				// add the row
				var row = tbl.tBodies[0].insertRow(num);
				
				// cell 1 - text/html
				var cell1 = row.insertCell(0);
				cell1.innerHTML = "<span class='normal'>"+fname+"</span><br><span style='color:#FFFF00; font-size:9px; font-weight:normal;'>"+investment_text+"</span><br><span style='color:#FFFF00; font-size:9px; font-weight:normal;'>"+country_text+"</span>";
			
				var el = document.createElement('input');
				el.type = 'hidden';
				el.name = 'hdnfranchise[]';
				el.id = 'hdnCart' + iteration;
				el.value = fid;
				
				cell1.appendChild(el);
				
				// cell 2 - remove button
				var cell2 = row.insertCell(1);
				var btnEl = document.createElement('input');
				btnEl.setAttribute('type', 'checkbox');
				btnEl.setAttribute('checked', 'checked');
//				btnEl.setAttribute('type', 'image');
//				btnEl.setAttribute('src', 'images/del.gif');
				btnEl.onclick = function () {deleteCurrentRow(this, iteration, fid)};
			//		btnEl.className = 'button';
				cell2.appendChild(btnEl);
			
				CART_COUNT++;
			
				row.myRow = new myRowObject(textNode, cell1, btnEl);
			}
		}else{
			alert("You can add only 10 franchises in your wishlist.");
			document.getElementById('limitRow').style.display = document.getElementById('headRow').style.display;
			document.getElementById('limitTd').innerHTML = "You can add only 10 franchises in your wishlist.";
		}
	}//isLoaded if
}

// CONFIG: this entire function is affected by myRowObject settings
// If there isn't a checkbox in your row, then this function can't be used.
function deleteChecked()
{
	if (hasLoaded) {
		var checkedObjArray = new Array();
		var cCount = 0;
	
		var tbl = document.getElementById(TABLE_NAME);
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
			if (tbl.tBodies[0].rows[i].myRow && tbl.tBodies[0].rows[i].myRow.three.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows[i].myRow.three.checked) {
				checkedObjArray[cCount] = tbl.tBodies[0].rows[i];
				cCount++;
			}
		}
		if (checkedObjArray.length > 0) {
			var rIndex = checkedObjArray[0].sectionRowIndex;
			deleteRows(checkedObjArray);
			reorderRows(tbl, rIndex);
		}
	}
}

// If there isn't an element with an onclick event in your row, then this function can't be used.
function deleteCurrentRow(obj, iter, fid)
{
	if (hasLoaded) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		var cookNewVal = "";

		deleteRows(rowArray);

		CART_COUNT--;

		if(CART_COUNT == '1'){
			document.getElementById("trCartEmpty").style.display=document.getElementById("trBlank").style.display;
			document.getElementById("trSubmit").style.display='none';
		}

		//on removing any item from cart...update the cookie as well by removing that franchise id from the cookie
		cookVal = readCookie("countItems");
//		alert(fid);
		cookArr = cookVal.split("<break>");
		cookItemCount = cookArr.length;
		for(var a=0; a < cookItemCount; a++){
			if(cookArr[a]!=fid){
				if(cookNewVal == "")
					cookNewVal = cookArr[a];
				else
					cookNewVal = cookNewVal+"<break>"+cookArr[a];
			}
		}
//		alert(cookNewVal);
		//assign the new value to the cookie
		createCookie("countItems",cookNewVal,1);
		
		document.getElementById('limitTd').innerHTML = '';
		document.getElementById('limitRow').style.display = 'none';

		ajax_savecart(iter, '', '', '', '', 'remove');

//		reorderRows(tbl, rIndex);
	}
}

function reorderRows(tbl, startingIndex)
{
	if (hasLoaded) {
		if (tbl.tBodies[0].rows[startingIndex]) {
			var count = startingIndex + ROW_BASE;
			for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {
			
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.one.data = count; // text
				
				// CONFIG: next line is affected by myRowObject settings
//				tbl.tBodies[0].rows[i].myRow.two.name = INPUT_NAME_PREFIX + count; // input text
//				tbl.tBodies[0].rows[i].myRow.two.innerHTML = tbl.tBodies[0].rows[i+1].myRow.two.innerHTML; // input text
				
				// CONFIG: next line is affected by myRowObject settings
//				var tempVal = tbl.tBodies[0].rows[i].myRow.two.value.split(' '); // for debug purposes
//				tbl.tBodies[0].rows[i].myRow.two.value = count + ' was' + tempVal[0]; // for debug purposes
				
				// CONFIG: next line is affected by myRowObject settings
//				tbl.tBodies[0].rows[i].myRow.four.value = count; // input radio
				
				// CONFIG: requires class named classy0 and classy1
//				tbl.tBodies[0].rows[i].className = 'classy' + (count % 2);
				
				count++;
			}
		}
	}
}

function deleteRows(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
}

function openInNewWindow(frm)
{
	// open a blank window
	var aWindow = window.open('', 'TableAddRow2NewWindow',
	'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
	
	// set the target to the blank window
	frm.target = 'TableAddRow2NewWindow';
	
	// submit
	frm.submit();
}

function findInArray(needle, arr){
	for(var x=0; x < arr.length; x++){
		if(needle == arr[x])
			return true;
	}
	return false;
}
