var replaces=[];
var magic='';
var mCode='';
var mUser='';

function start() {
	show('menu/main.html');
	showLogin('login/index.html')
}

function showAlert(text){
	showPopWin(text, 300, 100);
}

function show(file){
	el("main").innerHTML='Cargando...';
	Net.get(file,reShow);
}

function reShow(http){
	var main=el("main");
	var response=http.responseText;
	
	for (var i=0;i<replaces.length;i=i+2) {
		response=response.replace(new RegExp("\\${"+replaces[i]+"}","g"),replaces[i+1]);
	}
	
	main.innerHTML=response;
}

function showLogin(file){
	el("login_in").innerHTML='Cargando...';
	Net.get(file,reShowLogin);
}

function reShowLogin(http){
	var main=el("login_in");
	var response=http.responseText;
	
	for (var i=0;i<replaces.length;i=i+2) {
		response=response.replace(new RegExp("\\${"+replaces[i]+"}","g"),replaces[i+1]);
	}
	
	main.innerHTML=response;
}

//For quick codes

function getQuickDirectURL() {
	var url=el("url").value;
	
	if ((url==null)||(url.length==0)) {
		showAlert("Debes indicar una URL");
	} else if (url.indexOf("://")<0) {
		showAlert("URL incorrecta");
	} else {
		Net.post("actions/directUrl.php?"+Math.random(),"url="+url,resQuickDirectURL);
	}
}

function resQuickDirectURL(http){
   	var xml=xmlParse(http.responseText);

	var list=[];
	list=xml.getElementsByTagName('error',list);
	if (list.length>0) {
		showAlert(list[0].firstChild.nodeValue);
	}else {
		list=xml.getElementsByTagName('code',list);
		if (list.length>0) {
			replaces=[];
			replaces.push('url');
			replaces.push(el("url").value);
			replaces.push('code');
			replaces.push(list[0].getAttribute('value'));
			replaces.push('quick');
			replaces.push(list[0].getAttribute('quick'));
			show("content/quickDirectUrl.html");
		} else {
			showAlert("Respuesta no v&aacute;lida");
		}
	}
}

function getQuickDirectSMS() {
	var phone=el("number").value;
	var text=el("content").value;
	
	if ((phone==null)||(phone.length==0)) {
		showAlert("Debes indicar un tel&eacute;fono");
	} else 	if ((text==null)||(text.length==0)) {
		showAlert("Debes indicar un texto");
	} else {
		Net.post("actions/directSms.php?"+Math.random(),"phone="+phone+"&text="+text,resQuickDirectSMS);
	}
}

function resQuickDirectSMS(http){
   	var xml=xmlParse(http.responseText);

	var list=[];
	list=xml.getElementsByTagName('error',list);
	if (list.length>0) {
		showAlert(list[0].firstChild.nodeValue);
	}else {
		list=xml.getElementsByTagName('code',list);
		if (list.length>0) {
			replaces=[];
			replaces.push('phone');
			replaces.push(el("number").value);
			replaces.push('text');
			replaces.push(el("content").value);
			replaces.push('code');
			replaces.push(list[0].getAttribute('value'));
			replaces.push('quick');
			replaces.push(list[0].getAttribute('quick'));
			show("content/quickDirectSms.html");
		} else {
			showAlert("Respuesta no v&aacute;lida");
		}
	}
}

function getQuickDirectPhone() {
	var phone=el("number").value;
	var name=el("name").value;
	
	if ((phone==null)||(phone.length==0)) {
		showAlert("Debes indicar un tel&eacute;fono");
	} else {
		Net.post("actions/directPhone.php?"+Math.random(),"phone="+phone+"&name="+name,resQuickDirectPhone);
	}
}

function resQuickDirectPhone(http){
   	var xml=xmlParse(http.responseText);

	var list=[];
	list=xml.getElementsByTagName('error',list);
	if (list.length>0) {
		showAlert(list[0].firstChild.nodeValue);
	}else {
		list=xml.getElementsByTagName('code',list);
		if (list.length>0) {
			replaces=[];
			replaces.push('phone');
			replaces.push(el("number").value);
			replaces.push('name');
			replaces.push(el("name").value);
			replaces.push('code');
			replaces.push(list[0].getAttribute('value'));
			replaces.push('quick');
			replaces.push(list[0].getAttribute('quick'));
			show("content/quickDirectPhone.html");
		} else {
			showAlert("Respuesta no v&aacute;lida");
		}
	}
}

//For register

function checkLength(size,field,out){
	var value=el(field).value;
	
	if (value.length<size){
		el(out).innerHTML='(m&iacute;n. ' + size + ' car&aacute;cteres)';
	} else {
		el(out).innerHTML='OK';
	}
}

function checkEquals(field,field2,out){
	if (el(field).value!=el(field2).value){
		el(out).innerHTML='No coincide';
	}
}

function checkMail(field,out){
	if (isValidEmail(el(field).value)) {
		el(out).innerHTML='OK';
	} else {
		el(out).innerHTML='(correo no v&aacute;lido)';
	}
}

function isValidEmail(mail){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	if (mail.search(validRegExp) == -1) {
      return false;
    } 
    return true; 
}

function registerUser() {
	var info='n';
	if (el('info').checked){
		info='y';
	}
	
	if ((el('user').value.length>=4)&&(el('pass').value.length>=4)&&(el('repass').value==el('pass').value)&&isValidEmail(el('mail').value)){
		Net.post("actions/newUser.php","user="+el('user').value+"&pass="+el('pass').value+"&info="+info+"&mail="+el('mail').value,resRegisterUser);
	}else {
		showAlert("Comprueba los datos de registro");
	}
}

function resRegisterUser(http) {
   	var xml=xmlParse(http.responseText);

	var list=[];
	list=xml.getElementsByTagName('error',list);
	if (list.length>0) {
		showAlert(list[0].firstChild.nodeValue);
	}else {
		var list=[];
		list=xml.getElementsByTagName('user',list);
		if (list.length>0) {
			var user=list[0].firstChild.nodeValue;
			var list=[];
			list=xml.getElementsByTagName('pass',list);
			if (list.length>0) {
				var pass=list[0].firstChild.nodeValue;
				el('i_user').value=user;
				el('i_pass').value=pass;
				login();
			}else {
				showAlert("Respuesta no v&aacute;lida");
			}
		}else {
			showAlert("Respuesta no v&aacute;lida");
		}
	}
}

// For 23HQ

function searchhq23(page) {
	var data=el('hq23s').value;
	
	if ((!data)||(data=='')) {
		showAlert("Debes indicar lo que quieres buscar");
	} else {
		el('hq23Main').style.display='block';
		el('hq23Select').style.display='none';
		el('hq23Result').innerHTML='<br /><br />Cargando...';
		Net.get("23hq/actions/search.php","page="+page+"&text="+data,resSearchhq23);
	}
}

function resSearchhq23(http){
   	var xml=xmlParse(http.responseText);

	el('hq23Result').innerHTML='';

	var list=[];
	list=xml.getElementsByTagName('rsp',list);
	if (list.length==0) {
		showAlert("Imposible realizar la b&uacute;squeda");
	}
	else {
		if(list[0].getAttribute('stat')!='ok'){
			var list=[];
			list=xml.getElementsByTagName('err',list);
			if (list.length==0) {
				showAlert("Imposible realizar la b&uacute;squeda");
			} else {
				showAlert(list[0].getAttribute('msg'));
			}
		} else {
			var list=[];
			list=xml.getElementsByTagName('photos',list);
			if (list.length==0) {
				showAlert("Imposible realizar la b&uacute;squeda");
			} else {
				var page=parseInt(list[0].getAttribute('page'));
				var pages=parseInt(list[0].getAttribute('pages'));
				var total=parseInt(list[0].getAttribute('total'));
				
				var result='<br />';
				
				result+=' Hay '+total+' im&aacute;genes. P&aacute;gina '+page+' de '+pages+'.<br/><br/>';
				if (page>1) {
					result+="<a href='#' onclick='searchhq23("+ 1 +")'>Primera</a> - ";
				} else {
					result+="Primera - ";
				}
				if (page>1) {
					result+="<a href='#' onclick='searchhq23("+ (page-1) +")'>Anterior</a> - ";
				} else {
					result+="Anterior - ";
				}
				if (page<pages) {
					result+="<a href='#' onclick='searchhq23("+ (parseInt(page)+1) +")'>Siguiente</a> - ";
				} else {
					result+="Siguiente - ";
				}
				if (page<pages) {
					result+="<a href='#' onclick='searchhq23("+ pages +")'>&Uacute;ltima</a> ";
				} else {
					result+="&Uacute;ltima ";
				}
				result+='<br /><br />';
		
				if (total>0){		
					var list=[];
					list=xml.getElementsByTagName('photo',list);
					for (var i=0;i<list.length;i++) {
						result=result+"<a href='#' onclick='selecthq23(\""+list[i].getAttribute('server')+"\",\""+list[i].getAttribute('id')+"\",\""+list[i].getAttribute('secret')+"\",\""+list[i].getAttribute('title')+"\")'>"+
							"<img class='content_image' src='http://www.23hq.com/"+list[i].getAttribute('server')+"/"+list[i].getAttribute('id')+"_"+list[i].getAttribute('secret')+"_s.jpg' title='"+list[i].getAttribute('title')+"' height='100' width='100' />"+
							"</a>";
					}
				}
				el('hq23Result').innerHTML=result;
			}
		}
	}
}

function selecthq23(server,id,secret,title){
	var result='';
	
	result+=title;
	result+='<br />';
	result+="<img id='theimage' class='content_image' src='http://www.23hq.com/"+server+"/"+id+"_"+secret+"_m.jpg' /><br />";
	result+='<div id="outhq23">Tama&ntilde;o de la imagen: <select id="size" onchange="replacehq23()"><option value="t">Peque&ntilde;o</option><option value="m" selected>Grande</option></select><br />';
	result+='<br />';
	result+='<a href="#" onclick="Net.post(\'actions/directUrl.php?\'+Math.random(),\'url=\'+el(\'theimage\').src,resSelecthq23);">Generar c&oacute;digo MobiScanner</a><br />';
	result+='</div><br />';
	result+='<a href="#" onclick="el(\'hq23Main\').style.display=\'block\';el(\'hq23Select\').style.display=\'none\';" />Volver</a>';

	el('hq23Select').innerHTML=result;
	
	el('hq23Select').style.display='block';
	el('hq23Main').style.display='none';
}

function replacehq23() {
	var name=el('theimage').src;
	el('theimage').src=name.substring(0,name.length-5)+el('size').value+'.jpg';
}

function resSelecthq23(http){
   	var xml=xmlParse(http.responseText);

	var list=[];
	list=xml.getElementsByTagName('error',list);
	if (list.length>0) {
		showAlert(list[0].firstChild.nodeValue);
	}else {
		list=xml.getElementsByTagName('code',list);
		if (list.length>0) {
			var code=list[0].getAttribute('value');
			var quick=list[0].getAttribute('quick');
			var name=el('theimage').src;
			el('theimage').src=name.substring(0,name.length-5)+'t.jpg';
			el('outhq23').innerHTML='<br /><img src="actions/code.php?value='+code+'&quick='+quick+'" />';
		} else {
			showAlert("Respuesta no v&aacute;lida");
		}
	}
}

// For Flickr

function searchFlickr(page) {
	var data=el('flickrs').value;
	
	if ((!data)||(data=='')) {
		showAlert("Debes indicar lo que quieres buscar");
	} else {
		el('flickrMain').style.display='block';
		el('flickrSelect').style.display='none';
		el('flickrResult').innerHTML='<br /><br />Cargando...';
		Net.get("flickr/actions/search.php","page="+page+"&text="+data,resSearchFlickr);
	}
}

function resSearchFlickr(http){
   	var xml=xmlParse(http.responseText);

	el('flickrResult').innerHTML='';
	
	var list=[];
	list=xml.getElementsByTagName('rsp',list);
	if (list.length==0) {
		showAlert("Imposible realizar la b&uacute;squeda");
	}
	else {
		if(list[0].getAttribute('stat')!='ok'){
			var list=[];
			list=xml.getElementsByTagName('err',list);
			if (list.length==0) {
				showAlert("Imposible realizar la b&uacute;squeda");
			} else {
				showAlert(list[0].getAttribute('msg'));
			}
		} else {
			var list=[];
			list=xml.getElementsByTagName('photos',list);
			if (list.length==0) {
				showAlert("Imposible realizar la b&uacute;squeda");
			} else {
				var page=parseInt(list[0].getAttribute('page'));
				var pages=parseInt(list[0].getAttribute('pages'));
				var total=parseInt(list[0].getAttribute('total'));
				
				var result='<br />';
				
				result+=' Hay '+total+' im&aacute;genes. P&aacute;gina '+page+' de '+pages+'.<br/><br/>';
				if (page>1) {
					result+="<a href='#' onclick='searchFlickr("+ 1 +")'>Primera</a> - ";
				} else {
					result+="Primera - ";
				}
				if (page>1) {
					result+="<a href='#' onclick='searchFlickr("+ (page-1) +")'>Anterior</a> - ";
				} else {
					result+="Anterior - ";
				}
				if (page<pages) {
					result+="<a href='#' onclick='searchFlickr("+ (parseInt(page)+1) +")'>Siguiente</a> - ";
				} else {
					result+="Siguiente - ";
				}
				if (page<pages) {
					result+="<a href='#' onclick='searchFlickr("+ pages +")'>&Uacute;ltima</a> ";
				} else {
					result+="&Uacute;ltima ";
				}
				result+='<br /><br />';
		
				if (total>0){		
					var list=[];
					list=xml.getElementsByTagName('photo',list);
					for (var i=0;i<list.length;i++) {
						result=result+"<a href='#' onclick='selectFlickr(\""+list[i].getAttribute('server')+"\",\""+list[i].getAttribute('id')+"\",\""+list[i].getAttribute('secret')+"\",\""+list[i].getAttribute('title')+"\")'>"+
							"<img class='content_image' src='http://static.flickr.com/"+list[i].getAttribute('server')+"/"+list[i].getAttribute('id')+"_"+list[i].getAttribute('secret')+"_s.jpg' title='"+list[i].getAttribute('title')+"' height='75' width='75'/>"+
							"</a>";
					}
				}
				el('flickrResult').innerHTML=result;
			}
		}
	}
}

function selectFlickr(server,id,secret,title){
	var result='';
	
	result+=title;
	result+='<br />';
	result+="<img id='theimage' class='content_image' src='http://static.flickr.com/"+server+"/"+id+"_"+secret+"_m.jpg' /><br />";
	result+='<div id="outFlickr">Tama&ntilde;o de la imagen: <select id="size" onchange="replaceFlickr()"><option value="t">Peque&ntilde;o</option><option value="m" selected>Grande</option></select><br />';
	result+='<br />';
	result+='<a href="#" onclick="Net.post(\'actions/directUrl.php?\'+Math.random(),\'url=\'+el(\'theimage\').src,resSelectFlickr);">Generar c&oacute;digo MobiScanner</a><br />';
	result+='</div><br />';
	result+='<a href="#" onclick="el(\'flickrMain\').style.display=\'block\';el(\'flickrSelect\').style.display=\'none\';" />Volver</a>';

	el('flickrSelect').innerHTML=result;
	
	el('flickrSelect').style.display='block';
	el('flickrMain').style.display='none';
}

function replaceFlickr() {
	var name=el('theimage').src;
	el('theimage').src=name.substring(0,name.length-5)+el('size').value+'.jpg';
}

function resSelectFlickr(http){
   	var xml=xmlParse(http.responseText);

	var list=[];
	list=xml.getElementsByTagName('error',list);
	if (list.length>0) {
		showAlert(list[0].firstChild.nodeValue);
	}else {
		list=xml.getElementsByTagName('code',list);
		if (list.length>0) {
			var code=list[0].getAttribute('value');
			var quick=list[0].getAttribute('quick');
			var name=el('theimage').src;
			el('theimage').src=name.substring(0,name.length-5)+'t.jpg';
			el('outFlickr').innerHTML='<br/><img src="actions/code.php?value='+code+'&quick='+quick+'" />';
		} else {
			showAlert("Respuesta no v&aacute;lida");
		}
	}
}

// For login

function login() {
	var user=el('i_user').value;
	var pass=el('i_pass').value;
	
	if (user.length==0){
		showAlert('Debes indicar un nombre de usuario');
	} else if (pass.length==0) {
		showAlert('Debes indicar la clave');
	} else {
		Net.post("login/actions/login.php","user="+user+"&pass="+pass,resLogin);
	}
}

function resLogin(http) {
   	var xml=xmlParse(http.responseText);

	var list=[];
	list=xml.getElementsByTagName('error',list);
	if (list.length>0) {
		showAlert(list[0].firstChild.nodeValue);
	}else {
		var list=[];
		list=xml.getElementsByTagName('user',list);
		if (list.length>0) {
			magic=list[0].getAttribute('magic');
			mCode=list[0].getAttribute('code');
			mQuick=list[0].getAttribute('quick');
			mCounter=list[0].getAttribute('visits');
			mUser=list[0].firstChild.nodeValue;
			replaces=[];
			replaces.push('user');
			replaces.push(mUser);
			showLogin('login/loged.html');
			yourCodes();
		}else {
			showAlert("Respuesta no v&aacute;lida");
		}
	}
}

function logout() {
	Net.get("login/actions/login.php","out=1",resLogout);
}

function resLogout() {
	show('menu/main.html');
	showLogin('login/index.html');
	magix='';
	mUser='';
	mCode='';
	mQuick='';
	mCounter='';
}

// For user

var codeList=[];
var currentCode=0;
var currentCodeType=0;

function yourCodes(){
	el("main").innerHTML='Cargando...';
	Net.post("user/actions/codes.php?"+Math.random(), "magic="+magic, resYourCodes);
}

function resYourCodes(http){
   	var xml=xmlParse(http.responseText);

	var list=[];
	list=xml.getElementsByTagName('error',list);
	if (list.length>0) {
		showAlert(list[0].firstChild.nodeValue);
	}else {
		list=xml.getElementsByTagName('code',list);
		if (list.length>0) {
			codeList=list;
			showUser('user/index.html');
		} else {
			showAlert("Respuesta no v&aacute;lida");
		}
	}
}

function showUser(file){
	el("main").innerHTML='Cargando...';
	Net.get(file+'?'+Math.random(),reShowUser);
}

function reShowUser(http){
	var main=el("main");
	var response=http.responseText;

	main.innerHTML=response;
	
	showCode(0);
	changeCode();
}

function showCode(n,change){
	el('codeSelection').innerHTML=numbers(n+1,codeList.length);

	el('codeUrl').value="";
	el('codeName').value="";
	el('codePhone').value="";
	el('codeText').value="";
	el('codeDescription').value="";

	if (codeList[n]){
		currentCode=n;
		var code=codeList[n].getAttribute('value');
		var quick=codeList[n].getAttribute('quick');
		el('codeImage').src='actions/code.php?value='+code+'&quick='+quick;
		el('codeCounter').innerHTML=codeList[n].getAttribute('visits');
				
		var type=null;
		if (codeList[n].firstChild!=null){
			type=codeList[n].firstChild.nodeName;
		}
		
		if (type==null) {
			currentCodeType=0;
			el('codeType').selectedIndex=0;
		} else if (type=='url') {
			currentCodeType=3;
			el('codeType').selectedIndex=3;
			if (codeList[n].firstChild.firstChild.firstChild){
				el('codeUrl').value=codeList[n].firstChild.firstChild.firstChild.nodeValue;
			}else {
				el('codeUrl').value='';
			}
			if (codeList[n].firstChild.lastChild.firstChild) {
				el('codeDescription').value=codeList[n].firstChild.lastChild.firstChild.nodeValue;
			} else{
				el('codeDescription').value='';
			}
		} else if (type=='phone') {
			currentCodeType=2;
			el('codeType').selectedIndex=2;
			el('codePhone').value=codeList[n].firstChild.getAttribute("phone");
			if (codeList[n].firstChild.firstChild.firstChild) {
				el('codeName').value=codeList[n].firstChild.firstChild.firstChild.nodeValue;
			}else {
				el('codeName').value='';
			}
			if (codeList[n].firstChild.lastChild.firstChild) {
				el('codeDescription').value=codeList[n].firstChild.lastChild.firstChild.nodeValue;
			}else {
				el('codeDescription').value='';
			}
		} else if (type=='sms') {
			currentCodeType=1;
			el('codeType').selectedIndex=1;
			el('codePhone').value=codeList[n].firstChild.getAttribute("phone");
			if (codeList[n].firstChild.firstChild.firstChild) {
				el('codeText').value=codeList[n].firstChild.firstChild.firstChild.nodeValue;
			}else {
				el('codeText').value='';
			}
			if (codeList[n].firstChild.lastChild.firstChild) {
				el('codeDescription').value=codeList[n].firstChild.lastChild.firstChild.nodeValue;
			}else {
				el('codeDescription').value='';
			}
		}
	}else {
		showAlert("C&oacute;digo no encontrado");
	}
}

function changeCode() {
	var codeType=el('codeType').selectedIndex;
	if (codeType==0) {
		el('codeUrlZone').style.display="none";
		el('codeNameZone').style.display="none";
		el('codePhoneZone').style.display="none";
		el('codeTextZone').style.display="none";
		el('codeDescriptionZone').style.display="none";
	}else if (codeType==1){
		if (currentCodeType==1) {
			showCode(currentCode);
		}else {
			el('codePhone').value="";
			el('codeText').value="";
			el('codeDescription').value="";
		}
		el('codeUrlZone').style.display="none";
		el('codeNameZone').style.display="none";
		el('codePhoneZone').style.display="";
		el('codeTextZone').style.display="";
		el('codeDescriptionZone').style.display="";
	}else if (codeType==2){
		if (currentCodeType==2) {
			showCode(currentCode);
		}else {
			el('codeName').value="";
			el('codePhone').value="";
			el('codeDescription').value="";
		}
		el('codeUrlZone').style.display="none";
		el('codeNameZone').style.display="";
		el('codePhoneZone').style.display="";
		el('codeTextZone').style.display="none";
		el('codeDescriptionZone').style.display="";
	}else if (codeType==3){
		if (currentCodeType==3) {
			showCode(currentCode);
		}else {
			el('codeUrl').value="";
			el('codeDescription').value="";
		}
		el('codeUrlZone').style.display="";
		el('codeNameZone').style.display="none";
		el('codePhoneZone').style.display="none";
		el('codeTextZone').style.display="none";
		el('codeDescriptionZone').style.display="";
	}
}

function saveCode() {
	var codeType=el('codeType').selectedIndex;

	if (codeType==0) {
		Net.post('user/actions/empty.php','magic='+magic+'&code='+codeList[currentCode].getAttribute('value'),reSaveCode);
	} else if (codeType==3) {
		var url=el('codeUrl').value;
		if (!url || (url.length==0)) {
			showAlert("Debes indicar una URL");
		}else {
			var description=el('codeDescription').value;
			Net.post('user/actions/url.php','magic='+magic+'&code='+codeList[currentCode].getAttribute('value')+'&description='+description+'&url='+url,reSaveCode);
		}
	} else if (codeType==2) {
		var phone=el('codePhone').value;
		if (!phone || (phone.length==0)) {
			showAlert("Debes indicar el tel&eacute;fono");
		}else {
			var name=el('codeName').value;
			var description=el('codeDescription').value;
			Net.post('user/actions/phone.php','magic='+magic+'&code='+codeList[currentCode].getAttribute('value')+'&description='+description+'&phone='+phone+"&name="+name,reSaveCode);
		}
	} else if (codeType==1) {
		var phone=el('codePhone').value;
		if (!phone || (phone.length==0)) {
			showAlert("Debes indicar el tel&eacute;fono");
		}else {
			var text=el('codeText').value;
			if (!text||(text.length==0)) {
				showAlert("Debes introducir un texto");
			}else {
				var description=el('codeDescription').value;
				Net.post('user/actions/sms.php','magic='+magic+'&code='+codeList[currentCode].getAttribute('value')+'&description='+description+'&phone='+phone+"&text="+text,reSaveCode);
			}
		}
	}
}

function reSaveCode(http) {
   	var xml=xmlParse(http.responseText);

	var list=[];
	list=xml.getElementsByTagName('error',list);
	if (list.length>0) {
		showAlert(list[0].firstChild.nodeValue);
	}else {
		list=xml.getElementsByTagName('ok',list);
		if (list.length>0) {
			var codeType=el('codeType').selectedIndex;
		
			if (codeType==3){
				currentCodeType=3;
				codeList[currentCode]=xmlParse('<code value="'+codeList[currentCode].getAttribute('value')+'">'+
					'<url><address>'+el('codeUrl').value+'</address><description>'+el('codeDescription').value+'</description></url></code>').firstChild;
			}else if (codeType==2){
				currentCodeType=2;
				codeList[currentCode]=xmlParse('<code value="'+codeList[currentCode].getAttribute('value')+'">'+
					'<phone phone="'+el('codePhone').value+'"><name>'+el('codeName').value+'</name><description>'+el('codeDescription').value+'</description></phone></code>').firstChild;
			}else if (codeType==1){
				currentCodeType=1;
				codeList[currentCode]=xmlParse('<code value="'+codeList[currentCode].getAttribute('value')+'">'+
					'<sms phone="'+el('codePhone').value+'"><text>'+el('codeText').value+'</text><description>'+el('codeDescription').value+'</description></sms></code>').firstChild;
			}else if (codeType==0){
				currentCodeType=0;
				codeList[currentCode]=xmlParse('<code value="'+codeList[currentCode].getAttribute('value')+'"></code>').firstChild;
			}
			showAlert("Guardado");
		} else {
			showAlert("Respuesta no v&aacute;lida");
		}
	}
}

function numbers(n,max){
	var result="";
	
	for (var i=1;i<=max;i++){
		if (i==n) {
			result+=' '+i;
		} else {
			result+=' <a href="#" onclick="showCode('+(i-1)+');changeCode();">'+i+'</a>';
		}
	}
	
	return result;
}

function yourPage() {
	replaces=[];
	replaces.push('code');
	replaces.push(mCode);
	replaces.push('quick');
	replaces.push(mQuick);
	replaces.push('visits');
	replaces.push(mCounter);
	show('yourpage/index.html');
}

//For your page

function submitYourPage() {
	var data=window.get_xhtml ? get_xhtml(oW.document.body) :oW.document.body.innerHTML;

	Net.post("actions/submit.php", "data="+data, resSubmitYourPage);
}

function resSubmitYourPage(http){
   	var xml=xmlParse(http.responseText);

	var list=[];
	list=xml.getElementsByTagName('error',list);
	if (list.length>0) {
		showAlert2(list[0].firstChild.nodeValue);
	}else {
		list=xml.getElementsByTagName('ok',list);
		if (list.length>0) {
			showAlert2("Guardado");
		} else {
			showAlert2("Respuesta no v&aacute;lida");
		}
	}
}

function showAlert2(text){
	el('infoDivIn').innerHTML=text;
	el('infoDiv').style.display='block';
}

function getYourPage(){
	Net.get("actions/get.php", resGetYourPage);
}

function resGetYourPage(http){
   	var xml=xmlParse(http.responseText);

	var list=[];
	list=xml.getElementsByTagName('error',list);
	if (list.length>0) {
		showAlert(list[0].firstChild.nodeValue);
	}else {
		oW.document.body.innerHTML=http.responseText;
		o('formEdited').submit();
	}
}


