// JavaScript Document
function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}
function showHideLayers()
{ 
  var i, visStr, obj, args = showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3)
  {
    if ((obj = findObj(args[i])) != null)
    {
      visStr = args[i+2];
      if (obj.style)
      {
        obj = obj.style;
        if(visStr == 'show') visStr = 'block';
        else if(visStr == 'hide') visStr = 'none';
      }
      obj.display = visStr;
    }
  }
}

var idprod=0, idprodfoto=0;
function Dettagli(id, idfoto, objtitolo, titolo, objdescrizione, descrizione, objinformazioni) {
	var obj;
	idprod=id;
	idprodfoto=idfoto;
	if ((obj = findObj(objtitolo)) != null) {
		obj.innerHTML = titolo;
	}
	if ((obj = findObj(objdescrizione)) != null) {
		obj.innerHTML = descrizione;
	}
	if ((obj = findObj(objinformazioni)) != null) {
		obj.innerHTML = '<a href="contatto.php?idprod='+id+'&idprodfoto='+idfoto+'">Richiedi informazioni</a>';
	}
}

function RichiestaInformazioni(idprod, idsottoprod) {
	window.document.location.href='informazioni.php?id='+idprod+'&idvar='+idsottoprod;
}

function openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}

function EstraiStringa(tipo, campo, ricerca){
	if (tipo=='prima')
		campo = campo.substring(0, campo.lastIndexOf(ricerca));
	else
		campo = campo.substr(campo.lastIndexOf(ricerca)+1);
	return campo;
}

function is_array(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function validateForm(){ //funzioni richieste: EstraiStringa - is_array
	var i,p,q,nm,test,num,min,max,errors='',args=validateForm.arguments;
	for (i=0; i<(args.length-2); i+=3) {
		nm=args[i];
		val=args[i+1];
		args[i+2];
		test=(is_array(args[i+2])) ? args[i+2][0] : args[i+2];
	    if (val) {
//			nm=val.name;
//			if (nm.indexOf('_')!=-1){
//				nm=(nm.substr(nm.indexOf('_')+1));
//			}
			if ((val=val.value)!="" && val!='undefined') {
				if (test.indexOf('isEmail')!=-1) {
					p=val.indexOf('@');
					if (p<1 || p==(val.length-1)) errors+='- '+nm+' non è una e-mail valida.\n';
				} else if (test.indexOf('File')!=-1){
//					var ext = test=args[i+3];
					var ext = args[i+2][1];
//					alert(ext);
					var ext2 = ',' + ext + ',';
					var ext_file = EstraiStringa('dopo', val, '.');
					if (ext2.toLowerCase().indexOf(',' + ext_file.toLowerCase() + ',') == -1) errors+='- '+nm+' deve essere un file di tipo ' + ext.toUpperCase().replace(/,/g, ', ') + '.\n';
				} else if (test!='R') {
					num = parseFloat(val);
					if (isNaN(val)) errors+='- '+nm+' non è un numero valido.\n';
					if (test.indexOf('inRange') != -1) {
						p=test.indexOf(':');
						min=test.substring(8,p); max=test.substring(p+1);
						if (num<min || max<num) errors+='- '+nm+' non è un numero tra '+min+' e '+max+'.\n';
					}
				}
			} else if (test.charAt(0) == 'R') errors += '- '+nm+' è obbligatorio.\n';
		}
	}
	if (errors) alert('ATTENZIONE: si sono verificati i seguenti errori:\n'+errors);
  	return (errors == '');
}

function SelezionaCheckBox(formattuale, checkVal, reg){
	re = new RegExp(reg);
//	alert(reg + ' _ ' + re);
	for(i = 0; i < formattuale.elements.length; i++){
		elm = formattuale.elements[i];
		if (elm.type == 'checkbox'){
			if (re.test(elm.id))
				elm.checked = checkVal;
		}
	}
}
