// form_functions.js
// Author: Jim Robinson
// Created: September 29, 2008
// Updated: October 2, 2008
// Contact: webmaster@techie-jim.net
// Note: This file uses the code of other authors that is free or GPL licensed and credited.

// Random range function
function rand( min, max ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Leslie Hoare
    // *     example 1: rand(1, 1);
    // *     returns 1: 1

    if( max ) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    } else {
        return Math.floor(Math.random() * (min + 1));
    }
}

// Generate 23-character unique id
function uniqid() {
	uid = '';
    for(i=1;i<=23;i++) {
		if(rand(0,1)==1) isNumeric = true;
		else isNumeric = false;
		if(isNumeric) {
			chr = String.fromCharCode(rand(48,57));
		}
		else {
			if(rand(0,1)==1) isUppercase = true;
			else isUppercase = false;
			if(isUppercase) {
				chr = String.fromCharCode(rand(65,90));
			}
			else {
				chr = String.fromCharCode(rand(97,122));
			}
		}
		uid += chr;
	}
	return uid;
}

// Create formRowHandle Object
function formRowHandle() {
	var formRowObj = new Object();
	var numOfRows = 0;

	var reNumberRows = function(tBodyNode) {
		for(var rowNum=1;rowNum<tBodyNode.rows.length;rowNum++) {
			if(tBodyNode.rows[rowNum].cells[0].firstChild.nodeValue!=rowNum)
				tBodyNode.rows[rowNum].cells[0].firstChild.nodeValue=rowNum;
		}
	}

	this.addNewRow = function(parentId) {
		parentElem = document.getElementById(parentId);
		if(parentElem != 'undefined') {
			newRowElem = this.createRow((numOfRows+1));
			newRowElem.addToParent(parentElem);
			var uidCount = 1;
			while(formRowObj[newRowElem.getId()]!=undefined) {
				if(!newRowElem.setId(uniqid()) || uidCount>10)
					return false;
				uidCount++;
			}
			formRowObj[newRowElem.getId()] = newRowElem;
			numOfRows++;
			return true;
		}
		else {
			return false;
		}
	}

	this.addExistingRow = function(elementId) {
		existingElem = document.getElementById(elementId);
		if(existingElem != 'undefined') {
			eRowObj = new formRow(0,false);
			eRowObj.setRef(existingElem);
			formRowObj[elementId] = eRowObj;
			numOfRows++;
			return true;
		}
		else {
			return false;
		}
	}

	this.removeRow = function(objId) {
		if(this.getNumberOfRows()>1) {
			if(formRowObj.hasOwnProperty(objId)) {
				rowElement = formRowObj[objId].getRef();
				rowElement.style.backgroundColor = '#FF0000';
				if (confirm("Are you sure you want to delete this row?")) {
					var tBodyNode = formRowObj[objId].getRef().parentNode;
					formRowObj[objId].del();
					delete formRowObj[objId];
					reNumberRows(tBodyNode);
					numOfRows--;
				}
				else {
					rowElement.style.backgroundColor = 'transparent';
				}
			}
		}
		else {
			alert('You cannot delete all rows.');
		}
	}

	this.getNumberOfRows = function() {
		return numOfRows;
	}

	this.createRow = function(rowNumber) {
		newFormRow = new formRow(uniqid(),true,rowNumber);
		return newFormRow;
	}
}

// Create formRow Object
function formRow(id,createRow,rowNumber) {
	var rowRef = false;

	var create = function(id,rowNumber) {
		rowRef = document.createElement('tr');
		rowRef.setAttribute('id',id);
		for(i=0;i<=4;i++) {
			td = document.createElement('td');
			td.setAttribute('align','center');
			if(i==0) {
				input = document.createTextNode(rowNumber);
			}
			else if(i==1) {
				input = document.createElement('input');
				input.setAttribute('type','text');
				input.setAttribute('size','3');
				input.setAttribute('maxlength','10');
				input.setAttribute('name','qty[]');
			}
			else if(i==2) {
				input = document.createElement('input');
				input.setAttribute('type','text');
				input.setAttribute('size','20');
				input.setAttribute('maxlength','20');
				input.setAttribute('name','pNumber[]');
			}
			else if(i==3) {
				input = document.createElement('input');
				input.setAttribute('type','text');
				input.setAttribute('size','30');
				input.setAttribute('maxlength','100');
				input.setAttribute('name','desc[]');
			}
			else if(i==4) {
				input = document.createElement('input');
				input.setAttribute('type','button');
				input.setAttribute('name','deleteBtn');
				input.setAttribute('value','X');
				input.onclick = function() {
					formRowHandler.removeRow(id);
				};
			}
			td.appendChild(input);
			rowRef.appendChild(td);
		}
	}

	this.addToParent = function(parentNode) {
		parentNode.tBodies[0].appendChild(rowRef);
	}

	this.del = function() {
		rowRef.parentNode.deleteRow(rowRef.sectionRowIndex);
	}

	this.setRef = function(elemRef) {
		rowRef = elemRef;
	}

	this.getId = function() {
		return rowRef.id;
	}

	this.setId = function(id) {
		if(rowRef.id = id)
			return true;
		else
			return false;
	}

	this.getRef = function() {
		return rowRef;
	}

	if(createRow) {
		create(id,rowNumber);
	}
}

// Initialize form row handler
formRowHandler = new formRowHandle();

// DOM_complete function (runs when page's DOM is loaded)
function DOM_complete() {
	watch_deliver_radio(document.getElementById('form_dMethod_deliver'));
}

// watch_deliver_radio function watches for changes to the
// checked property of the radio button and displays the
// Address fields
function watch_deliver_radio(deliver_radio_element) {
	var deliver_location_element_id = 'deliverAddress';
	var deliver_location_element = document.getElementById(deliver_location_element_id);

	var show_location_element = function() {
		deliver_location_element.style.display = '';
	}

	var hide_location_element = function() {
		deliver_location_element.style.display = 'none';
	}

	if(deliver_radio_element==undefined || deliver_location_element==undefined)
		return false;

	if(deliver_radio_element.value=='Deliver') {
		if(deliver_radio_element.checked)
			show_location_element();
		else
			hide_location_element();
	}
	else if (deliver_radio_element.value=='Pick Up') {
		if(deliver_radio_element.checked) {
			hide_location_element();
		}
	}
}