var divName = 'mouseFloat';
var floatVis = "hidden";
var working = "false";

/* 
	move mouse on x
*/
function mouseX(event)
{
		// capture event on different browsers
	if(!event)
		event = window.event;


	if(event.pageX)
		return event.pageX;
	else if(event.clientX)
		return event.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
	else
		return 0;
}

/* 
	move mouse on y
*/
function mouseY(event)
{

	if(!event)
		event = window.event;


	if (event.pageY)
		return event.pageY;
	else if(event.clientY)
		return event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	else
		return 0;
}

/* 
	calls follow for div, and sets contents
*/
function follow(event,img,inner)
{
	if(img == "" && inner == "")
	{
			// incorrect call
		return;
	}

	if(document.getElementById)
	{
			// correct call
			
		var obj = document.getElementById(divName);
		
			// only if it's not already displayed
		if(floatVis == "hidden")
		{
			floatVis = "visible";
			obj.style.left = (parseInt(mouseX(event))+10) + 'px';
			obj.style.top = (parseInt(mouseY(event))+10) + 'px';
			
			fadeIn(divName, 1500);

				// check if img included
			if(img != "")
			{
					// include picture
				obj.innerHTML = "<table class='ubertable'><tr><td><img src='"+img+"' /></td><td><span class='small'>"+inner+"</span></td>";
			}
			else
			{
					// just text
				obj.innerHTML = "<span class='small'>"+inner+"</span>";
			}
		}
	}
}

/* 
	hides mouseFloat div on losing focus
*/
function stopFollow()
{
		// get style and set to hidden in 5 secs
	if(working != "true")
	{
		setTimeout('fadeOut(divName, 1500)', 5000);
		working = "true";
	}
}

/*
	fading in/out functions
*/
function fadeIn(target, milli)
{
	var speed = Math.round(milli / 100);
	var timer = 0;	
	
	for(x=1;x<=85;x++)
	{
		setTimeout("setOpacity(" + x + ",'" + target + "')",(timer * speed));
		timer++;
	}
}

function fadeOut(target, milli)
{
	//working = "true";
	var speed = Math.round(milli / 100);
	var timer = 0;	
	
	for(x=85;x>=0;x--)
	{
		setTimeout("setOpacity(" + x + ",'" + target + "')",(timer * speed));
		timer++;
	}
}

function setOpacity(opac, target)
{
	var obj = document.getElementById(target).style;

	obj.opacity = (opac / 100);
	obj.filter = "alpha(opacity=" + opac + ")"; 

	if(opac==0)
	{
		floatVis = "hidden";
		working = "false";
	}
}

function checkLog()
{
	if(document.ASHAaccFrm.ASHAacc.value == "")
	{
		alert("Password field is blank");

		return false;
	}

	return true;
}

function ASHAmenu(action)
{
	document.ASHAmenuFrm.ASHAaction.value = action;
	document.ASHAmenuFrm.submit();
}

function radioCheck(target)
{
	document.ASHAnew.type[target].checked = true; 
	disableEnable(target);
}

function disableEnable(target)
{
	form = document.ASHAnew;
	
	form.stock.disabled = true;
	form.cardType.disabled = true;
	
	form.price.disabled = true;
	form.width.disabled = true;
	form.height.disabled = true;
	form.printType.disabled = true;
	
	if(target == 0 || target == 2)
	{
			// print
		form.price.disabled = false;
		form.width.disabled = false;
		form.height.disabled = false;
		form.printType.disabled = false;
	}
	
	if(target == 1 || target == 2)
	{
			// card
		form.stock.disabled = false;
		form.cardType.disabled = false;
	}
}

function checkAdd()
{
	form = document.ASHAnew;
	err = document.getElementById("addError");
	errContent = document.getElementById("addErrorContent");
	error = 0;
	
	form.title.style.background = "#FFFFFF";
	form.width.style.background = "#FFFFFF";
	form.height.style.background = "#FFFFFF";
	form.price.style.background = "#FFFFFF";
	form.stock.style.background = "#FFFFFF";
		
	if(form.title.value == "")
	{
		error++;
		form.title.style.background = "#FF0000";
	}
	

		// check type
	if(form.type[0].checked == true)
	{
		addType = "Print";
	}
	else if(form.type[1].checked == true)
	{
		addType = "Card";
	}
	else if(form.type[2].checked == true)
	{
		addType = "Both";
	}
	
		// validate according to type
	if(addType == "Print" || addType == "Both")
	{
		if(form.width.value == "" || form.width.value <= 0 || isNaN(form.width.value) == true)
		{
			error++;
			form.width.style.background = "#FF0000";
		}
		else
		{
				// check for decimal place
			if(form.width.value.indexOf('.') == -1)
			{
					// no decimal found, treat as int
				form.width.value = parseInt(form.width.value, 10);
			}
			else
			{
					// decimal found, treat as float
				form.width.value = parseFloat(form.width.value);
			}
		}

		if(form.height.value == "" || form.height.value <= 0 || isNaN(form.height.value) == true)
		{
			error++;
			form.height.style.background = "#FF0000";
		}
		else
		{
				// check for decimal place
			if(form.height.value.indexOf('.') == -1)
			{
					// no decimal found, treat as int
				form.height.value = parseInt(form.height.value, 10);
			}
			else
			{
					// decimal found, treat as float
				form.height.value = parseFloat(form.height.value);
			}
		}

		if(form.price.value == "" || form.price.value < 0 || isNaN(form.price.value) == true)
		{
			error++;
			form.price.style.background = "#FF0000";
		}
		else
		{
				// parse as float - removes trailing 0s and adds 0 in front of a decimal point
			form.price.value = parseFloat(form.price.value);
			
				// check if decimal place present
			if(form.price.value.indexOf('.') == -1)
			{
					// check if room for adding two decimal places
				if(form.price.value.length > 3)
				{
						// too many whole numbers, error
					error++;
					form.price.style.background = "#FF0000";
				}
				else
				{
						// no decimal place, add .00
					form.price.value = form.price.value + ".00";
				}
			}
		
		
				// decimal checks and corrections
			if(form.price.value.substring(form.price.value.indexOf('.')+1).length > 2)
			{
					// too many decimals, flag as error (auto correct bad idea)
					error++;
					form.price.style.background = "#FF0000";
			}
			else if(form.price.value.substring(form.price.value.indexOf('.')+1).length == 1)
			{
					// only one decimal place, shove a 0 on the end.
				form.price.value = form.price.value + "0";
			}
			else if(form.price.value.substring(form.price.value.indexOf('.')+1).length == 0)
			{
					// has decimal place, but is missing both 0s, add another
				form.price.value = form.price.value + "00";
			}
				
				// to check that a "decimalised" value is still greater than 0
			if(form.price.value <= 0)
			{
				error++;
				form.price.style.background = "#FF0000";
			}
		}
	}
	
	if(addType == "Card" || addType == "Both")
	{
		if(form.stock.value == "" || form.stock.value <= 0 || isNaN(form.stock.value) == true)
		{
			error++;
			form.stock.style.background = "#FF0000";
		}
		else
		{
				// check for decimal place
			if(form.stock.value.indexOf('.') != -1)
			{
					// decimal found, invalid number
				error++;
				form.stock.style.background = "#FF0000";
			}
		}
	}
	
	if(error >=1)
	{
			// been an error, display
		err.style.display = "inline";
		errContent.innerHTML = "All appropriate fields must be completed; numerical fields must be positive and may not contain any letters. Maximum limits may not be exceeded and up to two decimal places are allowed on dimensions.<br /><br />Incorrect fields have been marked <span class=\"original\">red</span>.";
		
		return false;
	}
	else
	{
			// no error, hide error div and return true
		err.style.display = "none";

		return true;
	}
}

function checkThumb()
{
	form = document.ASHAnewThumb;
	err = document.getElementById("addError");
	errContent = document.getElementById("addErrorContent");
	
	error = 0;
	
	form.thumbUpload.style.background = "#FFFFFF";
	
	if(form.thumbUpload.value == "")
	{
		error++;
		form.thumbUpload.style.background = "#FF0000";
	}

		// split field value to find extention
	splitChar = form.thumbUpload.value.lastIndexOf(".");

	uploadPath = form.thumbUpload.value;
	uploadExt = uploadPath.substr(splitChar+1);
	
		// to not get confused with different case extention
	uploadExt = uploadExt.toUpperCase();

	if(uploadExt != "JPEG" & uploadExt != "JPG")
	{
		error++;
		form.thumbUpload.style.background = "#FF0000";
	}


	if(error >=1)
	{
			// been an error, display
		err.style.display = "inline";
		errContent.innerHTML = "The file you browsed for must be an JPG (JPEG) file.<br /><br />Please ensure that the file dimensions and size are correct before attempting to upload, as ASHA can only verify these once the file has been uploaded to the server (no larger than 64KB or greater than 250px by 200px).<br /><br />The incorrect upload field has been marked <span class=\"original\">red</span>.";

		return false;
	}
	else
	{
			// no error, hide error div and return true
		err.style.display = "none";

		return true;
	}
}

function checkLargeImage()
{
	form = document.ASHAnewLargeImage;
	err = document.getElementById("addError");
	errContent = document.getElementById("addErrorContent");
	
	error = 0;
	
	form.largeUpload.style.background = "#FFFFFF";
	
	if(form.largeUpload.value == "")
	{
		error++;
		form.largeUpload.style.background = "#FF0000";
	}

		// split field value to find extention
	splitChar = form.largeUpload.value.lastIndexOf(".");

	uploadPath = form.largeUpload.value;
	uploadExt = uploadPath.substr(splitChar+1);
	
		// to not get confused with different case extention
	uploadExt = uploadExt.toUpperCase();

	if(uploadExt != "JPEG" & uploadExt != "JPG")
	{
		error++;
		form.largeUpload.style.background = "#FF0000";
	}


	if(error >=1)
	{
			// been an error, display
		err.style.display = "inline";
		errContent.innerHTML = "The file you browsed for must be an JPG (JPEG) file.<br /><br />Please ensure that the file size is correct before attempting to upload, as ASHA can only verify this once the file has been uploaded to the server (no larger than 500KB).<br /><br />The incorrect upload field has been marked <span class=\"original\">red</span>.";

		return false;
	}
	else
	{
			// no error, hide error div and return true
		err.style.display = "none";

		return true;
	}
}

function confirmMsg(details)
{
	var response = confirm("Are you sure you wish to "+details+"?");
	
	if(response == true)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function updateOnChange()
{
		// only call if form exists
	thumb = document.getElementById("thumbForm").value;
	large = document.getElementById("largeForm").value;

	doUpdate(thumb);
	doUpdate(large);

}

function doUpdate(target)
{
	fromFrm = document.ASHAnew;
	toFrm = new Array(document.ASHAnewThumb, document.ASHAdelThumb, document.ASHAnewLarge, document.ASHAdelLarge);

	if(fromFrm.type[0].checked == true)
	{
		addType = "Print";
	}
	else if(form.type[1].checked == true)
	{
		addType = "Card";
	}
	else if(form.type[2].checked == true)
	{
		addType = "Both";
	}
	
	if(fromFrm.hide.checked == true)
	{
		addForm = "on";
	}
	else
	{
		addForm = "";
	}
	
	toFrm[target].title.value = fromFrm.title.value;
	toFrm[target].type.value = addType;
	toFrm[target].width.value = fromFrm.width.value;
	toFrm[target].height.value = fromFrm.height.value;
	toFrm[target].stock.value = fromFrm.stock.value;
	toFrm[target].price.value = fromFrm.price.value;
	toFrm[target].cardType.value = fromFrm.cardType.value;
	toFrm[target].printType.value = fromFrm.printType.value;
	toFrm[target].hide.value = addForm;
}

function trMouseOver(element, hidden, backColour)
{
		// must do each cell as IE can't do rows
	cellArray = element.getElementsByTagName('td');

	for (x=0; x<cellArray.length; x++)
	{
		if(cellArray[x].className != "blanktd")
		{
			cellArray[x].style.backgroundColor = backColour;
		}
	}
	
	target = document.getElementById(hidden);

	cellArray = target.getElementsByTagName('td');

	for (x=0; x<cellArray.length; x++)
	{
		if(cellArray[x].className != "blanktd")
		{
			cellArray[x].style.backgroundColor = backColour;
		}
	}
}

function hiddenToggle(elem)
{
	target = document.getElementById(elem).style;
	if(target.display == "none")
	{
		target.display = "";
	}
	else
	{
		target.display = "none";
	}
}

function printAddress(person)
{
	domain = "frenchpointerprints";
	type = "com";
	// "<a href="mailto:"+person+"@"+domain+"."+type+">"+person+"@"+domain+"."+type+"</a>"
	document.write("<a href='mailto:"+person+"@"+domain+"."+type+"'>"+person+"@"+domain+"."+type+"</a>");
}
