/**
 Scripts in this file MUST work in both facebook and wishpot.  They will
 be included in the page beneath the facebook and wishpot-specific javascripts.  
 **/


//Toggle the display of an object (not an id) between block and none
function ToggleBlockDisplay(obj) {
    return ToggleDisplayByObject(obj, "block");
}

function ToggleDisplayByObject(obj, displaytype) {
    if (WPJS.GetStyle(obj, "display") == displaytype)
        WPJS.SetStyle(obj, "display", "none");
    else
        WPJS.SetStyle(obj, "display", displaytype);
}

function ToggleEnabled(obj) {
    WPJS.SetDisabled(obj, !WPJS.GetDisabled(obj));
}

/**
A validator function that matches the signature of an ASP validator for
use with things that require a number to be positive.
**/
function ValidatePositiveNumeric(source, args) {
    //default to true
    args.IsValid = true;

    //always needs to be numeric, regardless of what type of alert
    var numericValue = parseFloat(args.Value);
    if (isNaN(args.Value) || numericValue <= 0) {
        args.IsValid = false;
        WPJS.SetInnerXHTML(source, "Value must be a number greater than zero.");
        return;
    }
}

/** Given an image, scales it to fit inside our 150x150 product box **/
function fitToProductBox(img) {
    if (null == img) return;
    var heightBigger = (WPJS.GetClientHeight(img) > WPJS.GetClientWidth(img));

    if (heightBigger) {
        WPJS.SetStyle(img, "width", "150px");
        if (WPJS.GetClientHeight(img) > 0)
            WPJS.SetStyle(img, "marginTop", Math.round((150 - WPJS.GetClientHeight(img)) / 2) + "px");
    }
    else {
        WPJS.SetStyle(img, "height", "150px");
        if (WPJS.GetClientWidth(img) > 0)
            WPJS.SetStyle(img, "marginLeft", Math.round((150 - WPJS.GetClientWidth(img)) / 2) + "px");
    }
}

/** Used so we can easily style inputs of different types */
function AppendInputTypeClasses() 
{  
	if (!document.getElementsByTagName )   
		return;  
	var inputs = document.getElementsByTagName('input');  
	var inputLen = inputs.length;  
	for(var i=0;i<inputLen;i++) 
	{   
		if(inputs[i].getAttribute('type'))    
			inputs[i].className += ' '+inputs[i].getAttribute('type');  
	}
}

function DrawRating(itemId, rating) {
    var ratingDiv = document.getElementById('RatingString_' + itemId);
    if (rating == 0) WPJS.SetInnerXHTML(ratingDiv, "<b>No Rating</b>");
    else if (rating == 1) WPJS.SetInnerXHTML(ratingDiv, "<b>I hate it!</b>");
    else if (rating == 2) WPJS.SetInnerXHTML(ratingDiv, "<b>I don't like it</b>");
    else if (rating == 3) WPJS.SetInnerXHTML(ratingDiv, "<b>I like it</b>");
    else if (rating == 4) WPJS.SetInnerXHTML(ratingDiv, "<b>I really like it</b>");
    else if (rating == 5) WPJS.SetInnerXHTML(ratingDiv, "<b>I love it!</b>");
}

function SetExistingRating(itemId) {
    var rating = WPJS.GetValue(document.getElementById('RatingInput_' + itemId));
    DrawRating(itemId, rating);
}

function SetRating(itemId, rating) {
    WPJS.SetValue(document.getElementById('RatingInput_' + itemId), rating);
}

