﻿function isIE(){
    var browser;
    if($.browser.mozilla)
      return false;
    else if($.browser.msie)
      return true;
    else if($.browser.opera)
      return false;
    else if($.browser.safari)
      return false;
    else
      return false;
}
function checkIEUnder9()
{

   if ( isIE() )
   {
      //return $.browser.version;
      if ($.browser.version<9) return true;
      return false;
   }
   return false;
}


function createOptions(obj,_items) {
    //_items is an array of strings that represent the values of
    //a select list, as in each option of the list. obj is the select object
    if(_items == null || _items.query.length==0) { return;}
    var item = null;
    for(var i = 0; i < _items.query.length; i++) {
        item = document.createElement("option");
        item.setAttribute("value",_items.query[i].id);
        item.appendChild(document.createTextNode(_items.query[i].title));
        
        obj.appendChild(item);
    }
}
function createOptions_1(objName,_items){
    if(_items == null || _items.query.length==0) { return;}
    var item = null;
    for(var i = 0; i < _items.query.length; i++) {
        $('#'+objName).
          append($("<option></option>").
          attr("value",_items.query[i].id).
          text(_items.query[i].title));   
//        item = document.createElement("option");
//        item.setAttribute("value",_items.query[i].id);
//        item.appendChild(document.createTextNode(_items.query[i].title));
//        
//        obj.appendChild(item);
    }
}

function createOptions1(obj,_items) {
    //_items is an array of strings that represent the values of
    //a select list, as in each option of the list. obj is the select object
    if(_items == null || _items.query.length==0) { return;}
    var item = null;
        item = document.createElement("option");
        item.setAttribute("value","");
        item.appendChild(document.createTextNode("請選擇"));
        obj.appendChild(item);
    for(var i = 0; i < _items.query.length; i++) {
        item = document.createElement("option");
        item.setAttribute("value",_items.query[i].COMPANYNO + "|" + _items.query[i].MARK + "|" + _items.query[i].InsName + "|" + _items.query[i].Unit + "|" + _items.query[i].INC + "|" + _items.query[i].Buyer);
        item.appendChild(document.createTextNode("(" + _items.query[i].MARK + ")" + _items.query[i].InsName));
        obj.appendChild(item);
    }
}

function createTDs(obj,_items) {
    if(_items == null || _items.query.length==0) { return;}
    var item = null;
    for(var i = 0; i < _items.query.length; i++) {
        item = document.createElement("td");
        item.className=_items.query[i].id;
      //  item.innerText=_items.query[i].title;
        if (document.all) { // IE;
                 
            item.innerText=_items.query[i].title;
        }
        else{
            item.innerHTML=_items.query[i].title;
            
        }
        obj.appendChild(item);
    }
}

//remove any existing children from an Element object
function reset(obj){
    if(obj != null && obj.hasChildNodes()){
        while(obj.hasChildNodes())
        {
            obj.removeChild(obj.firstChild);
        }
//        for(var i = 0; i < obj.childNodes.length; i++){
//            obj.removeChild(obj.firstChild);
//        }
    }
}

function openwin(obj)
{
    var objParent=obj;
    var rootItem=document.getElementById("PolicyResult2");
    var divs=rootItem.getElementsByTagName("div");
    var spans=rootItem.getElementsByTagName("span");
    var index=0;
    for (var i = 0; i < divs.length; i++)
    {
        if(spans[i]==objParent)
        {
            index=i;
            break;
        }
    }
    win=open('','','toolbar=0,width=790,height=600,top=0,left=10000');
    win.document.write("<link rel=stylesheet href=css/style.css type=text/css>");
    win.document.write("<title>線上即時保單健檢-台灣人壽</title>");
    win.document.write("<img src=images/background.jpg width=770 height=132 border=0>");
    win.document.write("<div class='printTitle'>" + spans[index].innerHTML + "</div>");
    win.document.write("<hr />");
    win.document.write(divs[index].innerHTML);
    win.document.write("<img src=images/Footer.jpg width=770 height=40 border=0>");
    win.document.location.reload();
    win.print();
    window.setTimeout("win.close()",1000);
}

function IfDateA(y,m,d){
	var yn = y;
	var mn = m;
	var dn = d;
	if (!IfNumberA(yn,"年份"))
		return false;
	if (!IfNumberA(mn,"月份"))
		return false;
	if (!IfNumberA(dn,"日"))
		return false;		
	var stdate = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if (((Math.floor(yn)%4==0) && (Math.floor(yn)%100!=0)) || (Math.floor(yn)%400==0))
		stdate[1] = 29;
	if (Math.floor(yn) < 1770 )
	{
		alert('日期:年份的格式不符，請輸入1770~9999。');
		if (y !='undefined')
		return false;
	}	
	if (Math.floor(mn) > 12 || Math.floor(mn) < 1)
	{
		alert('日期:月份的格式不符，請輸入1~12。');
		if (m !='undefined')
		return false;
	}
	if (Math.floor(dn) > Math.floor(stdate[Math.floor(mn)-1]) || Math.floor(dn) < 1)
	{
		alert('日期:天數的格式不符，請輸入1~' + stdate[Math.floor(mn)-1] + '。');
		if (d !='undefined')
		return false;
	}
	return true;
}

function IfNumberA(obj,Msg){
	if (IfEmptyA(obj,Msg))
		return false;			
	if (isNaN(obj))
	{
		if (String(Msg).length > 0)
			alert(Msg + "必須是數字。");
			if (typeof(obj) !='undefined')
						return false;
					return false;
	}	
	return true;
}
function IfEmptyA(obj,Msg){
	if ($trim(obj).length == 0)
	{
		if (String(Msg).length > 0)
			alert(Msg + "不可以空白。");
			if (typeof(obj) !='undefined')
						return true;
		return true;
	}	
	return false;
}

function $trim(x)
{
    return x.replace(/^\s+/, '').replace(/\s+$/, '');
}
