var jsPPC = "";
var jsKW = "";
var jsEID = "";

if (window.console){
      if (!window.console.write){
      console.write = function(msg) {
        try{console.log(msg);}catch(err){}
      }
    }
  }else {
    console = {}
    console.write = function(){} 
  }

$.queryVal = function(name){
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) {return "";}
    return results[1] || "";
};
function fnSetCookie(sName,sVal,nDays) {
    var dtCur = new Date();
    dtCur.setTime(dtCur.getTime() + (nDays * 24 * 60 * 60 * 1000));
    document.cookie = sName +"=" + escape(sVal) + ";expires=" + dtCur.toGMTString() + ";path=/";
}
function fnGetCookie(sVal) {
    if (document.cookie.length > 0) {
        nValStart = document.cookie.indexOf(sVal + "=");
        if (nValStart != -1) {
            nValStart = nValStart + sVal.length + 1;
            nValEnd = document.cookie.indexOf(";", nValStart);
            if (nValEnd == -1) {nValEnd = document.cookie.length;}
            return unescape(document.cookie.substring(nValStart, nValEnd));
        }
    }
    return "";
}
function fnPPCSet(){
    nDays = 45;
    fnSetCookie("ldsrc","ppc",nDays);
    fnSetCookie("ppc",jsPPC,nDays);
    fnSetCookie("kw",jsKW,nDays);
    // Write Num, then form values
    $('#phoneNumber,.asiPhoneNumber').html(nPPCNum);
    if (typeof nPPCNum2 != "undefined") 
      $('#phoneNumber2,.asiPhoneNumber2').html(nPPCNum2);
    $('#formLdSrc').html('<input type="hidden" name="CRM_Leads_Notes" value="PPC:'+jsPPC+'" /><input type="hidden" name="CRM_Leads_Notes" value="KW:'+jsKW+'" /><input type="hidden" name="Search_Engine" value="'+jsPPC+'" /><input type="hidden" name="Search_Keyword" value="'+jsKW+'" /><input type="hidden" name="CRM_Lead_ContactType" value="189" />');
}
function fnEIDSet(){
    nDays = 5;
    fnSetCookie("ldsrc","eid",nDays);
    fnSetCookie("eid",jsEID,nDays);
    // Write form values
    $('#formLdSrc').html('<input type="hidden" name="CRM_Leads_Notes" value="E-Mail ID:'+jsEID+'" /><input type="hidden" name="Email_Id" value="'+jsEID+'" />');
}
function fnLeadSrcCheck() {
    // 1. Thru queries
    jsPPC = $.queryVal('ppc');
    jsKW = $.queryVal("kw");
    jsEID = $.queryVal("eid");
    if(jsPPC !== "" && jsEID !== ""){
        alert ("Lead Source Manipulation Error");
        jsPPC="";
        jsKW="";
        jeEID="";
    }else if (jsPPC !== "") {
        fnPPCSet();
    }else if (jsEID !== "") {
        fnEIDSet();
    } else {
        // 2. Then Cookies
        jsLdSrc = fnGetCookie('ldsrc');
        if (jsLdSrc == "ppc") {
            jsPPC = fnGetCookie('ppc');
            jsKW = fnGetCookie('kw');
            fnPPCSet();
        }else if(jsLdSrc == "eid"){
            jsEID = fnGetCookie('eid');
            fnEIDSet();
        }
    }
}
jQuery.fn.ready(function($){
    fnLeadSrcCheck();
    $('#jsddm').dropDownMenu({
        timer: 1000,
        parentMO: 'parent-hover',
        childMO: 'child-hover1'
    });
    if ($.browser.msie) {
      var nDecimalLoc=jQuery.browser.version.indexOf(".");
      var nVersion=jQuery.browser.version.slice(0,nDecimalLoc);
      $('body').addClass ("ie");
      $('body').addClass ("ie"+nVersion);
    }
    // Blank windows for non-local site links
    $("a[href*=http://]").not("a[href*="+location.host+"]").attr("target","_blank");
    $("a[href*=https://]").not("a[href*="+location.host+"]").attr("target","_blank");
  
/* Track debug info */
$('form').each(function(){
   $(this).append('<input type="hidden" name="CRM_Leads_Debug" value="URL:'+window.location+' - UA:'+navigator.userAgent+'" />');
});
  
/**
 * If your on the partner edit page, add http:// to the url fields that dont have it on submit
 */

if( $('#addressURL').length ){
  $('form[name=listItemForm]').submit(function(){
    var url_fields = ['#addressURL', '#facebookURL', '#twitterURL', '#youtubeURL', '#linkedinURL', '#wordpressURL'];
    
    for( var i = 0; i < url_fields.length; i++ ){
      current_value = $(url_fields[i]).val();
      
      if( current_value != '' && current_value.indexOf('http://') == -1 && current_value.indexOf('https://') == -1 ) {
        new_value = 'http://'+current_value;
        $(url_fields[i]).val(new_value);
      }
    }

  });
}
  
  
  
  
});
function fnSiteInit(){
  //Dead Function
}
