// JavaScript Document

	function switchOp(url) {
		window.location.href = url;
		return false;
	}

	function NewWindow(mypage, myname, w, h, scr) {
//			var q = mypage.indexOf("?");
			var name = mypage.substr(0, 6);
			if( myname == 'name') myname = name;
			if( myname == 'name') myname = 'name';
		LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
		TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
		settings = 'height=' + h + ',width=' + w + ',top=' + TopPosition + ',left=' + LeftPosition;
		if(scr) settings = settings + ', scrollbars=yes';
		var win = window.open(mypage,myname,settings); win.focus();
		return false;
	}
	var dhtmlwin;
	function NewWindow_dhtml(url, name, width, height) {
	 dhtmlwin=dhtmlwindow.open("editbox", "iframe", url, " ", "width="+width+"px,height="+height+"px,resize=1,scrolling=1,center=1", "recal");
	}
	
	
	function postData() {
		var okay = true;
		if (document.getElementById("username").value == "" || document.getElementById("password").value == "") { alert("Please Provide Username & Password..."); okay = false; }
		 if (okay) document.frm.submit();
		}

	function deleteSelected() {
		if(confirm('Are You Sure ?')) {
			document.frm.submit();
		}
	}

	function getNPPage(combo, direction /* 1, -1*/) {
		cb = document.getElementById(combo);
		var page = cb.options[cb.selectedIndex].value;
		
		if(cb.selectedIndex == cb.options.length - 1 && direction == 1) return;
		if(cb.selectedIndex == 0 && direction == -1) return ;
		cb.selectedIndex = (page-1) + direction;
		
	}

	function createPages(combo, pages, current_page) {
		cb = document.getElementById(combo);
		cb.options.length = 0;
		if(pages < 2) {
			cb.options.length = cb.options.length+1;
			cb.options[cb.options.length-1].value = "";
			cb.options[cb.options.length-1].text = "N/A";
			return;
		}	

		for(i = 1; i <= pages; i++) {
			cb.options.length = cb.options.length+1;
			cb.options[cb.options.length-1].value = i;
			cb.options[cb.options.length-1].text = i;
			if( i == current_page) {
			cb.options[cb.options.length-1].selected = true;
			}
		}
		
	}

	function selectOption(selectbox, value) {
		for(i = 0; i < selectbox.options.length; i++) {
			if( selectbox.options[i].value == value) {
			selectbox.options[i].selected = true;
			}
		}
		
	}

	
	function checkAll(name_string) {
		els = document.getElementsByTagName("input");
		for(i = 0; i < els.length; i++) {
			el = els[i];
			if(el.type == "checkbox") {
				if(el.name.indexOf(name_string) > -1) {
					el.checked = !el.checked;
				}
			}
		}
	}

	function unCheckAll(name_string) {
		els = document.getElementsByTagName("input");
		for(i = 0; i < els.length; i++) {
			el = els[i];
			if(el.type == "checkbox") {
				if(el.name.indexOf(name_string) > -1) {
					el.checked = false;
				}
			}
		}
	}

	
	function getTable(table_id) {
		return document.getElementById(table_id);
	}
	
	function clearTable(table) {
		while(table.rows.length > 2) {	// excluding the header
			table.deleteRow(2);
		}
	}

	function clearTableEx(table, row_index) {
		while(table.rows.length > row_index) {	// excluding the header
			table.deleteRow(row_index);
		}
	}
	
	function addCell(row) {
		return row.insertCell(row.cells.length);
	}
	
	
	function addRow(tbl, color, row_id) {
			
			var lastRow = tbl.rows.length;
			var row = tbl.insertRow(lastRow);
			row.setAttribute("Bgcolor",  color);
			row.bgColor = color;
			row.id = row_id;
		return row;	  
	}
	
	function addBoldText(txt) {
		var bold = document.createElement("B");
		bold.innerHTML = txt;
		return bold;
	}
	function addTextBox(id, name, val, size) {
		var inp = document.createElement("INPUT");
		inp.type= 'text';
		inp.value = val;
		inp.size = size;
		inp.name = name;
		inp.id = id;
		return inp;
	}

	function addText(txt) {
		var bold = document.createElement("span");
		bold.innerHTML = txt;
		return bold;
	}
		
	function addPopupLink(txt, url, w, h) {
		var linkNode = document.createElement('A');
		//var txtNode = document.createTextNode(txt);
		linkNode.innerHTML = txt;
		linkNode.href = '#';
		linkNode.onclick = function() { NewWindow(url, 'name', w, h, true); return false; }; 
		//linkNode.appendChild(txtNode);
		return linkNode;
	}

	function addLink(txt, url) {
		var linkNode = document.createElement('A');
		linkNode.href = url;
		linkNode.innerHTML=txt;
		return linkNode;
	}
		
	function addCheckBox(name, value, checked) {
			  var input = document.createElement('input');
			  input.name = name;
			  input.value = value;
			  input.type = 'checkbox';
		return input;
	}
	function addHidden(name, id, value) {
			  var input = document.createElement('input');
			  input.type = 'HIDDEN';
			  input.name = name;
			  input.id = id;
			  input.value = value;
		return input;
	}
	
	function addSelectOption(selectbox, value, text, selected) {
		var opt = document.createElement('OPTION');
		opt.value = value;
		opt.innerHTML = text;
		opt.selected = selected;
		selectbox.appendChild(opt);
	
	}

	function setCookie( name, value, expires, path, domain, secure ) {
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );

		/*
		if the expires variable is set, make the correct 
		expires time, the current script below will set 
		it for x number of days, to make it for hours, 
		delete * 24, for minutes, delete * 60 * 24
		*/
		if ( expires )	{
		expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );

		document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
	}

// with this test document.cookie.indexOf( name + "=" );
	function getCookie( check_name ) {
		// first we'll split this cookie up into name/value pairs
		// note: document.cookie only returns name=value, not the other components
		var a_all_cookies = document.cookie.split( ';' );
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false; // set boolean t/f default f
		
		for ( i = 0; i < a_all_cookies.length; i++ ) {
			// now we'll split apart each name=value pair
			a_temp_cookie = a_all_cookies[i].split( '=' );
			
			// and trim left/right whitespace while we're at it
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		
			// if the extracted name matches passed check_name
			if ( cookie_name == check_name ) {
				b_cookie_found = true;
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if ( a_temp_cookie.length > 1 ) 	{
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				}
				// note that in cases where cookie is initialized but no value, null is returned
				return cookie_value;
				break;
			}
			a_temp_cookie = null;
			cookie_name = '';
		}
		if ( !b_cookie_found )	{
			return null;
		}
	}			

	function getCheckedValue(radioObj) {
		if(!radioObj)
			return "";
		var radioLength = radioObj.length;
		if(radioLength == undefined)
			if(radioObj.checked)
				return radioObj.value;
			else
				return "";
		for(var i = 0; i < radioLength; i++) {
			if(radioObj[i].checked) {
				return radioObj[i].value;
			}
		}
		return "";
	}

	function showHideSrch(element) {
		var div = document.getElementById(element);
		var img = document.getElementById(element + "_img");
		if(div.style.visibility == "hidden") {
			showDiv(element);
			img.src = '/gfx/bt_flit_up.gif';
			setCookie( "show_"+element, 1, 365);
		} else {
			hideDiv(element);
			img.src = '/gfx/bt_flit_dn.gif';
			setCookie( "show_"+element, 2, 365);
		}
	}

	function utf8_decode ( str_data ) {
	    // http://kevin.vanzonneveld.net
	    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
	    // +      input by: Aman Gupta
	    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +   improved by: Norman "zEh" Fuchs
	    // +   bugfixed by: hitwork
	    // +   bugfixed by: Onno Marsman
	    // *     example 1: utf8_decode('Kevin van Zonneveld');
	    // *     returns 1: 'Kevin van Zonneveld'
	 
	    var tmp_arr = [], i = ac = c1 = c2 = c3 = 0;
	 
	    str_data += '';
	 
	    while ( i < str_data.length ) {
	        c1 = str_data.charCodeAt(i);
	        if (c1 < 128) {
	            tmp_arr[ac++] = String.fromCharCode(c1);
	            i++;
	        } else if ((c1 > 191) && (c1 < 224)) {
	            c2 = str_data.charCodeAt(i+1);
	            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
	            i += 2;
	        } else {
	            c2 = str_data.charCodeAt(i+1);
	            c3 = str_data.charCodeAt(i+2);
	            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
	            i += 3;
	        }
	    }
	 
	    return tmp_arr.join('');
	}
	
	function base64_decode( data ) {
	    // http://kevin.vanzonneveld.net
	    // +   original by: Tyler Akins (http://rumkin.com)
	    // +   improved by: Thunder.m
	    // +      input by: Aman Gupta
	    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +   bugfixed by: Onno Marsman
	    // -    depends on: utf8_decode
	    // *     example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
	    // *     returns 1: 'Kevin van Zonneveld'
	 
	    // mozilla has this native
	    // - but breaks in 2.0.0.12!
	    //if (typeof window['btoa'] == 'function') {
	    //    return btoa(data);
	    //}
	 
	    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, dec = "", tmp_arr = [];
	 
	    data += '';
	 
	    do {  // unpack four hexets into three octets using index points in b64
	        h1 = b64.indexOf(data.charAt(i++));
	        h2 = b64.indexOf(data.charAt(i++));
	        h3 = b64.indexOf(data.charAt(i++));
	        h4 = b64.indexOf(data.charAt(i++));
	 
	        bits = h1<<18 | h2<<12 | h3<<6 | h4;
	 
	        o1 = bits>>16 & 0xff;
	        o2 = bits>>8 & 0xff;
	        o3 = bits & 0xff;
	 
	        if (h3 == 64) {
	            tmp_arr[ac++] = String.fromCharCode(o1);
	        } else if (h4 == 64) {
	            tmp_arr[ac++] = String.fromCharCode(o1, o2);
	        } else {
	            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
	        }
	    } while (i < data.length);
	 
	    dec = tmp_arr.join('');
	    dec = utf8_decode(dec);
	 
	    return dec;
	}	


	function onEnter(e, func) {
	        if(window.event)  {
	                key = window.event.keyCode;     //IE
	        }  else  {
	                key = e.which;     //firefox
	        }
		if(key == 13) {
			func();
			return false;	
		}	 
		return true;
	}

	function swap(id, where) {
		document.getElementById('act').value = where;
		document.getElementById('id').value = id;
		refreshTable();
	}
	
	function closeCurrentWindow() {
		window.close();
	/*
		try {
			parent.dhtmlwin.close();
			return;
		} catch (e) {}

		try {
			dhtmlwin.close();
			return;
		} catch (e) {}
		*/
	}

	function checkSubmit( ) {
	  var argv = checkSubmit.arguments;
	  var argc = argv.length;
	  var error = false;
	  for (var i = 0; i < argc && !error; i++) {
			if(!document.getElementById(argv[i]).value) {
				alert( argv[i] + " is mandatory ");
				error = true;
			}
	   }
	   
	   if(!error) document.frm.submit();

	}

	function ro(who, how) {
		if (how) who.style.backgroundColor='#E6E4D9'; else who.style.backgroundColor='#ffffff';		
	}

	function $$(element_id) {
		var element = document.getElementById(element_id);
		return element;
	} 	

	function removeRow(table_id, row_id) {
		tbl = getTable(table_id);
		r = $$(row_id);
		tbl.deleteRow(r.rowIndex);
	}
	
	function exchange(i, j, tableID)
	{
		var oTable = document.getElementById(tableID);
		var trs = oTable.tBodies[0].getElementsByTagName("tr");
		
		if(i == j+1) {
			oTable.tBodies[0].insertBefore(trs[i], trs[j]);
		} else if(j == i+1) {
			oTable.tBodies[0].insertBefore(trs[j], trs[i]);
		} else {
			var tmpNode = oTable.tBodies[0].replaceChild(trs[i], trs[j]);
			if(typeof(trs[i]) != "undefined") {
				oTable.tBodies[0].insertBefore(tmpNode, trs[i]);
			} else {
				oTable.appendChild(tmpNode);
			}
		}
	}		
	