//////////////////////// Función para saber la posición de un caracter en un string \\\\\\\\\\\\\\\\\\\
function strpos(str, ch) {
for (var i = 0; i < str.length; i++)
if (str.substring(i, i+1) == ch) return true;
return false;
}

/////////// Función para postear el form de login \\\\\\\\\\\\\
function submitForm(theform) {

  var btn = document.getElementById('SubmitLoginBtn');
  var val = btn.value;
  var status = AjaxRequest.submit(
    theform
    ,{
		'onLoading':function() { btn.disabled = true; btn.value = "PROCESSING..."; }
	    ,'onLoaded':function() { btn.disabled = false; btn.value = val; }
      ,'onSuccess':function(req){
										r = req.responseText;

										if(strpos(r, "/"))
										{
											document.location.href = r;										
										}else{
											alert(r);
										}
								}
    }
  );
  return status;

}

/////////// Función para postear el form de recuperar contraseña \\\\\\\\\\\\\
function submitRecoverPass(theform) {

  var btn = document.getElementById('SubmitRecoverPBtn');
  var val = btn.value;
  var status = AjaxRequest.submit(
    theform
    ,{
		'onLoading':function() { btn.disabled = true; btn.value = "PROCESSING..."; }
	    ,'onLoaded':function() { btn.disabled = false; btn.value = val; }
      ,'onSuccess':function(req){
										r = req.responseText;
											alert(r);
								}
    }
  );
  return status;

}

/////////// Función para postear el form de suscribir al newsletter \\\\\\\\\\\\\
function suscribe(theform) {

  var fld = document.getElementById('SuscriberEmail');

  var status = AjaxRequest.submit(
    theform
    ,{
		'onLoading':function() { fld.disabled = true; fld.value = "PROCESSING..."; }
	    ,'onLoaded':function() { fld.disabled = false; fld.value = ""; },
      'onSuccess':function(req){
										r = req.responseText;

										//if(r == 1)
//										{
//											alert('Already on database.');
//										}else if(r == 2){
//											alert('Thanks for registering.');
//										}else if(r == 3){
//											alert('Please, enter a valid email.');
//										}
										alert(r);
								}
    }
  );
  return status;

}


/////////// Función para elegir el precio de un producto \\\\\\\\\\\\\
function changePrice(obj, v)
{

	for(i=1;i<4;i++)
	{
		document.getElementById('pr_'+i).className = '';
	}

	document.getElementById(obj).className = 'selected';
	document.getElementById('selected_price').value = v;
}



	//función que inicia las tabs
	function initTabs()
	{

		for(i=1;i<=6;i++)
		{
			document.getElementById("country-selector-" + i).style.display = 'none';
		}
			//guardamos en una variable el valor de la cookie que nos dice que tab
			//debemos dejar abiera
			var c_tab = ReadCookie('open_country_tab');

			if(c_tab != '')
			{
				switchTab(c_tab)
			}else{
				document.getElementById("country-selector-1").style.display = 'block';
				document.getElementById("tab-1").className = 'selected';
			}
	}

	//funcion que muestra una tab
	function switchTab(show)
	{
		for(i=1;i<=6;i++)
		{
			if(i != show)
			{
				document.getElementById("country-selector-" + i).style.display = 'none';
				document.getElementById("tab-" + i).className = '';
			}else{
				document.cookie =  'open_country_tab='+ show +'; expires=Thu, 2 Aug 2020 20:47:11 UTC; path=/';
				document.getElementById("tab-" + show).className = 'selected';
				document.getElementById("country-selector-" + show).style.display = 'block';
			}
		}

	}

	function ReadCookie(cookieName)
	{
 		var theCookie = ""+document.cookie;
		var ind = theCookie.indexOf(cookieName);

	 	if (ind == -1 || cookieName == "") return "";

 		var ind1 = theCookie.indexOf(';',ind);
	 	if (ind1 == -1) ind1=theCookie.length;

		return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
	}





/////////// Función para chequear fecha válida de delivery \\\\\\\\\\\\\
function validateDeliveryDate(theform, prodid) {

  var btn = document.getElementById('big-button');
  var val = btn.value;
  var status = AjaxRequest.submit(
    theform
    ,{
		'onLoading':function() { btn.disabled = true; btn.value = "PROCESSING..."; }
	    ,'onLoaded':function() { btn.disabled = false; btn.value = val; }
      ,'onSuccess':function(req){
										r = req.responseText;

										if(r == true)
										{
											location.href = '/cart/add/'+ prodid +'/'+ document.forms['order_form']['orderdate'].value +'/'+ document.getElementById('selected_price').value;
										}else{
											alert(r);
										}
								}
    }
  );
  return status;

}



/////////// Función para chequear fecha válida de delivery \\\\\\\\\\\\\
function changeDeliveryDate(theform) {

  //var btn = document.getElementById('add_to_cart');
  var status = AjaxRequest.submit(
    theform
    ,{
		'onLoading':function() { //btn.disabled = true; btn.value = "PROCESSING..."; 
		}
	    ,'onLoaded':function() { //btn.disabled = false; btn.value = "Add to cart"; 
		}
      ,'onSuccess':function(req){
										r = req.responseText;

										if(r == true)
										{										
											theform.action = '/es/shipping.html';																		
											theform.onsubmit = '';																									
											theform.submit();											
										}else{
											alert(r);
										}
								}
    }
  );
  return status;

}



/////////////////////////// Función para agregar/borrar un gift \\\\\\\\\\\\\\\\\\\\\\\
function gift(url)
{

  AjaxRequest.get(
    {
      'url': url
      ,'onLoading':function() { document.getElementById('cart-container').innerHTML = '<img src="/img/ajax-loader.gif" vspace="6" />'; }	
      ,'onLoaded':function() { document.getElementById('cart-container').innerHTML = ''; }	
      ,'onSuccess':function(req){ var r = req.responseText; document.getElementById('cart-container').innerHTML = r; }
    }
  );

}

/////////////////////////// Función para abrir un popup \\\\\\\\\\\\\\\\\\\\\\\
	var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos)
{
	if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
	else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win=window.open(mypage,myname,settings);
}


///////////////////////// Función para limitar caracteres \\\\\\\\\\\\\\\\\\\\\\\
function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {
        var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "counttxt";
        var countBody = opt_countBody ? opt_countBody : "countBody";
        var maxSize = opt_maxSize ? opt_maxSize : 1024;

        var field = document.getElementById(countedTextBox);

        if (field && field.value.length >= maxSize) {
                field.value = field.value.substring(0, maxSize);
        }
        var txtField = document.getElementById(countBody);
                if (txtField) { 
                //txtField.innerHTML = field.value.length;
                txtField.innerHTML = maxSize - field.value.length;
        }
}


function f_monitor(field, v)
{
	var val = field.value;
	
	if (val == v) { field.value = '' };	
}

function b_monitor(field, v)
{
	
	if (field.value == '') { field.value = v };
	
}
