/*
 * (c) Copyright 2003 Napster, LLC.  All rights reserved.
 *
 * ----------------------------------------------------------------------------
 *
 * SCRIPT: util.js
 *
 * AUTHOR(S): 
 *
 * DESCRIPTION: utility functions
 *
 * $Id: util.js,v 1.6 2008/08/14 23:58:50 ntester Exp $
 *
 * ----------------------------------------------------------------------------
 *
 */

var myForm;
var submitted = 0;
var imgOK = 0;
var newImg = new Image();
newImg.src = "/images/gfc_pleasewait.gif";

function checkSubmit() {
	if (imgOK < 1) {
		setTimeout("checkSubmit()",1000);
	}
	else {
		if (submitted < 1) {
			submitted = 1;
			myForm.submit();
		}
	}
}

function changeImg(form,img) {
	myForm = eval("document.forms." + form);
	myImg = eval("document." + img);

	if (submitted < 1) {
		myImg.src = newImg.src;
		myImg.alt = "WAIT";
		imgOK = 1;
		setTimeout("checkSubmit()", 1000);
	}
}

function popUp(url,window_name,width,height,location,toolbars,statusbar,resizable,scrollbars) {
	var cookies = getCookie();

	if (url.indexOf('http://') < 0 && cookies['COUNTRYCODE'] && cookies['COUNTRYCODE'] != 'US') {
		var host = ('sms.' + cookies['COUNTRYCODE'] + '.napster.com').toLowerCase();
		url = 'http://' + host + url;
	}

	window.open(url,window_name,"WIDTH=" + width + ",HEIGHT=" + height + ",LOCATION=" + location + ",TOOLBARS=" + toolbars + ",STATUSBAR=" + statusbar + ",RESIZABLE=" + resizable + ",SCROLLBARS=" + scrollbars);
}

function popUpExternal(url,window_name,width,height,location,toolbars,statusbar,resizable,scrollbars) {
	window.open(url,window_name,"WIDTH=" + width + ",HEIGHT=" + height + ",LOCATION=" + location + ",TOOLBARS=" + toolbars + ",STATUSBAR=" + statusbar + ",RESIZABLE=" + resizable + ",SCROLLBARS=" + scrollbars);
}

function writeDLbutton(lfm,id,type) {
	if (type == 'track') {
		var opcode = 'http://opcode.napster.com/?op=download_tracks&ids=' + id;

		if ((lfm == 'Y') || (!lfm)) {
			var imgwrite = '<A HREF="' + opcode + '&perm=Y"><IMG SRC="/images/btn14_buy_up.gif" WIDTH="24" HEIGHT="13" BORDER="0" ALT="buy"></A>';
		}
		else {
			var imgwrite = '<A HREF="' + opcode + '"><IMG SRC="/images/btn14_download_up.gif" WIDTH="50" HEIGHT="13" BORDER="0" ALT="download"></A>';
		}

	}
	document.write(imgwrite);
}

function openVid(id) {
	var myCookies = getCookie();
	self.location.href = "http://opcode.napster.com/?op=pop_url&width=348&height=434&video=1&url=http://" + self.location.hostname + "/duet/radio/video_player.html?vid=" + id + "&LFM=" + myCookies['LFM'];
}

function getCookieRaw() {
	var results = new Array();

	if (document.cookie.length > 0) {
		var cookie  = document.cookie;
		var cookies = cookie.split("\; ");

		for (var i = 0; i < cookies.length; i++) {
			var namevalue = cookies[i].split("=");
			results[namevalue[0]] = namevalue[1];
		}
	}

	return results;
}

function getCookie() {
	var results = new Array();

	if (document.cookie.length > 0) {
		var cookie  = document.cookie;
		var cookies = cookie.split("\; ");

		for (var i = 0; i < cookies.length; i++) {
			var namevalue = cookies[i].split("=");
			results[namevalue[0]] = unescape(namevalue[1]);
		}
	}

	return results;
}

function getCookieHash(cookieName) {
	var results = new Array();
	var cookies = getCookieRaw();
	var cookie = cookies[cookieName];
//	alert('cookie is ' + cookie);
	if (cookie != null && cookie.length > 0) {
		var names = cookie.split("&");

		for (var i = 0; i < names.length; i += 2) {
			results[unescape(names[i])] = unescape(names[i + 1]);
		}
	}

	return results;
}

function updateCookieHash(cookieName,key,value) {
	var existingCookie = getCookieHash(cookieName);
	var newValues = new Array();
	var set = 0;
	for (var i in existingCookie) {
		if(i == key) {
			newValues[newValues.length] = escape(key);
			newValues[newValues.length] = escape(value);
			set++;
		}
		else {
			newValues[newValues.length] = escape(i);
			newValues[newValues.length] = escape(existingCookie[i]);
		}
	}
	// maybe this is a new value, so see if we updated
	if(set < 1) {
		newValues[newValues.length] = escape(key);
		newValues[newValues.length] = escape(value);
	}
	var newVal = newValues.join("&");
	document.cookie = cookieName + "=" + newVal + "; path=/; domain=.napster.com";
}

function getCountryJS() {
        var cookies = getCookie();
        var scJS;

        if (cookies['COUNTRYCODE'] && cookies['COUNTRYCODE'] == 'GB') {
                scJS = '_GB';
        }
        else if (cookies['COUNTRYCODE'] && cookies['COUNTRYCODE'] == 'CA') {
                scJS = '_CA';
        }
/* Dustan added */
        else if (cookies['COUNTRYCODE'] && cookies['COUNTRYCODE'] == 'JP') {
            scJS = '_JP';
        }
        else {
                scJS = '';
        }

        return scJS;
}

function getQuery() {
	var results = new Array();

	if (top.location.search.substring(1).length > 0) {
		var query = top.location.search.substring(1);
		var pairs = query.split('&');

		for (i = 0; i < pairs.length; i++) {
			var tmp = pairs[i].split('=');
			results[tmp[0]] = unescape(tmp[1]);
		}
	}

	return results;
}

function email_check(str){
	var objRegExp = /^/;
	//var objRegExp =  /^((?:(?:(?:(\w|~)[~\.\-\+]?)*)(\w|~))+)\@((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.([a-zA-Z]{2,6})$|^$/;
	return objRegExp.test(str);
}

function imgRollover(imgName,imgSrc) {
	document.images[imgName].src = imgSrc;
}

function cantfind() {
	self.location.href = "http://opcode.napster.com/?op=pop_url&height=450&width=680&url=http://" + self.location.hostname + "/duet/find/cantfind.html";
}

function imageErrorHandler(imageID,size) {
	var img = eval(imageID);
	if (size == 75) {
		img.src = "/images/gfc_75x75_default.gif";
	} else {
		img.src = "/images/gfc_150x150_default.gif";
	}
}

function getWWWHost(country) {
	var cookies = getCookie();
	var wwwHost;

	if ((country == undefined || country == '') && cookies['COUNTRYCODE']) {
		country = cookies['COUNTRYCODE'];
	}

	if (country == 'GB') {
		wwwHost = 'www.napster.co.uk';
	}
	else if (country == 'CA') {
		wwwHost = 'www.napster.ca';
	}
    /* Dustan Added */
    else if (country == 'JP') {
        wwwHost = 'www.napster.jp';
    }
	else if (country == 'DE') {
		wwwHost = 'www.napster.de';
	}
	else {
		wwwHost = 'www.napster.com';
	}

	return wwwHost;
}

function getSupportEmailAddr(country) {
	var cookies = getCookie();
	var emailAddr;

	if (country == '' && cookies['COUNTRYCODE']) {
		country = cookies['COUNTRYCODE'];
	}

	if (country == 'GB') {
		emailAddr = 'support@napster.co.uk';
	}
	else if (country == 'CA') {
		emailAddr = 'support@napster.ca';
	}
    /* Dustan Added */
    else if (country == 'JP') {
        emailAddr = 'support@napster.jp';
    }
	else {
		emailAddr = 'support@napster.com';
	}

	return emailAddr;
}

function getPayPalUrl(cust_id, mp_id) {
	var cookies = getCookie();

	var business = new Array();
	business["CA"] = 'capaypal@napster.com';
	business["GB"] = 'ukpaypal@napster.com';
	business["US"] = 'uspaypal@napster.com';
	business["DE"] = 'depaypal@napster.com';
    business["JP"] = 'japaypal@napster.com';

	var currency_code = new Array();
	currency_code["CA"] = 'CAD';
	currency_code["GB"] = 'GBP';
	currency_code["US"] = 'USD';
	currency_code["DE"] = 'EUR';
    currency_code["JP"] = 'JPY';

	var return_url = new Array();
	return_url["US"] = 'sms.napster.com';
	return_url["CA"] = 'sms.ca.napster.com';
	return_url["GB"] = 'sms.gb.napster.com';
	return_url["DE"] = 'sms.de.napster.com';
    return_url["JP"] = 'sms.jp.napster.com'; 

        var mp_desc_txt = new Array();
	mp_desc_txt["US"] = 'Authorization for Napster to initiate payments from your PayPal account for purchases of Napster products and/or subscription services. Your verification of purchase from Napster will be required before a payment is initiated.';
	mp_desc_txt["CA"] = mp_desc_txt["US"];
	mp_desc_txt["GB"] = mp_desc_txt["US"];
	mp_desc_txt["DE"] = 'Napster ist berechtigt Zahlungen beim Kauf von Napster Produkten oder Dienstleistungen von Ihrem PayPal Konto abzubuchen. Bevor eine Abbuchung beim Kauf bei Napster durchgeführt wird, ist Ihre Bestätigung erforderlich.';
    mp_desc_txt["JP"] = mp_desc_txt["US"];

	var zipcode = '00000';
	var countryCode = cookies["COUNTRYCODE"] && cookies["COUNTRYCODE"] != null ? cookies["COUNTRYCODE"] : 'US'; //default to US

	var q = '?business=' + escape(business[countryCode]);
	q += '&cmd=_xclick-merchant';
	if (mp_id && mp_id.length > 0) q += '&mp_id=' + mp_id;
	q += '&mp_pay_type=i';
	q += '&mp_max_min=15.00';
	q += '&mp_max=1000.00';
	q += '&mp_max_edit=1';
	q += '&mp_desc=' + escape(mp_desc_txt[countryCode]);
	q += '&currency_code=' + currency_code[cookies["COUNTRYCODE"]];
	q += '&mp_custom=' + escape('custid=' + cust_id + '&address1=ADDRESS&city=CITY&state=XX&zipcode=' + zipcode);
	q += '&return=' + escape('http://'+return_url[countryCode]+'/duet/account/ac_paypal_return.html?country_code=' + countryCode);

	var url = 'https://www.paypal.com/cgi-bin/webscr' + q;

	return url;
}

function getClickBuyUrl(cust_id, mp_id) {
    var cookies = getCookie();
	var url;

    var countryCode = cookies["COUNTRYCODE"] && cookies["COUNTRYCODE"] != null ? cookies["COUNTRYCODE"] : 'US'; //default to US

    var q = 'cust_id=' + cust_id;
    q += '&country_code=' + countryCode;

    // for QA
    //var url = 'http://premium-51jebe73ptkm1u.eu.clickandbuy.test.myfirstgate.de/ac_clickandbuy_return.html?'+ q;
    // for Dev
    //var url = 'http://premium-ovmwtg21uy3l8k.eu.clickandbuy.test.myfirstgate.de/ac_clickandbuy_return.html?'+ q;

    if (countryCode == 'GB') {
        url = ' https://uk.clickandbuy.com/newauth/http://premium-6v10uoxgivhgi3.uk.clickandbuy.com/ac_clickandbuy_return.html?Nation=UK&lang=en&'+ q;
    }
    else if (countryCode == 'DE') {
        url = 'https://eu.clickandbuy.com/newauth/http://premium-6heooruezims0l.eu.clickandbuy.com/ac_clickandbuy_return.html?Nation=DE&lang=de&'+ q;
    } else {
        url = 'http://www.clickandbuy.com/US/en/';
}

    return url;
}




function trackFlashLink(title,name1,name2,goTo) {
var linkName = "Flash - " + title;
if (name1) {
linkName += (" - " + name1);
}
if(name2) {
linkName += (" - " + name2);
}

try { 
s_linkType="o";
s_linkName=linkName;
s_prop2="Homepage";
s_lnk=s_co('');
s_gs("napster");
}
catch (e) {}
if (goTo.indexOf('tab=music') > -1) {
// skip the opcode, since we're staying with this tab anyway
goURL = goTo.split('url=');
self.location.href = goURL[1];
}
else {
self.location.href = "http://opcode.napster.com?" + goTo;
}
}

	function TableResize(){
		var objTableCollection = document.getElementsByTagName( "TABLE" );
		for( var intTableCount = 0; intTableCount < objTableCollection.length; intTableCount++ ){
			if( objTableCollection[intTableCount].id.match( 'ResizeTable' ) ){
				var objACollection = objTableCollection[intTableCount].getElementsByTagName( "A" );
				for( var intACount = 0; intACount < objACollection.length; intACount++ ){
					if( objACollection[intACount].innerText != '' ){
						var objParent = objACollection[intACount].parentElement;
						status = objTableCollection[intTableCount].parentElement.parentElement.id;
						objACollection[intACount].style.pixelWidth = new Number( objTableCollection[intTableCount].parentElement.parentElement.scrollWidth ) - 74;
						objACollection[intACount].style.textOverflow = 'ellipsis';
						objACollection[intACount].style.overflow = 'hidden';
						objACollection[intACount].style.whiteSpace = 'nowrap';
					}
				}
			}
		}
	}

    try {
        window.attachEvent( "onload", TableResize );
        window.attachEvent( "onresize", TableResize );
    } catch (e) {
    }

function goNapsterlinks() {
window.open("http://www.napster.com/napsterlinks/","napsterlinks");
}

function goNarchive() {
window.open("http://narchive.napster.com/","narchive");
}

function goFreeWeb() {
window.open("http://www.napster.com/","freeweb");
}
