/**
 * Fetnet eCare DM print javascript program
 * Referenced library:
 *   jQuery.js (http://jquery.com)
 *   enhancement.js (Collector & Creator & Editor: Terence Chao)
 *
 * Created by Terence Chao Since 2009/07
 */

// PayType Class Definition
var PayType = function( payTypeTd, payTypeDetailTd, payTypeRadio, payTypeSign, attHeader, att ) {
    this.payTypeTd = payTypeTd;
    this.payTypeDetailTd = payTypeDetailTd;
    this.payTypeRadio = payTypeRadio;
    this.payTypeSign = payTypeSign;
    this.attHeader = attHeader;
    this.att = att;
    this.selectedStatus = false;
    this.validate = {};
};

PayType.prototype.addValidator = function( name, validators ) {
    this.validate[name] = validators;
};

PayType.prototype.init = function() {
    $( '#' + this.payTypeTd ).attr( 'colSpan', '4' );
    $( '#' + this.payTypeDetailTd ).hide();
    $( '#' + this.payTypeRadio ).attr( 'checked', null );
    $( '#' + this.payTypeSign ).hide();
    $( '#' + this.attHeader ).hide();
    $( '#' + this.att ).hide();
    this.selectedStatus = false;
    
    // init should remove class from ui component
    this.removeValidator();
};

PayType.prototype.typeSelected = function() {
    $( '#' + this.payTypeTd ).attr( 'colSpan', '2' );
    $( '#' + this.payTypeDetailTd ).show();
    this.selectedStatus = true;
    
    var name;
    for(name in this.validate) {
        if(typeof this.validate[name] !== 'function') {
            $( '#' + name ).addClass( this.validate[name] );
        } // if not function
    } // for
};

PayType.prototype.printState = function() {
    $( '#' + this.payTypeTd ).attr( 'colSpan', '1' );
    $( '#' + this.payTypeTd ).attr( 'width', '33%' );
    $( '#' + this.payTypeDetailTd ).attr( 'colSpan', '2' );
    $( '#' + this.payTypeSign ).show();
    $( '#' + this.attHeader ).show();
    $( '#' + this.att ).show();
};

PayType.prototype.removeValidator = function() {
    var name;
    for(name in this.validate) {
        if(typeof this.validate[name] !== 'function') {
            $( '#' + name ).removeClass( this.validate[name] );
        } // if not function
    } // for
};

PayType.prototype.reset = function() {
    $( '#' + this.payTypeTd ).show();
    this.init();
};

PayType.prototype.hide = function() {
    $( '#' + this.payTypeTd ).hide();
    $( '#' + this.payTypeDetailTd ).hide();
    
    // hide should remove class from ui component
    this.removeValidator();
};

PayType.prototype.afterPrint = function() {
};

// Global Variable for stored pay type 
var DMApp = {};

// 怪怪Validate
DMApp.validateValueGroupAtLeastOne = function(type, idregex, errlist, errmsg) {
    var checked = false;
    if( Object.prototype.toString.call( 'object' )) {
        $( 'input[type="' + type + '"]' ).each( function() {
            if(idregex.test( $(this).attr( 'id' ) )) {
                if( $(this).attr( 'value' ).length > 0 ) {
                    checked = true;
                }
            }
        } );
    } else {
        throw new Error( 'need regexp object' );
    }
    if( !checked ) {
        errlist.push( errmsg );
    }
    return checked;
};

DMApp.validateCheckedGroupAtLeastOne = function(type, idregex, errlist, errmsg) {
    var checked = false;
    // regex object 在不同實作版本 typeof 結果不一樣，有的是object有的是function
    // 但不會是 regexp 哈哈
    if( typeof idregex === 'object' || typeof idregex === 'function' ) {
        $( 'input[type="' + type + '"]' ).each( function() {
            if(idregex.test( $(this).attr( 'id' ) )) {
                if( $(this).attr( 'checked' ) == true ) {
                    checked = true;
                }
            }
        } );
    } else {
        throw new Error( 'need regexp object' );
    }
    if( !checked ) {
        errlist.push( errmsg );
    }
    return checked;
};

DMApp.validateCreditCardExpiredDateFromThisDate = function( monthCompId, yearCompId, validRange, errlist ) {
    if( /^0[1-9]{1}|^1[0-2]{1}/.test( $( '#' + monthCompId ).attr( 'value' ) ) ) {
        var t = new Date();
        var d2 = new Date( '20' + $( '#' + yearCompId ).attr( 'value' ) + ' ' + $( '#' + monthCompId ).attr( 'value' ) + ' 01');
        //alert(d2);
        var diff = (d2 - t)/86400000;
        //alert(diff);
        if(diff < 0) {
            errlist.push( '很抱歉, 您的信用卡已經過期!\n' );
            return false;
        } else if( diff <= validRange ) {
            errlist.push( '很抱歉, 此張信用卡即將到期, 請選擇其他信用卡辦理自動轉帳!\n' );
            return false;
        }
        return true;
    } else {
        errlist.push( '信用卡月份輸入資料錯誤!\n' );
        return false;
    }
};

DMApp.checkAlternative = function( firstId, secondId, errlist, errstyle ) {
    if( $( '#' + firstId ).attr( 'value' ) !== '' )
        return true;
    else if ( $( '#' + secondId ).attr( 'value' ) !== '' )
        return true;
    else {
        errlist.push( $( '#' + firstId ).attr( 'alt' ) + ' 或 ' + $( '#' + secondId ).attr( 'alt' ) + '至少須填寫一個\n' );
        if( errstyle ) {
            document.getElementById( firstId ).setAttribute( 'style', errstyle );
            document.getElementById( secondId ).setAttribute( 'style', errstyle );
        }
        return false;
    }
};

/**
 * 預設Array.prototype.toString的行為會用「,」分隔每個element
 * 這裡顯示errlist不需要「,」，所以改寫
 */
Array.method( 'toString', function() {
    var i;
    var result = '';
    for(i=0; i<this.length; i++) {
        result = result + this[i];
    } // for
    return result;
});

