var textBackup = new Object();

countVinNumber = function(element) { 
 
   $('.vin-search-character-info span.value', $(this).parents('form:first'))[0].innerHTML = 17 - this.value.length;
}

$(document).ready( function() {   
   $('.page-action :text, .page-action :password, #loginbox :text, #loginbox :password, .vin-search-form-container :text').bind('focus', function() {
                                                   if (textBackup[this.name] == null) {
                                                      textBackup[this.name] = this.value;
                                                   }
                                                   this.value=''

                                               }
                                                );
   $('.page-action :text, .page-action :password, #loginbox :text, #loginbox :password, .vin-search-form-container :text').bind('blur', function() {
                                                  if (this.value == '') {
                                                     this.value = textBackup[this.name];                                                     
                                                  }
                                             }
                                                );
   $('input.vin-search-text').bind('keyup', countVinNumber);

   $('.view-box-foldable img.open').show();
   $('.view-box-foldable div.openable').hide();

   $('.view-box-foldable .open').bind('click', function() {
      var img = $('img.open', this.parentNode)[0];
      if (img.opened) {
         img.src = '/image/arrow_closed.gif';
         img.opened = false;
         $('.openable', this.parentNode).slideUp("fast");
      }
      else {         
         img.src = '/image/arrow_opened.gif';
         img.opened = true;
         $('div.openable', this.parentNode).slideDown("fast");
      }
   });



   /*
   $('div.social-bookmark').bind('mouseenter', function() {      
      $('.dot', this).hide();
      $(this).animate({width: '260px'}, 300);
   }
   );

   $('div.social-bookmark').bind('mouseleave', function() {
      $(this).animate({width: '150px'}, 300);
      $('.dot', this).show();
   }
   );
   */

});showHide=function(a){$("#loginbox .form-container").toggle("normal");return false};$(document).ready(function(){$("#loginbox .login").bind("click",showHide)});
function ZoomPicture(PictureID, Language, Width, Height) {

   if ((Width/Height > 4/3 && Width > window.screen.availWidth - 35) || (Width/Height < 4/3 && Height > window.screen.availHeight - 150)) {

      NewWindowWidth = window.screen.availWidth - 20;

      NewWindowHeight = window.screen.availHeight - 150;

   } else {

      NewWindowWidth = Width + 50;

      NewWindowHeight = Height + 80;

   }

   NewWindowFeatures = "top=20,left=10,width="+NewWindowWidth+",height="+NewWindowHeight+",location=0,directories=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1";

   var PictureZoom = window.open("http://www.carfax.se/generalpurpose/zoompicture.php?PictureID=" + PictureID + "&Language=" + Language, "PictureZoom", NewWindowFeatures);

   PictureZoom.focus();
}

function SwapImage(ImageName, ImageFileName) {

   document.images[ImageName].src = ImageFileName;

}

function SwitchDisplay(id) {
	if ('#' != id.substr(0, 1)) {
	   id = '#'+id;
	}
    if ('block' == $(id).css('display')) {
    	$(id).css('display', 'none');
    } else { 
    	$(id).css('display', 'block');
    }
}

function OpenURLInNewWindow(URL) {

   if (URL) {

      NewWindowWidth = (window.screen.availWidth - 20);
      NewWindowHeight = (window.screen.availHeight - 150);

      NewWindowFeatures = "top=20,left=10,width="+NewWindowWidth+",height="+NewWindowHeight+",location=1,directories=0,menubar=1,toolbar=1,status=1,scrollbars=1,resizable=1";

      var NewWindow = window.open("http://www.carfax.se/index/referer.php?URL="+URL+"&S=", "PreviewWindow", NewWindowFeatures);

      NewWindow.focus();
   }
}

function OpenURLInNewWindowSized(URL, NewWindowWidth, NewWindowHeight) {

   if (URL) {

      //NewWindowWidth = (window.screen.availWidth - 20);
      //NewWindowHeight = (window.screen.availHeight - 150);

      NewWindowFeatures = "top=20,left=10,width="+NewWindowWidth+",height="+NewWindowHeight+",location=1,directories=0,menubar=1,toolbar=1,status=1,scrollbars=1,resizable=1";

      var NewWindow = window.open("http://www.carfax.se/index/referer.php?URL="+URL+"&S=", "PreviewWindow", NewWindowFeatures);

      NewWindow.focus();
   }
}

function RoundToDecimal(ValueToRound, NoOfDecimal) {

   ValueToRound = Math.round(ValueToRound*Math.pow(10,2))/Math.pow(10,2);
   ValueToRoundString = "" + ValueToRound;
   CommaPos = ValueToRoundString.length - ValueToRoundString.indexOf(".");
   if (CommaPos == 2) {
      ValueToRoundString += "0";
   } else {
      if (CommaPos == ValueToRoundString.length + 1) {
         ValueToRoundString += ".00";
      }
   }
   return ValueToRoundString;
}



var marked_row = new Array;

function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;

    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 5. Sets the new color...
   newColor = thePointerColor;
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
                theCells[c].style.backgroundColor = newColor;
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

function enableDisable(el, dis) {
	document.getElementById(el).disabled=dis;
}

function togglePaymentMethod(type) {
	var endis;
	if (type == 'paypal') {
		endis=true;
	} else {
		endis=false;
	}
	enableDisable('card_type',endis);
	enableDisable('card_number',endis);
	enableDisable('card_holder',endis);
	enableDisable('exp_month',endis);
	enableDisable('exp_year',endis);
	enableDisable('exp_month',endis);
	enableDisable('cvc',endis);
}

