//########## Resize of images ##########
 
  max_width = 100;
  max_height = 100;
  max_ratio = max_width / max_height;
  
  product = new Array();
  product[1] = new Image();
  product[2] = new Image();
  product[3] = new Image();
  product[4] = new Image();
  product[5] = new Image();

  function resize(id,name,src) {

    product[id].src = src;
 
    ratio = product[id].width / product[id].height;

    if ( ratio >= max_ratio && product[id].width > max_width )
      document[name].width = max_width;


    if ( ratio < max_ratio && product[id].height > max_height )
      document[name].height = max_height;

  }

//########## Show images in popup ##########

  function showimage(id,src,name,section) {
       
    product[id].src = src;
    
    iH = product[id].height + 40;
    iW = product[id].width + 20;

    sL = (screen.width - iW) / 2;
    sT = (screen.height - iH) / 2;

    if (sL < 1) sL = 1;
    if (sT < 1) sT = 1;
 
    windowFeatures = "left=" + sL + ",top=" + sT + ",width=" + iW + ",height=" + iH + ",toolbar=no,location=no,status=no, menubar=no,scrollbars=no, resizable=no";
    s = "expicture.php?src=" + src + "&name=" + name + "&seccionactual=" + section;
    window.open(s,"",windowFeatures); //Pop Up
  }

//########## More code ##########

function SetText(id,txt) {
        if(txt!='') txt = 'Ir a<br><b>'+txt+'</b>';
        if (document.getElementById) document.getElementById(id).innerHTML=txt;
        else if (document.layers)    document[id].innerHTML=txt;
        else if (document.all)       document.all[id].innerHTML=txt;
}

function getFileExt(sPTF,bDot){
    if(bDot!=true) bDot = false;
    return sPTF.substr(sPTF.lastIndexOf('.')+((!bDot)?1:0))
}

function gosurveyresults(){
  document.location = "index.php?cont=surveyresult";
}

function goloadurl(url){
  document.location = url;
}

function stringlength(s){
 s += '^';
 var length = s.indexOf('^');
 return length;
}

function trim(s){
 s += '|';
 var length = s.indexOf('|');
 var r = "";
 
 var i = 0;
 while(i<length && s.charAt(i)==' ') i++;

 var u = length-1;
 while(u>0 && s.charAt(u)==' ') u--;
 
 for(var k=i; k<=u; k++) r+=s.charAt(k);
 return r;
}

function ValidateSearch( theform ) {
   theform.q.value = trim(theform.q.value);
   if (theform.q.value == '') {
      alert('Debe introducir un texto a buscar. Imposible continuar.');
      return false;
   }
   return true;
}

function printPage() { 
  bV = parseInt(navigator.appVersion)
  if (bV >= 4) window.print();
}

function goCompare(){
 window.open("excompare.php","","scrollbars=1, resizable=1, width=640, height=550, status=0, location=0, menubar=0");
}

function goFreeList(){
 var agree=confirm("Se dispone a vaciar la lista de productos a comparar. ¿Desea continuar?");
 if (agree)
    document.location = "index.php?cont=freelist";
}

function search(){
  
 q = document.forms['frm_sort'].q.value;
 t = document.forms['frm_sort'].t.value;
 sort = document.forms['frm_sort'].sort.value;
 if(sort!='none'){
   document.forms['frm_sort'].action = 'index.php?q='+q+'&t='+t+'&sort='+sort;    
   document.forms['frm_sort'].submit();
 }
}

function searchp(){
 codc = document.forms['frm_sort'].codc.value;
 titc = document.forms['frm_sort'].titc.value;
 sort = document.forms['frm_sort'].sort.value;
 if(sort!='none'){
   document.forms['frm_sort'].action = 'index.php?codc='+codc+'&titc='+titc+'&sort='+sort;    
   document.forms['frm_sort'].submit();
 }
}

function selectfees(){
 gastoenvio = document.forms['frm_fees'].sfees.value;
 if(gastoenvio!="" && gastoenvio!="#"){
   document.forms['frm_fees'].action = 'index.php?cont=cart';  
   document.forms['frm_fees'].submit();
 }
}

function selectformapago(){
 fpago = document.forms['frm_fpagos'].sfpago.value;
 if(fpago !="" && fpago !="#"){
   document.forms['frm_fpagos'].action = 'index.php?cont=cart';  
   document.forms['frm_fpagos'].submit();
 }
}

function selectProduct(){
 pcod = document.forms['frm_products'].pcod.value;
 if(pcod!="" && pcod!="#"){
   document.forms['frm_products'].action = 'index.php';  
   document.forms['frm_products'].submit();
 }
}

function check(CCNum,CCType){

var letter = /^[a-zA-Z\s]+$/;
var msg = 'La tarjeta de crédito que ha introducido no ha podido ser validada. Por favor compruebe los datos y pruebe otra vez.';

//########## Check Visa ##########
	if(CCType.indexOf('Visa') !=-1){
		if ((CCNum.length == 13 || CCNum.length == 16) && (CCNum.substring(0,1) == 4))
           return true
        else{
	       alert(msg);  
	 	   return false;
	    }
    }

//########## Check Delta ##########
	if(CCType.indexOf('Delta') !=-1){
		if ((CCNum.length==16) && (CCNum.match(/^(413733|413734|413735|413736|413737|4462|453978|453979|454313|454313|454432|454433|454434|454435|454742|456725|456726|456727|456728|456729|45673|456740|456741|456742|456743|456744|456745|46583|46584|46585|46586|46587|484409|484410|49096|49097|492181|492182|498824)/)))
           return true;
		else{
			alert(msg);  
        	return false;
	    }
    }

//########## Check Solo ##########
	if(CCType.indexOf('Solo') !=-1){
		if ((CCNum.length==16 || CCNum.length==18 || CCNum.length==19) && (CCNum.match(/63345|63346|63347|63348|63349|6767/)))
  	       return true;
		else{
			alert(msg);  
	        return false;
        }
    }

//########## Check Mastercard ##########
	if(CCType.indexOf('Mastercard') !=-1){
		var firstdig=CCNum.substring(0,1);
		var seconddig=CCNum.substring(1,2);
		if ((CCNum.length == 16 || CCNum.length == 19) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5)))
           return true;
		else{
			alert(msg);  
            return false;
     	}
    }

//########## Check Diners ##########
	if(CCType.indexOf('Diners') !=-1){
		firstdig = CCNum.substring(0,1);
		seconddig = CCNum.substring(1,2);
		if ((CCNum.length == 14 || CCNum.length == 17)  && (firstdig == 3) && ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
           return true;
		else{
			alert(msg);  
 	        return false;
    	}
    }

//########## Check Amex ##########
	if(CCType.indexOf('Amex') !=-1){
		firstdig = CCNum.substring(0,1);
		seconddig = CCNum.substring(1,2);
		if (((CCNum.length == 15)  || (CCNum.length == 18)) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7)))
           return true;
		else{
			alert(msg);  
            return false;
     	}
    }

//########## Check Switch ##########
	if(CCType.indexOf('Switch') !=-1){
		if ((CCNum.length==16) && (CCNum.match(/^(491101|491102|564182|633300|633302|633303|633304|633305|633306|633307|633308|633309|633310|633311|633312|633313|633314|633315|633316|633317|633318|633319|633320|633321|633322|633323|633324|633325|633326|633327|633328|633329|633330|633331|633332|633333|633334|633335|633336|633337|633338|633339|633340|633341|633342|633343|633344|633345|633346|633347|633348|633349|675900|675902|675903|675904|675906|675907|675908|675909|675910|675911|675912|675913|675914|675915|675916|675917|675919|675920|675921|675922|675923|675924|675925|675926|675927|675928|675929|675930|675931|675932|675933|675934|675935|675936|675937|675941|675942|675943|675944|675945|675946|675947|675948|675949|675963|675964|675965|675966|675967|675968|675969|675970|675971|675972|675973|675974|675975|675976|675977|675978|675979|675980|675981|675982|675983|675984|675985|675986|675987|675988|675989|675990|675991|675992|675993|675994|675995|675996|675997|675999)/))||
		   (CCNum.length==18) && (CCNum.match(/^(675938|675939|675940|490302|490303|490304|490305|490306|490307|490308|490309|490335|490336|490337|490338|490339|491174|491175|491176|491177|491178|491179|491180|491181|491182)/))||
		   (CCNum.length==19) && (CCNum.match(/^(4936|633301|675901|675905|675918|675950|675951|675952|675953|675954|675955|675956|675957|675958|675959|675960|675961|675962|675998)/)))
	        return true;
	    else{
	    	alert(msg);  
        	return false;
	    }
    }

//########## Check JCB ##########
	if(CCType.indexOf('jcb') !=-1){
		if ((CCNum.length==16) && (CCNum.match(/^(3088|3096|3112|3158|3337|3528)/)))
	       return true;
		else{
			alert(msg);  
         	return false;
		}
	}
}


function confirmSubmit(){
 if(!document.forms['frmsend'].aceptacondiciones.checked){
   alert("Debe aceptar las condiciones de compra para continuar con el pedido. Imposible continuar.");
   return false;
 }
 
 if(document.forms['frmsend'].creditcard.value=='1'){
   var anio = document.forms['frmsend'].CCEYear.options[document.forms['frmsend'].CCEYear.selectedIndex].value;
   var mes = document.forms['frmsend'].CCEMonth.options[document.forms['frmsend'].CCEMonth.selectedIndex].value;
   var cctype = document.forms['frmsend'].CCType.options[document.forms['frmsend'].CCType.selectedIndex].value;
   var ccnum = document.forms['frmsend'].CCNum.value; 
   var ccname = document.forms['frmsend'].CCName.value; 
   if(ccname==""){
      alert("Debe introducir el nombre del titular de la tarjeta de crédito.");
      return false;
   }

   if(ccnum==""){
      alert("Debe introducir un número de tarjeta de crédito.");
      return false;
   }

   var today = new Date();
   var expiry_date = new Date(anio, mes);
   if(expiry_date < today){
		alert('Su tarjeta de crédito ha expirado.');
		document.forms['frmsend'].CCEYear.focus();
     	return false;
   }

   var ccnum = ccnum.replace(/ /g, "");
   if(!check(ccnum,cctype))
      return false;
   //else
   //   alert("Tarjeta correcta!");
 }

 var agree=confirm("Se dispone a enviar el pedido. ¿Desea continuar?");
 if (agree)
    return true ;
 else
    return false ;
}

function go_but_confirm ( urltogo, msgdlg ){
 if(confirm(msgdlg))
    location = urltogo;
}

function openwindow( url ){
    window.open(url,'','scrollbars=1, resizable=1, width=350, height=350, status=0, location=0, menubar=0');
}


function openwindow2( url ){
    window.open(url,'','scrollbars=1, resizable=1, width=600, height=550, status=0, location=0, menubar=0');
}

function openwindowWind( url ){
    window.open(url,'','scrollbars=1, resizable=1, width=700, height=320, status=0, location=0, menubar=0');
}

function OpenForPrint( url ){
    window.open(url,'','scrollbars=1, resizable=1, width=700, height=520, status=0, location=0, menubar=0');
}

function confirmSendAvMsg(theForm){

 theForm.nombre.value = trim(theForm.nombre.value);
 theForm.email.value = trim(theForm.email.value);
 theForm.comentario.value = trim(theForm.comentario.value);

 if(theForm.nombre.value==''){
   alert("Debes introducir tu nombre. Imposible continuar.");
   return false;
 }
 if(theForm.email.value==''){
   alert("Debes introducir tu email. Imposible continuar.");
   return false;
 }
 if(theForm.comentario.value==''){
   alert("Debes introducir tu comentario. Imposible continuar.");
   return false;
 }
 return true;
}

