function SelectAllCheckBoxes(objChk){
	var docInputs = document.getElementsByTagName("input");
	
	for(var i=0;i<docInputs.length;i++){
		if(docInputs[i].type=="checkbox")
		{
			if(docInputs[i].id.indexOf("chk")>0){
				if(objChk.checked==true)
					docInputs[i].checked=true;
				else
					docInputs[i].checked=false;
			}
		}
	}
}
function getQueryStringValue(qString) {
	  var allVars = window.location.search.substring(1).split("&");
	  for (var i=0;i<allVars.length;i++) {
	    var variable = allVars[i].split("=");
	    if (variable[0].toUpperCase() == qString.toUpperCase()) {
	      return variable[1];
	    }
	  }
	  return "null";
}
var serverWin;
function BrowseServer(objControl){
	if(serverWin)
		serverWin.close();
	serverWin = window.open(localPath+"FileManager/browser.aspx?control="+objControl,"","resizable=yes,dependent=yes,status=no,toolbar=no,width=800,height=600,left=0,top=0");
}
function LTrim(strText)
{
	while (strText.substring(0,1) == ' ')
			strText = strText.substring(1, strText.length);
	return strText;
} 
function RTrim(strText)
{
	while (strText.substring(strText.length-1,strText.length) == ' ')
			strText = strText.substring(0, strText.length-1);
	return strText;
}
function Trim(strText)
{
	return RTrim(LTrim(strText));
}
function CheckBlank(objText,strMessage)
{
	if(Trim(objText.value)=="")
	{
		alert(strMessage);
		objText.focus();
		return false;
	}
	return true;
}
function CheckBlankOtherTextBoxCheckBoxList(objCheckBox,errMsg){
	var chkOtherName = objCheckBox[0].name+"_Other_Chk";
	var txtOtherName = objCheckBox[0].name+"_Other";
	if(document.getElementById(chkOtherName).checked)
	{
		if(Trim(document.getElementById(txtOtherName).value)=="")
		{
			alert(errMsg);
			document.getElementById(txtOtherName).focus();
			return false;
		}
		return true;
	}
	return true;
}

//function CheckBlankOtherTextBox

function CheckBlankCheckBoxRadio(objChk,errMsg){
	var checkedOne = false;
	for(var i=0;i<objChk.length;i++){
		if (objChk[i].checked)
			checkedOne = true;
	}
	if(!checkedOne){
		var chkOtherName = objChk[0].name+"_Other_Chk";
		//alert(chkOtherName);
		if(objChk.type!="radio" && document.getElementById(chkOtherName)!=null)
			if(document.getElementById(chkOtherName).checked)
				checkedOne = true;
	}
	if(!checkedOne){
		alert(errMsg);
		objChk[0].focus();
		return false;
	}
	return true;
}

function CheckOtherTextBoxDDLList(objDDL,errMsg){
	var checkedOne = false;
	for(var i=0;i<objDDL.options.length;i++){
		if(objDDL.options[i].value=="other"){
			if(objDDL.options[i].selected)
				checkedOne=true;
		}
	}
	if(checkedOne){
		var txtOtherName = objDDL.name + "_Other"
		if(Trim(document.getElementById(txtOtherName).value)==""){
			alert(errMsg);
			document.getElementById(txtOtherName).focus();
			return false;
		}
	}
	return true;
}

function CheckBlankDropDownList(objDDL,errMsg){
	var checkedOne = false;
	for(var i=0;i<objDDL.options.length;i++){
		if(objDDL.options[i].selected)
			checkedOne=true;
	}
	if(!checkedOne){
		alert(errMsg);
		objDDL.focus();
		return false;
	}
	return true;
}

// Function to validate inputs
function checkTBKeyPress(obj,tbtype,length,precision)
{
	//ADDITIONAL FEATURE to add
	//SPECIAL CHARATERS
	
	//ADD CHECKING FOR TEXTAREA
	
	if(obj.value.length >=length)
	{
		window.event.keyCode=0;
		return;
	}
		
	if(tbtype=="alphanumeric")
	{
		return;
	}

	if(tbtype=="alpha")
	{
		if((window.event.keyCode<65 || window.event.keyCode>90) && 
		   (window.event.keyCode<97 || window.event.keyCode>122))
		{
			window.event.keyCode=0;
			return;
		}
	}	
	
	if(tbtype=="float")
	{			
		if(window.event.keyCode==46)
		{			
			if(precision==0)
			{
				window.event.keyCode=0;
				return;
			}
							
			if(length-obj.value.length==1)
			{
				window.event.keyCode=0;
				return;
			}
			
			if(obj.value.length==0)
			{
				obj.value="0";
				return;
			}
			
			var ind=obj.value.indexOf(".");
			if(ind>=0)
				window.event.keyCode=0;
		}
		else if(window.event.keyCode<48 || window.event.keyCode>57)
		{
			window.event.keyCode=0;
			return;
		}

		var ind=obj.value.indexOf(".");
		if(ind>=0)
		{
			var sstr=obj.value.substring(ind);
			if(sstr.length>precision)
				window.event.keyCode=0;
			return;
		}			
	}

	if(tbtype=="integer")
	{
		if(window.event.keyCode<48 || window.event.keyCode>57)
		{
			window.event.keyCode=0;
			return;
		}
	}		
}
var popUpWinHtmlEditor;
function popupHtmlEditor(objControlId){
	if(popUpWinHtmlEditor)
		popUpWinHtmlEditor.close();
	popUpWinHtmlEditor=window.open(adminPath+'popupHtmlEditor.aspx?control='+objControlId,'','width=850;height=850');
	popUpWinHtmlEditor.moveTo(2,5);
	popUpWinHtmlEditor.focus();
}


function checkLength(str, strName, strMinLength, strMaxLength)
{
	error = "";
	if ((str.length < strMinLength) || (strng.length > strMaxLength)) {
		error="Length of " + strName + " should be between " + strMinLength + " and " + strMaxLength;
		return(error);
    }
}
// TABBED DIV HIDE SHOW
function TogglePans(objLink,panID){
	var currentTD = objLink.parentNode;
	var currentTabRow = currentTD.parentNode;
	var allTDs = currentTabRow.getElementsByTagName("TD");
	
	for(var j=0;j<allTDs.length;j++){
		if(allTDs[j]==currentTD){
			allTDs[j].className="CurrentTab";
		}
		else{
			if(allTDs[j].className!="NoTab")
				allTDs[j].className="Tab";
		}
	}
	var preFix = "_Toggle";
	var newID = preFix+panID;
	var pans = document.getElementsByTagName("div");
	for(var i=0;i<pans.length;i++){
		if(pans[i].id.indexOf(preFix)>-1){
			if(pans[i].id == newID)
				pans[i].className="show";
			else
				pans[i].className="hide";
		}
	}
}
// END TABBED DIV HIDE SHOW

// Email Validation

function checkEmail(objEmail,strMsg){
	if(CheckBlank(objEmail,strMsg)){
		var filter= /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(objEmail.value))
			return true;
		else{
			alert(strMsg);
			objEmail.select();
			objEmail.focus();
			return false;
		}
	}
}
function ValidEmail(strEmail){
	var filter= /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(strEmail))
			return true;
		else{
			return false;
		}
}
function RemoveNewLine(strContent){
	strContent = strContent.replace("\r\n","<br>");
	return strContent;
}
// List Box Items MoveMent
function TransferListItem(objFromList,objToList,blnAll){
	if(objFromList.selectedIndex==-1 && !blnAll){
		alert("Please select an item.");
		objFromList.focus();
		return;
	}
	if(blnAll==true){
		var counter = objFromList.options.length;
		var tempOpt;
		for(var i=0;i<counter;i++){
			tempOpt = document.createElement("option");
			tempOpt.value = objFromList.options[0].value;
			tempOpt.text = objFromList.options[0].text;
			objToList.options.add(tempOpt);
			objFromList.options.remove(0);
		}
	}
	else{
		var options = objFromList.options;
		for(var i=0;i<options.length;i++){
			var tempOpt;
			if(options[i].selected){
				tempOpt = document.createElement("option");
				tempOpt.value=options[i].value;
				tempOpt.text=options[i].text;
				objToList.options.add(tempOpt);
				//objFromList.options.remove(i);
			}
		}
		for(var i=options.length;i>=0;i--){
			try{
			if(options[i].selected){
				objFromList.options.remove(i);
			}
			}
			catch(err){}
		}
		
		if(objFromList.options.length!=0)
			objFromList.options[0].selected=true;
	}
}
function ListItemMoveUp(objList){
		var list = objList;
		if(list.selectedIndex==-1){
			alert("Please select item.");
			list.focus();
			return;
		}
		if(list.selectedIndex==0){
			alert("Item is at top.");
			list.focus();
			return;
		}
		var currIndex = list.selectedIndex;
		var currOpt = list.options[currIndex];
		var upOpt = list.options[currIndex-1]
		var tmpOpt = document.createElement("option");

		tmpOpt.value=currOpt.value;
		tmpOpt.text=currOpt.text;


		currOpt.value=upOpt.value;
		currOpt.text=upOpt.text

		upOpt.value=tmpOpt.value;
		upOpt.text=tmpOpt.text

		upOpt.selected=true;
		currOpt.selected=false;
		tmpOpt=null;
}
function ListItemMoveDown(objList){
		var list = objList;
		if(list.selectedIndex==-1){
			alert("Please select item.");
			list.focus();
			return;
		}
		if(list.selectedIndex==list.options.length-1){
			alert("Item is at bottom.");
			list.focus();
			return;
		}
		var currIndex = list.selectedIndex;
		var currOpt = list.options[currIndex];

		var downOpt = list.options[currIndex+1]
		var tmpOpt = document.createElement("option");

		tmpOpt.value=downOpt.value;
		tmpOpt.text=downOpt.text;

		downOpt.value=currOpt.value;
		downOpt.text=currOpt.text

		currOpt.value=tmpOpt.value;
		currOpt.text=tmpOpt.text

		downOpt.selected=true;
		currOpt.selected=false;
		tmpOpt=null;
}
function FireSearch(){
	var txtSearch = document.getElementById("searchText");
	if(CheckBlank(txtSearch,"Input keywords to search.")){
		document.forms[0].action="/Search.aspx?search="+txtSearch.value;
		document.forms[0].submit();
	}
}

// Sort List
function ts_getInnerText(el) {
	if (typeof el == "string") return el;
	if (typeof el == "undefined") { return el };
	if (el.innerText) return el.innerText;
	var str = "";
	
	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //ELEMENT_NODE
				str += ts_getInnerText(cs[i]);
				break;
			case 3:	//TEXT_NODE
				str += cs[i].nodeValue;
				break;
		}
	}
	return str;
}



function parse_list_to_array(list_id, attribute){
	var list = document.getElementById(list_id);
	var cs = list.childNodes;
	var list_array = new Array();

	var l = cs.length;
	for (var i = 0; i < l; i++) {
		node = cs[i];
		if (node.nodeName == "OPTION"){
			if(!attribute){
				var value = ts_getInnerText(node);
				list_array.push([node, value.toUpperCase()]);
			}
			else{
				list_array.push([node, node.getAttribute(attribute)]);
			}
		}
	}
return list_array; //returns an array with the node in [0] and the attribute in [1]
}


function sortList(objListId,order){

var list_id = objListId;
var sortby = '';

var array = parse_list_to_array(list_id, sortby);

// Work out a type to sort by
var itm = array[1][1];
sortfn = mysortfn_by_attribute;
/*
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^[£$]/)) sortfn = ts_sort_currency;
*/
if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric;


array.sort(sortfn);

switch (order){
case "desc":
array.reverse();
break;
}

var list = document.getElementById(list_id);

for (var k = 0; k < array.length; k++){
list.appendChild(array[k][0]);
}

return;
}

function mysortfn_by_attribute(a,b) {
  // Note that each thing we are passed is an array, so we don't compare the things
  // we're passed; instead, we compare their second column
  if (a[1]<b[1]) return -1;
  if (a[1]>b[1]) return 1;
  return 0;
}

function ts_sort_numeric(a,b) { 
    aa = a[1]
    if (isNaN(aa)) aa = 0;
    bb = b[1]
    if (isNaN(bb)) bb = 0;
    return bb-aa;
}
