// Written by Lahoucine Lhamous.
// For General Nanotechnology/Metadigm

//function isBlank( s)
//function verify(f)
//function verify1() 
//function Confirm(f)
//function makedeletelist(f,n)
//function makenotifylist(f,n)
//function clicie()
//function clicns(e)
//function MaskRightClick()

 
function isBlank( s)
{
	for ( var i=0; i < s.length; i++)
		{
		var c= s.charAt(i);
		if ((c!= ' ') && (c != '\n') && (c != '\t'))
			{
	 		return false;
			}
		}
	return true;
}

function verify(f)
{
   var msg;
	var empty_fields="";
	var errors="";
	var mailer;

   // this function is call within the <FORM> tag as follow.
   // <FORM name="formname" Action="your action" Method="your method"
   //   onSubmit="this.fieldname.optional=true;...; return verify(this);"

	for ( var i=0; i < f.length; i++)
		{
		var e = f.elements[ i];
		if ((( e.type == "text") || ( e.type == "password") || ( e.type == "textarea")) && !e.optional)
			{
			if ( (e.value == null) || (e.value == "") || (isBlank( e.value)))
				{
            
				empty_fields +="\n    "+ e.name;
				continue;
				}
			if ( e.name == "email")
				{
				mailer = e.value;
				if (	mailer.length < 5 || mailer.indexOf('@') == -1 || mailer.indexOf(' ') != -1)
					{
					empty_fields ="\n    "+ e.name + " empty or contains invalid characters";
					continue;
					}
				}
			if ( e.numeric || (e.min != null) || ( e.max != null))
				{
				var v = parseFloat( e.value);
				if (  isNaN(v)
			     	|| ((e.min != null) && ( v < e.min))
				  	|| ((e.max != null) && ( v > e.max))
					)
					{
					errors += "- the field " + e.name + " must be a number";
					if ( e.min != null)
				   	errors += " that is greater than " + e.min;
					if ( e.max != null && e.min != null)
				   	errors += " and less than " + e.max;
					else if ( e.max != null)
				   	errors += " that less than " + e.max;
					}
				}
			}
		}
	if ( !empty_fields && !errors)
	   return true;
	msg  = "_____________________________________________________________\n";
	msg += "The form was not submitted because of the following error(s).\n";
	msg += "Please correct these error(s) and re-submit.\n";
	msg += "_____________________________________________________________\n";
	if ( empty_fields)
		{
		msg += " - The following required field(s) are empty:" + empty_fields + "\n";
		if ( errors)
		   msg += "\n";
		}
	msg += errors;
	alert ( msg);
	return false;
}

function verify1() {
	var first  = document.asker.first.value;
	var last   = document.asker.last.value;
	var phone  = document.asker.phone.value;
   var mailer = document.asker.email.value;
	var address = document.asker.address1.value;
   var city   = document.asker.city.value;
	var state  = document.asker.state.value;
	var zip    = document.asker.zip.value;

	if (first.length < 2 || first.indexOf(' ') != -1) {
		alert ("A valid first name must not contain spaces!!!");
      document.asker.first.focus();
		return false;
		}
   if (last.length < 2 || last.indexOf(' ') != -1) {
		alert ("A valid last name must not contain spaces.");
      document.asker.last.focus();
		return false;
		}
   if (address.length < 2 ) {
		alert ("Enter a valid address.");
      document.asker.address1.focus();
		return false;
		}
   if (city.length < 2) {
		alert ("Enter a valid city.");
      document.asker.city.focus();
		return false;
		}
   //selectionname = document.imagename.options[form.imagename.selectedIndex].text;
   //selection = form.imagename.options[form.imagename.selectedIndex].value;

   if (state.length < 2) {
	 	alert ("Enter a valid state at least 2 char.");
      document.asker.state.focus();
		return false;
		}

   if (zip.length < 2 || zip.indexOf(' ') != -1) {
		alert ("A valid Zip must not contain spaces <5 numb at least>.");
      document.asker.zip.focus();
		return false;
		}
   if (phone.length < 2 || phone.indexOf(' ') != -1) {
		alert ("Enter a valid phone number.");
      document.asker.phone.focus();
		return false;
		}

   if (mailer.length < 5 || mailer.indexOf('@') == -1 || mailer.indexOf(' ') != -1) {
		alert ("Enter a valid e-mail address. Remove any spaces you might have.");
      document.asker.email.focus();
		return false;
		}
      if( document.asker.first.value == document.asker.last.value)
      {
        alert ("This occurence exist already");
        document.asker.last.focus();
        return false;
      }
      if( document.asker.first.value == document.asker.phone.value || document.asker.phone.value == document.asker.last.value )
      {
        alert ("This occurence exist already");
        document.asker.phone.focus();
        return false;
      }
      if( document.asker.first.value == document.asker.address1.value || document.asker.last.value == document.asker.address1.value || document.asker.phone.value == document.asker.address1.value)
      {
        alert ("This occurence exist already");
        document.asker.address1.focus();
        return false;
      }
      if( document.asker.first.value == document.asker.city.value || document.asker.last.value == document.asker.city.value || document.asker.phone.value == document.asker.city.value ||  document.asker.address1.value== document.asker.city.value )
      {
        alert ("This occurence exist already");
        document.asker.city.focus();
        return false;
      }
      if( document.asker.first.value == document.asker.state.value || document.asker.last.value == document.asker.state.value || document.asker.phone.value == document.asker.state.value ||  document.asker.address1.value== document.asker.state.value || document.asker.city.value == document.asker.state.value)
      {
        alert ("This occurence exist already");
        document.asker.state.focus();
        return false;
      }
      if( document.asker.first.value == document.asker.zip.value || document.asker.last.value == document.asker.zip.value || document.asker.phone.value == document.asker.zip.value || document.asker.address1.value == document.asker.zip.value || document.asker.city.value == document.asker.zip.value || document.asker.state.value == document.asker.zip.value)
      {
        alert ("This occurence exist already");
        document.asker.zip.focus();
        return false;
      }

           var spellCheck = confirm("You entered< " + mailer + " > as your e-mail address. Choose OK if this is correct, or Cancel to make changes.");
	if (!spellCheck) {
                document.asker.email.focus();
		return false;
		}
  }

function Confirm(f)
{
	msg="\nYou are about to Generate a passcode\n\n"+
       "Continue?\n\n";
   return confirm(msg);
}

function makedeletelist(f,n)
{
  	var list;
   var first;
   var mytmp
   var mymsg;
   var nbchecked;

   first = 1;
   nbchecked = 0;

   mymsg = "";
  	for ( k=1; k <= n; k++)
		{
      v = eval ("document.form1.CB"+k+".checked");
      if ( v)
         {
         nbchecked++;
         op = eval("document.form1.CB"+k+".value");
         if ( first)
            {
            list = op;
            mytmp  = op;
            first = 0;
            }
         else
         	{
         	list = list + "=" + op;
            mytmp= mytmp + " \n" + op;
            }
         }
      }

   if ( nbchecked == 0)
      {
      alert("To delete users, at least one (or more) user(s) should be selected\nby checking the check box on the far left column.");
      return false;
      }

	document.form1.userlist.value = list;
   if ( list.length)
   	{

      mymsg ="Are you sure you want to delete the user(s) below?:\n"+ mytmp + "\n\n";
      mymsg += "\nDelete ?\n";
      if ( confirm(mymsg))
      	return true;
      else
         return false;
      }
     else
     	  return false;
}

function makenotifylist(f,n)
{
  	var list;
   var first;
   var mytmp
   var mymsg;
   var nbchecked;

   first = 1;
   nbchecked = 0;
	var loop=0;


   mymsg = "";
  	for ( k=1; k <= n; k++)
		{
      v = eval ("document.form2.CB"+k+".checked");
      if ( v)
         {
         nbchecked++;
         op = eval("document.form2.CB"+k+".value");
         if ( first)
            {
            list = op;
            mytmp  = op;
            first = 0;
            }
         else
         	{
				loop++;
         	list = list + "=" + op;
				if ( loop==30)
					{
					loop = 0;
					mytmp= mytmp + " \n" + op;
					}
				else
					mytmp= mytmp + "," + op;
            }
         }
      }
   if ( nbchecked == 0)
      {
      alert("To notify users, at least one (or more) user(s) should be selected\nby checking the check box on the far left column.");
      return false;
      }

	document.form2.notifylist.value = list;
   if ( list.length)
   	{
		if ( nbchecked > 100)
			{
			mymsg ="Are you sure you want to notify the user(s) about " + nbchecked + "?:\n\n";
			mymsg += "\nNotify ?\n";
			}
		else
			{
			mymsg ="Are you sure you want to notify the user(s) below?:\n"+ mytmp + "\n\n";
			mymsg += "\nNotify ?\n";
			}

      if ( confirm(mymsg))
      	return true;
      else
         return false;
      }
     else
     	  return false;
}


function clicie() {
 // Fonction de détection pour Internet Explorer
 if (event.button==2) {
  alert("Thanks for visiting Metadigm Site");
 }
}

function clicns(e){
 // Fonction pour Netscape
 if (e.which==3){
   alert("Thanks for visiting Metadigm Site");
   return false;
 }
}

function MaskRightClick()
{
	if (document.all) 
	   { 
	   document.onmousedown=clicie;
	   }
	if (document.layers) 
	   {
	   document.captureEvents(Event.MOUSEDOWN); document.onmousedown = clicns;
	   }
}


