﻿
/*
    对 标有属性va:num = 1 的input进行数据化验证
*/
var _listdiv_ref;
var _body_height = 0;

function _go(e)
{
    try
    {
        var temp = errorList.item(e.target).value[0]
        
        temp.focus();
        temp.click();
        
        var _p = Point.Get(temp);
        var _listdiv = $("_error_list");
        
        if(_body_height < (_p.Y + _listdiv.scrollHeight + 40) ) //要向上
            _listdiv.style.top  = _p.Y - _listdiv.scrollHeight;
        else
            _listdiv.style.top  = _p.Y + 21;
        
        _listdiv_ref = temp;
        e.style.color = "#ff8800";
    }
    catch(ex){}
}
function _update()
{
    if(validateExpr())
        validateMustIn();
}
var errorList = new HashTable();
errorList.show = function()
{
    var _listdiv = $("_error_list");
    if(_listdiv == null)
    {
        _listdiv = document.createElement("div");
        _listdiv.id = "_error_list";
        _listdiv.style.cssText="border:solid 1px black; width:220px; position:absolute;z-index:10;background-color:White;padding:10px;";
        document.forms[0].appendChild(_listdiv);
    }
    
    var html = "<div>\n<div style='line-height:200%;'>" + window.errorHint + "<a href='javascript:_update();'>(" + window.errorRefurbish + ")</a></div>\n";
    
    var len = errorList.length;
    var item;
    var innerHTML="";
    for(var i=0;i<len;i++)
    {
        item = errorList.item(i);
        innerHTML+="<span style='padding-left:20px; line-height:140%; cursor:default;' target='"+item.key+"' onclick='_go(this);'>" + (i+1) + ". " + item.value[1] + "</span><br/>\n";
    }
    
    html+= innerHTML + "</div>\n";
    
    
    if(event != null)
    {
        var src = event.srcElement;
    
        if(src.nodeName == "input" || _listdiv_ref == null)
            _listdiv_ref = src;
    }
    
    if(_body_height == 0)
        _body_height = document.body.scrollHeight;
        
    
    var _p = Point.Get(_listdiv_ref);
    
    //document.write(html); 
    _listdiv.innerHTML = html;
    
    
    if(_listdiv_ref == src)
        _listdiv.style.left = _p.X - (_listdiv.scrollWidth - 69);
    
    
    if(_body_height < (_p.Y + _listdiv.scrollHeight + 10) ) //要向上
        _listdiv.style.top  = _p.Y - _listdiv.scrollHeight;
    else
        _listdiv.style.top  = _p.Y + 21;
    
    _listdiv.style.display = "";
}

errorList.hide = function()
{
    var _listdiv = $("_error_list");
    
    if(_listdiv != null)
        _listdiv.style.display = "none";
}

//------------------------------------------------------------------------------------------

function limitNumber()
{
    var e = event.srcElement;
        
    if(!e.readOnly)
    {   
        var code = event.keyCode;
        
    	if(code < 48)
        	return;
        
        if(code == 190 || code == 110)
        {
            var ind = e.value.indexOf(".");
            
            if(ind == 0)
            {
                e.value = "";
                
                return;
            }
            
            if(e.value.indexOf(".",ind+1) < 0)
                return;           
        }
        
        if(code >= 48 && code <= 57)
            return;
        
        if(code >= 96 && code <=105)
            return;
            
        var expr = e.getAttribute("va:expr");
        
        if(e.getAttribute("va:num") != "1" && expr != "post" && expr !="num")
            return;
        
        if(e.type != "text")
            return;
                
        var temp = parseFloat(e.value);
        
   	 	if(isNaN(temp)) 
   	 	{
        	e.value= "";
        	
        	return false;
        }
        else
        {
            e.value = temp;
            
            return true;
        }
    }
    
    return false;
}

//onkeyup

//
//核对 va:mustIn=1 的项,是否都已有数据[暂只对input项有效]
//
function validateMustIn(c)
{
    var list = $PV("va:mustin","1","input",c);
    
    var len = list.length;
    var item;
    var check = true;
    errorList.clear();
    
    for(var i=0; i<len; i++)
    {
        item = list[i];
        
        if((item.value.length < 1) && (item.disabled == false) && (Disp(item)))
        {
            errorList.add(item.id, [item,getEmsg(item["va:emsg"])]);
            check = false;
        }
    }
    if(check == false)
        errorList.show();
    else
        errorList.hide();
        
    return check;
}

var MAIL_RE = new RegExp("(.*)@(.*)");
var HTTP_RE = new RegExp("http(s)?://([\w-]+\.)+[\w-]+(/[\w-])*");
var POST_RE = new RegExp("^[1-9]{1}[0-9]{5}$");
var DATE_RE = new RegExp("^([1-9]{1})([0-9]{3})[-]{1}([0-9]{1,2})[-]{1}([0-9]{1,2})$");
var NUM_RE  = new RegExp("^[0-9]*[\.]?[0-9]*$");
var ORG_CODE_RE = new RegExp("^[0-9A-Z]{8}\-[0-9A-Z]{1}(\([0-9A-Z]{2}\))?$");

function validateExpr(c)
{
    var list = $PV("va:expr",null,"input",c);
    
    var check = true;
    var itemcheck = true;
    errorList.clear();
    
    $Foreach(list,function(e,i)
    {        
        itemcheck = true;
        
        switch(e["va:expr"])
        {
            case "num":
                itemcheck = (NUM_RE.test(e.value)  || e.value == "");
                break;
            case "date":
                itemcheck = (DATE_RE.test(e.value) || e.value == "");
                break;
                
            case "post":
                itemcheck = (POST_RE.test(e.value) || e.value == "");
                break;
            
            case "mail":
                itemcheck = (MAIL_RE.test(e.value) || e.value == "");
                break;                
                
            case "http":
                itemcheck = (HTTP_RE.test(e.value) || e.value == "");
                break;
                
            case "orgcode":
                itemcheck = (ORG_CODE_RE.test(e.value) || e.value == "");
                break;            
        }
        
        if(itemcheck == false)
        {
            errorList.add(e.id,[e, getEmsg(e["va:emsg"])]);
            check = false;
        }
    });
    
    if(check == false)
        errorList.show();
    else
        errorList.hide();
    
    return check;
}

function Disp(e)
{
    try
    {
        return e.parentElement.parentElement.style.display != "none";
    }
    catch(ex){return true;}
}

function getEmsg(key)
{
    if(key == null)
        return "1";
    else
    {
        try
        {   
            if(key.substr(0,1) == "#")
            {
                var temp = $(key.substr(1)).innerText.replace(/:/,"");
                return temp.replace(/：/,"");
            }
            else
            {
                return key;
            }
        }
        catch(ex)
        {
            alert( "#"+key+" 对象没有 innerText属性");
        }
    }
}

/*

va:expr="post"
va:expr="mail"
va:expr="http"
va:expr="orgcode"
va:expr="num"

va:emsg="#ID"

*/

