//===================================================
//Document Object Model HTML  Utility
//===================================================
/** Init. table */
function clearTableRow(table){
	while(table.rows.length > 0) table.deleteRow(0);
}

function createElem(tagName, name){
	var isIE = ((navigator.userAgent.toLowerCase().indexOf("msie") != -1) 
				&& (navigator.userAgent.toLowerCase().indexOf("opera") == -1));
	if(isIE && name != null) 
		return document.createElement("<"+tagName+" name="+name+">");
	return document.createElement(tagName);
}

function createTNode(text){
	return document.createTextNode(text);
}

function changeInnerText(pNode, text){
	if(pNode.firstChild != null) {
		pNode.replaceChild(document.createTextNode(text), pNode.firstChild);
	}else{
		pNode.appendChild(document.createTextNode(text));
	}
}
	
function addCell(tr, child, className, colspan){
	var td = createElem("TD");
	td.appendChild(child);
	tr.appendChild(td);
	if(!className && className != '' || className != "undefined"){
		td.className = className;
	}
	
	if(colspan > 0) {
		td.setAttribute("colSpan", colspan);
	}

	td.setAttribute("height", "18");

	return td;
}
	
function addCell(tr, child, className, width){
	var td = createElem("TD");
	td.appendChild(child);
	tr.appendChild(td);
	if(!className && className != '' || className != "undefined"){
		td.className = className;
	}
	
	td.setAttribute("width", width);

	return td;
}
	
function addTCell(tr, text, className){
	return addCell(tr, createTNode(text), className, 0);
}
	
function addTCell(tr, text, className, colspan){
	return addCell(tr, createTNode(text), className, colspan);
}

function addTCell(tr, text, className, width){
	return addCell(tr, createTNode(text), className, width);
}

function addTColSpanCell(tr, text, colspan, className){
	return addCell(tr, createTNode(text), className, colspan);
}

function addRowLine(tr, colspan) {
 	var td = createElem("TD");
	td.setAttribute("colSpan", colspan);
	td.setAttribute("bgColor", "#eedca2");
	td.setAttribute("height", "1");
		
	tr.appendChild(td);
	return td;
}

function addFootRowLine(tr, colspan) {
 	var td = createElem("TD");
	td.setAttribute("colSpan", colspan);
	td.setAttribute("bgColor", "#f4d425");
	td.setAttribute("height", "2");
		
	tr.appendChild(td);
	return td;
}

function addMRowLine(tr, colspan, Height) {
 	var td = createElem("TD");
	td.setAttribute("colSpan", colspan);
	td.setAttribute("bgColor", "#9AC3D2");
	td.setAttribute("height", Height);
		
	tr.appendChild(td);
	return td;
}

// Public popup.
function c_open(name, height, width){
	attr = "status=no,resizable=yes,scrollbars=auto";
	attr = attr + ",height="+height;
	attr = attr + ",width="+width;
	attr = attr + ",top="+(screen.height-height)/2;
	attr = attr + ",left="+(screen.height-width)/2;
	return window.open("", name, attr);
}
	
// Public Error
function c_error(msg, a_msg){
	var a_node = createElem("A");
	a_node.setAttribute("href", "javascript:;");
	a_node.onclick=function(){alert(a_msg);}
	changeInnerText(a_node, msg);
	return a_node;
}

function makePageNode(root){

	var currpage=root.getAttribute("currpage");
	var startpage = root.getAttribute("startpage");
	var cont_out = createElem("SPAN");
	
	cont_out.appendChild(document.createElement('BR'));	

	if(root.getAttribute("beforeview") == "Y"){
		//var textnode = document.createTextNode(' << ');
		var imgNode = document.createElement("img");
		imgNode.setAttribute("src", "/figWeb/images/shopping/btn/prev1.gif");
		imgNode.setAttribute("border", "0");
		imgNode.setAttribute("height", "17");
		var container = createContainerForClick(imgNode);
		container.setAttribute("page", startpage - 1);
		container.onclick=function(){
			page_click(this.getAttribute("page"));
		}
		cont_out.appendChild(container);
		cont_out.appendChild(document.createTextNode('  '));
	}

	while(startpage <= parseInt(root.getAttribute("endpage"))){
		var textnode = document.createTextNode(' ' + startpage + ' ');
		var container = createContainerForClick(textnode);
		if(currpage == startpage){
			container.className="curr";
		}
		container.setAttribute("page", startpage);
		container.onclick=function(){
			page_click(this.getAttribute("page"));
		}
		cont_out.appendChild(container);
		startpage++;
	}
	
	if(root.getAttribute("nextview") == "Y"){
		cont_out.appendChild(document.createTextNode('  '));
		//var textnode = document.createTextNode(' >> ');
		var imgNode = document.createElement("img");
		imgNode.setAttribute("src", "/figWeb/images/shopping/btn/next1.gif");
		imgNode.setAttribute("border", "0");
		imgNode.setAttribute("height", "17");
		var container = createContainerForClick(imgNode);
		container.setAttribute("page", startpage);
		container.onclick=function(){
			page_click(this.getAttribute("page"));
		}
		cont_out.appendChild(container);
	}
	
	return cont_out;
}

function clearElement(_elem){
	while(_elem.childNodes.length > 0){
		_elem.removeChild(_elem.childNodes.item(0));
	}
}

function createContainerForClick(textnode){
	var cont = createElem("SPAN");
	var page_n = createElem("A");
	page_n.setAttribute("href", "javascript:;");
	page_n.appendChild(textnode);
	cont.appendChild(page_n);
	return cont;
}


/*
---------------------------------------------------------------------------

 * 	This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2, or (at your option)
 *      any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program (see the file COPYING); if not, write to the
 *      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *      Boston, MA  02111-1307, USA
 
This code is copyright 2003 by Matthew Eernisse (mde@state26.com)

Additional bugfixes by Mark Pruett (mark.pruett@comcast.net)

---------------------------------------------------------------------------	
*/ 

// The var docForm should be a reference to a <form>

function formData2QueryString(docForm) {

	var strSubmitContent = '';
	var formElem;
	var strLastElemName = '';
	
	for (i = 0; i < docForm.elements.length; i++) {
		
		formElem = docForm.elements[i];
		switch (formElem.type) {
			// Text fields, hidden form elements
			case 'text':
			case 'hidden':
			case 'password':
			case 'textarea':
			case 'select-one':
				strSubmitContent += formElem.name + '=' + paramEncode(formElem.value) + '&'
				break;
				
			// Radio buttons
			case 'radio':
				if (formElem.checked) {
					strSubmitContent += formElem.name + '=' + paramEncode(formElem.value) + '&'
				}
				break;
				
			// Checkboxes
			case 'checkbox':
				if (formElem.checked) {
					// Continuing multiple, same-name checkboxes
					if (formElem.name == strLastElemName) {
						// Strip of end ampersand if there is one
						if (strSubmitContent.lastIndexOf('&') == strSubmitContent.length-1) {
							strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
						}
						// Append value as comma-delimited string
						strSubmitContent += ',' + paramEncode(formElem.value);
					}
					else {
						strSubmitContent += formElem.name + '=' + paramEncode(formElem.value);
					}
					strSubmitContent += '&';
					strLastElemName = formElem.name;
				}
				break;
				
		}
	}
	
	// Remove trailing separator
	strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
	return strSubmitContent;
}

function paramEncode(s) {
	s = replace(s, "%", String.fromCharCode(30));
	s = replace(s,  "&", String.fromCharCode(31));
	
	return s;
}


/********************************
	Mark the selected row of table.
*********************************/
function selectRow(tHeaderName, selTR, rows) {
	var trLength = document.getElementById(tHeaderName).rows.length;

	var i = 0;
	var cnt = 2;
	
	if(rows) {
		cnt = rows;
	}
	
	while(i < trLength) {
		document.getElementById(tHeaderName).rows(i).className="td5";
		i += cnt;
	}

	selTR.className="select_row";
}

function unSelectRow(tHeaderName, selTR, rows) {
	var trLength = document.getElementById(tHeaderName).rows.length;

	var i = 0;
	var cnt = 2;
	
	if(rows) {
		cnt = rows;
	}
	
	while(i < trLength) {
		document.getElementById(tHeaderName).rows(i).className="td5";
		i += cnt;
	}

	selTR.className="unselect_row";
}

/*****************************
	Check radio button
******************************/
function radioChecked(obj) {	
	for(i=0; i < obj.length; ++i) {
		if(obj[i].checked)
			return true;
	}
	return false;
}

/********************************
	Check radio button of "val"
*********************************/
function checkRadio(obj, val) {
	for(i=0; i < obj.length; ++i) {
		if(obj[i].value == val)
			obj[i].checked = true;
	}
}

/********************************
	Return value of radio button
*********************************/
function returnRadioValue(obj, val) {
	var r = "";
	for(i=0; i < obj.length; ++i) {
		if(obj[i].checked == true)
			r = obj[i].value;
	}
	return r;
}


/*********************************
	Select a option of the select from of "val"
*********************************/
function selectOptions(obj, val) {
	for(i=0; i < obj.options.length; ++i) {
		if(obj.options[i].value == val)
			obj.options[i].selected = true;
	}
}


/*********************************
	Check "maxlen" in "filed"
*********************************/
function CheckStrLen(maxlen,field){
	var temp; //entered char...
   	var msglen;
   	msglen = maxlen*2;
   	var value= field.value;
   	l =  field.value.length; 
   	tmpstr = "" ;

   	if (l == 0){
    	value = maxlen*2;
   	}else{
    	for(k=0;k<l;k++){
     		temp =value.charAt(k);
     		if (escape(temp).length > 4)
   				msglen -= 2;
     		else
   				msglen--;
    
     		if(msglen < 0){
   				alert("Maximum input letter is"+(maxlen*2)+".");
    			field.value= tmpstr;
   				break;
     		}else{
   				tmpstr += temp;
    		}
    	}
   	}
}

function disableBtn(idx) {
	idx -= 1;

	document.all["btnTD" + idx + "-1"].style.display = "none";
	document.all["btnTD" + idx + "-2"].style.display = "none";
	document.all["btnTD" + idx + "-3"].style.display = "none";
	document.all["btnTD" + idx + "-4"].style.display = "none";
}

function enableBtn(idx) {
	idx -= 1;
	
	document.all["btnTD" + idx + "-1"].style.display = "block";
	document.all["btnTD" + idx + "-2"].style.display = "block";
	document.all["btnTD" + idx + "-3"].style.display = "block";
	document.all["btnTD" + idx + "-4"].style.display = "block";
}

function changeTitle(txtTitle) {
	document.all["titleTD"].innerText = txtTitle;
}
/**
Only use this if you want to block below function.
function pageKeyCheck(){    
	if(event.keyCode==13){ 
	}     
	if( (event.ctrlKey && (event.keyCode == 78 || event.keyCode == 82)) || 
       (event.keyCode >= 112 && event.keyCode <= 123)){ 
       	event.keyCode = 0; 
       	event.cancelBubble = true; 
       	event.returnValue = false; 
       }
   } 
function fnblockrip2() { return false };
document.oncontextmenu	= fnblockrip2;
document.ondragstart	= fnblockrip2;
document.onselectstart	= fnblockrip2;
document.ondoubleclick 	= fnblockrip2;
document.onkeydown = pageKeyCheck; 	
**/
