//_________________________________
//______________START_______________
//Dette script kontrollere at der ikke er nogen tomme felter i kontaktformularen

function isInteger (s)
{ var i;
  if (isEmpty(s))
    if (isInteger.arguments.length == 1) return 0;
    else return (isInteger.arguments[1] == true);
  for (i = 0; i < s.length; i++)
  { var c = s.charAt(i);
    if (!isDigit(c)) return false;
  }
  return true;
}

function isEmpty(s)
{ return ((s == null) || (s.length == 0))
}

function isDigit (c)
{ return ((c >= "0") && (c <= "9"))
}


function validatePosInt(s)
{ //var s = document.frmInput3.txtInputPosInt.value;
  switch(isPositiveInteger(s))
  { case true:
    alert(s + " is a positive integer");
    break;
    case false:
    alert(s + " is not a positive integer");
  }
}

function isPositiveInteger (s)
{ var secondArg = false;
  if (isPositiveInteger.arguments.length > 1)
    secondArg = isPositiveInteger.arguments[1];

       // The next line is a bit byzantine.  What it means is:
       // a) s must be a signed integer, AND
       // b) one of the following must be true:
       //    i)  s is empty and we are supposed to return true for
       //        empty strings
       //    ii) this is a positive, not negative, number

   return (isSignedInteger(s, secondArg)
   && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}


function isSignedInteger (s)
{ if (isEmpty(s))
    if (isSignedInteger.arguments.length == 1) return false;
    else return (isSignedInteger.arguments[1] == true);
  else
  { var startPos = 0;
    var secondArg = false;
    if (isSignedInteger.arguments.length > 1)
      secondArg = isSignedInteger.arguments[1];
    // skip leading + or -
    if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
      startPos = 1;
    return (isInteger(s.substring(startPos, s.length), secondArg))
  }
}

function validateNNInt(s)
{ //var s = document.frmInput4.txtInputNNInt.value;
  switch(isNonnegativeInteger(s))
  { case true:
      alert(s + " is non-negative");
      break;
    case false:
      alert(s + " is not non-negative");
  }
}

function isNonnegativeInteger (s)
{ var secondArg = false;
  if (isNonnegativeInteger.arguments.length > 1)
    secondArg = isNonnegativeInteger.arguments[1];

       // The next line is a bit byzantine.  What it means is:
       // a) s must be a signed integer, AND
       // b) one of the following must be true:
       //    i)  s is empty and we are supposed to return true for
       //        empty strings
       //    ii) this is a number >= 0

  return (isSignedInteger(s, secondArg)
  && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}



//*************************************************//

function verify_kontakt() {

//Følgende validerer syntaksen for en mail:
    var mail_RegExp = /^(|[\w\.\__\-]+@[\w\__\-]+\.[\w\.\__]+)$/i;

// test emails:
  var mail1 = document.kontakt.order_email_input.value;
  var mail2 = document.kontakt.order_email2_input.value;

// amount products:
  var prod1 = document.kontakt.product_one_input.value;
  var prod2 = document.kontakt.product_two_input.value;
  var prod3 = document.kontakt.product_three_input.value;

//checker at feltet order_company_input ikke er tomt
  if(!(isPositiveInteger(prod1) || isPositiveInteger(prod2) || isPositiveInteger(prod3))) {
    alert ('Please fill in a positive number for at least one product');
    return false;
  }
  if(document.kontakt.order_company_input.value=='') {
    alert ('Company or institution name must be filled in');
    return false;
  }
  if(document.kontakt.order_contact_input.value=='') {
    alert ('A contact name must be filled in');
    return false;
  }
  if(mail1!=mail2) {
    alert ('Both emails have to be identical');
    return false;
  }
  if(document.kontakt.order_email_input.value=='') {
    alert ('Please fill in your email');
    return false;
  }
  if(!mail_RegExp.test(document.kontakt.order_email_input.value.toLowerCase())) {
    alert ('Your email does not seem to be valid. Please correct it and try again');
    return false;
  }
  else
    kontakt.submit();
}

// kontrol af formular ved metodefortælling (blog indlæg)

function verify_blog() {

//Følgende validerer syntaksen for en mail:
    var mail_RegExp = /^(|[\w\.\__]+@[\w\__]+\.[\w\.\__]+)$/i;

// test emails:
  var mail1 = document.blog.blog_email_input.value;
  var mail2 = document.blog.blog_email2_input.value;

//checker at feltet order_company_input ikke er tomt
  if(document.blog.blog_company_input.value=='') {
        alert ('Company or institution name must be filled in');
    return false;
  }
  if(document.blog.blog_contact_input.value=='') {
        alert ('A contact name must be filled in');
    return false;
  }
  if(mail1!=mail2) {
        alert ('Both emails have to be identical');
    return false;
  }
  if(document.blog.blog_email_input.value=='') {
        alert ('Please fill in your email');
    return false;
  }
  if(!mail_RegExp.test(document.blog.blog_email_input.value.toLowerCase())) {
        alert ('Your email does not seem to be valid. Please correct it and try again');
    return false;
  }
  else
    blog.submit();
}
//________________SLUT_____________
//_________________________________


