var WLSearchBoxConfiguration=
{
	"global":{
		"serverDNS":"search.live.com",
		"market":"en-US"
	},
	"appearance":{
		"autoHideTopControl":false,
		"width":800,
		"height":550,
		"theme":"Blue"
	},
	"scopes":[
		{
			"type":"web",
			"caption":"&#x57;&#x65;&#x20;&#x43;&#x61;&#x6e;&#x20;&#x42;&#x75;&#x79;&#x20;&#x47;&#x6f;&#x6f;&#x67;&#x6c;&#x65;",
			"searchParam":"site:www.wecanbuygoogle.com"
		}
		,
		{
			"type":"web",
			"caption":"&#x57;&#x65;&#x62;",
			"searchParam":""
		}
	]
}

function loadFormSignUp(){
	document.getElementById('users_counter').innerHTML = ''
	+ '<form id="frmSignUp" name="frmSignUp" method="POST" action="new_user.php">'
	+ '<div id="div_sign_up_form">'
	+ '<label for="email">We only need Email</label> <input type="text" name="email" id="email" value="" class="cajas required email" /><br/>'
	+ '<label for="name">and Name</label> <input type="text" name="name" id="name" class="cajas required" /><br/>' 
	+ 'We\'ll let you know when it\'s time to buy google  <input type="button" name="btn_sign_up" value="Sign Up" onclick="validarForm(\'frmSignUp\');">' 
	+ '<br />By clicking Sign Up, you are indicating that you have read and agree to the <a href="terms_of_use.php" rel="nofollow">Terms of Use</a> and <a href="privacy_policy.php" rel="nofollow">Privacy Policy</a>.'
	+ '</div>'
	+ '</form>';
	
	setFocusControl('frmSignUp','email');
}


function validarLink(cadena) {
	var http = "http://";
	var es_url;
	if(cadena.length <= 7){
		es_url = false;
	} else {
		es_url = http.indexOf(cadena.substring(0, 7)) != - 1;
	}
	
	return(es_url);
}

function updateFormHour(campo,hora,minuto){
	if(document.getElementById(hora).value=='')document.getElementById(hora).value = '00';
	if(document.getElementById(minuto).value=='')document.getElementById(minuto).value = '00';
	
	document.getElementById(campo).value = document.getElementById(hora).value + ":" + document.getElementById(minuto).value;
}

function foroSeoSubmitSearchForm(domain,formName){
	var controller = "foros";
	var frm = document.forms[formName];
	
	if(document.getElementById('in_posts').checked==true){
		controller = "forosp";
	}
	
	if(document.getElementById('search_string').value.length < 4){
		alert('La cadena de búsqueda es demasiado corta');
		return false;
	} else {
		frm.action = domain + url_google(document.getElementById('search_string').value,'-' + controller +'.htm');
		return true;
	}

	var frm = document.forms[formName];
	
}

function seoSubmitSearchForm(domain,formName){
	var frm = document.forms[formName];
	if(document.getElementById('search_string').value.length < 4){
		alert('La cadena de búsqueda es demasiado corta');
		return false;
	} else {
		frm.action = domain + url_google(document.getElementById('search_string').value,'-' + formName +'.htm');
		return true;
	}
}

function url_google(texto,controller){
	texto=texto.replace(/(À|Á|Â|Ã|Ä|Å|Æ)/gi,'a');
	texto=texto.replace(/(È|É|Ê|Ë)/gi,'e');
	texto=texto.replace(/(Ì|Í|Î|Ï)/gi,'i');
	texto=texto.replace(/(Ò|Ó|Ô|Ö)/gi,'o');
	texto=texto.replace(/(Ù|Ú|Û|Ü)/gi,'u');
	texto=texto.replace(/(Ñ)/gi,'n');
	texto=texto.replace(/("|')/gi,'');
	texto=texto.replace(/(-)/gi,'_');
	texto=texto.replace(/(·)/gi,'.');
	
	texto = texto.toLowerCase();
	
	return encodeURIComponent(texto).replace(/%20/g,"_") + controller;
}

function validarBaja(formName){
	var frm = document.forms[formName];
	
	var flag = false;
	size = frm.elements['pq'].length;
	for ( var i = 0; i < size; i++ ) {
		if(frm.elements['pq'][i].checked){
			flag = true;
			
			break;
		}
	}
	
	if(flag){
		submitForm(formName);
	} else {
		alert('Tienes que seleccionar una razón');
	}
}

function submitDeleteForm(frmName,message){
	var a=confirm(message);
	if(a==true)	{
		submitForm(frmName);
	}
}

function submitDeleteLink(url_var,message){
	var a=confirm(message);
	if(a==true)	{
		window.location = url_var;
	}
}

function updateAllSelectAndSubmit(frmName){
	var myForm = document.forms[frmName];
	var nElements = myForm.length;
	
	for(var i=0;i< nElements;i++){
		if(myForm.elements[i].getAttribute('multiple')!=null){
			var select = document.getElementById(myForm.elements[i].getAttribute('id'));
			var nOptions = select.options.length;
			
			for(var j=0;j < nOptions; j++){
				select.options[j].selected = true;
			}
		}
	}
	
	validarForm(frmName);
}

function loadDivApartados(id){
	if(id > 0){
	    ajax=nuevoAjax();
	    ajax.open("GET", "dir_ficha_carga_apartados.php?id_subcategoria="+id, true);
	    ajax.onreadystatechange=function() 
	    {
	        if (ajax.readyState==4)
	        {
				var padre = document.getElementById("capas");
				var hijo = document.createElement("DIV");
				hijo.setAttribute("id","div_apartado_" + id);
				
				if(padre.getAttribute('class')==null){
					hijo.setAttribute("className","fila_form alt");
				} else {
					hijo.setAttribute("class","fila_form alt");
				}
				
				hijo.innerHTML = ajax.responseText;
				if(hijo.innerHTML.length > 0){
					padre.appendChild(hijo);
				}
	        } 
	    }
	    ajax.send(null);
    }
}

function deleteDivApartado(id){
	var elem = document.getElementById("div_apartado_"+id);
	if(elem!=null){
		var padre = elem.parentNode;
		padre.removeChild(elem);
	} else {
		elem = document.getElementById("table_apartado_"+id);
		if(elem!=null){
			var padre = elem.parentNode;
			padre.removeChild(elem);
		}
	}
}

function deleteOption2 (id1, id2) {
	var select1 = document.getElementById (id1);
	var select2 = document.getElementById (id2);

	if (select2.selectedIndex > -1) {

		var opcion_antigua = select2.options[select2.selectedIndex];
		select2.options[select2.selectedIndex] = null;
		var opcion = new Option (opcion_antigua.text,opcion_antigua.value);
		select1.options[select1.options.length] = opcion;
		deleteDivApartado(opcion.value);
	} else {
		alert ("No has seleccionado ninguna opción");
	}
}

function addOption2 (id1, id2) {

	var select1 = document.getElementById (id1);
	var select2 = document.getElementById (id2);

	if (select1.selectedIndex > -1) {

		var opcion_antigua = select1.options[select1.selectedIndex];
		select1.options[select1.selectedIndex] = null;
		var opcion = new Option (opcion_antigua.text,opcion_antigua.value);
		select2.options[select2.options.length] = opcion;
		loadDivApartados(opcion.value);
	} else {
		alert ("No has seleccionado ninguna opción");
	}
}


function cargaProvincia(valor)
{
	if(valor > 0){
    ajax=nuevoAjax();
    ajax.open("GET", "dir_carga_provincia.php?id_pais="+valor, true);
    ajax.onreadystatechange=function() 
    { 
        if (ajax.readyState==4)
        { 
            document.getElementById("cargaProvincia").innerHTML=ajax.responseText;
        } 
    }
    ajax.send(null);
    }        
}

function loadAjax(controller,element){
    ajax=nuevoAjax();
    ajax.open("GET", controller, true);
    ajax.onreadystatechange=function() 
    { 
        if (ajax.readyState==4)
        { 
            document.getElementById(element).innerHTML=ajax.responseText;
        } 
    }
    ajax.send(null);
}

function nuevoAjax(){ 
    var xmlhttp=false; 
    try 
    { 
        // Creacion del objeto AJAX para navegadores no IE
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
    }
    catch(e)
    { 
        try
        { 
            // Creacion del objet AJAX para IE 
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        catch(E) { xmlhttp=false; }
    }
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); } 

    return xmlhttp; 
}

function updateSelectAndSubmit(selectId,frmName){
	var select = document.getElementById(selectId);
	var nOptions = select.options.length;
	
	for(var i=0;i < nOptions; i++){
		select.options[i].selected = true;
	}
	
	validarForm(frmName);
}

function deleteOption (id1, id2) {
	var select1 = document.getElementById (id1);
	var select2 = document.getElementById (id2);

	if (select2.selectedIndex > -1) {

		var opcion_antigua = select2.options[select2.selectedIndex];
		select2.options[select2.selectedIndex] = null;
		var opcion = new Option (opcion_antigua.text,opcion_antigua.value);
		select1.options[select1.options.length] = opcion;
		
		if(document.getElementById ('id_categoria_principal')!=null){
			var select3 = document.getElementById ('id_categoria_principal');
			var opcion_seleccionada = select3.options[select3.selectedIndex];
			
			var nElements = select3.options.length;
			for(var i=0; i < nElements; i++){
				select3.options[i] = null;
			}
			
			var opcion;
			nElements = select2.options.length;
			for(var i=0; i < nElements; i++){
				opcion = new Option (select2.options[i].text,select2.options[i].value);
				select3.options[i] = opcion;
				if(opcion_seleccionada.value == select3.options[i].value){
					select3.selectedIndex = i;
				}
				opcion = null;
			}
		}
		
	} else {
		alert ("No has seleccionado ninguna opción");
	}
}

function addOption (id1, id2) {

	var select1 = document.getElementById (id1);
	var select2 = document.getElementById (id2);
	
	if(select2.length > 3){
		alert ("No puedes seleccionar más de cuatro sectores");
	} else if (select1.selectedIndex > -1) {

		var opcion_antigua = select1.options[select1.selectedIndex];
		select1.options[select1.selectedIndex] = null;
		var opcion = new Option (opcion_antigua.text,opcion_antigua.value);
		var opcion_principal = new Option (opcion_antigua.text,opcion_antigua.value);
		select2.options[select2.options.length] = opcion;
		
		if(document.getElementById ('id_categoria_principal')!=null){
			var select3 = document.getElementById ('id_categoria_principal');
			select3.options[select3.options.length] = opcion_principal;
		}
	} else {
		alert ("No has seleccionado ninguna opción");

	}
}

function checkLoginPasswordBoletin(frmName){
	if(document.getElementById('password').value != document.getElementById('password2').value){
		alert("El password no coincide en las dos casillas");
		document.getElementById('password').value = "";
		document.getElementById('password2').value = "";
		setFocusControl(frmName,'password');
	} else if(document.getElementById('checkbox_suscrito').checked != true){
		alert("No has aceptado las condiciones de uso y la política de privacidad");
		setFocusControl(frmName,'checkbox_suscrito');
	} else {
		validarForm(frmName);
	}
}

function setFocusControl(frm,elem){
  var focusControl = document.forms[frm].elements[elem];

  if (focusControl!=null && focusControl.type!=null && focusControl.type != "hidden" && !focusControl.disabled) {
     focusControl.focus();
  }
}

function validarForm(formName){

	var requiredClass = "required";
	var emailClass = requiredClass + " email";
	var linkClass = requiredClass + " link";
	var frm = document.forms[formName];
	var class_style = "";
	for(var i=0;i < frm.elements.length; i++){
		//iexplorer & firefox compatibility
		if(frm.elements[i].getAttribute('class')==null){
			class_style = frm.elements[i].getAttribute('className');
		} else {
			class_style = frm.elements[i].getAttribute('class');
		}

		if(class_style!=null){
			if(class_style.substring(class_style.length - requiredClass.length, class_style.length)==requiredClass){
				if(frm.elements[i].value==""){
					alert("You must to complete the form: " + frm.elements[i].name);
					frm.elements[i].focus();
					
					return false;
				}
			} else if(class_style.substring(class_style.length - emailClass.length, class_style.length)==emailClass){
				if(!validarMail(frm.elements[i].value)){
					alert("El correo electrónico introducido no es correcto");
					frm.elements[i].focus();
					
					return false;
				}
			} else if(class_style.substring(class_style.length - linkClass.length, class_style.length)==linkClass){
				if(!validarLink(frm.elements[i].value)){
					alert("La URL no es correcta. Ejemplo de url: http://www.ediciona.com");
					frm.elements[i].focus();
					
					return false;
				}
			}
		}
	}
	
	submitForm(formName);
}

/**
 * It sends the frm form 
 * 
 * @param String frm
 */
function submitForm(frm) {
	document.forms[frm].submit();
}

/**
 * It validates wether mail String is an email
 *  
 * @param String mailString
 */
function validarMail(mailString) {
	var reMail=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if(!reMail.exec(mailString)) return false;
	return true;
}

function checkLen(id, max) {
	var txt;
	txt=document.getElementById(id);
	var n = txt.value.length;
	if (n > max) { 
		txt.value = txt.value.substring(0, max); 
		return false;
	}
}

function dir_Password_VS_Email(){
	if(document.getElementById('cambio_0').checked==true){
		changeClass('email','cajas');
		document.getElementById('email').disabled=true;
		changeClass('contrasenya_1','cajas required');
		document.getElementById('contrasenya_1').disabled=false;
		changeClass('contrasenya_2','cajas required');
		document.getElementById('contrasenya_2').disabled=false;
	} else {
		changeClass('email','cajas required');
		document.getElementById('email').disabled=false;
		changeClass('contrasenya_1','cajas');
		document.getElementById('contrasenya_1').disabled=true;
		changeClass('contrasenya_2','cajas');
		document.getElementById('contrasenya_2').disabled=true;
	}
}

function changeClass(elem,style){
	if(document.getElementById(elem)==null){alert(elem);}
	if(document.getElementById(elem).getAttribute('class')==null){
		document.getElementById(elem).setAttribute('className',style);
	} else {
		document.getElementById(elem).setAttribute('class',style);
	}
}

function validateChangeKeys(frmName){
	if(document.getElementById('cambio_0').checked==true){
		if(document.getElementById('contrasenya_1').value!=document.getElementById('contrasenya_2').value){
			alert("El password no coincide en las dos casillas");
			document.getElementById('contrasenya_1').value = "";
			document.getElementById('contrasenya_2').value = "";
			setFocusControl(frmName,'contrasenya_1');
		} else {
			validarForm(frmName);
		}
	} else if(document.getElementById('cambio_1').checked==true){
		validarForm(frmName);
	}
}


/**
 * This array is used to remember mark status of rows in browse mode
 */
//var marked_row = new Array;


function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('div');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

function submitLink(act){
	var fname = 'frmLink';
	document.forms[fname].action = act;
	
	submitForm(fname);
}
