// consts
var COOKIE_NAME = '28-LAVALEAD_COOKIE_JS';


function setCookie(name, value, expires, path, domain, secure) {
  document.cookie= name + "=" + escape(value) +
  ((expires) ? "; expires=" + expires.toGMTString() : "") +
  ((path) ? "; path=" + path : "") +
  ((domain) ? "; domain=" + domain : "") +
  ((secure) ? "; secure" : "");
}


function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else {
    begin += 2;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) {
    end = dc.length;
  }
  return unescape(dc.substring(begin + prefix.length, end));
}


function getFileName(){
  var pathParts = window.location.pathname.split('/');
  return pathParts[pathParts.length-1];
}


function contains(value, anArray){
  for (var i=0; i < anArray.length; i++) {
    if (value.indexOf(anArray[i]) != -1){
      return true;
    }
  }
  return false;
}


function getExistingQuery(removeValues){
  var params = '';
  var str = unescape(location.search.substring(1));
  var pairs = str.split("&");

  for (var i=0; i < pairs.length; i++) {
    if(!(contains(pairs[i], removeValues))) {
      params += pairs[i] + "&";
    }
  }
  return params;
}

function helixCJLava(rsp, notifyType, notifyEmail, baseUrl, pid) {
        return doLava( rsp, notifyType, notifyEmail, baseUrl, false, true, pid );
}

function helixCJLavaLead(rsp, notifyType, notifyEmail, baseUrl, pid) {
        return doLava( rsp, notifyType, notifyEmail, baseUrl, true, true, pid );
}

function helixLava(rsp, notifyType, notifyEmail, baseUrl) {
        return doLava( rsp, notifyType, notifyEmail, baseUrl, false, true, 0 );
}

function helixLavaLead(rsp, notifyType, notifyEmail, baseUrl) {
	return doLava( rsp, notifyType, notifyEmail, baseUrl, true, true, 0 );
}

function helixLavaSearch(state, countyNo, rsp, notifyType, notifyEmail, baseUrl) {
        return doLavaSearch( state, countyNo, rsp, notifyType, notifyEmail, baseUrl, false, true, 0 );
}

function helixLavaLeadSearch(state, countyNo, rsp, notifyType, notifyEmail, baseUrl) {
        return doLavaSearch( state, countyNo, rsp, notifyType, notifyEmail, baseUrl, true, true, 0 );
}

function includeComponent(params, state, countyNumber, rsp, notifyType, notifyEmail, baseUrl, showForm, showCjLink, pid) {
  var filename = getFileName();
  var mapUrl = baseUrl;

  // query values to check
  var qs = new Querystring();
  var isDone = qs.get('isdone');

  // user cookie
  var lavaCookie = getCookie(COOKIE_NAME);
  var scriptSrc = '';

  if ((state) && (!countyNumber)) {
    // if state and no countyNumber go to state page
    params += 'countydetailpage=' + formatAppPath(filename);
    params += '&mapPrefix=' + mapUrl + 'maps/us/';
    scriptSrc += baseUrl + 'lava/state.js?' + params;
  }
  else if ((countyNumber) && ((isDone || lavaCookie) || showForm == false)){
    setCookie(COOKIE_NAME, 'lavalead');
    params += 'countydetailpage=' + baseUrl + 'lava/leadProxy.html';
    params += '&registrationpage=' + baseUrl + 'lava/leadProxy.html?rsp=' + rsp;
    if (showCjLink && pid > 0) {
        cjUrl = escape('http://www.anrdoezrs.net/click-' + pid + '-10363514?url=');
        params += '&prefixAppPath=' + cjUrl;
    }
    scriptSrc += baseUrl + 'lava/search.js?rsp=' + rsp + '&' + params;
  }
  else if ((countyNumber) && (!isDone) && (showForm)) {
    // go to registration page
    params += 'url=' + escape(window.location.href + '&isdone=1') + '&';
    params += 'rsp=' + rsp + '&';
    params += 'domain=' + escape(window.location.hostname) + '&';
    params += 'refcode=' + rsp + '&';
    params += 'sn=' + 'lavalead' + '&';
    params += 'ntftype=' + notifyType + '&';
    params += 'ntfemail=' + notifyEmail + '&';

    scriptSrc += baseUrl + 'lava/serviceregistration.js?' + params;
  }
  else {
    scriptSrc += baseUrl + 'lava/homepage.js?rsp=' + rsp + '&mapPath=' + mapUrl + '&appPath=' + formatAppPath(filename);
  }

  document.write('<script type="text/javascript" src="' + scriptSrc + '"><\/scr' + 'ipt>');
  document.close();
}

function doLavaSearch(state, countyNumber, rsp, notifyType, notifyEmail, baseUrl, showForm, showCjLink, pid) {
  var removeParamenters = new Array('countydetailpage', 'registrationpage');
  var params = getExistingQuery(removeParamenters);
  params += "&st=" + state + "&cno=" + countyNumber + "&";
  includeComponent( params, state, countyNumber, rsp, notifyType, notifyEmail, baseUrl, showForm, showCjLink, pid );
}


function doLava(rsp, notifyType, notifyEmail, baseUrl, showForm, showCjLink, pid) {
  var removeParamenters = new Array('countydetailpage', 'registrationpage');
  var params = getExistingQuery(removeParamenters);
  var qs = new Querystring();
  var state = qs.get('st');
  var countyNumber = qs.get('cno');
  includeComponent( params, state, countyNumber, rsp, notifyType, notifyEmail, baseUrl, showForm, showCjLink, pid );
}

function formatAppPath(appPath) {
	if ( appPath == "" ) {
		return "/";
	} else {
		return appPath;
	} 
}
