function openWindow(url, title, atts, album_url) {
    var my_copy = album_url + ''; // to not modify top.location!
    if (album_url) {
        url = url + '&album_url=' + my_copy.replace(/&/, "--");
    }
    window.open(url, title, atts);
    return false;
}

function toggle_password() {
    var allow = document.getElementById('allow');
    var span = document.getElementById('password_message');
    var pwd = document.getElementById('password');
    if (allow.checked) {
        span.innerHTML = 'Verify your password:';
        pwd.style.display = 'inline';
    }
    else {
        span.innerHTML = 'Grant access to your private album';
        pwd.style.display = 'none';
    }
}

function init() {
    if (navigator.appVersion.indexOf("Mac") != -1) {
        var inputs = document.documentElement.getElementsByTagName('input');
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].className == 'std') {
                inputs[i].style.background_color = "#8E7B77";
            }
        }
    }

}

function clearmsg () {
    if (document.card.msg.value == 'Type your message here') {
        document.card.msg.value = '';
    }
}

var newlinecount = 0;
var alerted = false;
function newnewline() {
    newlinecount++;
    return newlinecount >= 5 ? '' : '\n';
}

function limitlength(tag) {
    var maxlength = 100;
    newlinecount = 0;
    var newvalue = tag.value.replace(/\n/g, newnewline);
    if (newvalue.length >= maxlength) {
        if (!alerted) {
            alert("Sorry, your postcard's message can have a maximum length of 100 characters.");
            alerted = true;
        }
        newvalue = newvalue.substr(0, maxlength - 1);
    }
    if (newvalue != tag.value) {
        tag.value = newvalue;
    }
}

//function to validate by length
function validlength(item, len) {
   return (item.length >= len);
}
//function to validate an email address
function validemail(item) {
   if (!validlength(item, 5)) return false;
   if (item.indexOf ('@', 0) == -1) return false;
   return true;
}
// display an error alert
function error(elem, text) {
   window.alert(text);
   elem.select();
   elem.focus();
}

function validate_postcard() {
	var errfound = false;
	if (!validlength(document.card.name.value,2)) {
		error(document.card.name,"Sorry, you must provide your name.\n\nThis will let the recipient know who sent the Postcard.");
       errfound = true;
    }
    if (!errfound && !validemail(document.card.email.value)) {
		  error(document.card.email,"Sorry, your email address is invalid.");
            errfound = true;
    }
	if (!errfound && !validlength(document.card.to_name.value,2)) {
		error(document.card.to_name,"Sorry, you must provide the recipient name.\n\nThis will help the recipient understand this card was addressed to him.");
        errfound = true;
    }
	if (!errfound && !validemail(document.card.to_email.value)) {
		error(document.card.to_email,"Sorry, your recipient email address is invalid.");
        errfound = true;
    }
	if (!errfound && !validlength(document.card.subject.value,2)) {
		error(document.card.subject,"Sorry, the subject is required.\n\nThis will help the recipient understand what the email Postcard is about.");
        errfound = true;
    }
    if (!errfound && document.card.allow && document.card.allow.checked && !document.card.password.value.length) {
		error(document.card.password, "Sorry, you need to enter your password.\n\nThis will allow the user to access your private album pages.");
        errfound = true;
    }
	if (!errfound && !document.card.msg.value) {
        if(window.confirm('You did not provide a message for your postcard.\n\nAre you sure you want to proceed (your postcard will be blank).')) {
            document.card.line1.value= '';
        }
        else {
            errfound = true;
        }
    }
    return !errfound;
}

