﻿var endHeight = new Array();

function Show(sElement) {

if ( document.getElementById(sElement) != null ) {
    document.getElementById(sElement).style.display = 'block' ; }
}
    
function Hide(sElement) {

if ( document.getElementById(sElement) != null ) {
    document.getElementById(sElement).style.display = 'none' ; }
}
    
function Focus(sElement) {

if ( document.getElementById(sElement) != null ) {
        document.getElementById(sElement).focus() ; }
}

function ShowContent(sItem)
{    
    document.getElementById(sItem).style.overflow = 'auto' ;
}

function HideContent(sItem)
{    
    document.getElementById(sItem).style.overflow = 'hidden' ;
}

function ParseUSNumber(sender, args)
{
   var num = args.Value.replace(/[^\d]/g,'');

   if(num.length != 10) {
        args.IsValid = false;
        return;                    

   } else 
   {
        args.IsValid = true;
        return;   
   }

}

function CheckAreaCode(sender, args)
{
    var num = args.Value.replace(/[^\d]/g,'');
    
   if( sAreaCodes.indexOf(num.substr(0,3)) >= 0 ) 
   {
        args.IsValid = true;
        return;                    

   } else 
   {
     args.IsValid = false;
     return;   
   }

}

function appendOption(selement,stext,svalue)
{
  var elOptNew = document.createElement('option');
    
  elOptNew.text = stext;

  elOptNew.value = svalue;
   
  var elSel = document.getElementById(selement);

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    try {
        elSel.add(elOptNew); // IE only
        }
        catch(ex) {
        
        alert(ex.description + elOptNew.text + elOptNew.value);
        }
  }
}

function BuildArrivalCity(value)
{
    // cleardown current options and add select
    document.getElementById('ctl00_quote_form_ArrivalCity').length = 0;
    appendOption('ctl00_quote_form_ArrivalCity',"City","City") ;

    var sCity = '';
    var sCityCode = '';
    
    startfrom = sCityStates.indexOf('|' + value + '|') + value.length + 2;
            
    endat = sCityStates.indexOf('|',startfrom);
    
    sStateCities = sCityStates.substr(startfrom,endat - startfrom);
    
    startfrom = 0 ;
    
    while(startfrom<sStateCities.length)
    {
    
      if ( startfrom > 0 )
      {
      startfrom = sStateCities.indexOf('/',startfrom) + 1 ;
      }
      
      endat =  sStateCities.indexOf('/',startfrom) - 1 ; 
      
      if ( endat == -2 )
      {
            endat = sStateCities.length ;
      }
      
      if ( sCity.length == 0 )
      {
        sCity  = sStateCities.substr(startfrom,endat - startfrom + 1);
      } else
      {
        sCityCode = sStateCities.substr(startfrom,endat - startfrom + 1);
      }
      
      if ( sCityCode.length > 0 )
      {
        appendOption('ctl00_quote_form_ArrivalCity',sCity,sCityCode) ;
        sCityCode = '' ;
        sCity = '' ;
      }     
   
      startfrom = endat;
       
    } 

}  

function ConfirmWorkNumber(value)
{

    if ( value == 'Work' )
    {
        if ( !confirm('Please confirm that you are happy for us to call you at work by clicking OK. If you would not like us to call you at work click CANCEL ans select another option') )
        {
            document.getElementById('ctl00_quote_form_Tel_Number_Type').selectedIndex = 0 ;
        }
    }
}

function BuildMakeSelect()
{    
    // clear down the existing select and add a defaul of select
    document.getElementById('ctl00_quote_form_VehicleMake').length = 0;
    appendOption('ctl00_quote_form_VehicleMake',"Make","Make") ;
    
    startfrom = 0 ;
    sValue = 0 ;
    
    while(startfrom<arrAutoMM.length)
    {

        // if his is the first pass we ignoire start from as the array does no start with a |
        if ( startfrom != 0 )
        {
            startfrom = arrAutoMM.indexOf('|',startfrom) + 1 ;
            
            // if startfrom = 0 we've reached the end of file
            if ( startfrom == 0 )
            {
                break;
            }
        }
        

      
        endat =  arrAutoMM.indexOf('|',startfrom) - 1 ;
        
     
        // this should never happen but if it does we exit
        if ( endat == -2 )
        {
           break;
        } 
           
        sAuto  = arrAutoMM.substr(startfrom,endat - startfrom + 1);

        sValue = sValue + 1 ;
             
        appendOption('ctl00_quote_form_VehicleMake',sAuto,sValue) ;
   
        startfrom = endat + 2;
               
    } 
}

function BuildModelSelect(value)
{
    // cleardown current options and add select
    document.getElementById('ctl00_quote_form_VehicleModel').length = 0;
    appendOption('ctl00_quote_form_VehicleModel',"Model","Model") ;

    var sModel = '';
    var sModelCode = 0 ;
    
    startfrom = ("|" + arrAutoMM).indexOf('|' + value + '|') + value.length + 1;
            
    endat = arrAutoMM.indexOf('|',startfrom);
    
    sModels = arrAutoMM.substr(startfrom,endat - startfrom);
    
    startfrom = 0 ;
    
    while(startfrom<sModels.length)
    {
    
      if ( startfrom > 0 )
      {
      startfrom = sModels.indexOf('^',startfrom) + 1 ;
      }
      
      endat =  sModels.indexOf('^',startfrom) - 1 ; 
      
      if ( endat == -2 )
      {
            endat = sModels.length ;
      }
      
      sModel  = sModels.substr(startfrom,endat - startfrom + 1);

      sModelCode = sModelCode + 1 ;

      appendOption('ctl00_quote_form_VehicleModel',sModel,sModelCode) ;
 
   
      startfrom = endat;
       
    } 

}  

function ClearTextBox(sElement,sValue)
{
   if   ( sElement.value.length == 0 )
        {
          sElement.value = sValue ;
        }
   else {
       
   // the textbox onclick event calls this to clear down a textbox when it is first clicked on
        if ( sElement.value == sValue )
            {
             sElement.value = "" ;
            }
        }
}

function ToggleContent(sItem,sHeight)
{    
    if  ( sHeight == '0' ) {
    // removeattribute doesn't seem to work in anything other than IE
    if  ( document.getElementById(sItem).style.removeAttribute ) 
    {
        document.getElementById(sItem).style.removeAttribute('height');
        }
        else
        { 
            document.getElementById(sItem).style.overflow = 'auto' ; }
    }
    else {
        endHeight[sItem] = document.getElementById(sItem).offsetHeight;
        document.getElementById(sItem).style.height = sHeight; }
}

var timerlen = 5;
var slideAniLen = 500;
var timerID = new Array();
var startTime = new Array();
var obj = new Array();
var moving = new Array();
var dir = new Array();

function slidedown(objname){
        if(moving[objname])
                return;

        moving[objname] = true;
        dir[objname] = "down";
        startslide(objname);
}

function slideup(objname){
        if(moving[objname])
                return;

        if(document.getElementById(objname).style.display == "none")
                return; // cannot slide up something that is already hidden

        moving[objname] = true;
        dir[objname] = "up";
        startslide(objname);
}

function startslide(objname){
        obj[objname] = document.getElementById(objname);

        startTime[objname] = (new Date()).getTime();

        if(dir[objname] == "down"){
                obj[objname].style.height = "1px";
        }

        obj[objname].style.display = "block";

        timerID[objname] = setInterval('slidetick(\'' + objname + '\');',timerlen);
}

function slidetick(objname){
        var elapsed = (new Date()).getTime() - startTime[objname];

        if (elapsed > slideAniLen)
                endSlide(objname)
        else {
                var d =Math.round(elapsed / slideAniLen * endHeight[objname]);
                if(dir[objname] == "up")
                        d = endHeight[objname] - d;

                obj[objname].style.height = d + "px";
        }

        return;
}

function endSlide(objname){
        clearInterval(timerID[objname]);

        if(dir[objname] == "up")
                obj[objname].style.display = "none";

        obj[objname].style.height = endHeight[objname] + "px";

        delete(moving[objname]);
        delete(timerID[objname]);
        delete(startTime[objname]);
        delete(endHeight[objname]);
        delete(obj[objname]);
        delete(dir[objname]);

        return;
}

function excerpts(sContainer) {

    var i  = 0;
    var firstParagraph = true ;
    var parNode = document.getElementById(sContainer);
    var nextNode =  document.getElementById(sContainer).firstChild;
    var anchorText = "";
    var newNodeAnchor = document.createElement("a");
    var newNodeTxt = "";
    var newNode = document.createElement("div");
    var innerText = "";
   
    if ( nextNode == null ) 
        { return ; }

    //Loop through the container until we find the first paragraph    
     while ( nextNode.tagName != 'P' )
     {
        nextNode = nextNode.nextSibling ;
      }

    // Loop through the other elements in the container
    while ( nextNode.parentNode.id == sContainer ) 
    {
	    
		    if ( nextNode.tagName == 'P' ) {
		        i = i+1;
		        
		        if ( firstParagraph ) {	
		        
		        	newNode = document.createElement("div");
		            newNode.id = "excerpt" + i;
		            newNode.className = "excerptbox";
		            	
		            //Firefox doesn't support innerText 
		            if ( document.all ) 
		                { innerText = nextNode.innerText; }
		            else
		                { innerText = nextNode.textContent; }     		                    
		             
		            // We display the first sentence as our excerpt 		           
		            if ( innerText.indexOf('.') != -1 ) {
		                newNodeTxt = document.createTextNode( innerText.substr( 0,innerText.indexOf('.') ) + " ... [" ); }
		            else {    
		                newNodeTxt = document.createTextNode( innerText + " ... [" ); }
		        
		             // Now we create an anchor that will expand the original paragraphs when clicked    
		            newNodeAnchor = document.createElement("a");
		            anchorText = "javascript:Hide('excerpt" + i + "');";
		            
		            if ( document.all )
		                { newNodeAnchor.innerText="More"; }
		            else
		                { newNodeAnchor.textContent="More"; }

                    firstParagraph = false ;
		        }
		        
		        //we add a slidedown to the anchortext
		        anchorText = anchorText + "slidedown('content" + i + "');";
		        
		        // we name our paragraph so that we can expand it later
		        nextNode.id= "content" + i ;
		    
		        //we save the paragraph height in an array which is used by the slidedown function
		        endHeight["content" + i] = nextNode.offsetHeight;
		    
		        //we hide our original paragraph
		        nextNode.style.display='none'; 
            
            }
            
            if ( nextNode.tagName == 'H2' ) {
	            // For each H2 heading we create a new div that will contain our excerpt and we give it a name so that we can hide it later

		        firstParagraph = true ;
		        newNodeAnchor.href = anchorText;
		        closeBracket = document.createTextNode( "]");		    
		        
		        //we insert our excerpt and anchor into the document just before our H2
		        newNode.appendChild( newNodeTxt );
		        newNode.appendChild( newNodeAnchor );
		        newNode.appendChild( closeBracket );
		        parNode.insertBefore(newNode, nextNode);
                }
             
            if ( nextNode.nextSibling != null ) {
                nextNode = nextNode.nextSibling; }
            else
               {   
                // write the last paragraph
                newNodeAnchor.href = anchorText;
	            closeBracket = document.createTextNode( "]");		    
		        
	            //we insert our excerpt and anchor into the document just before our H2
	            newNode.appendChild( newNodeTxt );
	            newNode.appendChild( newNodeAnchor );
	            newNode.appendChild( closeBracket );
	
	            parNode.insertBefore(newNode, nextNode); 
	            return; }
            
    }
             
}