﻿// JScript File



var E_SEP = '#';
var E_BULLET = '<li>';

var Errortype = {
    Notice: 'notice',
    Warning: 'warning',
    Completed: 'completed',
    Error: 'error'
}
var ELEMENT = {
    Prefix: '#ctl00_ContentPlaceHolder1_',
    Value: 'value',
    Id: 'id',
    
    GetObject:function(eid){
	    return $(this.Prefix + eid);
    },
    GetValue:function(e){
	    return e.attr(this.Value);
    },
    GetId:function(e){
	    return e.attr(this.Id);
    }
}

//check data
var charExp = /./
var letterExp = /[a-z]/i
var phoneExp =  /^\d{10}$/
var memberExp = /^\d{3}$/
var zipExp = /^\d{5}$|^\d{5}[\-\s]?\d{4}$/
var emailExp = /^[a-z][a-z_0-9\.]+@[a-z_0-9\.]+\.[a-z]{3}$/i
var pledgExp = /^\d*$|^\d*\.\d{2}$/
var userExp = /^[a-z][a-z_0-9\.]+[a-z_0-9\.]$/

var VALIDDATA = {
    //properties
    Notice: 'notice',
    Warning: 'warning',
    Completed: 'completed',
    Error: 'error',
    
    Separate: '#',
    Bullet: '<li>',
    TXTERRORS: '',
    //methods
    IsNullOrEmpty:function(param){
        if(param==null || trim(param).length==0) return true;
	    return false;
    },
    IsNumber:function(param){
        return !isNaN(param);
    },
    IsDate:function(param){
        if(this.IsNullOrEmpty(param)) return false;
        var minYear = 1900;
        var maxYear = 3000;
        re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
        if(regs = param.match(re)) {
            if(regs[1] < 1 || regs[1] > 31) return false;
            else if(regs[2] < 1 || regs[2] > 12) return false;
            else if(regs[3] < minYear || regs[3] > maxYear) return false;
        } 
        else return false;
        return true;
    },
    IsAlpha:function(param){
        return userExp.test(param)
    },
    IsEmail:function(param){
		var SearchEmail = ""
        re = /@/ig;
        SearchEmail = param.search(re);
        if(SearchEmail == -1) return false;    
        re = /\./ig;
        SearchEmail = param.search(re);
        if(SearchEmail == -1) return false;
        re = / /ig;
        SearchEmail = param.search(re);    
        if(SearchEmail != -1) return false;    
        re = /(@.+@)|(@@)|(@\.)|(\.\.)|(\.@)/ig; // Search for multiple @ and @.
        SearchEmail = param.search(re);
        if(SearchEmail >= 1) return false;
        re = /(\/)|(\[)|(\:)|(\{)|(\})|(\])|(\))/ig; // Search for illegal address characters
        SearchEmail = param.search(re);
        if(SearchEmail >= 1) return false;
        return true;    
    },

    
    Init:function(){
//        var pnl = $('#ctl00_ContentPlaceHolder1_pnlError');
//        var lbl = $('#ctl00_ContentPlaceHolder1_lblError');
//        if(this.IsNullOrEmpty(lbl.html())) pnl.hide();
//        else pnl.show();
    },
    Get_Error:function(errortype, tag){
        var msg = "<font class='" + errortype + "'>" + this.Bullet + Get_Definephrase(tag) + "</li></font>";
        return msg;
    },
    Get_ErrornoB:function(errortype, tag){
        var msg = "<div style='padding:5px;padding-left:15px;'><font class=" + errortype + ">" + Get_Definephrase(tag) + "</font></div>";
        return msg;
    },
    Show_Error:function(txt, msg){
        VALIDDATA.Clear_Error(this.TXTERRORS);
        if(this.IsNullOrEmpty(txt) || this.IsNullOrEmpty(msg)) return false;
        //var symclose = "<div style='float:right'>[<a href='javascript:void(0);' onclick=javascript:VALIDDATA.Clear_Error(); class='symclose'><img src='../images/symclose.gif' border='0' align='absmiddle' alt='Close' /></a>]</div>";
        $('#ctl00_ContentPlaceHolder1_lblError').html(msg);
        //$('#ctl00_ContentPlaceHolder1_pnlError').show().focus();
        this.TXTERRORS = txt;
        
	    var arr = txt.split(this.Separate);
	    if(arr==null || arr.length==0) return true;
	    for(var i=1; i<arr.length; i++){
	        var e = $("#" + arr[i]);
	        e.css("background-color","#fffbc6");
	    }
	    return true;
    },
    Show_Erroradd:function(txt, msg){
        if(this.IsNullOrEmpty(txt) || this.IsNullOrEmpty(msg)) return false;
        //$('#ctl00_ContentPlaceHolder1_pnlError').show().focus();
        var lbl = $('#ctl00_ContentPlaceHolder1_lblError');
        lbl.show();
        var temp = lbl.html() + msg;
        lbl.html(temp);
	    
	    var arr = txt.split(this.Separate);
	    if(arr==null || arr.length==0) return true;
	    for(var i=1; i<arr.length; i++){
	        var e = $("#" + arr[i]);
	        e.css("background-color","#fffbc6");
            if(i==1) e.focus();
	    }
	    return true;
    },
    Clear_Error:function(){
        $('#ctl00_ContentPlaceHolder1_lblError').html("");
        //$('#ctl00_pnlError').hide();

        var txt = this.TXTERRORS;
        if(this.IsNullOrEmpty(txt)) return false;
        var arr = txt.split(this.Separate);
	    if(arr==null || arr.length==0) return true;
	    for(var i=1; i<arr.length; i++){
	        $("#" + arr[i]).css("background-color","#ffffff");
	    }
	    return true;
    }
    
}



function trim(str) {
	return str.replace(/^\s+|\s+$/g,"");
}

function UICurrency2Num(param){
    param = param.replace(/^\s+\$|\$|\s+_|_|\s+,|,/g,"");
    if(VALIDDATA.IsNullOrEmpty(param)) return 0;
    if(param.indexOf('.')==0)
        param = '0' + param;
    if(param.indexOf('.')==param.length-1)
        param += '00';
    return Math.abs(param);
}
function Num2UICurrency(param){
    if (VALIDDATA.IsNullOrEmpty(param)) return '';
    var arrchar = param.toString().split('.');
    var n_head = arrchar[0];
    var n_foot = arrchar.length > 1 ? arrchar[1] : '';
    
    var n_head_result = '';
    if (n_head.length > 3){
        var n_head_mod = n_head.length % 3;
        var n_head_div = parseInt(n_head.length/3);

        for (i = 0; i <= n_head_div; i++){
            if(i==n_head_div && n_head_mod==0) break;
            if(n_head_mod!=0)
                if(i==0)
                    n_head_result += n_head.substring(0, n_head_mod) + ',';
                else
                    n_head_result += n_head.substring((i-1)*3+n_head_mod, (i-1)*3+n_head_mod+3) + ',';
            else
                n_head_result += n_head.substring(i*3, i*3+3) + ',';
        }
        n_head_result = n_head_result.substring(0, n_head_result.length-1);
    }
    return (n_head_result==''? n_head : n_head_result) + (n_foot != "" ? ("." + (n_foot.length==1? n_foot + '0' : n_foot.substring(0,2))) : ".00");
}

function UICurrency_In(e){
    var theelem = CC_Getelement(e.id);
    var value = theelem == null ? 0 : UICurrency2Num(theelem.value);
    theelem.value = value == 0 ? '' : value.toString();
    return;
}
function UICurrency_Out(e){
    var theelem = CC_Getelement(e.id);
    var value = theelem == null ? 0 : UICurrency2Num(theelem.value);
    if(value!=0)
        theelem.value = '$ ' + Num2UICurrency(value.toString());
    else
        theelem.value = '$ ';    
    return;
}
function UINumber_In(e){
    var keycode;
	if(window.event) keycode=window.event.keyCode;
	else if(e) keycode=e.which;
	else return true;
	//alert(keycode);
	if(keycode>47 && keycode<58){return true;}
	else return false;
}
function UINumber_Out(e){
    var theelem = CC_Getelement(e.id);
    theelem.value = VALIDDATA.IsNumber(theelem.value) ? theelem.value : 0;
    return;
}