// Checks if a field is empty, and if so, resets the input's content to the default value
function checkField( obj, value )
{
	// Match any amount of white spaces
	if( obj.value.match( " " ) || obj.value == "" )
	{
		obj.value = value;
	}
}

// Clears an input's content
function clearField( obj, value )
{
	if( obj.value == value )
	{
		obj.value = "";
	}
}

// Shows/Hides a div element
function showHide(divID) {
	document.getElementById(divID).style.display == 'none' ? document.getElementById(divID).style.display = 'block' : document.getElementById(divID).style.display = 'none';
}

// Redirects on changing size/length
function updateSizeLength(prod, caz, value, sec1, sec2)
{
	if (caz == 'size')
	{
		var second = new String;
		if (sec1 != 0) second += '&length='+sec1;
		if (sec2 != 0) second += '&colour='+sec2;
		if (value == 0) alert('Please select a valid size');
		else window.location = 'index.php?pag=details&pid='+prod+'&size='+value+second;
	}
	else if (caz == 'length')
	{
		var second = new String;
		if (sec1 != 0) second += '&size='+sec1;
		if (sec2 != 0) second += '&colour='+sec2;
		if (value == 0) alert('Please select a valid length');
		else window.location = 'index.php?pag=details&pid='+prod+'&length='+value+second;
	}
	else if (caz == 'colour')
	{
		var second = new String;
		if (sec1 != 0) second += '&size='+sec1;
		if (sec2 != 0) second += '&length='+sec2;
		if (value == 0) alert('Please select a valid colour');
		else window.location = 'index.php?pag=details&pid='+prod+'&colour='+value+second;
	}
}

// Redirects on changing size/length - college
function updateSizeLengthCol(prod, caz, value, sec1, sec2)
{
	if (caz == 'size')
	{
		var second = new String;
		if (sec1 != 0) second += '&length='+sec1;
		if (sec2 != 0) second += '&colour='+sec2;
		if (value == 0) alert('Please select a valid size');
		else window.location = 'index.php?pag=college&act=products&pid='+prod+'&size='+value+second;
	}
	else if (caz == 'length')
	{
		var second = new String;
		if (sec1 != 0) second += '&size='+sec1;
		if (sec2 != 0) second += '&colour='+sec2;
		if (value == 0) alert('Please select a valid length');
		else window.location = 'index.php?pag=college&act=products&pid='+prod+'&length='+value+second;
	}
	else if (caz == 'colour')
	{
		var second = new String;
		if (sec1 != 0) second += '&size='+sec1;
		if (sec2 != 0) second += '&length='+sec2;
		if (value == 0) alert('Please select a valid colour');
		else window.location = 'index.php?pag=college&act=products&pid='+prod+'&colour='+value+second;
	}
}

// Checks if the product can be added to cart
function checkOptions(size, length, colour, link)
{
	var error = new String;
	if (size != 'ok')
	{
		if (size == 'size' || size == '') error += 'Please select the size!\n';
	}
	if (length != 'ok')
	{
		if (length == 'length' || length == '') error += 'Please select the length!\n';
	}
	if (colour != 'ok')
	{
		if (colour == 'colour' || colour == '') error += 'Please select the colour!\n';
	}
	if (error != '') alert(error);
	else window.location = link;
}

// Shows and hides a div
function showHide(divID) 
{
	document.getElementById(divID).style.display == 'none' ? document.getElementById(divID).style.display = 'block' : document.getElementById(divID).style.display = 'none';
}

// Popup a picture
function pop(src)
{
	window.open('custom_images/products/full/'+stripPath(src),'poza','resizable=1, status=0, toolbar=0, location=0, menubar=0, width=520, height=520');
}

// Strips path from a picture's name
function stripPath(loc)
{
	return loc.replace(/^(((.+)\/)+)?(([\w\-_]+\.)+\w{3})$/, '$4');
}

// Change the picture
function changePic(newSrc)
{
	document.getElementById('poza_mare').src=newSrc;
}

// Checks if the product can be added to cart
function checkInputs()
{
	var error = new String;
	var name = document.getElementById('name').value;
	var address = document.getElementById('address').value;
	var code = document.getElementById('code').value;
	var telephone = document.getElementById('telephone').value;
	var email = document.getElementById('email').value;
	if (name == '') error += 'Please complete your name!\n';
	if (address == '') error += 'Please complete your address!\n';
	if (code == '') error += 'Please complete your postal code!\n';
	if (telephone == '') error += 'Please complete your telephone number!\n';
	if (email == '') error += 'Please complete your email address!\n';
	if (error != '')
	{
		error += '\n<< After you have completed all your info click "checkout" again in order to proceed! >>';
		alert(error);
	}
	else document.customerDetails.submit();
}