function validateLoginForm() {
	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 showPhoto(id, width, height) {
    destUrl = baseUrl + "/home/photo/" + id;
	newWindow = window.open(destUrl, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width='+width+', height='+height+', left=150, top=150');
}

function cancelAdmin(section) {
	document.forms[0].action = baseUrl+"/"+section;
	if (section == "product") {
	   document.forms[0].category_id.value = -1;
	}
	document.forms[0].submit();	
}

function displayImage()  {
	photoField = "document.forms[0].userfile.value";
	photoNameTemp = eval(photoField);
	
	// Check file extension
	if (!checkImageFileExtension(photoNameTemp)) {			
		return false;
	}
															
	photoName = "file://"+replaceChars(photoNameTemp);
	photoDisplay = "document.forms[0].userfile_display.src = '"+photoName+"'";
	if (photoNameTemp != "") {
		photoDisplay = "document.forms[0].userfile_display.src = '"+photoName+"'";
		document.forms[0].add_photo.disabled = false;
	} else {
		photoDisplay = "document.forms[0].userfile_display.src = '../images/no-image.gif'";
	}
	eval(photoDisplay);
}

function addImage()  {
    var imageName = document.forms[0].image_name.value;
    if (imageName == "") {
        alert("You must specify an image file.");
    } else {
    	var productId = document.forms[0].product_id.value;
    	document.forms[0].action = baseUrl+"/product/addImage/"+productId;
    	document.forms[0].submit();
    }
}

function checkImageFileExtension(filename) {
	if((filename.lastIndexOf(".jpg") == -1) 
			&& (filename.lastIndexOf(".JPG") == -1) 
			&& (filename.lastIndexOf(".jpeg") == -1) 
			&& (filename.lastIndexOf(".JPEG") == -1) 
			&& (filename.lastIndexOf(".gif") == -1) 
			&& (filename.lastIndexOf(".GIF") == -1)) {
		alert("You can upload only GIF and JPG files");
		return false;
	}
	return true;
}

function deleteImages()  {
	var productId = document.forms[0].product_id.value;
	document.forms[0].action = baseUrl+"/product/deleteImages/"+productId;
	document.forms[0].submit();
}

function replaceChars(entry) {
	out = "\\"; // replace this
	add = "/";  // with this
	temp = "" + entry; // temporary holder

	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function validateProductForm() {
	var title = document.forms[0].title.value;
	var price = document.forms[0].price.value;
	
	if (title == "") {
		alert("You must enter a product title.");
		document.forms[0].title.focus();
		return false;
	}
	if (price == "") {
		alert("You must enter a price.");
		document.forms[0].price.focus();
		return false;
	}
	return true;
}

function saveNoImage() {        
    if (validateProductForm()) {
        document.add_form.add_images.value = false;
        document.add_form.submit();
    } else {
        return false;
    }
}

function validateAddCategoryForm() {
	var category = document.add_form.category.value;
	
	if (category == "") {
		alert("You must enter a category name.");
		document.add_form.category.focus();
		return false;
	}
}

function validateEditCategoryForm() {
	var category = document.edit_form.category.value;
	
	if (category == "") {
		alert("You must enter a category name.");
		document.edit_form.category.value = document.edit_form.original_name.value;
		document.edit_form.category.focus();
		return false;
	}
}

function validateContactForm() {
    var fullName = document.contact_form.full_name.value;
	var email = document.contact_form.email.value;
	var phone = document.contact_form.phone.value;
	var query = document.contact_form.query.value;
	
	if (fullName == "") {
		alert("You must enter your full name.");
		document.contact_form.full_name.focus();
		return false;
	}
    if (email == "" && phone == "") {
		alert("You must enter either a phone number or an 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("Please enter a query or some comments.");
		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 confirmDelete(path) {
	if (confirm("Are you sure want to delete this entry from the database?")) {
		window.location = baseUrl+path;
	}
}