var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function checkmail(e,message){
        
	var returnval=emailfilter.test(e.value)
	if (returnval==false){
		alert(message)
		e.select()
		return false;
	}
	return returnval;
}


function formAdresseValidation(alertMsg, sameFieldMsg, fullFieldMsg, emailMsg){

	
	var same = false;
	if(document.nextStep.elements["same"].checked) {
		var fieldRequired = Array("firstName", "lastName", "email", "line1", "city", "country", "phone");
		var fieldDescription = sameFieldMsg.split("%");
		same = true;
	} else {
	    var fieldRequired = Array("firstName", "lastName", "email", "line1", "city", "country", "phone",
		                          "dfirstName", "dlastName", "demail", "dline1", "dcity", "dcountry", "dphone");
		var fieldDescription = fullFieldMsg.split("%");
	}
	
	var valid = true;
	alertMsg +="\n";
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = document.nextStep.elements[fieldRequired[i]];
		if (obj){
		    
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].value == ""){
					valid = false;
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					valid = false;
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					valid = false;
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					valid = false;
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (valid){
	    if(same) {
                 if(checkmail(document.nextStep.elements["email"],emailMsg))
                   document.nextStep.submit();
		} else {
	         if(checkmail(document.nextStep.elements["email"],emailMsg) && checkmail(document.nextStep.elements["demail"],emailMsg))
                   document.nextStep.submit();
		 
		}
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

function sameDisplay(objectId){
   var object = document.getElementById(objectId);
   state = object.style.display;
   if (document.nextStep.elements["same"].checked)
     object.style.display = 'none';
    else
      object.style.display = 'inline';
}

/**** TCIN, TCOUT validation *****/

function timeValidation (message,input) {
  var tc = input.value;
  var valid = true;
  if(tc!='') 
  {
    var tcTab = tc.split(':');
    if(tcTab.length!=4)
    {
      alert(tc + '   ' + message)
      input.select()
      return false;
    }
    for(var i = 0; i<4; i++)
    {
       if( tcTab[i].length!=2 || isNaN(tcTab[i])  )
       {  
	   valid = false;
       }
    }
    if(!valid)
    {
      alert(tc + '   ' + message)
      input.select()
      return false;
    }
  }
  return true;
}

function tcIntcOuntValidation (message,inputTcin,inputTcout,form) {
  var tcIn = inputTcin.value;
  var tcOut = inputTcout.value;
  var valid = true;
  if(tcIn!='') 
  {
    var tcInTab = tcIn.split(':');
    if(tcInTab.length!=4)
    {
      alert(tcIn + '   ' + message);
      inputTcin.select();
      return false;
    }
    for(var i = 0; i<4; i++)
    {
       if( tcInTab[i].length!=2 || isNaN(tcInTab[i])  )
       {  
	   alert(tcIn + '   ' + message);
           inputTcin.select();
           return false;
       }
    }
  }
  if(tcOut!='') 
  {
    var tcOutTab = tcOut.split(':');
    if(tcOutTab.length!=4)
    {
      alert(tcOut+ '   ' + message);
      inputTcout.select();
      return false;
    }
    for(var i = 0; i<4; i++)
    {
       if( tcOutTab[i].length!=2 || isNaN(tcOutTab[i])  )
       {  
	   alert(tcOut + '   ' + message);
           inputTcout.select();
           return false;
       }
    }
  }
  if(form!='') 
    form.submit();
  return true;
}

/***  confirm delete project action ***/

function confirmDeleteProject(message)
{
  if (confirm(message)==true)
    return true;
  else
    return false;
}

function clearSearchForm()
{
  document.searchform.collection.value='';
  document.searchform.aspectRatio.value     ='';
  document.searchform.resolution.value      ='';
  document.searchform.sourceFormat.value    ='';
  document.searchform.year_from.value       ='';
  document.searchform.year_until.value      ='';
  document.searchform.duration_from.value   ='';
  document.searchform.duration_until.value  ='';
  document.searchform.number.value          ='';
  document.searchform.keyword.value         ='';
  document.searchform.geo.value             ='';
  document.searchform.__son.checked =false;
  document.searchform.clipAvailable.checked =false;
  document.searchform.highDef.checked =false;

}

