
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 allValidExtChars(ext) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz";
  for (var i=0; i < ext.length; i++) {
    var letter = ext.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function arrowDisplay( btn, display, vis) {
    // Change mousepointer status
    if (display == '') {
        document.body.style.cursor='auto';
    }
    else {
        document.body.style.cursor='pointer';
    }
    // Set arrow image
    var obtn1 = document.getElementById(btn);
    obtn1.src = "tpl/img/" + btn + display + ".png";
    // Set arrow visibility
    var obtn2 = document.getElementById("pd" + btn);
    obtn2.style.visibility = vis;
}

function checkContactForm() {
    // Initialize 'stop' variable
    var stop = 0;
    // Check form entries
    var obtn1 = document.getElementById("name");
    if (obtn1.value == "") {
        obtn1.style.borderColor = "#FF0000";
        obtn1.style.borderWidth = "2px";
        stop = 1;
    }
    else {
        obtn1.style.borderColor = "#9C99A0";
        obtn1.style.borderWidth = "1px";
    }
    var obtn2 = document.getElementById("phone");
    if (obtn2.value == "") {
        obtn2.style.borderColor = "#FF0000";
        obtn2.style.borderWidth = "2px";
        stop = 1;
    }
    else {
        obtn2.style.borderColor = "#9C99A0";
        obtn2.style.borderWidth = "1px";
    }

    var obtn3 = document.getElementById("email");
    if (!isValidEmail(obtn3.value)) {
        obtn3.style.borderColor = "#FF0000";
        obtn3.style.borderWidth = "2px";
        stop = 1;
    }
    else {
        obtn3.style.borderColor = "#9C99A0";
        obtn3.style.borderWidth = "1px";
    }
    
    return stop;
}

function goToUrl(client) {
    // Create new window for Client website
    var attr = "height=" + parseInt(screen.availHeight-110) + ",width=" + parseInt(screen.availWidth-10) + ",top=0,left=0,menubar,toolbar,status,location,resizable,scrollbars"
    var clientWindow = window.open(client,"client",attr)
}

function isValidEmail(email, required) {
    if (required==undefined) {
        // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    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.length == email.indexOf(".") + 1) {
        // . must not be the last character
	    return false;
    }
    if (!allValidExtChars(email.substr(email.lastIndexOf(".") + 1))) {
        // check to make sure all extension characters are valid
        return false;
    }
    else if ((email.substr(email.lastIndexOf(".") + 1)).length < 2 || (email.substr(email.lastIndexOf(".") + 1)).length > 4) {
        // extension length must be between 2 and 4 characters
	    return false;
    }
    return true;
}

function mousepointerDisplay( status) {
    // Change mousepointer status
    document.body.style.cursor = status;
}

function submitForm( frm) {
    // Check form
    if (frm == 'mad-id_contactform') {
        var stop = checkContactForm();
    }
    else {
        var stop = 0;
    }
    // Submit form
    if (stop == 0) {
        var obtnfrm = document.getElementById(frm);
        obtnfrm.submit();
    }
}

function switchImage( image) {
    // Set image visibility
    for (var i = 1; i <= 20; i++) {
    	var obtn = document.getElementById("image" + i);
    	var obtnCom = document.getElementById("comment" + i);
    	if (image == "image" + i) {
    		obtn.style.visibility = 'visible';
    		obtnCom.style.visibility = 'visible';
    	}
    	else {
    		obtn.style.visibility = 'hidden';
    		obtnCom.style.visibility = 'hidden';
    	}
    }
}

function switchReel( reel) {
    // Set image and arrow visibility
    var obtn1 = document.getElementById("character-setup-index");
    var obtn2 = document.getElementById("matchmoving-index");
    var obtn3 = document.getElementById("previous");
    var obtn4 = document.getElementById("next");
    if (reel == 'character-setup') {
	    obtn1.style.visibility = '';
	    obtn2.style.visibility = 'hidden';
	    obtn3.style.visibility = 'hidden';
	    obtn4.style.visibility = '';
    }
    else if (reel == 'matchmoving') {
	    obtn1.style.visibility = 'hidden';
	    obtn2.style.visibility = '';
	    obtn3.style.visibility = '';
	    obtn4.style.visibility = 'hidden';
    }
}
