function addEntry(page) {
    location.href = page + "new.php";
}

function changeEntry(form, id, action) {
    var theForm = document.getElementById(form);
    theForm.changeID.value = id;
    theForm.action = action + "change.php";
    theForm.submit();
}

function deleteEntry(form, id, action) {
    if (confirm("Are you sure you want to delete this entry?")) {
        var theForm = document.getElementById(form);
        theForm.changeID.value = id;
        theForm.action = "delete" + action + ".php";
        theForm.submit();
    }
    else {
        return false;
    }
}

function checkOut(n) {

    var theForm = document.getElementById("frmPurchase");

    if (n == 0) {
    
        var startNum = theForm.sub_type.length - 1;
        var selectedOption = -1;
    
        for (i = startNum; i > -1; i--) {
            if (theForm.sub_type[i].checked) {
                selectedOption = i;
                i = -1;
            }
        }

        if  (selectedOption == -1) {
            alert("You must select a purchase option.");
            return false;
        }
        
    }
    
    // Above method does not work with only one radio button
    if (n == 1) {
    
        var isChecked = theForm.sub_type.checked;
        
        if (isChecked == false) {
            alert("You must select a purchase option.");
            return false;
        }
    
    }

    theForm.action = "paypal.php";
    theForm.submit();

}

function popUp(URL, title, options) {
    win = window.open(URL, title, options);
    win.moveTo(50, 50);
}

function emailValidate(form) {			

    var theForm = document.getElementById(form);

	if (theForm.email.value == "") {
		alert("Email address cannot be blank");
		return false;
	}

    else {
    
		rx = new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");

		if (!rx.test(theForm.email.value)) {
			alert("That email address is not valid"); 
			return false;
		}
		
		theForm.submit();
		return true;

	}

}

function paypal() {
    var theForm = document.getElementById("frmPaypal");
    theForm.submit();
}

function clEdit(format, letter) {
    var theForm = document.getElementById("frmCoverLetterList");
    theForm.letter.value = letter;
    theForm.do.value = "edit";
    theForm.format.value = format;
    if (format > 0) {
        theForm.action = "edit_tmpl_letter.php";
    }
    else {
        theForm.action = "edit_users_letter.php";
    }
    theForm.submit();
}

function clDelete(format, letter) {
    if (confirm("Are you sure you want to delete this letter?")) {
        var theForm = document.getElementById("frmCoverLetterList");
        theForm.format.value = format;
        theForm.letter.value = letter;
        theForm.action = "delete_cover_letter.php";
        theForm.submit();
    }
    else {
        return false;
    }
}

function clNew() {
    location.href="cover_letter_type.php";
}

function loadTemplate() {

    var theForm = document.getElementById("frmSelectLetter");
    theForm.submit();

}

function postAd() {

    var theForm = document.getElementById("frmPostAd");
    var isChecked = theForm.postAd.checked;
    
    if (isChecked == true) {
        theForm.action = "http://www.dciads.ca/resume/post.php";
    }
    else {
        theForm.action = "index.php";
    }
    
    theForm.submit();

}

function checkall(state, form)
{
	var theForm = document.getElementById(form);
	var formLength = theForm.elements.length;
	
	for (i = 0; i < formLength; i++)
	{
		if (theForm.elements[i].name == "id[]") theForm.elements[i].checked = state;
	}
}