// JavaScript Document

function isStore() {
    var _cbo_store = document.getElementById('cbo_store');
    if(_cbo_store.value == 'Other') {
        return ( isNull_cbo( document.getElementById('cbo_store'), true, 'Please select a store.' )
                    &&
                 isNull_txt( document.getElementById('txt_specify'), true, 'Please specify the store.' )
                    &&
                 isNull_txt( document.getElementById('txt_location'), true, 'Please enter the store location.' )
               );
        }
    else {
        return ( isNull_cbo( document.getElementById('cbo_store'), true, 'Please select a store.' )
                    &&
                 isNull_txt( document.getElementById('txt_location'), true, 'Please enter the store location.' )
               );        
    }	
}

function isIMEI_Check( _txt, intTxtLength, _isAlert, strErrorMsg )
{
    if( _txt.value != null && isNumeric( _txt.value ) == true && _txt.value.length == intTxtLength ){
        return true;
    }
    else{
        if( _isAlert ){
            window.alert( strErrorMsg );
            _txt.focus();
			_txt.select();
        }
        return false;
    }
}
	
function field_validation() {
	return ( isNull_txt( document.getElementById('txt_name'), true, 'Please enter your name.' ) 
	            &&
	         isNull_txt( document.getElementById('txt_address'), true, 'Please enter your address.' ) 
	            &&
	         isPhone_txt( 3, document.getElementById('txt_contact'), true, 'Please enter a valid contact number.' ) 
	            &&
	         isEmail_txt( document.getElementById('txt_email'), true, 'Please enter a valid email.' ) 
	            &&
	         isNull_txt( document.getElementById('txt_DOB'), true, 'Please enter your date of birth.' )
	            &&
	         isIMEI_Check( document.getElementById('txt_imei01'), 6, true, 'Please enter a valid IMEI.' )
	            &&
	         isIMEI_Check( document.getElementById('txt_imei02'), 2, true, 'Please enter a valid IMEI.' )
	            &&
	         isIMEI_Check( document.getElementById('txt_imei03'), 6, true, 'Please enter a valid IMEI.' )
	            &&
	         isIMEI_Check( document.getElementById('txt_imei04'), 1, true, 'Please enter a valid IMEI.' )
	            &&
	         isStore() 
	            &&
             isNull_txt( document.getElementById('txt_DOP'), true, 'Please enter the date of purchase.' )
	            &&
	         isNull_txt( document.getElementById('file_receipt'), true, 'Please attach the receipt.' )
	            &&
	         isChecked_chk( document.getElementById('chk_tnc'), true, 'Please read and agree to the terms and conditions.' )
	         );
}

function fnNumberOnly(e){
    var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
    
    if( ( keyCode >=  48 && keyCode <=  57 ) ||   //numbers 0 ~ 9 : 48 ~ 57
        ( keyCode >=  96 && keyCode <= 105 ) ||   //numbers from keypad 0 ~ 9 : 96 ~ 105         
        keyCode ==   8 ||    //BackSpace
        keyCode ==  46 ||    //Delete
        //keyCode == 110 ||    //fullstop(.) : 
        //keyCode == 190 ||    //fullstop(.) : keypad
        keyCode ==  37 ||    //left arrow
        keyCode ==  39 ||    //right arrow
        keyCode ==  35 ||    //End 
        keyCode ==  36 ||    //Home 
        keyCode ==   9       //Tab
    ) {
        return true;
    }
    else
        return false;
}

function sendTab( ctl, len, nextCtl) {
    var _txt = document.getElementById( ctl );
    var _nextTxt = document.getElementById( nextCtl );
    if( _txt.value.length == len ){
        _nextTxt.focus();
    }
}
