function SwapImg(id, file){
    var el = document.getElementById(id);
    el.src="images/"+file;
}


function isValidEmail(email) {
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
    return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
    return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}


function calculator(registerid){
    this.total = 0;
    this.register= registerid;

    this.add = function(value){
        this.total +=value;
    }
    this.check = function(checkedstate, value){
        if(checkedstate == true){
            this.total += value;
        }else{
            this.total -= value;
        }
    this.showTotal();
    }
    this.showTotal = function(){
    document.getElementById(this.register).value=this.total;
    }

}
var radio = 0;
function valradio(el){
if(el.checked){
radio =1;
}
}

function Check(d, required){
    var errorclass = 'formerror';
if(d == undefined || document.getElementById(d).value == 'N4VK7TS'){
if(d != undefined){
document.getElementById(d).className='';
}
    var bool = true;
    var errortxt = 'Please check the fields marked in red.'; 

    var errorcount = 0;
    for(var i=0; i< required.length; i++){
        formdoc = document.getElementById(required[i]);
        formdoc.className='';  
        if(formdoc.type == 'checkbox' && !formdoc.checked){
            formdoc.className=errorclass;  
            bool = false; 
        }
        if(formdoc.type == 'radio' && radio == 0){
            formdoc.className=errorclass;  
            bool = false; 
        }
        if((formdoc.type == 'textarea' || formdoc.type == 'text') && formdoc.value.length < 3){   
            formdoc.className=errorclass;
            bool = false; 
        }
        if(formdoc.type == 'select-one' && formdoc.selectedIndex == -1){
            formdoc.className=errorclass;
            bool = false; 
        }
        
        if(required[i].substr(required[i].length - 4) == 'mail'){
            if(!isValidEmail(formdoc.value)){
                formdoc.className=errorclass;     
                bool=false;
            }   
        }
    }
    if(!bool){
        alert(errortxt);
        return false;
    }
return true;
}
document.getElementById(d).className=errorclass;
alert('You have entered an incorrect code \nPlease note that the code is case sensitive');
return false;
}
