/*
this file contains all the javascript functions needed for ecosav.com
brendan roberts
September 2004
*/




// the popup window deal
    count = 0;
    function winName(file) {
      return 'Win' + count++;
    }

    function winName(file) {
      f = file.replace(/\//g,"_");
      f = f.replace(/\./g,"_");
      f = f.replace(/\-/g,"_");
      return f;
    }

    function openWindow(file, width, height) {
      unique = winName(file);

      var Win;
	  Win = open(file, 'win' + unique, 'toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=0,width=' + width + ',height=' + height + ',scrollbars=0');
      Win.focus();
    }




// and the image roller
function newImage(arg) {
     if (document.images) {
          rslt = new Image();
          rslt.src = arg;
          return rslt;
     }
}




// image swapper
function changeImages(id,name){
  if (document.images) {document.images[id].src=eval(name+".src"); }
}




// form validator for the contact page
function submitForm2(form) {
  form.submit();
}



function submitForm(form) {


 
var drop;

title = form.c_title.value;
companyname = form.c_company_name.value;
forename = form.c_forename.value;
surname = form.c_surname.value;
email = form.c_email.value;
phone = form.c_phone.value;
address = form.c_address_1.value;
town = form.c_town.value;
state = form.c_state.value;
postal = form.c_postal.value;


var msg = 'Wrong input\n';
var missingObl = true;
var msgObl = '\nThe following fields must be filled in:\n';
var missingFor = true;
var msgFor = '\nThe following fields contains wrong formatted data:\n';



if (isEmpty(title)) {
  missingObl = false;
  msgObl += '- Title\n';
}

if (isEmpty(forename)) {
  missingObl = false;
  msgObl += '- Forename\n';
}

if (isEmpty(surname)) {
  missingObl = false;
  msgObl += '- Surname\n';
}


if (isEmpty(email)) {
  missingObl = false;
  msgObl += '- Email\n';
}
else if(!isEmail(email)) {
     missingFor = false;
     msgFor += '- E-mail\n';
}

if (isEmpty(phone)) {
  missingObl = false;
  msgObl += '- Phone\n';
}
else if(!isNumber(phone)) {
     missingFor = false;
     msgFor += '- Phone\n';
}


if (isEmpty(address)) {
  missingObl = false;
  msgObl += '- Address\n';
}

if (isEmpty(town)) {
  missingObl = false;
  msgObl += '- Town\n';
}

if (isEmpty(state)) {
  missingObl = false;
  msgObl += '- State\n';
}

if (isEmpty(postal)) {
  missingObl = false;
  msgObl += '- Postal\n';
}









if (!missingObl || !missingFor) {
    if(!missingObl) {
      msg += msgObl;
    } 
    if (!missingFor) {
      msg += msgFor;
    }
    alert(msg);
  return false;
  }
  else {

 for(i=0;i<contactForm.length;i++)
  if(contactForm.elements[i].type == "submit" || contactForm.elements[i].type == "reset")
  contactForm.elements[i].disabled = true;


    return true;
}


  
function isNumber(theValue) {
    return !isNaN(Math.abs(theValue));
}

function isEmpty(theValue) {
    return (theValue.charAt(0) == ' ' || theValue == '');
}

function isEmail(strEmail) {
     if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
       return true;
   else
     return false;
}




}
