//------------------------------------------------------------------
// S: 보내는 사람 전화번호 입력 관련 스크립트
//------------------------------------------------------------------

function makeHipenForSnd( inputObj ) {
    if ( inputObj != null ) {
        var str = trim( removeHipen( inputObj.value ) );
        if ( str == "" ) {
        } else {            
            var recipient = addHipen( str, true );
            if ( recipient == "" ) {
                document.getElementById( inputObj.id ).value = "";
            } else {
                document.getElementById( inputObj.id ).value = recipient;   //Firefox버그 때문에 땜빵-inputObj.value = recipient;
            }
        }
    }
}

var check_input = function (v) {
		
	return true;
};

var ck_isNotDuplicated = function (v) {
	var result = true;
	v = v.replace(/\-/g,'');
	var inputs = e_('send_list').getElementsByTagName('input');
	for (var i=0; i < inputs.length; i++) {
		if (!/ncell/.test(inputs[i].name)) { 
			if (trim(addHipen(v)) == trim(inputs[i].value)) { //수정영역				
				result = false; 
				break;			
			}
		}
	}
	
	return result;
};

var insert_sendList = function () {

	var v = e_('send_phone').value;
	
	if ( check_input(v) ) {
		var ol = e_('send_list');
		var lis = ol.getElementsByTagName('li');
		
		if (lis.length > 9) {
			alert_module.show('alert', {
				msg : '허용 갯수 초과 '
			});
			return;
		}
		
		if (!ck_isNotDuplicated(v)) {
			alert_module.show('alert', {
				msg : '리스트에 이미 등록된 전화번호가 있습니다.'
			});
			return;
		}
		
		var index = lis.length;
		insert_field({
			name : '',
			phone : v,
			index : index
		});
	}	
}; 

var insert_sendList_byCheck = function () {
	var inputs = e_('addressScroll').getElementsByTagName('input');
	
	var ck_length = 0;
	var check_inputs = [];
	for (var i=0; i < inputs.length ; i++) {
		if (inputs[i].type == 'checkbox') {
			if (inputs[i].checked) {
				ck_length = ck_length + 1;
				check_inputs.push(inputs[i]);
			}
		}
	}
	
	if (ck_length == 0) {
		alert_module.show('alert', {
			msg : '선택하세요'
		});
		return;
	}
	
	var idx = e_('send_list').getElementsByTagName('li').length;
	var allNumber = ck_length +  idx;
	if (allNumber > 10) {
		alert_module.show('alert', {
			msg : '허용 갯수 초과'
		});
		return;
	}
	
	for (var i=0; i < check_inputs.length ; i++) {
		if (ck_isNotDuplicated(check_inputs[i].value)) {
			insert_field({
				name : check_inputs[i].name,
				phone : check_inputs[i].value,
				index : idx
			});
			check_inputs[i].checked = false;
		} else {
			alert_module.show('alert', {
				msg : '리스트에 이미 등록된 전화번호가 있습니다.'
			});
			break;
		}
		idx = idx + 1;
	}
};

var insert_field = function (opt) {
	var li, img, input, a, ol = e_('send_list');
	
	li = ol.appendChild( document.createElement('li') );
	li.id = 'p_' + opt.index;
	
	img = li.appendChild( document.createElement('img') );
	img.src = '/ysrimg/common/bg_num_0' + (opt.index+1) + '.gif';
	img.id = 'p_order_' + opt.index;
	
	input = li.appendChild( document.createElement('input') );
	input.name = 'ncell' + opt.index;
	input.id = 'ncell' + opt.index;
	input.value = (opt.name)? opt.name : '';
	input.title = '이름';
	input.className = 'name';
	input.size = '3';
	input.readOnly = 'readonly';
	
	input = li.appendChild( document.createElement('input') );
	input.name = 'cell' + opt.index;
	input.id = 'cell' + opt.index;
	var tmp_value = trim( removeHipen( opt.phone ) )
	input.value = addHipen(tmp_value);
	input.title = '전화번호';
	input.readOnly = 'readonly';
	input.size = '15';
	
	a = li.appendChild( document.createElement('a') );
	a.href = 'javascript:del_phoeList("' + li.id + '")';
	img = a.appendChild( document.createElement('img') );
	img.alt = '삭제';
	img.src = '/ysrimg/board/btn_delete_01.gif';	
	
	replaceText(e_('sendCnt'), parseInt(e_('sendCnt').firstChild.nodeValue) + 1);
};

var del_phoeList = function (id) { 
	e_(id).parentNode.removeChild(e_(id));
	
	// 순번 이미지 다시 셋팅 
	var images = e_('send_list').getElementsByTagName('img');
	var order = 0;
	for (var i = 0 ; i < images.length ; i++) {
		if (/p\_order\_/.test(images[i].id)) {
			order = order + 1;
			images[i].src = '/ysrimg/common/bg_num_0' + order + '.gif'; 
		}
	}
	
	replaceText(e_('sendCnt'), parseInt(e_('sendCnt').firstChild.nodeValue) - 1);
};

