/************************************************
** Function to determine soundex
** @param String (str)
** @return number (soundex value)
************************************************/
function soundex(str) {

    var s = '';
    var i, j, l, p = isNaN(p) ? 4 : p > 10 ? 10 : p < 4 ? 4 : p;
    var m = {
        BFPV: 1,
        CGJKQSXZ: 2,
        DT: 3,
        L: 4,
        MN: 5,
        R: 6
    };
    var r = (s = (str+'').toUpperCase().replace(/[^A-Z]/g, "").split("")).splice(0, 1);
    var sl = 0;

    sl = s.length;
    for (i = -1, l = sl; ++i < l;) {
        for (j in m) {
            if (j.indexOf(s[i]) + 1 && r[r.length-1] != m[j] && r.push(m[j])) {
                break;
            }
        }
    }

    return r.length > p && (r.length = p), r.join("") + (new Array(p - r.length + 1)).join("0");
}


/************************************************************
** Function to convert Array into delimiter separated String
** @param Array
** @return String (delimited string)
*************************************************************/
function getArrayAsString(arrayObj, delimiter) {

	var temp = "";

	if(arrayObj) {
		for(i=0; i < arrayObj.length; i++) {
			temp = temp + arrayObj[i] + delimiter;
		}
	}
	return temp;
}

/************************************************************
** Functions to trim the leading and trailing spaces
*************************************************************/
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}


/************************************************************
** Function to return &nbsp; string, if blank
** @param String (str)
** @return String (if blank, add &nbsp;)
*************************************************************/
function addNBSP(str) {
	str = str.trim();

	if(str && str != '') {
		return str;
	}
	else {
		return "&nbsp;";
	}
}


/************************************************************
** Function to return whether the filmsongvo
** is already present in the input list or not
** @param Array (list)
** @param FilmSongVO
** @return boolean (true or false)
*************************************************************/
function isAlreadyPresent(list, filmSongVO) {

	for(i=0; i<list.length; i++) {
	    if(list[i].filmName == filmSongVO.filmName && list[i].songName == filmSongVO.songName) {
			return true;
		}
	}

	return false;
}


function isChildArrayFullyInMasterWSoundex(childArray, masterArray) {

	if(childArray.length != masterArray.length) {
		return false;
	}

	for(i=0;i<childArray.length; i++) {
		childFoundInMaster = false;

		for(j=0;j<masterArray.length; j++) {
			if(soundex(childArray[i]) == soundex(masterArray[j])) {
				childFoundInMaster = true;
			}
		}

		if(childFoundInMaster == false) {
			return false;
		}
	}
	return true;
}


/************************************************************
** Function to return whether the filmYearVO
** is already present in the input list or not
** @param Array (list)
** @param FilmYearVO
** @return boolean (true or false)
*************************************************************/
function isFilmYearVOAlreadyPresent(list, filmYearVO) {

	for(i=0; i<list.length; i++) {
	    if(list[i].filmName == filmYearVO.filmName && list[i].year == filmYearVO.year) {
			return true;
		}
	}

	return false;
}

/************************************************************
** Function to return query string array
** @return {Array}
*************************************************************/
function getArgs() {

	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");

	for(var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}

	return args;
}

/**
*
*  Sortable HTML table
*  http://www.webtoolkit.info/
*
**/

function SortableTable (tableEl) {

	this.thead = tableEl.getElementsByTagName('thead');
	this.tfoot = tableEl.getElementsByTagName('tfoot');

	this.getInnerText = function (el) {
		if (typeof(el.textContent) != 'undefined') return el.textContent;
		if (typeof(el.innerText) != 'undefined') return el.innerText;
		if (typeof(el.innerHTML) == 'string') return el.innerHTML.replace(/<[^<>]+>/g,'');
	}

	this.getParent = function (el, pTagName) {
		if (el == null) return null;
		else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())
			return el;
		else
			return this.getParent(el.parentNode, pTagName);
	}

	this.sort = function (cell) {

	    var column = cell.cellIndex;
	    var itm = this.getInnerText(tableEl.rows[1].cells[column]);
		var sortfn = this.sortCaseInsensitive;

		if (itm.match(/\d\d[-]+\d\d[-]+\d\d\d\d/)) sortfn = this.sortDate; // date format mm-dd-yyyy
		if (itm.replace(/^\s+|\s+$/g,"").match(/^[\d\.]+$/)) sortfn = this.sortNumeric;

		this.sortColumnIndex = column;

	    var newRows = new Array();
	    for (j = 1; j < tableEl.rows.length; j++) {
			newRows[j-1] = tableEl.rows[j];
		}

		newRows.sort(sortfn);

		if (cell.getAttribute("sortdir") == 'down') {
			newRows.reverse();
			cell.setAttribute('sortdir','up');
		} else {
			cell.setAttribute('sortdir','down');
		}

		tBodyObj = document.createElement("tbody");
		tableEl.appendChild(tBodyObj);

		for (i=0;i<newRows.length;i++) {
			if(i%2==0) {
				newRows[i].className="even";
				newRows[i].onmouseover = function(){this.className='highlight';};
				newRows[i].onmouseout = function(){this.className='even';};
			}
			else {
				newRows[i].className="odd";
				newRows[i].onmouseover = function(){this.className='highlight';};
				newRows[i].onmouseout = function(){this.className='odd';};
			}
			tBodyObj.appendChild(newRows[i]);
		}

	}

	this.sortCaseInsensitive = function(a,b) {
		aa = thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]).toLowerCase();
		bb = thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]).toLowerCase();
		if (aa==bb) return 0;
		if (aa<bb) return -1;
		return 1;
	}

	this.sortDate = function(a,b) {
		aa = thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]);
		bb = thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]);
		date1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
		date2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
		if (date1==date2) return 0;
		if (date1<date2) return -1;
		return 1;
	}

	this.sortNumeric = function(a,b) {
		aa = parseFloat(thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]));
		if (isNaN(aa)) aa = 0;
		bb = parseFloat(thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]));
		if (isNaN(bb)) bb = 0;
		return aa-bb;
	}

	// define variables
	var thisObject = this;
	var sortSection = this.thead;

	// constructor actions
	if (!(tableEl.rows && tableEl.rows.length > 0)) {
	 return;
	}

	if (sortSection && sortSection[0].rows && sortSection[0].rows.length > 0) {
		var sortRow = sortSection[0].rows[0];
	} else {
		return;
	}

	for (var i=0; i<sortRow.cells.length; i++) {
		sortRow.cells[i].sTable = this;
		sortRow.cells[i].onclick = function () {
			this.sTable.sort(this);
			return false;
		}
	}

}


