
var frm_pc = document.getElementById("frmpc") ;
if (frm_pc)	frm_pc.onsubmit = validar_envio ;

var bt_add_friend = document.getElementById("addfriend") ;
if (bt_add_friend) bt_add_friend.onclick = add_friend ;


function remove_friend()
{
	tbody = document.getElementById("tblfriends") ;
	tr = this.parentNode.parentNode ;
	tbody.removeChild(tr) ;
}

function add_friend() 
{
	tbody = document.getElementById("tblfriends") ;
	
	//Maximo 10 personas
	cuantos = tbody.getElementsByTagName('tr').length ;
	if (cuantos >=10) return false ;
	
	//Creando la fila
	tr = document.createElement("tr") ;
	//Creando las celdas
	td1 = document.createElement("td") ;
	td2 = document.createElement("td") ;
	td3 = document.createElement("td") ;
	
	txt_nom = document.createElement("input") ;
	txt_nom.setAttribute("name", "friend_name[]") ;
	txt_nom.type = "text" ; 
	txt_nom.size = 25 ;
		
	txt_mail= document.createElement("input") ;
	txt_mail.setAttribute("name", "friend_email[]") ;
	txt_mail.type = "text" ; 
	txt_mail.size = 25 ;
	
	
	btn = document.createElement("input") ;
	btn.type = "button" ;
	btn.value = " x " ;
	btn.onclick = remove_friend ;
	
	td1.appendChild(txt_nom) ;
	td2.appendChild(txt_mail) ;
	td3.appendChild(btn) ;
	
	//Agregando las celdas
	tr.appendChild(td1) ;
	tr.appendChild(td2) ;
	tr.appendChild(td3) ;
	
		
	tbody.appendChild(tr) ;
	
	txt_nom.focus() ;
	return false ;
}


function validar_envio()
{
	tbody  = document.getElementById("tblfriends") ;
	nombre = Trim(frm_pc.cname.value) ;
	email  = Trim(frm_pc.cemail.value) ; 
	msg    = Trim(frm_pc.cmensaje.value);
	
	if (nombre.length < 2){
		alert("Ingrese su nombre") ;
		frm_pc.cname.focus() ;
		return false ;
	}
	if (!isMail(email)){
		alert("Ingrese su E-Mail") ;
		frm_pc.cemail.focus() ;
		return false ;
	}
	if (Trim(msg)==''){
		alert("Escriba el mensaje") ;
		frm_pc.cmensaje.focus() ;
		return false ;
	}
	
	trs = tbody.getElementsByTagName("tr") ;
	cuantos = 0
	for (i=0 ; i < trs.length ; i++){
		ctrls = trs[i].getElementsByTagName("input") ;
		nombre = Trim(ctrls[0].value) ;
		email  = Trim(ctrls[1].value) ;
		
		if (nombre.length == 0 && email.length == 0){	
			cuantos++ ;
			continue ;
		}else{
			
			if (nombre.length < 1){
				alert("Ingrese el nombre de su amigo") ;
				ctrls[0].focus() ;
				return false ;
			}
	
			if (!isMail(email)){
				alert("Ingrese el e-mail de su amigo") ;
				ctrls[1].focus() ;
				return false ;
			}
		}
		
	}
	
	if (cuantos==trs.length){
		alert("Ingrese el nombre de su amigo") ;
		ctrls = trs[0].getElementsByTagName("input")[0].focus() ;
		return false ;
	}
	
	return true ;
}

