function validateLogin() {
	var username = document.login_form.username.value;
	var password = document.login_form.password.value;
	
	if (username == "") {
		alert("You must enter a username.");
		document.login_form.username.focus();
		return false;
	}
	if (password == "") {
		alert("You must enter a password.");
		document.login_form.password.focus();
		return false;
	}
	
	encodePassword(password);
}

function encodePassword(password) {
    var hash = hex_md5(password);
	document.login_form.password.value = hash;
	return true;
}

function validateContactForm() {
    var fullName = document.contact_form.full_name.value;
	var location = document.contact_form.location.value;
	var phone = document.contact_form.phone.value;
	var query = document.contact_form.query.value;
	var email = document.contact_form.email.value;
	
	if (fullName == "") {
		alert("You must enter your full name.");
		document.contact_form.full_name.focus();
		return false;
	}
	if (phone == "" && email == "") {
		alert("You must enter either your phone number or email address.");
		document.contact_form.email.focus();
		return false;
	}
	if (phone != "" && !isNumeric(phone)) {
		alert("Your phone number can only contain numeric characters");
		document.contact_form.phone.focus();
		return false;
	}
	if (email != "") {
		// E-mail Validation by Henrik Petersen / NetKontoret
		// Explained at www.echoecho.com/jsforms.htm
		// Please do not remove this line and the two lines above.
		apos=email.indexOf("@"); 
		dotpos=email.lastIndexOf(".");
		lastpos=email.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
			alert("You must enter a valid email address");
				document.contact_form.email.focus();
			return false;
		}
	}
	if (query == "") {
		alert("You must enter your comments or query.");
		document.contact_form.query.focus();
		return false;
	}
	return true;
}

function isNumeric(strString) {
	var strValidChars = "0123456789+(). ";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function cancelAdmin(section) {
	document.forms[0].action = baseUrl+"/"+section;
	document.forms[0].submit();	
}

function validateProjectForm() {
    var title = document.getElementById('title');
	var summary = document.getElementById('summary');
	
	if (title.value == "") {
		alert("You must enter a project title.");
		title.focus();
		return false;
	}
	if (summary.value == "") {
		alert("You must enter a project summary.");
		summary.focus();
		return false;
	}
	return true;
}

function confirmDelete(path) {
	if (confirm("Are you sure want to delete this entry from the database?")) {
		window.location = baseUrl+path;
	}
}

function toggleDynamicContent() {
    toggleField = document.getElementById('dynamic_content');
    
    obj1 = document.getElementById('page_content');
    obj2 = document.getElementById('page_file');
    styleObj1=document.getElementById('page_content').style;
	styleObj2=document.getElementById('page_file').style;	
	
    if (toggleField.checked == true) {
		styleObj1.backgroundColor = "#FFFFFF";
		styleObj2.backgroundColor = "#EEEEEE";
		obj1.focus();
        
    } else {
		styleObj1.backgroundColor = "#EEEEEE";
		styleObj2.backgroundColor = "#FFFFFF";
		obj2.focus();
    }
}

// This adds an image to an existing page
function addPageImage() {
    imageName = document.getElementById('image_name');
    if (imageName.value == "") {
        alert("You must specify an image file. Click Browse...");
		imageName.focus();
		return;
    }
    document.edit_form.action = baseUrl + "/cms/addPageImage";
    document.edit_form.submit();
}

// This adds a new image at the same time as a new page
function addPageImage1() {
    if (!validatePageForm()) {
        return;
    }
    imageName = document.getElementById('image_name');
    if (imageName.value == "") {
        alert("You must specify an image file. Click Browse...");
		imageName.focus();
		return;
    }
    document.add_form.action = baseUrl + "/cms/addPageAndImage";
    document.add_form.submit();
}

function deletePageImages()  {
    var pageId = document.edit_form.page_id.value;
	document.edit_form.action = baseUrl+"/cms/deletePageImages/"+pageId;
	document.edit_form.submit();
}

// This adds an image to an existing project
function addProjectImage () {
    imageName = document.getElementById('image_name');
    if (imageName.value == "") {
        alert("You must specify an image file. Click Browse...");
		imageName.focus();
		return;
    }
    document.edit_form.action = baseUrl + "/project/addImage";
    document.edit_form.submit();
}

// This adds a new image at the same time as a new project
function addProjectImage1() {
    if (!validateProjectForm()) {
        return;
    }
    imageName = document.getElementById('image_name');
    if (imageName.value == "") {
        alert("You must specify an image file. Click Browse...");
		imageName.focus();
		return;
    }
    document.add_form.action = baseUrl + "/project/addProjectAndImage";
    document.add_form.submit();
}

function deleteProjectImages()  {
    var projectId = document.edit_form.project_id.value;
	document.edit_form.action = baseUrl+"/project/deleteImages/"+projectId;
	document.edit_form.submit();
}

// This adds an image to an existing exclusive item
function addExclusiveImage () {
    imageName = document.getElementById('image_name');
    if (imageName.value == "") {
        alert("You must specify an image file. Click Browse...");
		imageName.focus();
		return;
    }
    document.edit_form.action = baseUrl + "/exclusive/addImage";
    document.edit_form.submit();
}

// This adds a new exclusive item at the same time as a new project
function addExclusiveImage1() {
    if (!validateExclusiveForm()) {
        return;
    }
    imageName = document.getElementById('image_name');
    if (imageName.value == "") {
        alert("You must specify an image file. Click Browse...");
		imageName.focus();
		return;
    }
    document.add_form.action = baseUrl + "/exclusive/addExclusiveAndImage";
    document.add_form.submit();
}

function deleteExclusiveImages()  {
    var exclusiveId = document.edit_form.exclusive_id.value;
	document.edit_form.action = baseUrl+"/exclusive/deleteImages/"+exclusiveId;
	document.edit_form.submit();
}

function validatePageForm() {
    var pageLabel = document.getElementById('page_label');
	var dynamicContent = document.getElementById('dynamic_content');
	var pageContent = tinyMCE.getContent();
	var pageFile = document.getElementById('page_file');
	
	if (pageLabel.value == "") {
		alert("You must enter a page label.");
		pageLabel.focus();
		return false;
	}
	if (dynamicContent.checked == true) {
	    /*if (pageContent.value == "") {
    		alert("You must enter some page content.");
    		//pageContent.focus();
    		return false;
    	}*/
	} else {
	    if (pageFile.value == "") {
    		alert("You must enter a page filename.");
    		pageFile.focus();
    		return false;
    	}
	}
	return true;
}

function validateExclusiveForm() {
    var title = document.getElementById('title');
	var summary = document.getElementById('summary');
		
	if (title.value == "") {
		alert("You must enter an item title.");
		title.focus();
		return false;
	}
	if (summary.value == "") {
		alert("You must enter an item summary.");
		summary.focus();
		return false;
	}	

	return true;
}

function setMainPageImage(imageId) {
    new Ajax.Updater('null_div', baseUrl+'/cms/ajaxPageImage', { 
        method:'post', postBody:'page_id='+$F('page_id') +'&main_image_id='+imageId
    });
}

function setMainProjectImage(imageId) {
    new Ajax.Updater('null_div', baseUrl+'/project/ajaxMainImage', { 
        method:'post', postBody:'project_id='+$F('project_id') +'&main_image_id='+imageId
    });
}

function setMainExclusiveImage(imageId) {
    new Ajax.Updater('null_div', baseUrl+'/exclusive/ajaxMainImage', { 
        method:'post', postBody:'exclusive_id='+$F('exclusive_id') +'&main_image_id='+imageId
    });
}

function checkCategoryImage() {
    new Ajax.Updater('result_div', baseUrl+'/cms/ajaxCheckCategoryImage', { 
        method:'post', postBody:'image_name='+$F('image_name')
    });
}

function validateCategoryForm() {
    var categoryName = document.getElementById('category_name');
    var imageName = document.getElementById('image_name');
    var imageOk = document.getElementById('image_ok');
    var imageWidth = document.getElementById('image_width').value;
    var imageHeight = document.getElementById('image_height').value;
	
	if (categoryName.value == "") {
		alert("You must enter a category name.");
		categoryName.focus();
		return false;
	}
	if (imageName.value == "") {
		alert("You must select a category image. Click Browse...");
		imageName.focus();
		return false;
	}
	if (imageOk.value != "true") {
		alert("The category image must be "+imageWidth+" x "+imageHeight+" pixels.");
		return false;
	}
	
	
}
