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 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].machine_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 categorySelection = document.forms[0].category_id.selectedIndex;	
	var categoryId = document.forms[0].category_id.options[categorySelection].value;
	var brandSelection = document.forms[0].brand_id.selectedIndex;	
	var brandId = document.forms[0].brand_id.options[brandSelection].value;
	var rangeSelection = document.forms[0].range_id.selectedIndex;	
	var rangeId = document.forms[0].range_id.options[rangeSelection].value;
	var productName = document.forms[0].product_name.value;
	
	if (categoryId == -1) {
		alert("You must select a product category.");
		document.forms[0].category_id.focus();
		return false;
	}
	if (brandId == -1) {
		alert("You must select a product brand.");
		document.forms[0].brand_id.focus();
		return false;
	}
	if (rangeId == -1) {
		alert("You must select a product range.");
		document.forms[0].range_id.focus();
		return false;
	}
	if (productName == "") {
		alert("You must enter a product name.");
		document.forms[0].product_name.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 firstName = document.contact_form.first_name.value;
    var surname = document.contact_form.surname.value;
    var address = document.contact_form.address.value;
    var postcode = document.contact_form.postcode.value;
	var email = document.contact_form.email.value;
	var phone = document.contact_form.phone.value;
	var query = document.contact_form.query.value;
	
	if (document.contact_form.business_status[0].checked) {
        var businessStatus = 1;
    } else {
        var businessStatus = 0;
    }
	var sourceSelection = document.contact_form.source.selectedIndex;	
	var sourceId = document.contact_form.source.options[sourceSelection].value;
	if (businessStatus == 1) {
	    var typeSelection = document.contact_form.business_type.selectedIndex;
	    var typeId = document.contact_form.business_type.options[typeSelection].value;
	    if (typeId == "other") {
	        var otherType = document.contact_form.other_type.value;
	    }
	}
	if (sourceId == "other") {
	    var otherSource = document.contact_form.other_source.value;
	}
	
	/*var checkSelected = false;
	for (i = 0;  i < document.contact_form.brochure_id.length;  i++) {
		if (document.contact_form.brochure_id[i].checked) {
			checkSelected = true;
		}
	}
	if (!checkSelected) {
		alert("Please select at least brochure.");
		return (false);
	}*/
	
	if (firstName == "") {
		alert("You must enter your first name.");
		document.contact_form.first_name.focus();
		return false;
	}
	if (surname == "") {
		alert("You must enter your surname.");
		document.contact_form.surname.focus();
		return false;
	}
	if (address == "") {
		alert("You must enter your address.");
		document.contact_form.address.focus();
		return false;
	}
	if (postcode == "") {
		alert("You must enter your postcode.");
		document.contact_form.postcode.focus();
		return false;
	}
    if (email == "") {
		alert("You must enter 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 (businessStatus == 1) {
	    if (typeId == -1) {
		    alert("You must select a business type.");
		    document.contact_form.business_type.focus();
		    return false;
	    }
	}
	
	if (sourceId == -1) {
		alert("Please tell us how you heard of us.");
		document.contact_form.source.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;
	}
}

function setRanges() {
	var brandSelection = document.search_form.brand_id.selectedIndex;	
	var brandId = document.search_form.brand_id.options[brandSelection].value;
	
	rangeSelect = document.getElementById('range_id');
	rangeSelect.length = 0;
	
	if (brandId != -1) {
        rangeCount = brandValues[brandId].length - 1;
    	
    	var newrangeOption = document.createElement('option');
    	newrangeOption.value = -1;
    	newrangeOption.text = "All ranges";
    	
    	var insertAt = rangeSelect.options.length;
    	rangeSelect.options.length = rangeSelect.options.length +1;
    	rangeSelect.options[insertAt] = newrangeOption;
    		
    	for (x=0; x<rangeCount+1; x++) {
    	    rangeId = brandValues[brandId][x][0];
    		rangeText = brandValues[brandId][x][1];
    		
    		var newrangeOption = document.createElement('option');
    		newrangeOption.value = rangeId;
    		newrangeOption.text = rangeText;
    		
    		var insertAt = rangeSelect.options.length;
    		rangeSelect.options.length = rangeSelect.options.length +1;
    		rangeSelect.options[insertAt] = newrangeOption;
    	}
	} else {
	    var newrangeOption = document.createElement('option');
    	newrangeOption.value = -1;
    	newrangeOption.text = "All ranges";
    	
    	var insertAt = rangeSelect.options.length;
    	rangeSelect.options.length = rangeSelect.options.length +1;
    	rangeSelect.options[insertAt] = newrangeOption;
	}
}

function setAdminRanges() {
	var brandSelection = document.forms[0].brand_id.selectedIndex;	
	var brandId = document.forms[0].brand_id.options[brandSelection].value;
	
	rangeSelect = document.getElementById('range_id');
	rangeSelect.length = 0;
	
	if (brandId != -1) {
	   rangeCount = brandValues[brandId].length - 1;
	
    	var newrangeOption = document.createElement('option');
    	newrangeOption.value = -1;
    	newrangeOption.text = "Select range";
    	
    	var insertAt = rangeSelect.options.length;
    	rangeSelect.options.length = rangeSelect.options.length +1;
    	rangeSelect.options[insertAt] = newrangeOption;
    		
    	for (x=0; x<rangeCount+1; x++) {
    	    rangeId = brandValues[brandId][x][0];
    		rangeText = brandValues[brandId][x][1];
    		
    		var newrangeOption = document.createElement('option');
    		newrangeOption.value = rangeId;
    		newrangeOption.text = rangeText;
    		
    		var insertAt = rangeSelect.options.length;
    		rangeSelect.options.length = rangeSelect.options.length +1;
    		rangeSelect.options[insertAt] = newrangeOption;
    	}
	} else {
	    var newrangeOption = document.createElement('option');
    	newrangeOption.value = -1;
    	newrangeOption.text = "Select range";
    	
    	var insertAt = rangeSelect.options.length;
    	rangeSelect.options.length = rangeSelect.options.length +1;
    	rangeSelect.options[insertAt] = newrangeOption;
	}
}


function addCode() {
    validateFlag = true;
    productCode = document.forms[0].product_code.value;
	productDescription = document.forms[0].product_description.value;
	
	if (productCode == "") {
		alert("You must enter a product code.");
		document.forms[0].product_code.focus();
		validateFlag = false;
	}
	if (validateFlag && productDescription == "") {
		alert("You must enter a product description.");
		document.forms[0].product_description.focus();
		validateFlag = false;
	}
	
	if (validateFlag) {
        document.forms[0].add_code.value = true;
        document.forms[0].submit();
	}
}

function confirmDeleteCode(codeId) {
    if (confirm("Are you sure want to delete this product code?")) {
        document.edit_form.action = baseUrl+"/product/deleteCode";
        document.edit_form.delete_code.value = codeId;
        document.edit_form.submit();
    }
}

function confirmDeletePdf(pdfId) {
    if (confirm("Are you sure want to delete this PDF association?")) {
        document.edit_form.action = baseUrl+"/product/deletePdf";
        document.edit_form.delete_pdf.value = pdfId;
        document.edit_form.submit();
    }
}

function clearImage() {
    document.forms[0].image_name.value = "";
}

function clearBrandLogo() {
    document.forms[0].brand_logo.value = "";
}

function clearRangeLogo() {
    document.forms[0].range_logo.value = "";
}

function clearCategoryImage() {
    document.forms[0].category_image.value = "";
}

function loadBrands() {
    document.search_form.load_brands.value = "true";
    document.search_form.submit();
}

function loadAdminBrands() {
    document.forms[0].load_brands.value = "true";
    document.forms[0].save.value = "false";
    document.forms[0].submit();
}

function searchProducts() {
    document.search_form.action = baseUrl+"/home/search";
    document.search_form.submit();
}

function resetSearch() {
	var category_count = document.search_form.category_id.length;
	var brand_count = document.search_form.brand_id.length;
	var range_count = document.search_form.range_id.length;
	
	var reset_id = -1;
	
	// Reset the brand selections
	brandSelect = document.getElementById('brand_id');
	brandSelect.length=0;
	
	var newBrandOption = document.createElement('option');
	newBrandOption.value = -1;
	newBrandOption.text = "All brands";
	
	// position
	var insertAt = brandSelect.options.length;

    // create the space
    brandSelect.options.length=brandSelect.options.length + 1;
    // add the option
    brandSelect.options[insertAt] = newBrandOption;
    
    // Reset the range selections
    rangeSelect = document.getElementById('range_id');
	rangeSelect.length=0;
	
	var newRangeOption = document.createElement('option');
	newRangeOption.value = -1;
	newRangeOption.text = "All ranges";
	
	// position
	var insertAt = rangeSelect.options.length;

    // create the space
    rangeSelect.options.length=rangeSelect.options.length + 1;
    // add the option
    rangeSelect.options[insertAt] = newRangeOption;
    
    document.search_form.search_text.value = "";
	
	for (x=0; x<category_count; x++) {
		if (document.search_form.category_id.options[x].value == reset_id) {
			document.search_form.category_id.options[x].selected = true;
			break;
		}
	}
}

function addPdf() {
    validateFlag = true;
    pdfFilename = document.forms[0].pdf_name.value;
	
	if (pdfFilename == "") {
		alert("You must enter a PDF filename.");
		document.forms[0].pdf_name.focus();
		validateFlag = false;
	}
	
	if (validateFlag) {
        document.forms[0].add_pdf.value = true;
        document.forms[0].submit();
	}
}

function clearPdf() {
    document.forms[0].pdf_name.value = "";
    document.forms[0].pdf_name.focus();
}

function editProductCode(id) {
    destUrl = baseUrl + "/product/editCode/" + id;
	newWindow = window.open(destUrl, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=300, height=200, left=150, top=150');
}