var is_submitted = false;


// spits out a fully xhtml alphabet link list with a on and off state
function outputAlphaMenu(URL, variable)
{
	var i, ch;
	var menu = "";
	var x = variable.length + 2; // strip away the fat
	var searched = window.location.search.substring(x);

	
	for(var i=65; i<91; i++)
	{
		ch = String.fromCharCode(i);
		cls = (ch == searched)? "alphaTO" : "alpha";
		menu += "<a class='"+cls+"' href='" + URL + "?" + variable + "=" + ch + "'>";
		menu += (ch) + "</a>";
		menu += (ch == "Z")? "" : " ";
	}
		return(menu);
};

//moves cursor across phone

function tabNext(current,next) {
  if((current.value.length == current.maxLength) && (next != null)) {
    next.focus();
    next.select();
  }
}

function tabPrev(current,prev) {
  if((event.keyCode == 8) && (current.value.length == 0) && (prev != null)) {
    var tr = prev.createTextRange();
    tr.move('textedit');
    tr.select();
  }
}

//checks for Windows Media Player and redirects as necessary

function getPlayer(song,url) {
  if((!url) || (url == "")) {
    url = env_path;
  }
  var content_id;
  var radio = document.getElementsByName(song + '_radio');
  for(i = 0; i < radio.length; i++) {
  	if(radio[i].checked) {
  		content_id = radio[i].value;
  	}
  }
  window.open(url + '/media-player-detection.cds?cid=' + content_id,'player','width=400,height=300,location=no,menubar=no,status=no,toolbar=no,scrollbars=no,resizable=no');
}

function getStaticPlayer(content) {
  url = env_path;
  var popup_check = window.open(url + '/media-player-detection.cds?cid=' + content + '&lid=' + document.location.href,'player','width=400,height=300,location=no,menubar=no,status=no,toolbar=no,scrollbars=no,resizable=no');
  if(!popup_check) {
    document.location.href = url + '/media-player-detection.cds?cid=' + content + '&lid=' + document.location.href + '&pub=y';
  }
}

function getWallpaper(content_id) {
  url = env_path;
  window.open(url + '/wallpaper-popup.cds?cid=' + content_id,'player','width=400,height=350,location=no,menubar=no,status=no,toolbar=no,scrollbars=no,resizable=no');
}

//trim whitespace

function trim(val) {
  while(val.substr(0,1) == " ") {
    val = val.substr(1);
    if(val.length < 1) {
      return val;
    }
  }
  return val;
}

//validate and submit the search form

function submitSearchForm(form) {
  var field;
  var searchterm = form.sf.value;
  var literal = form.sf.value;

  var pattern = /[']/g;
  searchterm = searchterm.replace(pattern,"''");
  pattern = /[^A-Za-z0-9'\-\s]/g;
  searchterm = searchterm.replace(pattern,"");

  var return_search = "";
  return_search += "\"" + searchterm + "\" OR \"" + searchterm + "*\" OR (";
  var search_array = searchterm.split(/\s/);
  for(i = 0; i < search_array.length; i++) {
    return_search += "\"" + search_array[i] + "*\" AND ";
  }
  return_search = return_search.substring(0, return_search.length - 5);
  return_search += ")";
  searchterm = return_search;
  
  if(trim(form.sf.value) == "") {
    return false;
  }
  else {
    if(form.sc[0].checked) {
      field = "artist";
    }
    else if(form.sc[1].checked) {
      field = "title";
    }
    else {
      field = "both";
    }
    if(!is_submitted) {
    	is_submitted = true;
    	window.location = 'search.cds?field=' + field + '&sf=' + searchterm + '&literal=' + literal;
    	return false;
    }
    else {
    	return false;
    }
  }
}

function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 if (ind+cookieName.length+1 > ind1) return "";
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function urlEncode(url) {
  return escape(url).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

function submitForPurchase(songid,catid) {
  var url = env_path;
  var content_id;
  var radio = document.getElementsByName(songid + '_radio');
  for(i = 0; i < radio.length; i++) {
  	if(radio[i].checked) {
  		content_id = radio[i].value;
  	}
  }
  var lastLocation = urlEncode(document.location.href.replace('+', ' '));  
  document.location = url + '/shopping-cart.cds?bid=' + content_id + '&lid=' + lastLocation + '&cid=' + catid;
}

function submitStaticForPurchase(content,url) {
  if((!url) || (url == "")) {
    url = env_path;
  }
  var lastLocation = urlEncode(document.location.href.replace('+', ' '));  
  document.location = url + '/shopping-cart.cds?bid=' + content + '&lid=' + lastLocation;
}

function change_page(number) {
  new_search('page', number);
}

function url_search(url) {
  document.location.href = url + document.location.search; 
}

function new_search(name, value) {
  var search = document.location.search;
  search = remove_keys(search, name);	
  search = add_key(search, name, value);
  document.location.search = search;
}

function add_key(string, name, value) {
  value = new String(value);
  if (value.length > 0) {
    if (string.indexOf('?') < (string.length - 1)) {
      string = string + '&';
    }
    string = string + name + '=' + value;
  }
  return (string);
}

function remove_keys(string, name) {
  if (name.length > 0) {
    name = name + '=';
    string = new String(string);
    var start = string.indexOf(name);
    while(start > -1) {
      var temp = string.substring(0, start);
      var end = string.indexOf('&', start);
      if (end > -1) {
        temp = temp + string.substr(end + 1);
      }
      else {
        if (temp.charAt(temp.length - 1) == '&') {
          temp = temp.substring(0, temp.length - 1);
        }
      }
      string = temp;
      start = string.indexOf(name);
    }
  }
  return (string);
}

function getSearchKey(key) {
 var search=""+document.location.search;
 var ind=search.indexOf(key);
 if (ind==-1 || key=="") return ""; 
 var ind1=search.indexOf('&',ind);
 if (ind1==-1) ind1=search.length; 
 if (ind+key.length+1 > ind1) return "";
 return unescape(search.substring(ind+key.length+1,ind1));
}

function loadBilling() {
  if((document.billingform) && (document.billingform.billingpayment)) {
    if(document.billingform.billingpayment[0].checked || document.billingform.billingpayment[1].checked || document.billingform.billingpayment[2].checked || document.billingform.billingpayment[3].checked) {
      var credit = document.getElementById('creditcard');
      credit.style.display = 'inline';
      var telecheck = document.getElementById('telecheck');
      telecheck.style.display = 'none';
      var billing = document.getElementById('billinginfo');
      billing.style.display = 'none';
      var consent = document.getElementById('checkconsent');
      consent.style.display = 'none';
      var zip = document.getElementById('zip');
      zip.style.display = 'inline';
      //zip.childNodes[0].childNodes[0].setAttribute('id','formrow1');
      zip.setAttribute('id','formrow1');
    }
    if(document.billingform.billingpayment[4].checked) {
      var credit = document.getElementById('creditcard');
      credit.style.display = 'none';
      var telecheck = document.getElementById('telecheck');
      telecheck.style.display = 'inline';
      var billing = document.getElementById('billinginfo');
      billing.style.display = 'inline';
      var consent = document.getElementById('checkconsent');
      consent.style.display = 'inline';
      var zip = document.getElementById('zip');
      zip.style.display = 'inline';
      //zip.childNodes[0].childNodes[0].setAttribute('id','formrow1');
      zip.setAttribute('id','formrow1');
    }
    if((document.billingform.billingpayment[5] && document.billingform.billingpayment[5].checked) || (document.billingform.billingpayment[6] && document.billingform.billingpayment[6].checked)) {
      var credit = document.getElementById('creditcard');
      credit.style.display = 'none';
      var telecheck = document.getElementById('telecheck');
      telecheck.style.display = 'none';
      var billing = document.getElementById('billinginfo');
      billing.style.display = 'none';
      var consent = document.getElementById('checkconsent');
      consent.style.display = 'none';
      var zip = document.getElementById('zip');
      zip.style.display = 'none';
      //zip.childNodes[0].childNodes[0].setAttribute('id','formrow1');
      zip.setAttribute('id','formrow1');
    }
  }
}

function loadPayments() {
  if((document.billingform) && (document.billingform.billingpayment)) {
    if(document.billingform.billingcarrier[0].checked) {
      p_form.billingpayment[0].disabled = false;
      p_form.billingpayment[1].disabled = false;
      p_form.billingpayment[2].disabled = false;
      p_form.billingpayment[3].disabled = false;
      p_form.billingpayment[4].disabled = false;
      if(p_form.billingpayment[5]) {
        p_form.billingpayment[5].disabled = false;
      }
      if(p_form.billingpayment[6]) {
        p_form.billingpayment[6].disabled = false;
      }
    }
    if(document.billingform.billingcarrier[1].checked) {
      p_form.billingpayment[0].disabled = false;
      p_form.billingpayment[1].disabled = false;
      p_form.billingpayment[2].disabled = false;
      p_form.billingpayment[3].disabled = false;
      p_form.billingpayment[4].disabled = false;
      if(p_form.billingpayment[5]) {
        p_form.billingpayment[5].disabled = false;
      }
      if(p_form.billingpayment[6]) {
        p_form.billingpayment[6].disabled = true;
      }
    }
    if(document.billingform.billingcarrier[2].checked) {
      p_form.billingpayment[0].disabled = false;
      p_form.billingpayment[1].disabled = false;
      p_form.billingpayment[2].disabled = false;
      p_form.billingpayment[3].disabled = false;
      p_form.billingpayment[4].disabled = false;
      if(p_form.billingpayment[5]) {
        p_form.billingpayment[5].disabled = false;
      }
      if(p_form.billingpayment[6]) {
        p_form.billingpayment[6].disabled = true;
      }
    }
    if(document.billingform.billingcarrier[3].checked) {
      p_form.billingpayment[0].disabled = false;
      p_form.billingpayment[1].disabled = false;
      p_form.billingpayment[2].disabled = false;
      p_form.billingpayment[3].disabled = false;
      p_form.billingpayment[4].disabled = false;
      if(p_form.billingpayment[5]) {
        p_form.billingpayment[5].disabled = false;
      }
      if(p_form.billingpayment[6]) {
        p_form.billingpayment[6].disabled = true;
      }
    }
    if(document.billingform.billingcarrier[4].checked) {
      p_form.billingpayment[0].disabled = false;
      p_form.billingpayment[1].disabled = false;
      p_form.billingpayment[2].disabled = false;
      p_form.billingpayment[3].disabled = false;
      p_form.billingpayment[4].disabled = false;
      if(p_form.billingpayment[5]) {
        p_form.billingpayment[5].disabled = false;
      }
      if(p_form.billingpayment[6]) {
        p_form.billingpayment[6].disabled = true;
      }
    }
    if((document.billingform.billingcarrier[5]) && (document.billingform.billingcarrier[5].checked)) {
      p_form.billingpayment[0].disabled = false;
      p_form.billingpayment[1].disabled = false;
      p_form.billingpayment[2].disabled = false;
      p_form.billingpayment[3].disabled = false;
      p_form.billingpayment[4].disabled = false;
      if(p_form.billingpayment[5]) {
        p_form.billingpayment[5].disabled = false;
      }
      if(p_form.billingpayment[6]) {
        p_form.billingpayment[6].disabled = true;
      }
    }
  }
}

function loadCarrier(){
	var country = readCookie2('country');
	//alert("Country = " + country);
	if(document.billingform.country){
		document.billingform.country.value = country;
		//document.countryForm.country.value = country;
		// alert(document.billingform.country.value);
		changePaymentOptionsByCountry(document.billingform.country.value);
	}else{
		document.countryForm.country.value = country;
		//changePaymentOptionsByCountry(document.billingform.country.value);
	}
	//alert("SITE ID = " + document.billingform.siteID.value);

	



}
function removeItemFromCart(itemID) {
  var url = env_path;
  if(confirm('Are you sure you wish to remove this item?')) {
    //document.phoneCompForm.rid.value = itemID;
    //document.phoneCompForm.lid.value = urlEncode(document.location.href.replace('+', ' '));
    var lastLocation = urlEncode(document.location.href.replace('+', ' '));
    if(removeItemFromCart.arguments[1]) {
      document.location = url + '/shopping-cart.cds?rid=' + itemID + '&lid=' + lastLocation + '&s=s';
    }
    else {
      document.location = url + '/shopping-cart.cds?rid=' + itemID + '&lid=' + lastLocation;    
    }
    //document.phoneCompForm.submit();
  }
}

//function agree(check,sprint) {
function agree(sprint) {
  //var check = document.getElementById(check);
  var sprint = document.getElementById(sprint);
  //if(check.checked) {
      //document.getElementById('errorShopping').innerHTML = '';
      if(sprint.checked) {
      	document.location.href = env_path + '/billing-information.cds?sb=y';
      }
      else {
      	document.location.href = env_path + '/billing-information.cds';
      }
  //}
  //else if((document.phoneCompForm.carrier.value != '') && (document.phoneCompForm.manufacturer.value != '') && (document.phoneCompForm.model.value != '') && (document.getElementById('errorShopping').innerHTML == '')) {
  //  if(sprint.checked) {
  //    document.location.href = env_path + '/billing-information.cds?sb=y';
  //  }
  //  else {
  //   document.location.href = env_path + '/billing-information.cds';
  //  }
  //}
  //else if((document.phoneCompForm.carrier.value != '') && (document.phoneCompForm.manufacturer.value != '') && (document.phoneCompForm.model.value != '') && (document.getElementById('errorShopping').innerHTML != '')) {
  //  document.location.href = '#errorShopping';
  //}
  //else {
  //  document.getElementById('errorShopping').innerHTML = 'You must first make sure that your phone is compatible with the formats of the content you have specified before continuing with your purchase. Please select your wireless carrier, phone manufacturer, and phone model below, or click the checkbox to continue without checking your phone compatibility.';
  //  document.location.href = '#errorShopping';
  //}
}




function fastRemoveItemFromCart(itemID) {
  var url = env_path;
  if(confirm('Are you sure you wish to remove this item?')) {
    //document.phoneCompForm.rid.value = itemID;
    //document.phoneCompForm.lid.value = urlEncode(document.location.href.replace('+', ' '));
    var lastLocation = urlEncode(document.location.href.replace('+', ' '));
    if(fastRemoveItemFromCart.arguments[1]) {
      document.location = url + '/fast-shopping-cart.cds?rid=' + itemID + '&lid=' + lastLocation + '&s=s';
    }
    else {
      document.location = url + '/fast-shopping-cart.cds?rid=' + itemID + '&lid=' + lastLocation;    
    }
    //document.phoneCompForm.submit();
  }
}

//function agree(check,sprint) {
function fastAgree(sprint) {
  //var check = document.getElementById(check);
  var sprint = document.getElementById(sprint);
  //if(check.checked) {
      //document.getElementById('errorShopping').innerHTML = '';
      if(sprint.checked) {
      	document.location.href = env_path + '/fast-billing-information.cds?sb=y';
      }
      else {
      	document.location.href = env_path + '/fast-billing-information.cds';
      }
  //}
  //else if((document.phoneCompForm.carrier.value != '') && (document.phoneCompForm.manufacturer.value != '') && (document.phoneCompForm.model.value != '') && (document.getElementById('errorShopping').innerHTML == '')) {
  //  if(sprint.checked) {
  //    document.location.href = env_path + '/billing-information.cds?sb=y';
  //  }
  //  else {
  //   document.location.href = env_path + '/billing-information.cds';
  //  }
  //}
  //else if((document.phoneCompForm.carrier.value != '') && (document.phoneCompForm.manufacturer.value != '') && (document.phoneCompForm.model.value != '') && (document.getElementById('errorShopping').innerHTML != '')) {
  //  document.location.href = '#errorShopping';
  //}
  //else {
  //  document.getElementById('errorShopping').innerHTML = 'You must first make sure that your phone is compatible with the formats of the content you have specified before continuing with your purchase. Please select your wireless carrier, phone manufacturer, and phone model below, or click the checkbox to continue without checking your phone compatibility.';
  //  document.location.href = '#errorShopping';
  //}
}

function fastChangeSubscription(option,item,subsForm) {
  if(option == "on") {
    if(subsForm.subHidden.value != "subscription") {
      document.location.href = env_path + "/fast-shopping-cart.cds?rwp=y";
    }
  }
  if(option == "off") {
    if(subsForm.subHidden.value != "single") {
      var lastLocation = urlEncode(document.location.href.replace('+', ' '));
//      document.location = env_path + '/fast-shopping-cart.cds?rid=' + item + '&lid=' + lastLocation + '&s=s'
	  document.location = env_path + '/fast-shopping-cart.cds?rid=' + item + '&s=s'
    }
  }
}


function sagree(sprint) {
  var sprint = document.getElementById(sprint);
    if(sprint.checked) {
      document.location.href = env_path + '/package-information.cds?sb=y&member_package=y';
    }
    else {
      document.location.href = env_path + '/package-information.cds?member_package=y';
    }
}

function setEntranceCookie(prefix) {
  var expdate = new Date();
  expdate.setFullYear(2038);
  document.cookie = prefix + 'agree=y; expires=' + expdate.toGMTString() + '; path=/';
}

function changeSubscription(option,item,subsForm) {
  if(option == "on") {
    if(subsForm.subHidden.value != "subscription") {
      document.location.href = env_path + "/shopping-cart.cds?rwp=y";
    }
  }
  if(option == "off") {
    if(subsForm.subHidden.value != "single") {
      var lastLocation = urlEncode(document.location.href.replace('+', ' '));
      document.location = env_path + '/shopping-cart.cds?rid=' + item + '&lid=' + lastLocation + '&s=s'
    }
  }
}


function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=560,height=800,left = 0,top = 0');");
}


var newwindow = '';

function popitup(url)
{
	if (!newwindow.closed && newwindow.location)
	{
		newwindow.location.href = url;
	}
	else
	{
		newwindow=window.open(url,'landing','height=960,width=790, toolbar=0, scrollbars=1, location=0, statusbar=1,menubar=0,resizable=1');
		if (!newwindow.opener) newwindow.opener = self;
	}
	//if (window.focus) {newwindow.focus()}
	//return false;
}

//added 2006 08 14 for cookie handing

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie2(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function setCountryCookie(prefix, country){
	
	//alert("Country " + prefix);
	var cookieName='country';
	var country;
	//if(document.billingform && document.countryForm){
	if(document.billingform){
		country = document.billingform.country.value;
	}else{                   
		country = document.countryForm.country.value
		//alert("SubForm" + document.subForm.oneoff.src);
		if(country == 'UK'){
			//alert(document.subForm.subHidden.value)
			if(document.subForm.subHidden.value=='subscription'){
				document.subForm.sub.src = '/images/33p_membershipUK_on.gif'
				document.subForm.oneoff.src = '/images/150GBPUK_off.gif'
				if (document.phoneCompForm.subRingtones)
				{
					document.phoneCompForm.subRingtones.src = '/images/startingat33p.gif';
				}
				if (document.phoneCompForm.subWallpapers)
				{
					document.phoneCompForm.subWallpapers.src='/images/startingat33p.gif';
				}
			}else{
				document.subForm.sub.src = '/images/33p_membershipUK_off.gif'
				document.subForm.oneoff.src = '/images/150GBPUK_on.gif'
			}
		}else{
			if(document.subForm.subHidden.value=='subscription'){
				document.subForm.sub.src = '/images/67centmemberOn.gif'
				document.subForm.oneoff.src = '/images/67centNoMemberOff.gif'
				//alert(document.phoneCompForm.subRingtones.src);
				if (document.phoneCompForm.subRingtones)
				{
					document.phoneCompForm.subRingtones.src = '/images/button_less67.gif';
				}
				if (document.phoneCompForm.subWallpapers)
				{
					document.phoneCompForm.subWallpapers.src='/images/button_less67.gif';
				}
			}else{
				document.subForm.sub.src = '/images/67centmemberOff.gif'
				document.subForm.oneoff.src = '/images/67centNoMemberOn.gif'
			}			
		}
	}

	createCookie(cookieName, country, 9);
	//alert("CountryCookie " + readCookie2('country'));


}

function loadPaymentOptions(){
	
	var country = readCookie2('country');
	//alert("Country = " + country);
	

	document.countryForm.country.value = country;
	if(document.subForm){
		if(country == 'UK'){
			//alert(document.subForm.subHidden.value)
			if(document.subForm.subHidden.value=='subscription'){
				document.subForm.sub.src = '/images/33p_membershipUK_on.gif'
				document.subForm.oneoff.src = '/images/150GBPUK_off.gif'
				if (document.phoneCompForm.subRingtones)
				{
					document.phoneCompForm.subRingtones.src = '/images/startingat33p.gif';
				}
				if (document.phoneCompForm.subWallpapers)
				{
					document.phoneCompForm.subWallpapers.src='/images/startingat33p.gif';
				}
			}else{
				document.subForm.sub.src = '/images/33p_membershipUK_off.gif'
				document.subForm.oneoff.src = '/images/150GBPUK_on.gif'
			}
		}else{
			if(document.subForm.subHidden.value=='subscription'){
				document.subForm.sub.src = '/images/67centmemberOn.gif'
				document.subForm.oneoff.src = '/images/67centNoMemberOff.gif'
				//alert(document.phoneCompForm.SubRingtones.src);
				if (document.phoneCompForm.subRingtones)
				{
					document.phoneCompForm.subRingtones.src = '/images/button_less67.gif';
				}
				if (document.phoneCompForm.subWallpapers)
				{
					document.phoneCompForm.subWallpapers.src='/images/button_less67.gif';
				}

			}else{
				document.subForm.sub.src = '/images/67centmemberOff.gif'
				document.subForm.oneoff.src = '/images/67centNoMemberOn.gif'
			}			
		}
	}
}
