// JavaScript Document

function setCountDown()
{
	seconds--;
	if (seconds < 0)
	{
		minutes--;
		seconds = 59
	}
	
	
	document.getElementById("timer").innerHTML = minutes+" : "+seconds;
	SD=window.setTimeout( "setCountDown()", 1000 );
	if (minutes == '00' && seconds == '00')
	{ 
		seconds = "00"; window.clearTimeout(SD);
		//window.alert("Time is up. Press OK to continue."); // change timeout message as required
		window.location = "http://www.omericaorganic.com/CurrentDeals.php" // Add your redirect url
	} 
}



function changeplugs(pg) 
{
	var size_id = document.getElementById('prodSize').value;
	if(size_id != -1) {
		document.getElementById('plug_size').value = size_id;
	}
	else
	{
		document.getElementById('plug_size').value = "";
	}
	document.sizeform.action="plugs.php?pg=1";
	document.sizeform.submit();
}

function roundNumber(num, dec) 
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function selectQuantity()
{
	var quantity = document.getElementById('prodQuantity');
	var myindex = quantity.selectedIndex;
    var sel_index = quantity.options[myindex].id;
	var price = document.getElementById('price').value;
	new_price = sel_index * price;
	newprice = roundNumber(new_price,2);
	newprice = newprice.toFixed(2);
	document.getElementById('totalPrice').innerHTML = "$" + newprice;	
}

function changeSize()
{	
	var sizeId = document.getElementById('size_id');
	var myindex = sizeId.selectedIndex;
    var sel_price = sizeId.options[myindex].title;
	var mili = sizeId.options[myindex].id;
	document.getElementById('mili').innerHTML = mili + "mm";
	var quantity = document.getElementById('prodQuantity');
	quantity.selectedIndex = 0;
	document.getElementById('price').value = sel_price;
	document.getElementById('totalPrice').innerHTML = "$" + sel_price;	
}

function changePlugSize(discount)
{	
	var sizeId = document.getElementById('size_id');
	var myindex = sizeId.selectedIndex;
    var sel_price = sizeId.options[myindex].title;
	var mili = sizeId.options[myindex].id;
	document.getElementById('mili').innerHTML = mili + "mm";
	var quantity = document.getElementById('prodQuantity');
	quantity.selectedIndex = 0;
	var final_price = sel_price - (sel_price * discount/100);
	document.getElementById('price').value = final_price;
	document.getElementById('totalPrice').innerHTML = "$" + final_price;
	document.getElementById('orignalPrice').innerHTML = "$" + sel_price;	
}

function changeLength()
{
	var lengthId = document.getElementById('gauge_length');
	var myindex = lengthId.selectedIndex;
    var mili = lengthId.options[myindex].title;
	var inch = lengthId.options[myindex].id;
	document.getElementById('milimeter').innerHTML = mili + "mm";
	document.getElementById('inch').innerHTML = inch + "in";
}

function formsubmit()
{
	if(validateForm(document.prodForm) == false)
	{
		return false;
	}
	var quantity = document.getElementById('prodQuantity');
	var myindex = quantity.selectedIndex;
	var sel_qty = quantity.options[myindex].value;
	var price = document.getElementById('price').value;
	document.getElementById('qty').value = sel_qty;
	document.getElementById('total').value = document.getElementById('totalPrice').innerHTML;
}

function checkWood(objForm)
{
	myOption = -1;
	if(objForm.wood.checked)
	{
		myOption = 0;
	}

	for (i = objForm.wood.length-1; i > -1; i--)
	{
		if (objForm.wood[i].checked)
		{
			myOption = i;
		}
	}
	if (myOption == -1)
	{
		alert("You must select a wood");
		return false;
	}
	return true;
}

function checkInlay(objForm)
{
	myOption = -1;
	if(objForm.inlay.checked)
	{
		myOption = 0;
	}

	for (i = objForm.inlay.length-1; i > -1; i--)
	{
		if (objForm.inlay[i].checked)
		{
			myOption = i;
		}
	}
	if (myOption == -1)
	{
		alert("You must select an inlay");
		return false;
	}
	return true;
}


function checkWearingStyle(objForm)
{
	myOption = -1;
	if(objForm.wearing_style.checked)
	{
		myOption = 0;
	}

	for (i = 0; i < objForm.wearing_style.length; i++)
	{
		if (objForm.wearing_style[i].checked)
		{
			myOption = i;
		}
	}
	if (myOption == -1)
	{
		alert("You must select a wearing_style");
		return false;
	}
	return true;
}

function validateForm(objForm)
{
	var sel_size = document.getElementById('size_id').value;
	if(sel_size == -1)
	{
		alert("You must select a size");
		return false;
	}
	
	var style_id = document.getElementById('style_id').value;
	if(style_id == '1' || style_id == '2' || style_id == '4')
	{
		if(checkWood(document.prodForm) == false)
		{
			return false;
		}
	}
	if(style_id == '1' || style_id == '2' || style_id == '3')
	{
		if(checkWearingStyle(document.prodForm) == false)
		{
			return false;
		}
	}

	if(style_id == '2')
	{
		if(checkInlay(document.prodForm) == false)
		{
			return false;
		}
	}

	return true;
}
