﻿function UPLOAD_show_second()
{
  if (document.getElementById("qty").value == "Other")
  {
    document.getElementById("qty2").value = '';
    document.getElementById("qty2").style.visibility = 'visible';
  }
  else
  {
    document.getElementById("qty2").value = document.getElementById("qty").value;
    document.getElementById("qty2").style.visibility = 'hidden';
  }
}

function UPLOAD_phone(idthis,idnext,length,evt)
{
  if (document.getElementById(idthis).value.length >= (length))
    document.getElementById(idnext).focus();
  return UPLOAD_allow_Number(evt);
}

function UPLOAD_allow_Number(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	return (charCode >= 48 && charCode <= 57);
}

function UPLOAD_validate() {


    var borderGood = "solid 1px #aacfe4";
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;  //Regex to check if it's an email


    var ids = [];             //array to store the various inputs to check (as ID name)


    var checks = [];	      //array to store the various TYPE of check should be performed such as:
			      // ne = Anything in the input (value.length > 0)
                              // email = Check for email formatting AND that value.length > 0
                              // # = Any number specifies that the value.length >= #

    var errors = [];          //error message to be displayed by UPLOAD_error()


    ids[0] = "name";          //Person's name
    ids[1] = "email";         //Email address
    ids[2] = "jobName";       //Job name
    ids[3] = "qty2";          //Quantity input (hidden/non-hidden one)
    ids[4] = "nr1";           //Telephone number set 1 (Area code)
    ids[5] = "nr2";           //Telephone number set 2
    ids[6] = "nr3";           //Telephone number set 3
    ids[7] = "file";          //File input




    checks[0] = "ne";         //Name
    checks[1] = "email";      //Email
    checks[2] = "ne";         //JobName
    checks[3] = "ne";         //Qty2
    checks[4] = 3;            //Phone Number 1 (Area code)
    checks[5] = 3;            //Phone Number 2
    checks[6] = 4;            //Phone Number 3
    checks[7] = "ne";         //File


    errors[0] = "You need to specify your name";
    errors[1] = "You need to input a VALID email address";
    errors[2] = "You need to input a job name for reference";
    errors[3] = "You must select a quantity or input one into the custom quantity box";
    errors[4] = "You must specify a phone number";
    errors[5] = "You must specify a phone number";
    errors[6] = "You must specify a phone number";
    errors[7] = "You must select a file to upload";


    var _ENUM = 0;

    while (ids[_ENUM] != null)
    {
      document.getElementById(ids[_ENUM]).style.border = borderGood;
      _ENUM++;
    }

    _ENUM = 0; //reset _ENUM

    while (ids[_ENUM] != null)
    {
      if (typeof checks[_ENUM] == "number")  //# is supplied and value.length >= #
      {
        if (document.getElementById(ids[_ENUM]).value.length < checks[_ENUM])
        {
          UPLOAD_error(true,errors[_ENUM],ids[_ENUM]);
          return false;
        }
      }
      else if (checks[_ENUM] == "ne") //ne is supplied, and the input just needs a single character
      {
        if (document.getElementById(ids[_ENUM]).value == "")
        {
          UPLOAD_error(true,errors[_ENUM],ids[_ENUM]);
          return false;
        }
      }
      else if (checks[_ENUM] == "email") //email is supplied, and the input needs to be checked if it's an email
      {
        if (reg.test(document.getElementById(ids[_ENUM]).value) == false)
        {
          UPLOAD_error(true,errors[_ENUM],ids[_ENUM]);
          return false;
        }
        else
        {
          
        }
      }
      _ENUM++;
    }
    
    return "upload";
}

function UPLOAD_error(show,value,id)
{
  document.getElementById("error").style.visibility = (show) ? 'visible' :  'hidden';
  document.getElementById("errorinfo").innerHTML = value;
  if (id)
  {
    document.getElementById(id).style.border = "solid 1px #FF0000";
  }
}

