var objAjax = null;

function showhideControl(controlName, action) {
    var element = gE(controlName);
    
    if (element == null)
        return;
        
    element.style.display = action;
}

function toggleLocalFormDisplay() {

if (gE('localStores').style.display == 'none' || gE('localStores').style.display == '')
	gE('localStores').style.display = 'block';
else
	gE('localStores').style.display = 'none';
	
	return false;
}

function toggleRegion(dealerType) {
    var txtpostalcode = $('retailStoreZipCode');
    var localdealeranchor = $('localDealerAnchor');
    
    if (dealerType == 'local') {
        txtpostalcode.style.display = 'block';
        localdealeranchor.href = "javascript:showDealersData($('retailStoreZipCode').value,1,'" + dealerType + "',getCountry())";
    }
    else if (dealerType == 'national') {
        txtpostalcode.style.display = 'none';
        localdealeranchor.href = "javascript:showDealersData('',1,'" + dealerType + "',getCountry())";
    }
}

function showDealerByZipCode(rsLocale,rsOEM,rsSKU,rsZip,rnH,rnW,rbFC,rsCT,rbShowMenu,rnSCID,rsAName){
	var skuValue = "'" + rsSKU + "'";
	var localeValue = "'" + rsLocale.replace('_','') + "'";
	//alert(localeValue);
	// call the function for WebTrends tracking
	dcsMultiTrack('DCS.dcsuri','/' + localeValue + '/Dealer/ChannelIntelligenceGo.do','WT.ti','DymoDealer','WT.cg_n','IndirectLead', 'WT.si_n','WhereToBuy','WT.si_x','3','WT.pn_sku',skuValue);
	// open new window pointing to external URL
	var oWin=ykb_PopUp('cii_com_'+Math.round(Math.random()*1000),rsOEM,'http://dymo.links.channelintelligence.com/Pages/pp.asp?cii_nIID=-1&cii_sSKU='+rsSKU+'&cii_sCT=&cii_nRID=-1&cii_nRGID=835&cii_nVID=-1&cii_nDSID=-1&cii_nPGID=-1&cii_sCountry=US&cii_sCDATA=&cii_sSort=&cii_sSortOrder=&cii_sZip='+rsZip+'&cii_nRadius=15',(rnH&&rnH!=''?rnH:'430'),(rnW&&rnW!=''?rnW:'800'),'middle','center',true,(rbFC?'browser':''),true,true,true,(rbShowMenu?'true':'false'));
	oWin.focus();
}

// wraps the manu_CDLink in order to allow for a call to web trends
function do_CDLink(locale, name, uniqueName, sku) {
	var skuValue = "'" + sku + "'";
	var localeValue = "'" + locale.replace('_','') + "'";
	//alert(localeValue);
	// call web trends tracking
	dcsMultiTrack('DCS.dcsuri','/' + localeValue + '/Dealer/ChannelIntelligence.do','WT.ti','DymoDealer','WT.cg_n','IndirectLead', 'WT.si_n','WhereToBuy','WT.si_x','3', 'WT.pn_sku',skuValue);
	// call channel intelligence
	manu_CDLink(name, sku);
}

// ************************************************************************************
// Commerce Connect Implementation
// ************************************************************************************

function showDealersData(zipcode,currentpage,dealertype,countrycode)
{
    dealerType = dealertype;
	if (zipcode != '' || dealertype == 'local')
	{
		if (ValidateWTBZip(zipcode,zipRegExp))
		{
			alert(zipCodeError);
			return;
		}
	}

	document.getElementById("ccDealers").innerHTML = "<div class='wtbWait'><img src='/img/wait30trans.gif' /></div>";
    ret = DYMO.Web.ajaxService.Service.GetCommerceConnectHtml(locale, EAN, zipcode, currentpage, dealertype, countrycode, OnDealerInfoComplete, OnDealerInfoTimeOut, OnDealerInfoError);
	
}

function OnDealerInfoComplete(arg) {
	var output;

	if (arg == '')
	{
		output = returnErrorText();
	}
	else
	{
		output = arg;
	}
	
	document.getElementById("ccDealers").innerHTML = output;
}

function OnDealerInfoTimeOut(arg) {
	document.getElementById("ccDealers").innerHTML = returnErrorText();
}

function OnDealerInfoError(arg) {
	document.getElementById("ccDealers").innerHTML = returnErrorText();
}

function ValidateWTBZip(value, regex)
{	
	if (value.match(regex))
	{
		return false;
	}
	return true;
}

// Return true if value is a number
function isNumber(value) {
	if (value=="") return false;

	var d = parseInt(value);
	if (!isNaN(d)) return true; else return false;		

}

function returnErrorText() {
	var output = '<div id="no-dealers">';

    switch (dealerType) {
        case "local":
            output += localErrorText;
            break;
        case "online":
            output += onlineErrorText;
            break;
        case "national":
            output += nationalErrorText;
            break;
    }
    
    return output += '</div>';
}

function KeyCheck(e, anchor) {
    var keycode = (window.event) ? event.keyCode : e.which;

    if (keycode == 13) {
        
        location.href = anchor.href;
        return false;
    }    
    
    return true;
}