
//preload price buttons
price_000_on = new Image();
price_000_off = new Image();
price_099_on = new Image();
price_099_off = new Image();
price_199_on = new Image();
price_199_off = new Image();
price_249_on = new Image();
price_249_off = new Image();
price_299_on = new Image();
price_399_on = new Image();
price_499_on = new Image();
price_599_on = new Image();
price_699_on = new Image();
price_799_on = new Image();
price_899_on = new Image();
price_999_on = new Image();
buy_off = new Image();
buy_on = new Image();
listen_on = new Image();
listen_off = new Image();
btn_subscribe = new Image();

function initializePriceBuyButtons(baseUrl)
{
  price_000_on.src  = baseUrl + "/cingular/images/free.on.gif";
  price_000_off.src = baseUrl + "/cingular/images/get.off.gif";
  price_099_on.src  = baseUrl + "/cingular/images/99.on.gif";
  price_099_off.src = baseUrl + "/cingular/images/99.off.gif";
  price_199_on.src  = baseUrl + "/cingular/images/199.on.gif";
  price_199_off.src = baseUrl + "/cingular/images/199.off.gif";
  price_249_on.src  = baseUrl + "/cingular/images/249.on.gif";
  price_249_off.src = baseUrl + "/cingular/images/249.off.gif";
  
  price_299_on.src = baseUrl + "/cingular/images/btn_299.gif";
  price_399_on.src = baseUrl + "/cingular/images/btn_399.gif";
  price_499_on.src = baseUrl + "/cingular/images/btn_499.gif";
  price_599_on.src = baseUrl + "/cingular/images/btn_599.gif";
  price_699_on.src = baseUrl + "/cingular/images/btn_699.gif";
  price_799_on.src = baseUrl + "/cingular/images/btn_799.gif";
  price_899_on.src = baseUrl + "/cingular/images/btn_899.gif";
  price_999_on.src = baseUrl + "/cingular/images/btn_999.gif";
  
  buy_off.src = baseUrl + "/cingular/images/buy.off.gif";
  buy_on.src  = baseUrl + "/cingular/images/buy.on.gif";
  
  listen_on.src  = baseUrl + "/cingular/images/hear.on.gif";
  listen_off.src = baseUrl + "/cingular/images/hear.off.gif";

  btn_subscribe.src = baseUrl + "/cingular/images/btn_subscribe.gif";
}

function demosms(baseUrl) {
  window.open(baseUrl + "/cingular/demo_textmsg.html","demo","width=437,height=431,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no,status=no");
}

function demowireless(baseUrl) {
  window.open(baseUrl + "/cingular/demo_wireless.html","demo","width=437,height=431,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no,status=no");
}

function move_cursor(fromObject, toObject, maxCharacters)
{
	if(fromObject.value.length == maxCharacters)
	{
		toObject.focus();
	}
}

function forward_cursor(event,frm,fieldnum) {
  var which = event.which ? event.which : event.keyCode;
  if ( (which >= 48 && which <= 57) || (which >= 96 && which <= 105)) {
    if (fieldnum == 1) {
      if (frm.phone1.value.length == 3) {
        frm.phone2.focus();
      }
    }
    if (fieldnum == 2) {
      if (frm.phone2.value.length == 3) {
        frm.phone3.focus();
      }
    }
  }
}

function isAlphaNumericChar( ch ) 
{
  return ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch >= "0" && ch <= "9"))
}

function isAlphaNumericTest(  s )
{
    var begin = 0;
    var end = s.length;

    //Remove leading white space
   while (begin < end )
    { 
      var testchar = s.substring(begin, begin + 1);
     if( isAlphaNumericChar( testchar ) == false ) 
     {
        return false;
      } 
     begin++;
    }
     return true     
}


function purchase_check(frm) 
{		
    //Check the phone number
    phone = trim(frm.phone1.value) + trim(frm.phone2.value) + trim(frm.phone3.value);
    if (phone.length < 10 || isNaN(phone)) {
      alert("Please enter a valid phone number");
      frm.phone1.value = trim(frm.phone1.value);
      frm.phone2.value = trim(frm.phone2.value);
      frm.phone3.value = trim(frm.phone3.value);
      frm.phone1.focus();
      return false;
    }
    //Check the coupon code
    if (frm.couponCode != undefined) 
    {    	
    	var trimmed = trim(frm.couponCode.value);
    	
   	if (trimmed.length != 0)   	
    	{
    		if( (trimmed.length == 8) || (trimmed.length == 10) ){
				if((frm.allowCoupon.value == "true" &&  frm.allowToneCard.value == "true")
					&& (!isAlphaNumericTest(trimmed) || !(trimmed.length == 10 || trimmed.length == 8)))
				{
						
					alert("Oops! That Tone Card code is not valid. Please try again.");
					frm.couponCode.value = trimmed;
		  			frm.couponCode.focus();
	  				return false;

				}
				else if((frm.allowToneCard.value == "true" && frm.allowCoupon.value == "false") 
					&& (!isAlphaNumericTest(trimmed) || trimmed.length != 8))
				{
					alert("Oops! Your Tone Card is not valid. Please try again.");
					frm.couponCode.value = trimmed;
	  				frm.couponCode.focus();
		  			return false;
				}
				else if((frm.allowCoupon.value == "true" &&  frm.allowToneCard.value == "false") 
					&& (!isAlphaNumericTest(trimmed) || trimmed.length != 10))
				{
					alert("Oops! Your coupon is not valid. Please try again.");
					frm.couponCode.value = trimmed;
	  				frm.couponCode.focus();
	  				return false;
				}
			}
			else{
				alert("Oops! That Tone Card code is not valid. Please try again.");
				frm.couponCode.value = trimmed;
	  			frm.couponCode.focus();
		  		return false;
			}
    	} 
    }
	
    return true;

}

function is_valid_phone_format(str)
{
	var begin = 0;
	var end = str.length;
	var numCount = 0;
	
	while(begin < end){
		var c = str.substring(begin, begin + 1);
		// crude, but effective enough
		if(c >= "0" && c <= "9"){
			numCount++;
		}else if(c == "-" || c == "." || c == "(" || c == ")" || c == " "){
			// no-op
		}else{
			return false;
		}
		begin++;
	}
	return (numCount == 10);    
}

function tell_check(frm) 
{		
	var textExists = false;
    //Check the phone numbers
    if(frm.phone1.value.length > 0){
    	textExists = true;
    	//if(frm.phone1.value.length < 10 || isNaN(frm.phone1.value)){
    	if(!(is_valid_phone_format(frm.phone1.value))){
			alert("That number is not valid - please enter a 10-digit phone number.");
			frm.phone1.focus();
			return false;
		}
    }
    if(frm.phone2.value.length > 0){
    	textExists = true;
    	//if(frm.phone2.value.length < 10 || isNaN(frm.phone2.value)){
    	if(!(is_valid_phone_format(frm.phone2.value))){
			alert("That number is not valid - please enter a 10-digit phone number.");
			frm.phone2.focus();
			return false;
		}
    }
    if(frm.phone3.value.length > 0){
    	textExists = true;
    	//if(frm.phone3.value.length < 10 || isNaN(frm.phone3.value)){
    	if(!(is_valid_phone_format(frm.phone3.value))){
			alert("That number is not valid - please enter a 10-digit phone number.");
			frm.phone3.focus();
			return false;
		}
    }
    if(frm.phone4.value.length > 0){
    	textExists = true;
    	//if(frm.phone4.value.length < 10 || isNaN(frm.phone4.value)){
    	if(!(is_valid_phone_format(frm.phone4.value))){
			alert("That number is not valid - please enter a 10-digit phone number.");
			frm.phone4.focus();
			return false;
		}
    }
    if(frm.phone5.value.length > 0){
    	textExists = true;
    	//if(frm.phone5.value.length < 10 || isNaN(frm.phone5.value)){
    	if(!(is_valid_phone_format(frm.phone5.value))){
			alert("That number is not valid - please enter a 10-digit phone number.");
			frm.phone5.focus();
			return false;
		}
    }
    if(frm.phone6.value.length > 0){
    	textExists = true;
    	//if(frm.phone6.value.length < 10 || isNaN(frm.phone6.value)){
    	if(!(is_valid_phone_format(frm.phone6.value))){
			alert("That number is not valid - please enter a 10-digit phone number.");
			frm.phone6.focus();
			return false;
		}
    }
    if(!textExists){
		alert("Please enter at least one 10-digit phone number.");
		frm.phone1.focus();
		return false;
    }
    return true;
}

function search_check(frm) {
  var trimmed = trim(frm.searchTerm.value);
  if (trimmed.length < 2) {
    alert('Search term must be at least 2 characters in length.');
    frm.searchTerm.value = trimmed;
    frm.searchTerm.focus();
    return false;
  }
  return true;
}

function popup_view_url(url) {
  popup_listen_url(url);
}

function popup_listen_url(url) {
  window.open(url,"preview","width=355,height=290,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no");
}

function popup_preview_url(url)
{
  window.open( url, "preview", "width=355, height=374, status=no, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no" );
}

function popup_promo_listen(url) {
  window.open(url,"preview","width=355,height=290,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no");
}

function release_alert(deviceCategory) {
  window.open("endUserHTMLReleaseAlert?catalogName=Cingular+Extras+en&lang=en&promo=false&showCodes=false&dc=" + deviceCategory, "release", "width=480,height=480,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no,status=no");
}

function att_wireless( baseUrl) { 
  window.open( baseUrl + "/cingular/attwireless.html","demo","width=437,height=631,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no,status=no");
}

function subclub_info( baseUrl ) { 
  window.open( baseUrl + "/cingular/subclubinfo.html","Info","width=437,height=631,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no,status=no");
}

function nopromo(type) {
  alert("We're sorry, but " + type + " are not available for your handset.");
}

function nopromo_full(msg) {
  alert(msg);
}

function no_listen_preview(cdcid,ctid) {
  alert("Sorry, there is no audio preview available");
}

function no_video_preview(cdcid,ctid)
{
  alert( "Sorry, there is no video preview available" );
}


//subnav
var promoval = '&promo=false';

function isCategoryTreeDiv(div) {
  return (div.className == "categoryTreeLevel1" || div.className == "categoryTreeLevel2");
}

function repop_nav(catid, baseUrl)
{
 
  var divs = document.getElementsByTagName("div");
  var open = 0;
  for (var i = 0; (i<divs.length); i++)
  {
    if(isCategoryTreeDiv(divs[i]))
    {  
      var image = document.getElementById('img'+divs[i].id);
      var tmpdiv = document.getElementById('rt'+divs[i].id);
      
      //alert('open: ' + open + ' subs within(using ' + 'rt'+divs[i].id + '): ' + document.getElementById('rt'+divs[i].id).id);
      //alert('testing for ' + document.getElementById('td21st'+divs[i].id).id);
      if (open < 1 && document.getElementById('rt'+divs[i].id) != null)
      {
        divs[i].style.display = '';
        if (image != null)
        {
          image.src = baseUrl + '/cingular/images/menu_collapse_tl2.gif';
        }
        open++;
      }
      else if (divs[i].style.display == 'none')
      {
        if (image != null)
        {
          image.src = baseUrl + '/cingular/images/menu_expand_tl2.gif';919
        }
      }
  
      //expand within if its selected
      var link = document.getElementById('td21st'+divs[i].id);
      if (link != null)
      {
        //there exists something below
        //alert('found sub link, so theres something below..');
        if (divs[i].style.display == '')
        { 
          //it matches the current selected
          //document.getElementById(link.id.substring(6)).style.display = '';
          var image2 = document.getElementById('img'+link.id.substring(6));
          if (image2 != null)
          {
            image2.src = baseUrl + '/cingular/images/menu_collapse_tl2.gif';
          }
        }
      }
    }
  }

  highlight(catid);
}

function highlight(catid) {
   var link = document.getElementById('lnk'+catid);
   var td1 = document.getElementById('td1'+catid);
   var td2 = document.getElementById('td2'+catid);
   if (link != null && td1 != null && td2 != null) {
     link.style.fontWeight='bold';
     td1.style.backgroundColor='#ebebeb';
     td2.style.backgroundColor='#ebebeb';
   }
}

function toggle(divobj,imgobj, baseUrl) {
  //alert('div:'+divobj.id + " img:" + imgobj.id);
  //alert('div status: ' + divobj.style.display + ' img src: ' + imgobj.src);

  //hide show div
  if (divobj.style.display == '') {
    divobj.style.display = 'none';
    imgobj.src = baseUrl + '/cingular/images/menu_expand_tl2.gif';
  } else {
    divobj.style.display = '';
    imgobj.src = baseUrl + '/cingular/images/menu_collapse_tl2.gif';
  }
}

function toggleArrow(objId, baseUrl) {
  hidobj = document.getElementById(objId);
  imgobj = document.getElementById('img' + objId);

  //hide show div
  if (hidobj.style.display == '') {
    hidobj.style.display = 'none';
    imgobj.src = baseUrl + '/cingular/images/arrow-closed.gif';
  } else {
    hidobj.style.display = '';
    imgobj.src = baseUrl + '/cingular/images/arrow-open.gif';
  }
}


function getOpenCats() {
  var openCats = "";
  var divs = document.getElementsByTagName("div");

  for (var i = 0; i < divs.length; i++) {
    if(isCategoryTreeDiv(divs[i])) {
      if (divs[i].style.display == '' && divs[i].id != '') {
        openCats += divs[i].id + ",";
      }
    }
  }

  if (openCats != '') {
    openCats = openCats.substring(0,openCats.length-1);
  }

  return '&openCats=' + openCats;
}


function select_category_with_subcategories(catid, baseUrl) {
  var divobj = document.getElementById(catid)
  var imgobj = document.getElementById('img'+catid);

  //expand it
  if (divobj.style.display == 'none') {
    divobj.style.display = '';
    if (imgobj != null) {919
      imgobj.src = baseUrl + '/cingular/images/menu_collapse_tl2.gif';
    }
  }
}


function select_category_with_content(primaryTab, secondaryTab, catid, deviceCategory, baseUrl) {
  location.href = baseUrl + '/enduser/portal/endUserHTMLDir?c1=' + primaryTab + '&c2=' + secondaryTab + '&categoryID=' + catid + '&dc=' + deviceCategory + getOpenCats();
}

function select_page_with_content(primaryTab, secondaryTab, catid, siteNav, shortCodes, ignorePromo, deviceCategory, baseUrl ) {
  if(ignorePromo != "")
  {
  	ignorePromo = "&ignorePromo=" + ignorePromo;
  }
  else
  { 
  	ignorePromo = "";
  }
  location.href=baseUrl + '/enduser/portal/endUserHTMLDir?c1=' + primaryTab + '&c2=' + secondaryTab + '&categoryID=' + catid + '&dc=' + deviceCategory + '&fs=' + siteNav +shortCodes+ ignorePromo + getOpenCats();
}

function launch_mpi(form,url) {
  if (form.agreed.checked == true) {
    window.open(url,"mpi","width=776,height=430,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no,status=no");
  } else {
    alert("We're sorry, you must first read and agree to the above terms\nand conditions before using the Cingular Mobile Image Composer.");
  }
}

function mpi_tac() {
  window.open("http://cingular.picster.moviso.com/mpi/mpi_tac.jsp","mpitac","width=425,height=425,status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no");
}


//Jesper - trim function that works the same way as the trim method in java.lang.String
function trim(s)
{
  var begin = 0;
  var end = s.length;

  //Remove leading white space
  while (begin < end && s.substring(begin, begin + 1) <= ' ')
  {
    begin++;
  }

  //Remove trailing white space
  while (begin < end && s.substring(end - 1, end) <= ' ')
  {
    end--;
  }

  return (begin > 0 || end < s.length) ? s.substring(begin, end) : s;
}

function download_steps() {
  window.open( "../storefront/endUserPurchaseDownloadSteps.html","Info","width=500,height=470,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no,status=no");
}

function clickElementOnEnterKeyPress(event, elementId)
{
	var keyCode = event ? (event.which ? event.which : event.keyCode) : event.keyCode;
    if (keyCode == 13)
    {
		document.getElementById(elementId).click();
		return false;
    }
    return true;
}
