﻿var policyNo;
var companyName;
var currentButton;
var currentDeleteButton;
var currentSelect;

var sampleRoot;
var insertStyle;
var deleteStyle;
var confirm;
var selectStyle;

function cleanWhitespace( element ) {
// If no element is provided, do the whole HTML document
element = element || document;
// Use the first child as a starting point
var cur = element.firstChild;
// Go until there are no more child nodes
while ( cur != null ) {
// If the node is a text node, and it contains nothing but whitespace
if ( cur.nodeType == 3 && ! /\S/.test(cur.nodeValue) ) {
// Remove the text node
element.removeChild( cur );
// Otherwise, if it's an element
} else if ( cur.nodeType == 1 ) {
// Recurse down through the document
cleanWhitespace( cur );
}
cur = cur.nextSibling; // Move through the child nodes
}
}

window.onload=function(){
    cleanWhitespace();
    var url="company.htm";
    url += (url.indexOf("?")==-1?"?":"&") + "timestamp=" + new Date().getTime();
    httpRequest("GET",url,false,handleResponse);
                //setQueryString();
   // ResetResult(document.getElementById("PolicyResult2").getElementsByTagName("span")[0]);
   // SelectItem(document.getElementById("SelectItem1").childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0]);
}

function SelectItem(obj)
{
//    var rootItem=obj.parentNode.parentNode;
//    var tds=rootItem.getElementsByTagName("td");
//    var bodys=document.getElementById("SelectItem1").parentNode;
//    var tables=bodys.childNodes;
//    var index=0;
//    for (var i = 0; i < tds.length; i++)
//    {
//        if(tds[i].childNodes[0]==obj)
//        {
//            index=i;
//        }
//        tables[i*2+3].style.display="none";
//        tds[i].className="tdmenu_off";
//    }
//    tables[index*2+3].style.display="";
//    tds[index].className="tdmenu_on";
}

//function sendData(){
//    //setQueryString();
//    var url="Receive.aspx";
//    url += (url.indexOf("?")==-1?"?":"&") + "timestamp=" + new Date().getTime();
//    httpRequest("GET",url,true,handleResponse);
//}

//event handler for XMLHttpRequest
function handleResponse(){
    if(request.readyState == 4){
        if(request.status == 200){
            var resp =  request.responseText;
            if (resp != null){
                //return value is a JSON array
                //var objt = eval(resp);
                //var doc = request.responseText;
                //var info = getDocInfo(doc);
                var func = new Function("return "+resp);
                var objt = func();
//                var sel = document.createElement("select");
//                sel.setAttribute("id","company");
                
                createOptions_1("company",objt);
                
            //    var mainObj =  $('#PolicyBaseData2').children().children().childNodes[3];
                //reset(mainObj);
           //     mainObj.appendChild(sel);
                //mainObj.appendChild(sel);
                var selObj = $('#company');
                selObj.value="TW";
//                mainObj = document.getElementById("Text1");
//                reset(mainObj);
//                document.getElementById("Div1").style.visibility = "hidden";
//                mainObj = document.getElementById("addRows");
//                reset(mainObj);
                //stylizeDiv(doc,document.getElementById("Text1"));
           }
        } else {
            //alert(request.status);
            //alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
            alert("主機忙碌中，請稍後再試。");
        }
    }//end outer if
}

//function createOptions1(sel,_options) {
//    //_options is an array of strings that represent the values of
//    //a select list, as in each option of the list. sel is the select object
//    if(_options == null || _options.query.length==0) { return;}
//    var opt = null;
//    for(var i = 0; i < _options.query.length; i++) {
//        opt = document.createElement("option");
//        opt.setAttribute("value",_options.query[i].id);
//        opt.appendChild(document.createTextNode(
//        _options.query[i].c0 + ":" +
//        _options.query[i].c1 + ":" +
//        _options.query[i].c2 + ":" +
//        _options.query[i].c3 + ":" +
//        _options.query[i].c4 + ":" +
//        _options.query[i].c5 + ":" +
//        _options.query[i].c6 + ":" +
//        _options.query[i].c7));
//        sel.appendChild(opt);
//    }
//}



function stylizeDiv(bdyTxt,div){
    //reset DIV content
    div.innerHTML="";
    div.style.backgroundColor="yellow";
     div.innerHTML=bdyTxt;
}

function getDocInfo(doc){
    var root = doc.documentElement;
     var info = "<h3>Document root element name: <h3 />"+ root.nodeName;
     var nds;
     if(root.hasChildNodes())  {
           nds=root.childNodes;
           info+= "<h4>Root node's child node names/values:<h4/>";
           for (var i = 0; i < nds.length; i++){
               info+=  nds[i].nodeName;
               if(nds[i].hasChildNodes()){
                  info+=  " : \""+nds[i].firstChild.nodeValue+"\"<br />";
               } else {
                  info+=  " : Empty<br />";
               }
           }
     }
    return info;
}

