function SimpleSwap(el,which){
  el.src=el.getAttribute(which || "origsrc");
}

function SimpleSwapSetup(){
  var x = document.getElementsByTagName("a");
  for (var i=0;i<x.length;i++){
    var oversrc = x[i].getAttribute("oversrc");
    if (!oversrc) continue;
      
    // preload image
    // comment the next two lines to disable image pre-loading
    x[i].oversrc_img = new Image();
    x[i].oversrc_img.src=oversrc;
    // set event handlers
    x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
    x[i].onmouseout = new Function("SimpleSwap(this);");
    // save original src
    x[i].setAttribute("origsrc",x[i].src);
  }
}

var PreSimpleSwapOnload =(window.onload)? window.onload : function(){};
window.onload = function(){PreSimpleSwapOnload(); SimpleSwapSetup();}




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

  reason += validateEmail(theForm.email);
  reason += validateEmailSame(theForm.email, theForm.email_conf);
  reason += validateEmpty(theForm.name);
  reason += validateEmpty(theForm.phone);
  reason += validateEmpty(theForm.location);

  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

function validateEmailSame(femail, femailconf){
  var error = "";
  if (femail.value == femailconf.value){
    femail.style.background = 'White';
    femailconf.style.background = 'White';
  } else {
     femail.style.background = 'Yellow';
     femailconf.style.background = 'Yellow';
     error = "The email adresses are not the same.\n"
  } 
  return error;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field '" + fld.id + "' has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores

    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } else if (fld.style.background == "#fcff05"){
	error = "The username already exists.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function passwordConfirm(fldp, fldc) {
  var error = "";
 
    if (fldp.value != fldc.value) {
        fldp.style.background = 'Yellow'; 
	  fldc.style.background = 'Yellow';
        error = "The passwords do not match.\n"
    } else {
        fldp.style.background = 'White';
        fldc.style.background = 'White';
    }
    return error;  
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value != "") {
        
	    if ((fld.value.length < 5) || (fld.value.length > 15)) {
	        error = "The password is the wrong length. \n";
	        fld.style.background = 'Yellow';
	    } else if (illegalChars.test(fld.value)) {
	        error = "The password contains illegal characters.\n";
	        fld.style.background = 'Yellow';
	    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
	        error = "The password must contain at least one numeral.\n";
	        fld.style.background = 'Yellow';
	    } 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 = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}


function FillInvoice(f) {
  if(f.sameastrade.checked == true) {
    f.invoice_address.value = f.trading_address.value;
    f.invoice_postcode.value = f.trading_postcode.value;
    f.invoice_county.value = f.trading_county.value;
    f.invoice_telephone.value = f.telephone.value;
    f.invoice_fax.value = f.fax.value;
  }
}

function FillPickup(f) {
  if(f.sameasinvoice.checked == true) {
    f.pickup_address.value = f.invoice_address.value;
    f.pickup_postcode.value = f.invoice_postcode.value;
    f.pickup_county.value = f.invoice_county.value;

  }
}

function FillPayment(f) {
  if(f.sameastradedetails.checked == true) {
    f.payment_name.value = f.name.value;
    f.payment_email.value = f.email.value;
    f.payment_telephone.value = f.telephone.value;
    f.payment_fax.value = f.fax.value;
  }
}

