//product_detail.js

var GB_ROOT_DIR = "/App_Themes/RadioSystems/javascripts/greybox/";

function updatePrice(){
	loadProductInput();
}

function loadProductInput(){
	var which = document.productForm;
	for(i=0;which && i<which.length;i++) {
		var tempobj=which.elements[i];
		if(tempobj.name.substring(0,10)=="options[1]") {
			document.productForm.productInput.value = optionNumbers[tempobj.value];
			break;
		}
	}
}

function show_hide_div()
{
    $('#show').toggle();
    $('#extra_dealers').toggle();
}

function isValidZipCode()
{
	var zipcode = document.getElementById("zipcode").value;
	if(zipcode == null || zipcode.length != 5 || isNumeric(zipcode) != true)
	{
		alert("Please enter a valid 5 digit zipcode.");
		return false;
	}
	
	return true;
}

function isValidPostalCode() {
    var postalCode = document.getElementById("postalcode").value;
    var postalCodeRegExp = /^\w\d\w \d\w\d$/;

    if (postalCode == null || !postalCodeRegExp.test(postalCode)) {
        alert("Please enter a valid 6 character postal code (ie. A0A 0A0).");
        return false;
    }

    return true;
} 

function isNumeric(text)
{
	var validChars = "0123456789";
	
	for(var i = 0; i < text.length; i++)
	{
		var char = text.charAt(i);
		
		if(validChars.indexOf(char) == -1)
		{
			return false;
		}
	}
	
	return true;

}

function enterPressed(e)
{
	var kc;
	if(window.event)
	{
		kc=window.event.keyCode;
	}
	else if(e)
	{
		kc=e.which;
	}
	if (kc == 13)
	{
		return true;
	}
	
	return false;
}


var loading_image;
var bo_content;
var bo_container;
var runComplete = false;
function display_store_thumbs() {
    if (runComplete) {
        bo_container.toggle();
        return;
    } else {
        //if request is midprocess (ex. double clicked), return
        if (runComplete == null) return;
        runComplete = null;
        loading_image = $('#loading_image');
        bo_content = $('#bo_content');
        bo_container = $('#buy_online_container');
        loading_image.show();

        jQuery.ajax({
            type: "POST",
            url: "/Services/RadioSystems/DealerLocatorWebService.asmx/GetDealerLocatorOnlineDealers",
            data: "{'product':'" + sku + "', 'countryId':'" + 271 + "', 'imageSize':'1'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(result) {
                var resultList;

                if (typeof (JSON) !== 'undefined' &&
                typeof (JSON.parse) === 'function')
                    resultList = JSON.parse(result.d);
                else
                    resultList = eval('(' + result.d + ')');

                update_buy_online(resultList);
            }
        });
    }
}

function update_buy_online(dealerList) {
    var online_store_listings = document.getElementById('online_store_listings');

    var count = 0;

    var dealerContent = "";

    if (dealerList == null) {
        bo_content.html('<div id="no_dealers">There was an error processing your request.</div>');
        bo_container.show();
        loading_image.hide();
        return;
    }

    jQuery.each(dealerList, function() {
        if (this.Img == "" && this.Url != "") return;

        if (count == 3) {
            dealerContent += '<div id="show">';
            dealerContent += '<table height="50%" cellspacing="5" cellpadding="5" border="0" align="center" width="100%">';
            dealerContent += '<tbody><tr>';
            dealerContent += '<td align="right" valign="top" colspan="3"><a onmouseover=\'top.status="";return true;\' onclick="$(\'#extra_dealers\').toggle(); $(\'#show\').toggle()" href="#"><img alt="Show All" src="' + currentSite.domain + currentSite.name + '/media/Images/Buttons/show_all.gif" /></a></td>';
            dealerContent += '</tr>';
            dealerContent += '</tbody></table>';
            dealerContent += '</div>';

            dealerContent += '<div style="display: none;" name="extra_dealers" id="extra_dealers">';
        }

        dealerContent += "<a class=\"online_dealer_wrapper\" href=\"" + this.Url + "\" target=\"_blank\" rel=\"nofollow\" onclick=\"s = s_gi(reportSuite); s.linkTrackVars='eVar9'; s.eVar9='" + this.Name + "'; s.tl(this, 'e', '" + this.Name + "');\">"
        dealerContent += "<img class=\"dealer_image\" src=\"http://www.petsafe.net/PetSafe/media/Images/" + this.Img + "\" /></a>";

        count++;
    });

    if (count == 0) {
        bo_content.html('<div id="no_dealers">No Dealers Sell This Product Online</div>');
        bo_container.show();
        loading_image.hide();
        return;
    }
    else {
        if (count > 3) {
            dealerContent += '<div id="hide">';
            dealerContent += '<table height="100%" cellspacing="5" cellpadding="5" border="0" align="center" width="100%">';
            dealerContent += '<tbody><tr>';
            dealerContent += '<td align="right" valign="top" colspan="3"><a onmouseover=\'top.status="";return true;\' onclick="$(\'#extra_dealers\').toggle(); $(\'#show\').toggle()" href="#"><img alt="Show Less" src="' + currentSite.domain + currentSite.name + '/media/Images/Buttons/show_less.gif" /></a></td>';
            dealerContent += '</tr>';
            dealerContent += '</tbody></table>';
            dealerContent += '</div></div>';
        }
        bo_content.html(dealerContent);
        bo_container.show();
        loading_image.hide();
        runComplete = true;
    }
}

function buyOnlineClicked(that) {
    if (typeof doGoal == 'function') {
        doGoal(that);
    }
}