// JavaScript Document

function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateUsername(theForm.myname);
  //reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.myemail);
	reason += validatePassword(theForm.mypassword);
	reason += validatePassword2(theForm.mypassword2);	
  //reason += validatePhone(theForm.phone);
  //reason += validateEmpty(theForm.myname);
	//reason += validateEmpty(theForm.myemail);
	//reason += validateEmpty(theForm.mysubject);
	//reason += validateEmpty(theForm.mymessage);
      
  if (reason != "") {
    alert("Attention:\n" + reason);
    return false;
  }

  return true;
}

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateUsername(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "Please enter your name.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validatePassword(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "Please enter alphanumeric password.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validatePassword2(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Pink'; 
        error = "Please repeat your password\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Pink';
        error = "Please enter your email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Pink';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Pink';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function form_sent(ticketid)
{
	alert("Thank you. The contact form has been sent successfully.\nPlease allow 24-48 hours for us to reply your query.")
}

function form_notsent()
{
	alert("Sorry. The contact form can not be sent at this moment.\nPlease email us at admin@vacoplus.com")
}

///////////////////////clear default value onFocus////////////////////////////////////////
// specify the name of your form
var thisForm = "signup";

// load field names and default values into list
var defaultVals = new Array();
defaultVals[0] = new Array("myname", "YOUR FULL NAME");
defaultVals[1] = new Array("myemail", "YOUR E-MAIL ADDRESS");
defaultVals[2] = new Array("mypassword", "YOUR PASSWORD");
defaultVals[3] = new Array("mypassword2", "REPEAT YOUR PASSWORD");

// populate fields with default values on page load
function MPLoadDefaults() {
with (document.forms[thisForm]) {
for (var n=0; n<defaultVals.length; n++) {
var thisField = defaultVals[n][0];
var thisDefault = defaultVals[n][1];
if (elements[thisField].value == '')
elements[thisField].value = thisDefault;
}}}

// clear default value from field when selected
function MPClearField(field) {
var fieldName = field.name;
for (var n=0; n<defaultVals.length; n++) {
var thisField = defaultVals[n][0];
var thisDefault = defaultVals[n][1];
if (thisField == fieldName) {
if (field.value == thisDefault) field.value = '';
break;
}}}

// clear all defaults when form is submitted
function MPClearAll() {
with (document.forms[thisForm]) {
for (var n=0; n<defaultVals.length; n++) {
var thisField = defaultVals[n][0];
var thisDefault = defaultVals[n][1];
if (elements[thisField].value == thisDefault)
elements[thisField].value = '';
}}}

function popitup(url)
{
   newwindow = window.open(url, '', 'height=600, width=700, resizable=0, status=1, left=180, top=60, scrollbars=1');
   if (window.focus)
      { newwindow.focus() }
	  return false;
}

//checkboxes

checked=false;
function checkedAll(remove) {
	var aa= document.getElementById('remove');
	 if (checked == false)
          {
           checked = true
          }
        else
          {
          checked = false
          }
	for (var i =0; i < aa.elements.length; i++) 
	{
	 aa.elements[i].checked = checked;
	}
}

function confirmdelete()
{
  if(confirm("Are you sure you want to remove selected items?"))
  {
   	return true
  }
	
	else
	{
		return false
	}
}

function confirmadd()
{
  if(confirm("Are you sure you want to add this item?"))
  {
   	return true
  }
	
	else
	{
		return false
	}
}

function confirmedit()
{
  if(confirm("Are you sure you want to update this item?"))
  {
   	return true
  }
	
	else
	{
		return false
	}
}

function confirmupdateacc()
{
  if(confirm("Are you sure you want to update your account?"))
  {
   	return true
  }
	
	else
	{
		return false
	}
}

function ReverseDisplay(d) {
if(document.getElementById(d).style.display == "none") 
{ document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}