/* MAIN.JS */
// Extensions array
var extensionArray = new Array();
	extensionArray[0] = ".png";
	extensionArray[1] = ".jpg";
	extensionArray[2] = ".jpeg";
	extensionArray[3] = ".flv";		
// CLEAR EMAIL INPUT
function clearInput(field) {
	field.value= "";
}
// VALIDATE EMAIL
function emailValidation(emailAddress) {
	var emailRegEx =/^([a-zA-Z0-9])(([a-zA-Z0-9])*([\._-])?([a-zA-Z0-9]))*@(([a-zA-Z0-9\-])+(\.))+([a-zA-Z]{2,4})+$/;
	if (emailAddress.search(emailRegEx )==-1) {
		document.getElementById("email-validation").innerHTML='Sorry, "' + emailAddress + '" is not a valid email address.';
		return (false)
	} else {
		document.getElementById("email-validation").innerHTML="Thank you!";
		return (true);
	}
	
}
// IS NUMERIC
function isNumeric(num) {
	if(!/\D/.test(num)) {
		return true;
	} else if(/^\d+\.\d+$/.test(num)) {
		return true;
	} else {
		return false;
	}
}
// CHECK EXTENSION
function checkExtension(num) {
	var imageValue = eval("document.forms['product-form'].prod_image" + num + ".value");
	if (imageValue != '') {
		var imageType = imageValue.substr(imageValue.lastIndexOf('.'));
		var imageError = "image" + num + "-error";
		for(var i = 0; i < 3; i++) {
			if(imageType == extensionArray[i]) {
				document.getElementById(imageError).innerHTML='';
				return(true);
			} 
		}
		document.getElementById(imageError).innerHTML = "* Image must be of type 'jpg', 'jpeg' or 'png' *";
		return(false);
	}
return(true);
}
// ADMIN VALIDATION
function adminValidation() {
// Prod name
	if (document.forms['product-form'].prod_name.value.length > 40) {
		document.getElementById("name-error").innerHTML='* Product name must be under 40 characters *';
		return(false);
	} else {
		document.getElementById("name-error").innerHTML='';
	}
// Prod price
	if (!(isNumeric(document.forms['product-form'].prod_price.value))) {
		document.getElementById("price-error").innerHTML='* Product price must be numeric *';
		return(false);
	} else {
		document.getElementById("price-error").innerHTML='';
	}
// Prod video
// Isolate extension
	var videoType = document.forms['product-form'].prod_video.value.substr(document.forms['product-form'].prod_video.value.lastIndexOf('.'));
	if (videoType != ""){
		if(!(videoType == extensionArray[3])) {
			document.getElementById("video-error").innerHTML='* Video must be of type "flv" (flash video) *';
			return(false);
		} else {
			document.getElementById("video-error").innerHTML='';
		}
	}
// Prod image1
	if (checkExtension(1) == false) {
		return(false);
	}
// Prod image2	
	if (checkExtension(2) == false) {
		return(false);
	}
// Prod image3	
	if (checkExtension(3) == false) {
		return(false);
	}
return(true);
}
function displaySelect(value) {
	//alert (value);
	var selector = document.getElementById("category-select");
	var options = document.getElementById("prod-category");
	if (value == 'kabirski') {
		selector.style.display = "table-row";
		options.innerHTML='\
		<option value =\"rings\">Rings</option>\
		<option value =\"pendants\">Pendants</option>';
	} else if (value == 'pre-loved') {
		selector.style.display = "table-row";
		options.innerHTML='\
		<option value =\"gold\">Gold</option>\
		<option value =\"silver\">Silver</option>';
	} else {
		selector.style.display = "table-row";
		options.innerHTML='';
	}
}
