function urlencode (str) {
    str = (str+'').toString();
        // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
                                                                    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

function clearText(thefield){

if (thefield.defaultValue==thefield.value)

thefield.value = "";

} 

function submitSearch(){
	var si = document.getElementById("searchitem").value ==  "Enter Biz Name, Association or Product (e.g. Books)" ? '' : urlencode(document.getElementById("searchitem").value);
	var mnp = document.getElementById("minPrice") ? urlencode(document.getElementById("minPrice").value) : null;
	var mxp = document.getElementById("maxPrice") ? urlencode(document.getElementById("maxPrice").value) : null;
	var asc = urlencode(document.getElementById("assoc").value);
	var zipcode = urlencode(document.getElementById("zipcode").value);		
	var state = urlencode(document.getElementById("state").value);		
	window.location="results.php?search=" + si + "&minPrice=" + mnp + "&maxPrice=" + mxp +
		"&assoc=" + asc + "&zipcode=" + zipcode+ "&state=" + state;
}

function typeSearch(e){
	
	var si = document.getElementById("searchitem").value ==  "Enter Biz Name, Association or Product (e.g. Books)" ? '' : urlencode(document.getElementById("searchitem").value);
	var mnp = document.getElementById("minPrice") ? urlencode(document.getElementById("minPrice").value) : null;
	var mxp = document.getElementById("maxPrice") ? urlencode(document.getElementById("maxPrice").value) : null;
	var asc = urlencode(document.getElementById("assoc").value);
	var cat = urlencode(document.getElementById("cat").value);
	var zipcode = urlencode(document.getElementById("zipcode").value);
	var state = urlencode(document.getElementById("state").value);
	
	if (e.keyCode == 13) {
		window.location="results.php?search=" + si + "&minPrice=" + mnp + "&maxPrice=" + mxp
		 + "&assoc=" + asc + "&zipcode=" + zipcode  + "&state=" + state + "&cat=" + cat;
		return;
	}
	
	if (strlen(document.getElementById("searchitem").value) <= 4) return;
	
	
	$("#right").html('<center>Searching<br/><img src="images/preloader.gif" alt=""/></center>');
	
	if(aProd != null){
		aProd.abort(); 
	}
	if(aBiz != null){
		aBiz.abort(); 
	}
	
	var xhr = $.ajax({
		   type: "GET",
		   url: "results-ajax.php",
		   data:"search=" + si + "&minPrice=" + mnp + "&maxPrice=" + mxp + "&assoc=" + asc + "&zipcode=" + zipcode + "&state=" + state + "&cat=" + cat,
		   success: function(msg){
			 $("#right").html(msg);		
		   }
		 });
}

function SelectCategoryMenu(catval){ 
	document.getElementById("cat").value = catval;
	var si = document.getElementById("searchitem").value ==  "Enter Biz Name, Association or Product (e.g. Books)" ? '' : urlencode(document.getElementById("searchitem").value);
	var mnp = urlencode(document.getElementById("minPrice").value);
	var mxp = urlencode(document.getElementById("maxPrice").value);
	var asc = urlencode(document.getElementById("assoc").value);
	var cat = urlencode(document.getElementById("cat").value);
	var zipcode = urlencode(document.getElementById("zipcode").value);
	var state = urlencode(document.getElementById("state").value);	
	
	$("#right").html('<center>Searching<br/><img src="images/preloader.gif" alt=""/></center>');
	
	if(aProd != null){
		aProd.abort(); 
	}
	if(aBiz != null){
		aBiz.abort(); 
	}
	
	var xhr = $.ajax({
		   type: "GET",
		   url: "results-ajax.php",
		   data:"search=" + si + "&minPrice=" + mnp + "&maxPrice=" + mxp + "&assoc=" + asc + "&zipcode=" + zipcode + "&state=" + state + "&cat=" + cat,
		   success: function(msg){
			 $("#right").html(msg);		
		   }
		 });
}


function SearchClick(){
	
	if(document.getElementById("searchitem").value == "Enter Biz Name, Association or Product (e.g. Books)"){
		document.getElementById("searchitem").value = "";
	}
}

function sortProd(sortby){
	
				$("#prodresult").html('<center>Searching<br/><img src="images/preloader.gif" alt=""/></center>');
				
				$.ajax({
				   type: "GET",
				   url: sortby,
				   //data: '' ,
				   success: function(msg){
					 $("#product-item").html(msg);
					 
					 $('.prodresult').cycle({ /* just-listed*/
							//fx:     'scrollLeft',
							fx:     'fade',
							next:   '#nextsprodresult', 
							prev:   '#prevsprodresult',
							timeout: 0,
							pager:   '#nav',
							speed:  'fast'/*, 
							before:  onBefore,
							after:   onAfter*/
							});			
				   }
				 });
	
}

function GoAjaxSearch(){
		var si = document.getElementById("searchitem").value ==  "Enter Biz Name, Association or Product (e.g. Books)" ? '' : urlencode(document.getElementById("searchitem").value);
	var mnp = urlencode(document.getElementById("minPrice").value);
	var mxp = urlencode(document.getElementById("maxPrice").value);
	var asc = urlencode(document.getElementById("assoc").value);
	var cat = urlencode(document.getElementById("cat").value);
	var zipcode = urlencode(document.getElementById("zipcode").value);
	var state = urlencode(document.getElementById("state").value);	
	
	$("#right").html('<center>Searching<br/><img src="images/preloader.gif" alt=""/></center>');
	
	if(aProd != null){
		aProd.abort(); 
	}
	if(aBiz != null){
		aBiz.abort(); 
	}
	
	var xhr = $.ajax({
		   type: "GET",
		   url: "results-ajax.php",
		   data:"search=" + si + "&minPrice=" + mnp + "&maxPrice=" + mxp + "&assoc=" + asc + "&zipcode=" + zipcode + "&state=" + state + "&cat=" + cat,
		   success: function(msg){
			 $("#right").html(msg);		
		   }
		 });
}

function ResetLocalSearch(){
	var si = document.getElementById("searchitem").value ==  "Enter Biz Name, Association or Product (e.g. Books)" ? '' : urlencode(document.getElementById("searchitem").value);
	var mnp = urlencode(document.getElementById("minPrice").value);
	var mxp = urlencode(document.getElementById("maxPrice").value);
	var asc = urlencode(document.getElementById("assoc").value);
	var cat = urlencode(document.getElementById("cat").value);
	var zipcode = urlencode(document.getElementById("zipcode").value);
	var state = urlencode(document.getElementById("state").value);	
	var assoc = urlencode(document.getElementById("assoc").value);	

	document.getElementById("zipcode").value = "";
	document.getElementById("state").selectedIndex = 0;

	
	$("#right").html('<center>Searching<br/><img src="images/preloader.gif" alt=""/></center>');
	
	if(aProd != null){
		aProd.abort(); 
	}
	if(aBiz != null){
		aBiz.abort(); 
	}
	
	var xhr = $.ajax({
		   type: "GET",
		   url: "results-ajax.php",
		   data:"search="+si+"&minPrice="+mnp+"&maxPrice="+mxp+"&assoc="+assoc+"&zipcode=&state=&cat="+cat,
		   success: function(msg){
			 $("#right").html(msg);		
		   }
		 });
	
}

function changeLogoImage(url, img_id){
	
	//alert (document.getElementById(url).parentNode.parentNode.parentNode.parentNode.tagName );
	alert('logo was changed');
	if( document.getElementById(url).value == '') document.getElementById(img_id).src = 'https://www.talents2work.com/images/no-logo.jpg';
	
	document.getElementById(img_id).src = document.getElementById(url).value;
}

function strlen (string) {
    // Get string length  
    // 
    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/strlen    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Sakimori
    // +      input by: Kirk Strobeck
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman    // +    revised by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: May look like overkill, but in order to be truly faithful to handling all Unicode
    // %        note 1: characters and to this function in PHP which does not count the number of bytes
    // %        note 1: but counts the number of characters, something like this is really necessary.
    // *     example 1: strlen('Kevin van Zonneveld');    // *     returns 1: 19
    // *     example 2: strlen('A\ud87e\udc04Z');
    // *     returns 2: 3
    var str = string+'';
    var i = 0, chr = '', lgth = 0; 
    if (!this.php_js || !this.php_js.ini || !this.php_js.ini['unicode.semantics'] ||
            this.php_js.ini['unicode.semantics'].local_value.toLowerCase() !== 'on') {
        return string.length;
    } 
    var getWholeChar = function (str, i) {
        var code = str.charCodeAt(i);
        var next = '', prev = '';
        if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters)            
		if (str.length <= (i+1))  {
                throw 'High surrogate without following low surrogate';
            }
            next = str.charCodeAt(i+1);
            if (0xDC00 > next || next > 0xDFFF) {                throw 'High surrogate without following low surrogate';
            }
            return str.charAt(i)+str.charAt(i+1);
        } else if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate
            if (i === 0) {                throw 'Low surrogate without preceding high surrogate';
            }
            prev = str.charCodeAt(i-1);
            if (0xD800 > prev || prev > 0xDBFF) { //(could change last hex to 0xDB7F to treat high private surrogates as single characters)
                throw 'Low surrogate without preceding high surrogate';            }
            return false; // We can pass over low surrogates now as the second component in a pair which we have already processed
        }
        return str.charAt(i);
    }; 
    for (i=0, lgth=0; i < str.length; i++) {
        if ((chr = getWholeChar(str, i)) === false) {
            continue;
        } // Adapt this line at the top of any loop, passing in the whole string and the current iteration and returning a variable to represent the individual character; purpose is to treat the first part of a surrogate pair as the whole character and then ignore the second part        lgth++;
    }
    return lgth;
}

function slideAssoc(){
			jQuery('#header-image-container').slideUp("slow",function(){
				
				if(document.getElementById('search-form-top').innerHTML != "")
					{
						document.getElementById('show').innerHTML = '<img src="images/btn-stamp.png" alt="" id="show-img" onclick=" slideAssocHide()" /><div id="show-search"></div>';						
						document.getElementById('show-search').innerHTML = document.getElementById('search-form-top').innerHTML;
						var temp_search_value = jQuery("#search-form-top #searchitem").val();
						document.getElementById('search-form-top').innerHTML = "";
						document.getElementById('searchitem').value = temp_search_value;
						jQuery("#show").show();
					}
				
				$("#show img[title]").tooltip();
			});
}

function slideAssocHide(){
				var temp_search_value = document.getElementById('searchitem').value;
				document.getElementById('search-form-top').innerHTML = document.getElementById('show-search').innerHTML;
				document.getElementById('show-search').innerHTML = "";
	            jQuery("#show").hide();
				document.getElementById('searchitem').value = temp_search_value;
				jQuery('#header-image-container').slideDown("slow");
				jQuery('.tooltip').hide();
}

function showRequest(formData, jqForm, options) { 

} 
 
function showResponse(responseText, statusText, xhr, $form)  {
$.fancybox.close(); 

$.blockUI({ message: '<b>Your report has been sent and will be reviewed. Thank you.</b><br/><a href="javascript:$.unblockUI();">Close</a>' });
} 

