var enablepersist="on" //Enable saving state of content structure using session cookies? (on/off)
var collapseprevious="no" //Collapse previously open content when opening present? (yes/no)

var contractsymbol='- ' //HTML for contract symbol. For image, use: <img src="whatever.gif">
var expandsymbol='+ ' //HTML for expand symbol.


if (document.getElementById){
document.write('<style type="text/css">')
document.write('.switchcontent{display:none;}')
document.write('</style>')
}

function getElementbyClass(rootobj, classname){
var temparray=new Array()
var inc=0
var rootlength=rootobj.length
for (i=0; i<rootlength; i++){
if (rootobj[i].className==classname)
temparray[inc++]=rootobj[i]
}
return temparray
}

function sweeptoggle(ec){
var thestate=(ec=="expand")? "block" : "none"
var inc=0
while (ccollect[inc]){
ccollect[inc].style.display=thestate
inc++
}
revivestatus()
}


function contractcontent(omit){
var inc=0
while (ccollect[inc]){
if (ccollect[inc].id!=omit)
ccollect[inc].style.display="none"
inc++
}
}

function expandcontent(curobj, cid){
var spantags=curobj.getElementsByTagName("SPAN")
var showstateobj=getElementbyClass(spantags, "showstate")
if (ccollect.length>0){
if (collapseprevious=="yes")
contractcontent(cid)
document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"
if (showstateobj.length>0){ //if "showstate" span exists in header
if (collapseprevious=="no")
showstateobj[0].innerHTML=(document.getElementById(cid).style.display=="block")? contractsymbol : expandsymbol
else
revivestatus()
}
}
}

function revivecontent(){
contractcontent("omitnothing")
selectedItem=getselectedItem()
selectedComponents=selectedItem.split("|")
for (i=0; i<selectedComponents.length-1; i++)
document.getElementById(selectedComponents[i]).style.display="block"
}

function revivestatus(){
var inc=0
while (statecollect[inc]){
if (ccollect[inc].style.display=="block")
statecollect[inc].innerHTML=contractsymbol
else
statecollect[inc].innerHTML=expandsymbol
inc++
}
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function getselectedItem(){
if (get_cookie(window.location.pathname) != ""){
selectedItem=get_cookie(window.location.pathname)
return selectedItem
}
else
return ""
}

function saveswitchstate(){
var inc=0, selectedItem=""
while (ccollect[inc]){
if (ccollect[inc].style.display=="block")
selectedItem+=ccollect[inc].id+"|"
inc++
}

document.cookie=window.location.pathname+"="+selectedItem
}

function do_onload(){
uniqueidn=window.location.pathname+"firsttimeload"
var alltags=document.all? document.all : document.getElementsByTagName("*")
ccollect=getElementbyClass(alltags, "switchcontent")
statecollect=getElementbyClass(alltags, "showstate")
if (enablepersist=="on" && ccollect.length>0){
document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0"
firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
if (!firsttimeload)
revivecontent()
}
if (ccollect.length>0 && statecollect.length>0)
revivestatus()
}

if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
window.onunload=saveswitchstate

function submit_search_form(what)
{
if (!what.distance.value && !what.product.value && !what.distancefrom.value)
{
    alert("You must enter a search term");
    return false;
}
if (what.distance.value && !what.distancefrom.value)
{
    alert("You must supply the zip code to search from");
    return false;
}
if (!what.distance.value && what.distancefrom.value)
{
    alert("You must supply the distance from the zip code");
    return false;
}
if (what.distance.value)
{
    distanceterm=what.distance.value + "-" + what.distancefrom.value;
}
else
{
    distanceterm="All";
}
if (what.product.value)
{
    productterm=what.product.value;
}
else
{
    productterm="All";
}
redirecturl="http://www.cardealercheck.com/"+productterm+"/"+distanceterm.replace(/-/, '_')+"-search-dealers.html";
window.location.href=redirecturl;
return false;
}

function submit_advanced_search_form(what)
{
//alert(what.distance.value);
//alert(what.distancefrom.value);
//alert(what.product.value);
//alert(what.dealername.value);
//alert(what);
  if (!what.dealername.value) {
    alert("You must submit the dealername to search");
    return false;
  }
/*
if (what.distancefrom.value && !what.distance.value)
{
   alert("You must submit a distance from which to search");
   return false;
}
if (what.distance.value)
{
    if (what.distanceform.value)
    {
        distanceterm=what.distance.value + "-" + what.distancefrom.value;
    }
    else
    {
        alert("You must submit distance from value");
        return false;
    }
}
else
{
    distanceterm="All";
}

if (what.product.value)
{
    if (what.dealername.value)
    {
        productterm=what.product.value + "-" + what.dealername.value;
    }
    else
    {
        productterm=what.product.value;
    }
}
else
{
    if (what.dealername.value)
    {
        productterm=what.dealername.value;
    }
    else
    {
        productterm="All";
    }
}
*/
//alert(productterm);
//alert(distanceterm);
redirecturl="http://www.cardealercheck.com/"+what.dealername.value.replace('&','amph;')+"-search-dealers.html";
//alert(redirecturl);
window.location.href=redirecturl;
return false;
}



function addcode(text,what)
{
	var txtarea = what;
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos)
    {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	}
    else
    {
		txtarea.value  += text;
		txtarea.focus();
	}

}
function AddText(startTag,defaultText,endTag,what)
{
      if (what.createTextRange)
      {
         var text;
         what.focus(what.caretPos);
         what.caretPos = document.selection.createRange().duplicate();
         if(what.caretPos.text.length>0)
         {
            var sel = what.caretPos.text;
            var fin = '';
            while(sel.substring(sel.length-1, sel.length)==' ')
            {
               sel = sel.substring(0, sel.length-1)
               fin += ' ';
            }
            what.caretPos.text = startTag + sel + endTag + fin;
         }
         else
            what.caretPos.text = startTag+defaultText+endTag;
      }
      else what.value += startTag+defaultText+endTag;
}

function newwindow(page)
{
window.open(page,'help','width=300,height=200,resizable=yes');
}
function updaterecords(id, i)
{
what = '<table width="100%" align="center">';
what += '<tr>\n';
what += '<td width="150" valign="top" style="padding-top:2px "><input name="friendemail[]" type="text" style="width:125px; height:20px; font-family:Tahoma; font-size:11px "><input type="button" STYLE="font-size:8pt; background-color:#F7F7F7; color:#4279B5" value="New"  onClick="updaterecords(\'' + id + '\', ' + (i+1) + ');" /><\/span></td>\n';
what += '</tr>\n';
what += '</table>\n';

document.getElementById(id + '_' + i).innerHTML = '<br><input name="friendemail[]" type="text" style="width:100px; height:20px; font-family:Tahoma; font-size:11px "><span id="'+id+'_'+(i+1)+'"><input type="button" STYLE="font-size:8pt; background-color:#F7F7F7; color:#4279B5" value="New"  onClick="updaterecords(\'' + id + '\', ' + (i+1) + ');" /><\/span>'//<table width="100%" align="center"><tr><td><input type="text" name="friendemail[]" style="width:125px; height=20px; font-family:Tahoma; font-size:11px;"></td><td><span id="' + id + '_' + (i+1) + '"><input type="button" value="New" onClick="updaterecords(\'' + id + '\', ' + (i+1) + ');" /><\/span></td></tr></table>\n';
//alert(document.getElementById(id + '_' + i).innerHTML);
}

function setNotFakeReview(reviewId) {
  var sure = confirm("Are you sure this is not a fake review?");
  if (sure == true) {
    location.href = 'http://www.cardealercheck.com/fakeReview.php?reviewid='+reviewId;
  } else {
    return false;
  }
}
function setFakeReview(reviewId) {
  var sure = confirm("Are you sure this is a fake review?");
  if (sure == true) {
    location.href = 'http://www.cardealercheck.com/fakeReview.php?reviewid='+reviewId;
  } else {
    return false;
  }
}
function editContactPage() {
  document.getElementById('divUrlContact').style.display = "none";
  document.getElementById('divUrlContactEdit').style.display = "";
  document.getElementById('divEditContactButton').style.display = "none";
}
function editSpecialsPage() {
  document.getElementById('divUrlSpecials').style.display = "none";
  document.getElementById('divUrlSpecialsEdit').style.display = "";
  document.getElementById('divEditSpecialsButton').style.display = "none";
}
function editInventoryPage() {
  document.getElementById('divUrlInventory').style.display = "none";
  document.getElementById('divUrlInventoryEdit').style.display = "";
  document.getElementById('divEditInventoryButton').style.display = "none";
}