function openQuote(url) {
    var windowWidth = 1000;
    var windowHeight = 800;
    var centerWidth = (window.screen.width - windowWidth) / 2;
    var centerHeight = 10;

    window.open(url, 'AMIquoteWindow', 'height= ' + windowHeight + ',width=' + windowWidth + ',resizable=0,toolbar=0,scrollbars=1,left=' + centerWidth + ', top=' + centerHeight + '');
}

function hideShowElement(elementId, show) 
{
   var element = document.getElementById(elementId);
   if (element != null)
   {
     if (show)
     {
        element.style.display = "";
     }
     else
     {
        element.style.display = "none";
     }
   }
}

//Hide show sections based on is ami customer question
function hideShowContactFormIsAMICustomer(IsAMICustomer, AddressDivId, PhoneDivId, DobDivId, lblPhoneId, phoneTypeId) {
    var element = document.getElementById(AddressDivId);
    
    //Address section
    if (element != null) {
        
        if (IsAMICustomer) {            
            element.style.display = "none";
        }
        else {
            element.style.display = "";
        }                   
    }

    element = document.getElementById(PhoneDivId);
    var element2 = document.getElementById(lblPhoneId);
    var element3 = document.getElementById(phoneTypeId);
    //Phone section
    if (element != null) {

        if (IsAMICustomer) {
            element.style.display = "none";
            element2.style.display = "none";
            element3.style.display = "none";
        }
        else {
            element.style.display = "";
            element2.style.display = "";
            element3.style.display = "";
        }
    }

    element = document.getElementById(DobDivId);

    //Dob section
    if (element != null) {

        if (IsAMICustomer) {
            element.style.display = "";
        }
        else {
            element.style.display = "none";
        }
    }
}

function setElementRangeVisibility(isVisible, commaDelimitedElementIds) {
    var elements = commaDelimitedElementIds.split(',');
//    alert(elements.length.toString());
    for (var i = 0; i < elements.length; i++) {
        var element = document.getElementById(elements[i]);
        if (element != null) {
            if (isVisible) {
                element.style.display = '';
            } else {
                element.style.display = 'none';
            }
        }
    }
}

function showTip(control, tip) 
{
    var selectedClassName = 'controlSelected';
	
		var controls = document.getElementsByTagName('*');
		
		// Reset the look of all controls that have selected class
		for(var i=0; i<controls.length; i++) {
			if(controls[i].className.indexOf(selectedClassName) > -1) {
				controls[i].className = controls[i].className.substr(0, controls[i].className.indexOf(' '));
			}
		}
		
		control.className = control.className + ' ' + selectedClassName; 
		
		var rightCol = document.getElementById('rightCol');
		
		// Get new top position for the tips box
		
		var setTopPos = getPositionTop(control) - 26;

		var assistancePos = 0;
		
		// get position of the halp & assistance box
		var assistControl = document.getElementById('assistance');
		if (assistControl != null) {
		    var assistancePos = assistControl.offsetTop + assistControl.clientHeight;
		}
		
		// Make sure tips box does not overlap help box
		var tipsControl =	document.getElementById('tips');
		
		if(setTopPos > assistancePos) 
		{
			tipsControl.style.top = setTopPos + "px";
		}
		else 
		{
		  tipsControl.style.top = assistancePos - 20 + "px";
        }
			
		tipsControl.style.display = "block";
				
		// Set message for the tips box
		if(tip > -1) {
		  var content = HTMLDecode(listOfTips[tip]);
		   
			document.getElementById('tipMessage').innerHTML = content;
		}
		else {
			document.getElementById('tipMessage').innerHTML = '';
		}
}

function HTMLDecode(strEncodeHTML)
{
  var div = document.createElement('div');
  div.innerHTML = strEncodeHTML;
  
  if(navigator.appName != "Netscape")
  {
    return div.innerText;
  }
  else
  {
    return div.textContent;
  }
}

function getPositionTop(This)
{
  
  var pT = 0;
  var el = This;
  var canStart = false;
  
  while(el != null)
  {
    if (el.className ==  "roundSection")
    {
      canStart = true;
    }
    
    if (canStart)
    {
      
      pT+=el.offsetTop;
      break;
    }
    el = el.parentNode;
  }
  return pT;
  
}

function AcceptButtonClicked(AcceptedClientID, AcceptButton, DeclineButtonID)
{
  var declineButton = document.getElementById(DeclineButtonID);
  //var acceptButton = document.getElementById("AcceptButton");
  
  //declineButton.style.display = "none";
  declineButton.disabled = true;
  AcceptButton.src = "/images/acceptTick.gif";
      
  var Accepted = document.getElementById(AcceptedClientID);
  Accepted.value = "true";
  FireEvent(AcceptedClientID, "change");
  return false;
}

function DeclineButtonClicked(declineButton, isPopup,pageURL)
{
  if (declineButton.disabled)
  {
    return;
  }
  
  if (confirm("AMI will be unable to provide you with a quote") == true)
  {    
    
    if (isPopup.toLowerCase() == "true")
    {
      self.close();
    }
    else
    {      
      window.location = pageURL;
    }    
  }
}

//Shows and hides region based on options age entered in a text box
 function ShowUnder25(op, divUnder25Id, rdUnder25Id) {
 
  document.getElementById(divUnder25Id).style.display="none";
  
  
  var opValue = parseInt(op.value);
  
  if (opValue > 24)
  {
     document.getElementById(divUnder25Id).style.display="";
     document.getElementById(rdUnder25Id).focus();
  }
  
 }
 
 
//Shows and hides region based on options selected in a dropdown
 function ShowHouseCoverReg(op, marketValueCoverId, marketValueRentalCoverId) {

      document.getElementById(marketValueCoverId).style.display="none";
      document.getElementById(marketValueRentalCoverId).style.display="none";
      
      var opValue = op.options[op.selectedIndex].value
      
      if (opValue == 'Market Value Cover') {
        document.getElementById(marketValueCoverId).style.display="";
      }
      if (opValue == 'Rental - Market Value Cover') {
        document.getElementById(marketValueCoverId).style.display="";
        document.getElementById(marketValueRentalCoverId).style.display="";
      }
      if (opValue == 'Rental - Replacement Cover') {        
        document.getElementById(marketValueRentalCoverId).style.display="";
      }      
    }
  
  function HideShowDivSection(op, divId, value) {

      document.getElementById(divId).style.display='none';
       
      var opValue = op.options[op.selectedIndex].value
      
      if (opValue == value) {
        document.getElementById(divId).style.display="";
      }            
    }

 //Used by the content cover details to determine which section is displayed. This is based on option selected for Advanced content details
 function ShowContentsCoverReg(op,divAdditionalCover, divNational, divCarpetCover ,divNoExcess,divCoverAmt, isNationwideId, houseUsage) {

      document.getElementById(divAdditionalCover).style.display='none';
      
      document.getElementById(divNational).style.display='none';
      document.getElementById(divCarpetCover).style.display='none';
      document.getElementById(divNoExcess).style.display='none';
      document.getElementById(divCoverAmt).style.display='none';
       
      var opValue = op.options[op.selectedIndex].value
      var isNationwide = document.getElementById(isNationwideId)
      
      if (opValue == 'StandardContents') {
        
        document.getElementById(divAdditionalCover).style.display="";
        document.getElementById(divNational).style.display="";        
        
        if (RadiolistSelected(isNationwideId, 'yes'))
        {
            document.getElementById(divCoverAmt).style.display="";
        }
      }      
      
      if (opValue == 'AdvancedContents') {
        //document.getElementById(divNoExcess).style.display="";
        if (houseUsage == 'OwnerOccupier' || houseUsage == 'HolidayHome') 
        {   
            document.getElementById(divAdditionalCover).style.display="";
            document.getElementById(divCarpetCover).style.display="";
        }
      }
      
      if (opValue == 'PremierContents') {      
        document.getElementById(divAdditionalCover).style.display='none';        
      }
      
                    
    }
 
function isParentDisplayNone(control)
{
  var count = 0;
  return isParentDisplayNoneRecursive(control, count);
}

function isParentDisplayNoneRecursive(control, count)
{
  count++;
 // alert(control.id);
  if (control == null || control.style == null || count > 10)
  {
    return false;
  }
  else
  {
  //  alert(control.style.display);
    if (control.style.display == "none")
    {
      return true;
    }
    else
    {
      return isParentDisplayNoneRecursive(control.parentNode, count);
    }
  }
}

function replaceCharacters(sOrigString,sInChar,sOutChar) 
{  
    var origString = sOrigString;  
    var inChar = sInChar;  
    var outChar = sOutChar;  
    var newString = origString.split(inChar);  
    
    newString = newString.join(outChar);  
    
    return newString;
 }

 function GetTotalSumInsured(control, hullControlID, motorControlID, trailerID)
 {

    hullControl = document.getElementById(hullControlID);
    motorControl = document.getElementById(motorControlID);
    trailerControl = document.getElementById(trailerID);
    
    var sHull = replaceCharacters(hullControl.value,",","");   
    var sMotor = replaceCharacters(motorControl.value,",","");   
    var sTrailer = replaceCharacters(trailerControl.value,",","");
       
    var hullInt = parseInt(sHull);
    var motorInt = parseInt(sMotor);
    var trailerInt = parseInt(sTrailer);
    
        
    var total = 0;
    
    if (hullInt > 0)
    {
      total += hullInt;
    }
    
    if (motorInt > 0)
    {
      total += motorInt;
    }
    
    if (!isParentDisplayNone(trailerControl))
    {
      if (trailerInt > 0)
      {
        total += trailerInt;
      }
    }
    
    if (total > 0)
    {
      control.value = total;
    }
 }
 
 function HideShowIsYourBoatMoored(op,BoatMooredId, BoatLocationId)
 {
 
    document.getElementById(BoatMooredId).style.display='none';
    document.getElementById(BoatLocationId).style.display='none';
    
    var opValue = op.options[op.selectedIndex].value    
    
    if (opValue == 'Yacht' || opValue == 'Powerboat')
    {
      document.getElementById(BoatMooredId).style.display="";
      //if (RadiolistSelected(rdMooredListId, 'yes'))
      //{
      //  document.getElementById(BoatLocationId).style.display="";
      //}
    }      
    
 }
 
 function FireEvent(elementId, theEvent)
 {
    var element = document.getElementById(elementId);
    if (document.createEventObject)
    {
        // dispatch for IE
        var evt = document.createEventObject();
        element.fireEvent('on'+theEvent,evt);
    }
    else
    {
        // dispatch for firefox + others
   //     alert("Call firefox event");
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(theEvent, true, true ); // event type,bubbling,cancelable
        element.dispatchEvent(evt);
    }
}
//Check if the specific value for a radio button matches the value passed in
function RadiolistSelected (radioId, specificValue)
  {
    var radoObjList=document.getElementsByTagName("input");
	  var selectedCount = 0;
    var matchedSpecific = false;
    var vControl = document.getElementById(radioId);
    
    // Iterate through all input controls 
    for(var i=0;i<radoObjList.length;i++)
    {
     // Only interested in radio buttons
     if(radoObjList[i].type=="radio")
     {
      // Check whether control shares most of the same name as the parent table.
      if(radoObjList[i].id.indexOf(vControl.id) > -1)
      {
        // Check whether radio button is checked.
        if(radoObjList[i].checked)
        { 
          // If value matches a passed in specific value then stop.
          if(radoObjList[i].value == specificValue)
          {
            matchedSpecific = true;
            break;
          }
         selectedCount++;
         break;
        }
      }
     }
    }
    return matchedSpecific;
  }

function showContactTip(control, tip) {			
	// Get new top position for the tips box
	var setTopPos = getContactPositionTop(control);
	
	// Make sure tips box does not overlap help box
	document.getElementById('tips').style.top = setTopPos + 'px';
	
	// Set message for the tips box
	if(tip > -1) {
	//alert(listOfTips[tip]);
	//alert(document.getElementById('tipMessage').innerHTML);
		document.getElementById('tipMessage').innerHTML = listOfTips[tip];
	}
	else {
		document.getElementById('tipMessage').innerHTML = '';
	}
	
	return false;
}

function getContactPositionTop(This){
	  var pT = 0;
      var el = This;
      var canStart = false;
      
      while(el != null)
      {
        if (el.className ==  "roundSection")
        {
          canStart = true;
        }
        
        if (canStart)
        {    
          pT+=el.offsetTop;
          break;
        }
        el = el.parentNode;
      }
      return pT;
}
