var relloc = '/';

// Shopping Cart Functions -------------------------------------------------------------

// Hide the pricing details until all attributes are set
unsetItem = function(parent_id) {
	$('message_' + parent_id).innerHTML = '<p>Select Options For Price</p>';
	$('item_id_' + parent_id).value = 0;
	$('price_' + parent_id).style.display = 'none';
	$('button_' + parent_id).style.display = 'none';
	$('message_' + parent_id).style.display = 'block';
}

// Display the pricing details when all attributes are set
setItem = function(parent_id,item_id,cost) {
	$('item_id_' + parent_id).value = item_id;
	$('message_' + parent_id).style.display = 'none';
	$('price_' + parent_id).style.display = 'block';
	$('button_' + parent_id).style.display = 'block';
	$('cost_' + parent_id).innerHTML = cost;
}			

// Wipe out a select, and disable the item button
reset_attribute = function (parent_item_id,attribute_id) {	
	if($('attrib_' + parent_item_id + '_' + attribute_id)) {
		$('attrib_' + parent_item_id + '_' + attribute_id).options.length=1;
		$('attrib_' + parent_item_id + '_' + attribute_id).options[0] = new Option(String.fromCharCode(160) + String.fromCharCode(160), "0", true, true);								
		$('attrib_' + parent_item_id + '_' + attribute_id).disabled=true;
	}
	unsetItem(parent_item_id);
}

// AJAX in a new set of select options
enable_attribute = function (parent_item_id,attribute_id) {	
	//$('attribdiv_' + parent_item_id + '_' + attribute_id).innerHTML = '<img src="' + relloc + 'images/ajax_loading.gif" border="0" alt="Loading..." />';
	new Ajax.Updater('attribdiv_' + parent_item_id + '_' + attribute_id, '_ajax/ajax_cart_attributes.php?' + $('item_form_' + parent_item_id).serialize(), {
		evalScripts: true
	});									
}

submit_item = function (parent_item_id) {
	$('item_form_' + parent_item_id).submit();
}

// Form functions ----------------------------------------------------------------------

highlight_form = function(theelement) {
	new Effect.Highlight(theelement, { startcolor:'#e0d1e8',endcolor:'#ffffff' }); 
	return false;
}

eraseSearchBox = function(id,defaultvalue) {
	if($(id)) {
		if($(id).value == "") {
			$(id).value = defaultvalue;
		} else if ($(id).value == defaultvalue) {
			$(id).value = "";
		}
	}
}

focusshow = function(elem_id) {
	new Effect.Appear(elem_id);
}

blurhide = function(elem_id) {
	new Effect.Fade(elem_id);
}

reviewshow = function(elem_id) {
	new Effect.BlindDown(elem_id);
}

// Floating DIV functions --------------------------------------------------------------

centerPopup = function(divid) {

	var eltDims = $(divid).getDimensions();
	var browserDims = $(document.body).getDimensions(); 

	//var y  = (browserDims.height - eltDims.height) / 2;
	var y = 20;
	var x = (browserDims.width - eltDims.width) / 2;

	var styles = { position : 'absolute',
	top      : y + 'px',
	left     : x + 'px' };

	$(divid).setStyle(styles);
	$('outer').scrollTo();

}

unfadeBackground = function() {
	new Effect.Opacity($('outer'), { 
		from: 0.3, 
		to: 1.0, 
		duration: 0.5 
	});
} 

fadeBackground = function() {
	new Effect.Opacity($('outer'), { 
		from: 1.0, 
		to: 0.3, 
		duration: 0.5 
	});
}

triggerPopup = function(divid) {
	fadeBackground();
	centerPopup(divid);
	new Effect.Appear($(divid), {
		afterFinish: function() {
			//$(divid).scrollTo();
		}
	});
	return false;
}

closePopup = function(divid) {
	new Effect.Fade($(divid));
	unfadeBackground();
	return false;
}

// Validate a given email address ------------------------------------------------------

validateEmail = function(email) {
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(email.match(emailRegEx)){
		return true;
	} else {
		return false;
	}
}

// Switch product images onclick
switchImage = function(image) {
	$('defaultimage').src = image;
}

// Reviews -----------------------------------------------------------------------------

showReviewForm = function(parent_item_id,review_type_id) {

	new Ajax.Updater('popup-container-content',relloc + '_ajax/ajax_review_form.php?parent_item_id=' + parent_item_id + '&review_type_id=' + review_type_id, {
		evalScripts:true,
		onComplete: function() {
			triggerPopup('popup-container');
		}
	});

}

verifyReview = function(parent_item_id) {
	
	// Are all fields completed? Is all AOK?
	var name = $F(parent_item_id + '_name');
	var email = $F(parent_item_id + '_email');
	var review = $F(parent_item_id + '_review');
	var score = $F(parent_item_id + '_score');

	if(name!='' && name!=' ' && email!='' && email!=' ' && review!='' && review!=' ') {	
		if(validateEmail(email)) {		
			if(score > 0) {			
				$(parent_item_id + '_reviewform').submit();
			} else {
				alert('Please select a rating between 1 and 5 hearts');
			}		
		} else {
			alert('Please provide a valid email address');
		}	
	} else {
		alert('Please complete all fields');
	}	
}

reviewCount = function(parent_item_id) {

	c = $(parent_item_id + '_review');
	len = c.value.length;
	if(len>550) {
		html = "Top reviewer! Hint: Have you included the product name in the review?";
	} else if(len>250) {
		html = "That's great! Hint: What would you have liked to have known before you made your purchase?";
	} else if(len>150) {
		html = "You're getting there! Hint: Think about what you liked and didn't like about it";
	} else if(len>0) {
		html = "Keep writing! Hint: How would you describe the product or service to family and friends?";
	} else {
		html = " ";
	}
	$('reviewmessage').innerHTML=html;

}

star_rating_mouseover = function(parent_item_id,iStarID,iStarCount)
{
	var totalCount = iStarCount;
	var currentCount = iStarID;

	// De-activate all the stars
	for(i=0;i<totalCount;i++)
	{
		p = i + 1;
		$('star_' + parent_item_id + '_' + p).src = relloc + 'images/heart-empty.png';
	}

	//Activate the stars up to the current one
	for(i=0;i<currentCount;i++)
	{
		p = i + 1;
		$('star_' + parent_item_id + '_' + p).src = relloc + 'images/heart.png';
	}
}

star_rating_mouseout = function(parent_item_id,iStarCount)
{
	var totalCount = iStarCount;
	var currentCount = $(parent_item_id + '_score').value;

	// De-activate all the stars
	for(i=0;i<totalCount;i++)
	{
		p = i + 1;
		$('star_' + parent_item_id + '_' + p).src = relloc + 'images/heart-empty.png';
	}

	//Activate the stars up to the current value
	for(i=0;i<currentCount;i++)
	{
		p = i + 1;
		$('star_' + parent_item_id + '_' + p).src = relloc + 'images/heart.png';
	}
}

star_rating_set = function(parent_item_id,iStarID,iStarCount)
{
	var totalCount = iStarCount;

	// De-activate all the stars
	for(i=0;i<totalCount;i++)
	{
		p = i + 1;
		$('star_' + parent_item_id + '_' + p).src = relloc + 'images/heart-empty.png';
	}

	//Set the current value in the hidden field
	$(parent_item_id + '_score').value = iStarID;

	//Activate the stars up to the current value
	for(i=0;i<iStarID;i++)
	{
		p = i + 1;
		$('star_' + parent_item_id + '_' + p).src = relloc + 'images/heart.png';
	}
}

paginationFade = function(div) {
	new Effect.Highlight(div, { 
		duration:0.3,
		startcolor: '#e3d7be',
		endcolor: '#f0ece3'
	});
}
