var host='www.casadellibro.com';

IE4 = (document.all);
NS4 = (document.layers);
if (!IE4) {
	if (!NS4)
		NS4 = (document.getElementById);
}

var form1=false;
var form2=false;

if (NS4) document.captureEvents(Event.KEYPRESS);
document.onkeypress = doKey;

function validateAll(form) {
  if (isNotEmpty(form.tFP)) {
    if (!isNumber(form.tFP)) {
      //alert('La fecha no es correcta !')
      open_error_window('','La fecha no es correcta !');
      //form.tFP.focus()
      return false
    }
    if (form.tFP.value < 1000) {
      //alert('La fecha no es correcta !')
      open_error_window('','La fecha no es correcta !');
      //form.tFP.focus()
      return false
    }
  }

  if (!(isNotEmpty(form.tISBN))
  &&(!(isNotEmpty(form.tTi)))
  &&(!(isNotEmpty(form.tAu)))//  &&(!isNotEmpty(form.tCategoria)))
  &&(!(isNotEmpty(form.tEd)))
  &&(!(isNotEmpty(form.tFP)))
  &&((form.sL1[form.sL1.selectedIndex].value==-1)&&(form.sL2[form.sL2.selectedIndex].value==-1)&&(form.sL3[form.sL3.selectedIndex].value==-1))) {
    //alert('Por favor, especifica un criterio de búsqueda.');
	open_error_window('','Por favor, especifica un criterio de b&uacute;squeda.');
    return false
  }
  
  if (validate(form)){
  	form.tISBN.value=ReplaceChar(form.tISBN.value,'%','*');
	form.tTi.value=ReplaceChar(form.tTi.value,'%','*');
	form.tAu.value=ReplaceChar(form.tAu.value,'%','*');
	form.tEd.value=ReplaceChar(form.tEd.value,'%','*');
    return true;
  }
}

function redirectSearch(form) {
  if (validate(form)) {
    submitToUrl(form,'http://www.casadellibro.com/busquedas/resultadosin');
  }
}


function doKey(e) {
  whichASC = (NS4) ? e.which : event.keyCode;
  whichKey = String.fromCharCode(whichASC).toLowerCase();

  if (whichASC == 13) {
    //alert(whichASC+' form1='+form1+' form2='+form2)
    if (form1||(!form1 && !form2)) {
      //alert('form1||(!form1 && !form2)');
      if (document.fSearchAll.tValueForSearch.value != '') {
	  	  document.fSearchAll.tValueForSearch.value=ReplaceChar(document.fSearchAll.tValueForSearch.value,'%','*');
          document.fSearchAll.NotQueryAgain.value='false';
          document.fSearchAll.action='http://www.casadellibro.com/busquedas/resultadosin';
          document.fSearchAll.target='cdl';	
	  document.fSearchAll.submit();

      } else {
        if ((typeof document.fBusquedaAvanzada != 'undefined')&&(typeof document.fBusquedaAvanzada != null)) {
          if(validateAll(document.fBusquedaAvanzada))
            redirectSearch(document.fBusquedaAvanzada)
        };
      };
    };

    if (form2) {
      //alert('form2');
      if ((typeof document.fBusquedaAvanzada != 'undefined')&&(typeof document.fBusquedaAvanzada != null)) {
        if(validateAll(document.fBusquedaAvanzada))
          redirectSearch(document.fBusquedaAvanzada);
      };
    };
  }
}


function setFormModified(formName) {
  if (formName=='quick') {
  	var testTxt='*'+document.fSearchAll.tValueForSearch.value+'*';
    if(testTxt != '**') {
      form1=true;
      form2=false;
    };
  }
  if (formName=='advanced') {
      form1=false;
      form2=true;
  };
  //alert('apres form1='+form1+' form2='+form2);
}

function ejecutaBusqueda() {
	var query="";
	if (document.fSearchAll.tValueForSearch.value != '') {
		document.fSearchAll.tValueForSearch.value=ReplaceChar(document.fSearchAll.tValueForSearch.value,'%','*');
		document.fSearchAll.NotQueryAgain.value='false';
		
		document.fSearchAll.action='http://www.casadellibro.com/busquedas/resultadosin';
				
		document.fSearchAll.target='cdl';
		document.fSearchAll.submit();
		
	} else {
		open_error_window('','No se han especificado criterios de b&uacute;squeda');
	}
}




function ReplaceChar(String, OldChar, NewChar){
	for(var n=0; n<String.length; n++){
		if (String.charAt(n) == OldChar) {
			String = String.substring(0,n) + NewChar + String.substring(n+1,String.length);
		}
	}
	return String;
}

var windowTitleGlobal;
var titleTextGlobal;
var errorTextGlobal;
var windowRedirectUrl;

function open_error_window(errorText, titleText, urlRedirect, windowTitle, width, height, left, top)
{
//Use:
//open_error_window(errorText);
//open_error_window(errorText,'title');
//open_error_window(errorText,'title','http://www.casadellibro.com');
//...


var putItThere = null;

var chasm = screen.availWidth;
var mount = screen.availHeight;


if ((urlRedirect== null) || (urlRedirect=='undefined')) {
  windowRedirectUrl='';
} else {
  windowRedirectUrl=urlRedirect;
}



if ((windowTitle == null) || (windowTitle=='undefined')) {
  windowTitleGlobal ='::Casa Del Libro:: - Error';
} else {
  windowTitleGlobal=windowTitle;
}

if ((titleText == null)||(titleText=='undefined')) {
  titleTextGlobal='por favor,<br>compruebe los siguientes campos';
} else {
  titleTextGlobal=titleText;
}

if ((errorText == null)||(errorText=='undefined')) {
  errorText='';
} else {
  errorTextGlobal=errorText;
}

if ((width == null) || (width=='undefined'))
  width ='300';
if ((height == null) || (height=='undefined'))
  height ='300';
if ((left == null) || (left=='undefined'))
  left =((chasm - width - 10) * .5);
if ((top == null) || (top=='undefined'))
  top =((mount - height - 30) * .5);

new_window = open("http://www.casadellibro.com/cdl/cda/javascript/windowPopUp.html","displayWindow","width="+width+",height="+height+",left="+ left +",top="+ top);

}

function checkTextLength(textControl, textName, length) {
    if (textControl.value.length > length) {
      open_error_window('',textName+' no puede contener m&aacute;s de '+length+' caracteres.');
	  textControl.value=textControl.value.substring(0,length-1);
      //textControl.focus();
      return false;
    };
    return true;
}


	function testAffiliate() {
			var str= new String(window.location);
			var arrayOfStrings = str.split("?");
			if (arrayOfStrings.length>1) {
				var arrayOfIgual = arrayOfStrings[1].split("&");
				for (var i=0; i < (arrayOfIgual.length); i++) {
					var item = arrayOfIgual[i].split("=");
					if (item.length>1) {
						var prop= item[0];
						var value= item[1];
						alert(prop+" = "+value);
						if (prop=='ca') {
							alert("Acceso desde Afiliado");
							affiliate=value;
							setCookie(prop,value);
						}
					}
				}
			}
		}
			
		function setAffiliate(codigo) {
			setCookie("ca",codigo,"","/","","");
		}
			
		// name - name of the cookie
		// value - value of the cookie
		// [expires] - expiration date of the cookie (defaults to end of current session)
		// [path] - path for which the cookie is valid (defaults to path of calling document)
		// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
		// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
		// * an argument defaults when it is assigned null as a placeholder
		// * a null placeholder is not required for trailing omitted arguments
		function setCookie(name, value, expires, path, domain, secure) {
		  var curCookie = name + "=" + escape(value) +
		      ((expires) ? "; expires=" + expires.toGMTString() : "") +
		      ((path) ? "; path=" + path : "") +
		      ((domain) ? "; domain=" + domain : "") +
		      ((secure) ? "; secure" : "");
		  document.cookie = curCookie;
		}
		
		// name - name of the desired cookie
		// * return string containing value of specified cookie or null if cookie does not exist
		function getCookie(name) {
		  var dc = document.cookie;
		  var prefix = name + "=";
		  var begin = dc.indexOf("; " + prefix);
		  if (begin == -1) {
		    begin = dc.indexOf(prefix);
		    if (begin != 0) return null;
		  } else
		    begin += 2;
		  var end = document.cookie.indexOf(";", begin);
		  if (end == -1)
		    end = dc.length;
		  return unescape(dc.substring(begin + prefix.length, end));
		}
		
		// name - name of the cookie
		// [path] - path of the cookie (must be same as path used to create cookie)
		// [domain] - domain of the cookie (must be same as domain used to create cookie)
		// * path and domain default if assigned null or omitted if no explicit argument proceeds
		function deleteCookie(name, path, domain) {
		  if (getCookie(name)) {
		    document.cookie = name + "=" + 
		    ((path) ? "; path=" + path : "") +
		    ((domain) ? "; domain=" + domain : "") +
		    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
		  }
		}
		
		// date - any instance of the Date object
		// * hand all instances of the Date object to this function for "repairs"
		function fixDate(date) {
		  var base = new Date(0);
		  var skew = base.getTime();
		  if (skew > 0)
		    date.setTime(date.getTime() - skew);
		}