// JScript File

var colSynLogoLink='http://www.payscale.com';
var colLocation='/cost-of-living-calculator';

var htmlJobTitle=  '<div id="jobTitle" class="yui-ac"> '+ 
        '<input id="jobTitleInput" type="text" class="yui-ac-input" style="width: 180px" onfocus="colfocusJobTitle(this);" /> '+ 
         '<div id="jobTitleDataSource" class="yui-ac-container"></div></div>' ;

// Get job titles
function colgetJobTitles(jobTitleInput, jobTitleDataSource )
{
    var jobTitle = document.getElementById(jobTitleInput);
    var hiddenJobTitle = document.getElementById(COL_HiddenJobTitle);

    if (jobTitle != null)
    {
      if(hiddenJobTitle != null )
      {
          jobTitle.value = hiddenJobTitle.value
      }
    }       
}

var ac = [];
var acf = [];
var ds = [];
var aff = [];

function ActivateAutoComplete(id, fieldName, allowSkip, allowAdd, minCount)
{
  var element = document.getElementById(id);
    
  if (element != null)
  {
    if(element.acInitialized)
      return;
  
    element.acInitialized = true;
  }
  
	ds[id] = new YAHOO.widget.DS_XHR("complete.aspx", ['Answers', 'sv', 'vid', 'count', 'type']);
	ds[id].scriptQueryParam = 's';
	ds[id].scriptQueryAppend = 'field=' + fieldName + '&ss=' + (allowSkip ? 'y' : 'n') + '&sa=' + (allowAdd ? 'y' : 'n') + '&min=' + minCount;

	ac[id] = new YAHOO.widget.AutoComplete(id, id + "_complete", ds[id]);
	ac[id].useShadow = true;
	ac[id].useIFrame = true;
	ac[id].fieldName = fieldName;
	ac[id].id = id;
	ac[id].maxResultsDisplayed = 12;
	ac[id].queryDelay = 0.25;
	ac[id]._iFrameSrc = "/spacer.aspx?tag=autocomplete";
	ac[id].autoHighlight = true;
	ac[id].unmatchedItemSelectEvent.subscribe(colunmatchJobTitleEntered);   
}


// select whole job title on focus event
function colfocusJobTitle(source)
{
   source.select();
}

// clear job title, when title not found in list
function colunmatchJobTitleEntered(oSelf)
{
  
}

// close top input window
function colcloseTopInputWindow()
{
    var topInputPanel = document.getElementById(COL_TopInputPanelDiv); 

    if(topInputPanel != null)
    {
        topInputPanel.style.display = 'none'
    }
    var topLocationPanal = document.getElementById(COL_TopLocationPanelDiv);
    if(topLocationPanal != null)
    {   
        topLocationPanal.style.display = ''
    }
}

// open top input window
function colOpenTopInputWindow()
{
    var topInputPanel = document.getElementById(COL_TopInputPanelDiv);
 
    if(topInputPanel != null)
    {
        topInputPanel.style.display = ''
    }
    
//      var JobTitlePanel = document.getElementById('tdJobTitle');
//      alert(JobTitlePanel.innerHTML);
//    if(topLocationPanal != null)
//    {   
//        topLocationPanal.style.display = 'none'
//    }

    var topLocationPanal = document.getElementById(COL_TopLocationPanelDiv);
    if(topLocationPanal != null)
    {   
        topLocationPanal.style.display = 'none'
    }
    
    var hiddenSalary = document.getElementById(COL_HiddenSalary);
    var salaryText = document.getElementById(COL_Salary);
    if(hiddenSalary != null && salaryText != null)
    {
        salaryText.value = hiddenSalary.value;
    }
    
    var fromLocation = document.getElementById(COL_FromLocationId);    
    var toLocation = document.getElementById(COL_ToLocationId);
    
    var hiddenFromLocation = document.getElementById(COL_HiddenFromLocation);    
    var hiddenToLocation = document.getElementById(COL_HiddenToLocation);
    
    if(hiddenFromLocation != null && fromLocation != null)
    {
        fromLocation.value = hiddenFromLocation.value;
    }
    
    if(hiddenToLocation != null && toLocation != null)
    {
        toLocation.value = hiddenToLocation.value;
    }   
    
    colgetJobTitles('jobTitleInputTop','jobTitleInputTop_complete');
    
}

// clear all input 
function colclearAllParameters()
{
    var fromLocation = document.getElementById(COL_FromLocationId);
    var toLocation = document.getElementById(COL_ToLocationId);    
    var jobTitle = document.getElementById('jobTitleInput');  
    var salary = document.getElementById(COL_Salary);
    if (salary != null )
        salary.value ="";
    if(jobTitle != null)
        jobTitle.value ="";        
    if(fromLocation != null)
         fromLocation.value ="State, City";
    if(toLocation != null)
         toLocation.value ="State, City";     
}

// show results
function ValidateColForm(mustHaveTo) {
    var toLocation = document.getElementById(COL_ToLocationId);

    if (toLocation != null && toLocation.value == "")
    {
        if (mustHaveTo)
        {
            alert("Please enter at least the destination location."); 
        }
        return false;
    }
    
    return true;
    
    /*var curUrl = document.location.protocol + "//" + document.location.host + colLocation;
    
    var newUrl = curUrl+'/' + EncodeURI(toLocation.value);
    
    if(fromLocation != null && fromLocation.value != "" && fromLocation.value != toLocation.value )
    {
       newUrl += "/" + EncodeURI(fromLocation.value); 
    }
    else if(jobTitle != null && jobTitle.value != "" && jobTitle.value != defaultText)
    {
        newUrl += "/-"
    }
    
    if(jobTitle != null && jobTitle.value != "" && jobTitle.value != defaultText)
    {
       newUrl += "/" + EncodeURI(jobTitle.value); 
    }
    
    window.location = newUrl;   */
}

function EncodeURI(encodeThis)
{
    var newString = new String(encodeThis);
    var regExp = /\s+/g;
    newString = newString.replace(regExp,'-');
    regExp = /\/+/g;
    newString = newString.replace(regExp,'');
    return escape(newString);
}

function colSynSiteLink()
{
    window.open(colSynLogoLink,"Payscale","status=1,toolbar=1,menubar=1,location=1,resizable=1,titlebar=1,scrollbars=1");
}

/*
* Open a certain URL in a new window. If a popup blocker prevents this the link is opened in the
* current window.
*/
function fnDOM1Open( event ) {
        if ( window.event ) {
                var shiftKey = window.event.shiftKey;
        } else {
                var shiftKey = event.shiftKey;
        }
 
        if ( !shiftKey ) {
                var strURL = this.href;
                var oNewWindow = window.open( strURL );
 
                if ( !oNewWindow ) {
                        window.location.href = strURL;
                }
 
                return false;
        } else {
                return true;
        }
}
 
/*
* Assign an onclick event handler to all links having the classname "newWindow"
*/
function fnAssignPopupLinks() {
        var aAnchorList  = document.getElementsWithExternalRel();
        i               = aAnchorList.length;
 
        while (i--) {
                oAnchor  = aAnchorList[i];
 
                if ( oAnchor.href ) {
                        oAnchor.onclick = fnDOM1Open; // DOM level 1 approach
                }
        }
}

/*
* Function used to get elements which have a certain classname. The classname does not have to be
* the only classname an object has.
*/
document.getElementsWithExternalRel = function () {
        var s = [document.documentElement || document.body], i = 0, r = [], l = 0, e;
        var re = new RegExp('^external$'); // '(^|\\s)external(\\s|$)'
 
        do {
                e = s[i];
 
                while (e) {
                        /* Element nodes only */
                        if (e.nodeType == 1) {
                                if (e.rel && re.test(e.rel)) r[l++] = e;
 
                                s[i++] = e.firstChild;
                        }
 
                        e = e.nextSibling;
                }
        }
 
        while (i--);
 
        return r;
}
