function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters))
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }
}

function cancel() {
   window.location="/downloads.html";
}

function getElement(whichLayer)     {
     var elem;

 // this is the way the standards work
  if( document.getElementById )   {
    elem = document.getElementById( whichLayer );
                                  }
  // this is the way old msie versions work
  else if( document.all ) {
      elem = document.all[whichLayer];
                          }
  // this is the way nn4 works
  else if( document.layers )  {
      elem = document.layers[whichLayer];
                              }
  return elem;
                                    }

function toggleLayer( whichLayer )  {
  var vis, element;
  element=getElement(whichLayer);
  vis = element.style;

  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&element.offsetWidth!=undefined&&element.offsetHeight!=undefined)
    vis.display = (element.offsetWidth!=0&&element.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';

                                    }

function showLayer(whichLayer)   {
   var vis, element;
   element=getElement(whichLayer);
   element.style.display='block';
                                 }

function showLayerInline(whichLayer)   {
   var vis, element;
   element=getElement(whichLayer);
   element.style.display='inline';
                                 }

function hideLayer(whichLayer)   {
   var vis, element;
   element=getElement(whichLayer);
   element.style.display='none';
                                 }

function makeVisible(whichLayer)   {
   var vis, element;
   element=getElement(whichLayer);
   element.style.visibility='visible';
                                    }

function makeInvisible(whichLayer)   {
   var vis, element;
   element=getElement(whichLayer);
   element.style.visibility='hidden';
                                 }

// Enable or disable fields.
function fieldState(fieldID, disabledBoolean) {
   element=getElement(fieldID);
   element.disabled = disabledBoolean;
}

function isEmpty(field)    {
  element=getElement(field);
  (element.value=="") ? retval=true : retval=false;
  return retval;
                           }

function matches_regex(field, regex)   {
     var regex = new RegExp(regex);
     element=getElement(field);
     (regex.test(element.value)) ? retval=true : retval=false;
     return retval;
                                       }

function getRadioValue(elem) {
   element=getElement(elem);
   return element.checked;
}

function getDropDownValue(elem) {
    element=getElement(elem);
    return element.options[element.selectedIndex].value;
}

function setSMSChecked() {
    document.getElementById("sms-link").checked = true;
}

function setEmailChecked() {
    document.getElementById("email-link").checked = true;
}

function northAmericaCheck() {
    if(document.getElementById("country").value == "North America")
    {
        hideLayer('sms-opt'); hideLayer('phonefield'); showLayer('emailfield'); setEmailChecked();
    }
    else {
        showLayerInline('sms-opt'); showLayerInline('email-opt'); showLayer('phonefield'); hideLayer('emailfield'); setSMSChecked();
    }
}


function validate_download(frmName, product)  {
   // return true for valid form inputs

   valid_phone=false;
   valid_email=false;

   if(product == 'blackberry') {//We are not currently collecting phone for winmo so no need to check.
       if ((matches_regex("countrycode",'^[0-9]{1,4}$')) && (matches_regex("phonenumber",'^[0-9]{4,}$')) ) {
           valid_phone=true;
       }
   }

   if (matches_regex("email","^[_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$")){
      valid_email=true;
   }

   if (isEmpty("country"))  {
      alert("You must select your region to continue.");
      return false;
   }

   if (isEmpty("carrier") && (getDropDownValue("country") == "North America") )  {
      alert("You must select your carrier to continue.");
      return false;
   }

   if (isEmpty("phonetype"))  {
      alert("You must select your device model to continue.");
      return false;
   }


   if (product == 'blacberry' && getRadioValue("sms-link")) {
      if (!valid_phone) {
         alert("You must enter your phone number without any dashes or spaces.");
         return false;
      }
   }
   else if (getRadioValue("email-link")) {
      if (!valid_email) {
         alert("You must enter your e-mail address.");
         return false;
      }
   }

   var submitString = "document."+frmName+".submit()";
   eval(submitString);
   return true;

}


function validate_nokia_download(frmName)  {

   valid_phone=false;
   valid_email=false;

   if ((matches_regex("countrycode",'^[0-9]{1,4}$')) && (matches_regex("phonenumber",'^[0-9]{4,}$')) ) {
       valid_phone=true;
   }

   if (matches_regex("email","^[_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$")){
      valid_email=true;
   }

   if (isEmpty("phonetype"))  {
      alert(clientSideLocalizedParameters.get("pleaseSpecifyModel"));
      //alert("You must select your device model to continue.");
      return false;
   }

   if (getRadioValue("sms-link") == true) {
      if (valid_phone==false) {
         alert(clientSideLocalizedParameters.get("pleaseSpecifyPhoneNumber"));
         //alert("You must enter your phone number without any dashes or spaces.");
         return false;
      }
   }
   else if (getRadioValue("email-link") == true) {
      if (valid_email==false) {
         alert(clientSideLocalizedParameters.get("pleaseSpecifyEmail"));
         //alert("You must enter your e-mail address.");
         return false;
      }
   }

   var submitString = "document."+frmName+".submit()";
   eval(submitString);
   return true;
}


function setOtherCarrier()  {
   makeInvisible("sms-opt");

   emailbutton=getElement("email-link");
   emailbutton.checked=true;

   hideLayer('phonefield');
   showLayer('emailfield');

}

function unsetOtherCarrier()  {
   makeVisible("sms-opt");
   emailbutton=getElement("email-link");
}


function hideSMS(selectedIndexVal) {
   if ((selectedIndexVal=='Other')||(selectedIndexVal=='other')||(selectedIndexVal=='AT&T'))  {
      setOtherCarrier();
      //hideLayer('phonefield');
      //showLayer('emailfield');
   }
   else {
      unsetOtherCarrier();

      hideLayer('phonefield');
      showLayer('emailfield');
      emailbutton.checked=true;
   }
}

function hideSMSByCountry(selectedIndexVal) {
    emailbutton=getElement("email-link");

    if (selectedIndexVal=='Other') {

        makeInvisible("sms-opt");
        hideLayer("carrierfield");
        emailbutton.checked=true;
        hideLayer('phonefield');
        showLayer('emailfield');
    }
    else {
        showLayer("carrierfield");
        makeVisible("sms-opt");
    }
}

function setInitialDownloadFormState(){
    carrierDropdown = getElement("carrier");
    countryDropdown = getElement("country");

    hideSMS(carrierDropdown.options[carrierDropdown.selectedIndex].value);
    hideSMSByCountry(countryDropdown.options[countryDropdown.selectedIndex].value);
}

function setInitialUnsupportedFormState()   {
    //var carrierDropdown = getElement("carrier");
    //var carrierDropdownVal = carrierDropdown.options[carrierDropdown.selectedIndex].value;
    var carrierDropdownVal = getDropDownValue("carrier");
    var deviceDropdownVal = getDropDownValue("device");
    var handsetDropdownVal = getDropDownValue("handset");

    if ((deviceDropdownVal=='Other')||(deviceDropdownVal=='other'))  {
        showLayer("otherdevicefield");
    }
    else {
        hideLayer("otherdevicefield");
    }

    if (deviceDropdownVal=='Unsupported Windows Mobile Device')  {
        showLayer("handsetfield");
        showLayer("carrierfield");
    }
    else {
        hideLayer("handsetfield");
        hideLayer("carrierfield");
        hideLayer("othercarrierfield");
    }


    if (((carrierDropdownVal=='Other')||(carrierDropdownVal=='other')) && deviceDropdownVal=='Unsupported Windows Mobile Device')  {
        showLayer("othercarrierfield");
    }
    else {
        hideLayer("othercarrierfield");
    }


    if (((handsetDropdownVal =='Other' )||(handsetDropdownVal=='other')) && deviceDropdownVal=='Unsupported Windows Mobile Device'){
        showLayer("otherhandsetfield");
    }
    else {
        hideLayer("otherhandsetfield");
    }

    var carrierList = document.getElementById('carrier');
    var carrierVal = carrierList.options[carrierList.selectedIndex].value
    populateUnsupHandsets(carrierVal);

}

// Hide/unhide a text input field if 'Other' is chosen in dropdown.
function toggleOtherField(fieldName, selectedIndexVal, patternToMatch) {
   //if (selectedIndexVal == 'Other') {

   if (!patternToMatch) {
       patternToMatch = 'Other';
   }

   if (selectedIndexVal == patternToMatch) {
      showLayer(fieldName);
   }
   else {
      hideLayer(fieldName);
   }
}

var bbFields = new Array("bb_carriers", "bb_osver", "bb_phonetypes", "bb_vv_version", "bb_vv_downloadedfrom");
var iphoneFields = new Array("apple_carriers", "apple_osver", "apple_phonetypes", "apple_vv_version");
var nokiaFields = new Array("symbian_carriers", "symbian_phonetypes", "symbian_vv_version");
var winmoFields = new Array("winmo_carriers","winmo_phonetypes", "winmo_vv_version");
var androidFields = new Array("android_carriers","android_phonetypes", "android_vv_version");

var allFields = new Array();
allFields = allFields.concat(bbFields, iphoneFields, nokiaFields, winmoFields, androidFields);

var productPrefixes = new Array("apple", "bb", "symbian", "winmo", "android" );
var productSuffixes = new Array("carriers", "osver", "phonetypes","vv_version");


function supportHideDependentLayers() {
    for (var i in allFields) {
        hideLayer(allFields[i]);
    }
}

function supportShowFields(prefix) {
    var Product = new RegExp("^"+prefix+"_");

    for ( var i in allFields ) {
        if ( allFields[i].match(Product) ) {
            showLayer(allFields[i]);
        }
        else {
             hideLayer(allFields[i]);
        }
    }
}

function supportProductChange(selectedIndexVal) {

      switch(selectedIndexVal) {
          case '':
              supportHideDependentLayers();
              break;
          case 'Vlingo for BlackBerry':
              supportShowFields("bb");
              break;
          case 'Vlingo for iPhone':
              supportShowFields("apple");
              break;
          case 'Vlingo for Nokia':
              supportShowFields("symbian");
              break;
          case 'Vlingo for Windows Mobile':
              supportShowFields("winmo");
              break;
          case 'Vlingo for Android':
              supportShowFields("android");
              break;

      }
}

function supportSetInitFormState() {

    var productVal = getElement("product").value;
    var osVer = getElement("osver").value;

    if (osVer == 'Other') {
        showLayer('otherosfield');
    }

    if (isEmpty("product")) {
      supportHideDependentLayers();
    }
    else {
        supportProductChange(productVal);
    }
}

// Provide messaging about the support of various phones
function phoneSupportMsg(selectedIndexVal) {
    if (selectedIndexVal == 'Other')
    {
        alert('Vlingo is supported on the Pearl (81xx series), Pearl Flip (8220, 8230), Curve (83xx series, 8520), 88xx series, Curve 2 (8900), Bold (9000, 9700), Storm 1 and 2 (95xx series), and the Tour (96xx series). \n\nVlingo may work on other devices, however we cannot guarantee full functionality except on those listed above.');
    }
}

// Display an element by its ID if a dropdown selection matches matchingString
function displayOnlyIfChosen(selectedIndexVal, idToDisplay, matchingString ) {
    if (selectedIndexVal == matchingString ) {
        showLayer(idToDisplay);
    }
    else {
        hideLayer(idToDisplay);
    }
}

function populateUnsupHandsets(carrier) {

    var unsupWinMoATTPhones = new Array(
    "Samsung BlackJack SGH-i607",
    "Pantech C810 Duo", "AT&T TILT", "Motorola Q 9h", "Samsung SGH-i907 Epix",
    "HTC Fuze", "LG CT810 Incite", "HP iPAQ hw6510"
    );

    var unsupWinMoSprintPhones = new Array(       
    "Palm Treo 800w", "HTC Touch Pro", "Motorola Q", "Motorola Q 9c",
    "HTC Mogul PPC-6800", "HTC Touch CDMA", "HTC Touch Diamond", "Samsung SPH-I325 Ace"
    );

    var unsupWinMoVerizonPhones = new Array(
    "HTC Touch Pro", "HTC Touch Diamond", "Motorola Q", "Samsung SCH-I760",
    "Palm Treo 700w", "Motorola Q 9m", "Palm Treo 700wx", "Samsung SCH-i830",
    "Verizon Wireless SMT5800", "Verizon Wireless XV6800", "Verizon Wireless XV6700",
    "Samsung SCH-i730", "Samsung SCH-i600", "Motorola Q 9c"
    );

    var unsupWinMoAlltelPhones = new Array (
    "Motorola Q", "Motorola Q9c","HTC Touch CDM", "HTC Touch Pro", "Palm Treo 700wx"
    );

    var unsupWinMoTMobilePhones = new Array (
    "T-Mobile Wing", "T-Mobile Dash", "T-Mobile Shadow Copper",
    "T-Mobile MDA (US)", "HP iPAQ h6315", "T-Mobile SDA (US)"
    );


    var allUnsupPhones = new Array();
    allUnsupPhones = allUnsupPhones.concat(unsupWinMoATTPhones, unsupWinMoSprintPhones, unsupWinMoVerizonPhones, unsupWinMoAlltelPhones, unsupWinMoTMobilePhones);

    var phoneList = new Array();
    var emptySet = new Array();

    switch(carrier) {
        case 'AT&T':
            phoneList = unsupWinMoATTPhones;
            break;
        case 'Sprint':
            phoneList = unsupWinMoSprintPhones;
            break;
        case 'Verizon':
            phoneList = unsupWinMoVerizonPhones;
            break;
        case 'Alltel':
            phoneList = unsupWinMoAlltelPhones;
            break;
        case 'T-Mobile':
            phoneList = unsupWinMoTMobilePhones;
            break;
        default:
            phoneList = emptySet;
            break;
    }

    var handsetList = document.getElementById('handset');

    for (var i = 0; i < handsetList.options.length; ++i) {
        if ((handsetList.options[i].value != 'Other') && (handsetList.options[i].value != ''))  {
            handsetList.remove(i);
        }
    }

    var startindex=2;
    for ( var j=1 in phoneList) {
        //alert("Existing option length: "+handsetList.options.length);
        handsetList.options[startindex] = new Option(phoneList[j]);
        startindex++;
    }

}

function setInitialSMSFormState(countryCode, phoneNumber, warning) {
    countryCodeField=getElement("countrycode");
    phoneNumberField=getElement("phonenumber");
    warningText=getElement("warningtext");

    countryCodeField.value=countryCode;
    phoneNumberField.value=phoneNumber;
    warningText.innerHTML = warning;

}

function numbersonly(e){
var unicode=e.charCode ? e.charCode : e.keyCode;

    if (unicode != 8  && unicode != 13) { //if the key isn't the backspace key (which we should allow)
        if (unicode<48||unicode>57) //if not a number
        return false //disable key press
    }
}

function enterToSubmit(e, frmName, validationFunc) {
var unicode=e.charCode ? e.charCode : e.keyCode;

if ( unicode == 13 ) {
    
    var execStr = validationFunc + "(\'" + frmName + "\')";
    
    eval(execStr);
    
    //validate_download(frmName);
    }
}

function showHiddenGBSprites() {
    // Show the video again if we came from a page that has a video.
    if (top.document.getElementById('video-container') != null) {
        top.window.location='javascript:showLayer(\'video-container\');'
    }

    if (top.document.getElementById('download') != null) {
        top.window.location='javascript:showLayer(\'download\');'
    }

    if (top.document.getElementById('download-blackberry') != null) {
        top.window.location='javascript:showLayer(\'download-blackberry\');'
    }

    if (top.document.getElementById('download-nokia') != null) {
        top.window.location='javascript:showLayer(\'download-nokia\');'
    }

    if (top.document.getElementById('download-winmo') != null) {
        top.window.location='javascript:showLayer(\'download-winmo\');'
    }
    if (top.document.getElementById('download-android') != null) {
        top.window.location='javascript:showLayer(\'download-android\');'
    }

}

function hideGBSprites() {
    if (top.document.getElementById('video-container') != null) {
        top.window.location='javascript:hideLayer(\'video-container\');'
    }

    if (top.document.getElementById('download') != null) {
        top.window.location='javascript:hideLayer(\'download\');'
    }

    if (top.document.getElementById('download-blackberry') != null) {
        top.window.location='javascript:hideLayer(\'download-blackberry\');'
    }

    if (top.document.getElementById('download-nokia') != null) {
        top.window.location='javascript:hideLayer(\'download-nokia\');'
    }

    if (top.document.getElementById('download-winmo') != null) {
        top.window.location='javascript:hideLayer(\'download-winmo\');'
    }

    if (top.document.getElementById('download-android') != null) {
        top.window.location='javascript:hideLayer(\'download-android\');'
    }

}
