function checkClear(obj, v) {
	if(obj.value == v) obj.value = '';
}

function checkEmpty(obj, v) {
	if(obj.value == '') obj.value = v;
}

//Nifty("div.jobSearchContent");

if(typeof sIFR == "function"){
       sIFR.replaceElement("h1#jobSearchTitle", named({sFlashSrc:"/SiteCollectionDocuments/Flash/fontname_F4F3F3.swf", sColor: "#898989", sWmode: "transparent"}));
};

function onClickSaveSearch( ) {
	var cb  = document.getElementById("job-search-save-search");
	var div	= document.getElementById("SavedSearchNameDiv");
	div.style.display = (cb.checked == true) ? "" : "none";
}

function doJobSearch()
{
	var hasError = false;
	
	var searchError      = document.getElementById("job-search-error");
	
	var searchKeywords   = document.getElementById("job-search-keywords");
	var searchCategory   = document.getElementById("job-search-category");
	var searchCity       = document.getElementById("job-search-city");
	var searchState      = document.getElementById("job-search-state");
	var searchZipCode    = document.getElementById("job-search-zip");
	var searchPageIndex  = document.getElementById("job-search-page-index");
	var searchPageSize   = document.getElementById("job-search-page-size");
	var searchPath       = document.getElementById("job-search-path");
	var searchEmployment = document.getElementById("job-search-employment");
	var searchEducation  = document.getElementById("job-search-education");
	var searchExperience = document.getElementById("job-search-experience");
	
	var searchRadius     = document.getElementById("job-search-radius");
	
	var saveSearch       = document.getElementById("job-search-save-search");
	var saveSearchName   = document.getElementById("job-search-save-name");
	
	// similar job search
	var similarKeywords   = document.getElementById("similar-job-search-keywords");
	var similarCategory   = document.getElementById("similar-job-search-category");
	var similarLocation   = document.getElementById("similar-job-search-location");
	var similarCity       = document.getElementById("similar-job-search-city");
	var similarState      = document.getElementById("similar-job-search-state");
	var similarEmployment = document.getElementById("similar-job-search-employment");
	var isSimilarSearch = false;
	var hasSimilarValue = false;
	
	// misc
	var submenuId = document.getElementById("job-search-submenuid");
	
	//validate criteria
    //validate zip
    var zipCodeRegExp = new RegExp(/^\d{5}(-\d{4})?$|^\D{1}\d{1}\D{1}\-?\ ?\d{1}\D{1}\d{1}$/);
    if (searchZipCode && !isNullOrEmpty(searchZipCode.value) && zipCodeRegExp.test(searchZipCode.value) == false)
    {
        searchError.innerHTML="Please provide a valid zip code";
        return;
    }
    //validate radius
    if (searchRadius && !isNullOrEmpty(searchRadius.value) && isNaN(parseInt(searchRadius.value,10)) )
    {
        //searchError.innerHTML="Please provide a valid search radius";
        //return;
    }
    //check if zip code is specified if search radius is > 0
    if (searchRadius && !isNullOrEmpty(searchRadius.value) && isNaN(parseInt(searchRadius.value,10)) == false )
    {
        if (searchZipCode && parseInt(searchRadius.value,10) > 0 && isNullOrEmpty(searchZipCode.value))
        {
            //searchError.innerHTML="Please provide a zip code.";
            //return;
        }
    }
    //check if state is specify if city is specified
    if(searchCity && searchState && !isNullOrEmpty(searchCity.value) && isNullOrEmpty(searchState.value))
    {
        searchError.innerHTML="Please provide a state.";
        return;
    }
                
	//build target URL for site
	var loc = document.location.href;
	var baseURL = loc.substring(0,loc.indexOf("/", 7));
	var target = baseURL + searchPath.value;
		
	// clear default field values
	if (searchKeywords && (searchKeywords.value == " - keywords - ")) {
		searchKeywords.value = "";
	}

	if (searchCategory && (searchCategory.options[searchCategory.selectedIndex].value == "")) {
		searchCategory = null;
	}

	if (searchCity && (searchCity.value == " - city - ")) {
		searchCity = null;
	}

	if (searchState && (searchState.options[searchState.selectedIndex].value == "")) {
		searchState = null;
	}

	if (searchZipCode && (searchZipCode.value == " - zip code - ")) {
		searchZipCode = null;
	}
	
	if (searchEmployment && (searchEmployment.options[searchEmployment.selectedIndex].value == "")) {
		searchEmployment = null;
	}
	
	if (searchEducation && (searchEducation.options[searchEducation.selectedIndex].value == "")) {
		searchEducation = null;
	}
	
	if (searchExperience && (searchExperience.options[searchExperience.selectedIndex].value == "")) {
		searchExperience = null;
	}
	
	if (searchRadius && (searchRadius.options[searchRadius.selectedIndex].value == "")) {
		searchRadius = null;
	}
	
	target += "?kws=";

	if (searchKeywords) {
		target += escape(searchKeywords.value);
	} else if (similarKeywords) {
		isSimilarSearch = true;
		
	 	if (similarKeywords.checked) {
			target += escape(similarKeywords.value);
			hasSimilarValue = true;
		}
	}

	if (searchCategory) {
		target += "&cat=" + searchCategory.options[searchCategory.selectedIndex].value;
	} else if (similarCategory) {
		isSimilarSearch = true;
		
		if (similarCategory.checked) {
			target += "&cat=" + similarCategory.value;
			hasSimilarValue = true;
		}
	}
	
	if (similarLocation) {
		isSimilarSearch = true;

	 	if (similarLocation.checked) {
			hasSimilarValue = true;
			
			if (similarCity) {
				target += "&cty=" + escape(similarCity.value);
			}
		
			if (similarState) {
				target += "&stt=" + similarState.value;
			}
		}
		
	} else {
		if (searchCity) {
			target += "&cty=" + escape(searchCity.value);
		}
		
		if (searchState) {
			target += "&stt=" + searchState.options[searchState.selectedIndex].value;
		}
	}
	
	if (searchZipCode) {
		target += "&zip=" + escape(searchZipCode.value);
	}
	
	if (searchEmployment) {
		target += "&emp=" + searchEmployment.options[searchEmployment.selectedIndex].value;
	} else if (similarEmployment) {
		isSimilarSearch = true;
		
		if (similarEmployment.checked) {
			hasSimilarValue = true;
			target += "&emp=" + similarEmployment.value;
		}
	}
	
	if (searchEducation) {
		target += "&edu=" + searchEducation.options[searchEducation.selectedIndex].value;
	}
	
	if (searchExperience) {
		target += "&exp=" + searchExperience.options[searchExperience.selectedIndex].value;
	}
	
	if (searchRadius) {
		target += "&rds=" + searchRadius.options[searchRadius.selectedIndex].value;
	}
	
	if (searchPageIndex) {
		target += "&pdx=" + searchPageIndex.value;
	}
	
	if (searchPageSize) {
		target += "&psz=" + searchPageSize.value;
	}
	
	if (saveSearch && saveSearch.checked) {
		if (saveSearchName && (saveSearchName.value != "")) {
			target += "&svn=" + escape(saveSearchName.value);
		} else {
			alert("Please provide a name for your saved search.");
			hasError = true;
		}
	}
	
	if (submenuId) {
	    target += "&submenuid=" + submenuId.value;
	}
		
	// if this is a similar seach, but no boxes have been checked, let the user know
	if (isSimilarSearch && !hasSimilarValue) {
		alert("Please check at least one search criterion.");
		hasError = true;
	}
	
	if (!hasError) {
		//redirect to search page, adding on target URL and search string as querystring parameters
		document.location.href = target
	}
}//end function

function saveJob(jobSeekersPath, aujid, jobName) {
	var xhr;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xhr=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xhr=new ActiveXObject("Msxml2.XMLHTTP");st
		} catch (e) {
			try {
				xhr=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	if (xhr) {
    	xhr.onreadystatechange=function() {
    		if(xhr.readyState==4) {

			    if (xhr.responseText == "true") {
			        saveJobDiv = document.getElementById("save-job-" + aujid);
    				
				    if (saveJobDiv.innerHTML) {
				        saveJobDiv.innerHTML = "<img src='/_LAYOUTS/adecco/images/jobsearch/but_saved.gif' />";
				    } else {
					var saved_image = document.createElement("img");
					saved_image.setAttribute("src", "/_LAYOUTS/adecco/images/jobsearch/but_saved.gif");
				        saveJobDiv.replaceChild(saved_image, saveJobDiv.getElementsByTagName("a").item(0));
				    }
			    } else {
				    alert("There was an error when saving this job. Please try again later.");
			    }
		    }
	    }
	    
    	xhr.open('POST', jobSeekersPath + "/savejob.aspx", true);
    	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xhr.send("aujid="+aujid + "&name=" + jobName);
	}
	
	return false;
}

function isNullOrEmpty(text)
{
    if(text == null)
        return true;
    if(text == "")
        return true;
    return false;
}

function InitializePopup(hoveritem, evt)
{
    hoverevent = evt;
    itemYaxis = mousePositiion(evt);
    popupTimer=setTimeout("ShowPopup('" + hoveritem.id + "')",350);
} 
function ReleasePopup()
{
    clearTimeout(popupTimer);
}
// Provide hover over functionality on the job results page
function ShowPopup(hoveritemid)
{      
    hoveritem = document.getElementById(hoveritemid);
     //set job title   
    title = document.getElementById("JobTitlePopup");
    title.innerHTML = hoveritem.innerHTML;
    //set job details url
    title.href = hoveritem.href;    
    
    viewDetailsLink = document.getElementById("ViewDetailsLinkPopup")
    viewDetailsLink.href = hoveritem.href    
    
    //set job description
    jobDescTag = document.getElementById("Description_" + hoveritem.id);
    description = document.getElementById("JobDescriptionPopup");
    description.innerHTML = jobDescTag.value;       
    
    hp = document.getElementById("hoverpopup");    
    var posArray=findPos(hoveritem);
    // Set position of hover-over popup
    hp.style.top = posArray[1] + "px";
    hp.style.left = posArray[0] + "px";    
    // Set popup to visible
    hp.style.visibility = "Visible";
}

function HidePopup()
{    
    hp = document.getElementById("hoverpopup");
    hp.style.visibility = "Hidden";
}

function findPos(obj)
{      
    
    var posX = obj.offsetLeft;var posY = obj.offsetTop;
    var jobSearchResultsContainerId = "JobSearchResultsContainer_" + obj.id; // this id is set on the search results page. The container is used to determin the posstion of the popup
    
    jobSearchResultsContainer = document.getElementById(jobSearchResultsContainerId);    
    
    var IE = document.all?true:false
    if(IE)
    {
        posX += jobSearchResultsContainer.offsetLeft + jobSearchResultsContainer.offsetParent.offsetLeft;
        posY += jobSearchResultsContainer.offsetTop + jobSearchResultsContainer.offsetParent.offsetTop;
    }
    
    posX += 200;
    //posY += 15;
    var winSize = getWinSize();    
    var top; 
    //var itemYposition =  mousePositiion(evt);     
    // 160 is the height of the popup (defined in css)
    if(itemYaxis + 160 > winSize[1]) 
        top = posY - 160 - 15; //move up
    else
        top = posY + 20;
    if(top < 0)
        top = posY;
          
    posY = top;
    var posArray=[posX,posY]
    return posArray;
}

function getWinSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  var winSize=[myWidth,myHeight]
  
  //alert (myHeight);
   
  return winSize;
}

function mousePositiion(evt) {
    
    var e =  evt;
    
	var posx = 0;
	var posy = 0;
	if (!e) 
	    var e = window.event;
	
	posx = e.clientX ;
	posy = e.clientY ;
	
	return posy;	
}
