var isInternetExplorer = (navigator.appName.indexOf("Microsoft") != -1);

function countSpaces(obj){
	var iLength = obj.value.length;
	var strSpaces = obj.value.match(new RegExp("( )", "g"));
	var countSpaces = strSpaces ? strSpaces.length : 0;
	return countSpaces;
}

function countLineBreaks(obj){
	var iLength = obj.value.length;
	var strLineBreaks = obj.value.match(new RegExp("(\\n)", "g"));
	var countLineBreaks = strLineBreaks ? strLineBreaks.length : 0;
	return countLineBreaks;
}

function textCounter(field, counter_field, maxlimit) {
	var lineBreaks = countLineBreaks(field);
	var adjust = isInternetExplorer ? 1 : 0;
	if (field.value.length - lineBreaks * adjust > maxlimit){
		field.value = field.value.substring(0, maxlimit + lineBreaks * adjust);
		field.focus();
	} else {
		counter_field.value = maxlimit - field.value.length + lineBreaks * adjust;
	}
}

function backToSection(backToURL, forceBackToURL){
	if(forceBackToURL == null) forceBackToURL = false;
	if(history.length > 1 && !forceBackToURL) history.back(); else window.location.href = backToURL;
}

function hideStatus() {
	window.defaultStatus='';
	window.status='';
	return true;
}

function searchReset() {
	tot = document.search_form.elements.length;
	for(i=0;i<tot;i++) {
		if (document.search_form.elements[i].type == 'text') {
			document.search_form.elements[i].value = "";
		} else if (document.search_form.elements[i].type == 'checkbox' || document.search_form.elements[i].type == 'radio') {
			document.search_form.elements[i].checked = false;
		} else if (document.search_form.elements[i].type == 'select-one') {
			document.search_form.elements[i].selectedIndex = 0;
		}
	}
	if ((document.search_form.country_id) || (document.search_form.state_id) || (document.search_form.region_id) || (document.search_form.city_id) || (document.search_form.area_id)) {
		searchLocationReset();
	}
}

function easyFriendlyUrl(name2friendlyurl, target, validchars, separator) {
	var str = "";
	var i;
	var exp_reg = new RegExp("[" + validchars + separator + "]");
	var exp_reg_space = new RegExp("[ ]");
	name2friendlyurl.toString();
	name2friendlyurl = name2friendlyurl.replace(/^ +/, "");
	for (i=0 ; i<name2friendlyurl.length; i++) {
		if (exp_reg.test(name2friendlyurl.charAt(i))) {
			str = str+name2friendlyurl.charAt(i);
		} else {
			if (exp_reg_space.test(name2friendlyurl.charAt(i))) {
				if (str.charAt(str.length-1) != separator) {
					str = str + separator;
				}
			}
		}
	}
	if (str.charAt(str.length-1) == separator) str = str.substr(0, str.length-1);
	document.getElementById(target).value = str.toLowerCase();
}

function searchLocationReset() {
	if (document.search_form.country_id) {
		if (document.search_form.state_id) {
			while (document.search_form.state_id.options.length>1) {
				deleteIndex=document.search_form.state_id.options.length-1;
				document.search_form.state_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.region_id) {
			while (document.search_form.region_id.options.length>1) {
				deleteIndex=document.search_form.region_id.options.length-1;
				document.search_form.region_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length>1) {
				deleteIndex=document.search_form.city_id.options.length-1;
				document.search_form.city_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	} else if (document.search_form.state_id) {
		if (document.search_form.region_id) {
			while (document.search_form.region_id.options.length>1) {
				deleteIndex=document.search_form.region_id.options.length-1;
				document.search_form.region_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length>1) {
				deleteIndex=document.search_form.city_id.options.length-1;
				document.search_form.city_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	} else if (document.search_form.region_id) {
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length>1) {
				deleteIndex=document.search_form.city_id.options.length-1;
				document.search_form.city_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	} else if (document.search_form.city_id) {
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	}
}

function $(id) {
	return document.getElementById(id);
}

function showText(text) {
	return unescape(text);
}

function displayMap() {
	var index, totalamount=10;
	if ($.cookie('showMap') == 1) {
		$('#resultsMap').css('display', '');
		$('#linkDisplayMap').text('' + showText(LANG_JS_LABEL_HIDEMAP) + '');
		for (index=1; index<=totalamount; index++) {
			if (document.getElementById('summaryNumberID'+index)) {
				document.getElementById('summaryNumberID'+index).className = 'summaryNumber isVisible';
			}
		}
		$.cookie('showMap', '0', { expires: 7, path: '/'});
		initialize();
	} else {
		$('#resultsMap').css('display', 'none');
		$('#linkDisplayMap').text('' + showText(LANG_JS_LABEL_SHOWMAP) + '');
		for (index=1; index<=totalamount; index++) {
			if (document.getElementById('summaryNumberID'+index)) {
				document.getElementById('summaryNumberID'+index).className = 'summaryNumber isHidden';
			}
		}
		$.cookie('showMap', '1', { expires: 7, path: '/'});
	}
}

function deselectAll() {
    var v1 = '';
    var v2 = '';

    v1 = Get_Cookie('array_listing_id_compare');
    if (v1!=null){   
        var array_listings = v1.split(';');    
        var x=0;
        while (x < array_listings.length) {
             if(array_listings[x]!=''){
                 v = v1.replace(array_listings[x],'');            
                 if (document.getElementById('imgListingCompare'+array_listings[x]))
                    document.getElementById('imgListingCompare'+array_listings[x]).src = DEFAULT_URL + '/images/checkbox_uncheck.gif';    
             }
             x++;
        }
        Set_Cookie('array_listing_id_compare', '', '', '/find-addiction-treatment', '', '');
    }
         
    v2 = Get_Cookie('array_listing_id_compare_sp');         
    if (v2!=null){   
        var array_listings = v2.split(';');    
        var x=0;
        while (x < array_listings.length) {
             if(array_listings[x]!=''){
                  v = v2.replace(array_listings[x],'');
                 if (document.getElementById('imgListingCompare'+array_listings[x]))
                    document.getElementById('imgListingCompare'+array_listings[x]).src = DEFAULT_URL + '/images/checkbox_uncheck.gif';    
             }
             x++;
        }
        Set_Cookie('array_listing_id_compare_sp', '', '', '/find-addiction-treatment', '', '');
    } 
    v3 = Get_Cookie('array_listing_id_compare_vendor');         
    if (v2!=null){   
        var array_listings = v2.split(';');    
        var x=0;
        while (x < array_listings.length) {
             if(array_listings[x]!=''){
                  v = v2.replace(array_listings[x],'');
                 if (document.getElementById('imgListingCompare'+array_listings[x]))
                    document.getElementById('imgListingCompare'+array_listings[x]).src = DEFAULT_URL + '/images/checkbox_uncheck.gif';    
             }
             x++;
        }
        Set_Cookie('array_listing_id_compare', '', '', '/find-addiction-treatment', '', '');
    }     
                              
}

function SelectToCompare(ListingId,ListingType){
    var v = '';
    var exists=false;
    var ct = 0;
    var block = false;
    
    if(ListingType==1) {
        v = Get_Cookie('array_listing_id_compare');
    }
    
    if(ListingType==2) {
        v = Get_Cookie('array_listing_id_compare_sp');
    }
    
    if(ListingType==3) {
        v = Get_Cookie('array_listing_id_compare_vendor');
    }    
    
    if (v!=null){   
        var array_listings = v.split(';');    
        var x=0;
        while (x < array_listings.length && exists==false) {
             if(array_listings[x]!=''){
                 ct++;
                 if(array_listings[x]==ListingId){
                    exists=true;
                 }
             }
             x++; 
        }
        if(ct > 4){
           block=true;
        } 

    }
    
    
    if(exists==false){  
        if(block==true){
            alert('Please select up to 5 Listings to compare!');
            return false;
        }
                
        if (v!=null){
            if (v.length > 0){ 
                v = v + ';' + ListingId; 
            }else{
                v = ListingId;
            }
        }else{
            v = ListingId;
        }
        if (document.getElementById('imgListingCompare'+ListingId)) {
            document.getElementById('imgListingCompare'+ListingId).src = DEFAULT_URL + '/images/checkbox.gif';
        }
    }else{
        v = v.replace(ListingId,'');
        if (document.getElementById('imgListingCompare'+ListingId)) {
            document.getElementById('imgListingCompare'+ListingId).src = DEFAULT_URL + '/images/checkbox_uncheck.gif';
        }
    } 
    if(ListingType==1) {
        Set_Cookie('array_listing_id_compare', v, '', '/find-addiction-treatment', '', '');
    }
    
    if(ListingType==2) {
        Set_Cookie('array_listing_id_compare_sp', v, '', '/find-addiction-treatment', '', '');
    }
    if(ListingType==3) {
        Set_Cookie('array_listing_id_compare_vendor', v, '', '/find-addiction-treatment', '', '');
    }    
    
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
    var today = new Date();    today.setTime( today.getTime() );
    if ( expires ){    expires = expires * 1000 * 60 * 60 * 24;}
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';    var cookie_name = '';
    var cookie_value = '';    var b_cookie_found = false; 
    for ( i = 0; i < a_all_cookies.length; i++ ){
        a_temp_cookie = a_all_cookies[i].split( '=' );
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
        if ( cookie_name == check_name ){
            b_cookie_found = true;
            if ( a_temp_cookie.length > 1 )    {cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );    }
            return cookie_value;
            break;}
        a_temp_cookie = null;
        cookie_name = '';}if ( !b_cookie_found ){    return null;}
}

function OnlyNumber(f,typef){
    if(typef=='number'){ var digits=",.0123456789";}
    if(typef=='date'){ var digits="/0123456789";}
    var f_temp; 
    for (var i=0;i<f.value.length;i++){
      f_temp=f.value.substring(i,i+1);       
      if (digits.indexOf(f_temp)==-1){
            f.value = f.value.substring(0,i);
            break;
      }
    }
}

function CheckMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    mail.value = mail.value.toLowerCase();
    if(mail.value!=''){
        if(er.test(mail.value)){ 
            return true; 
        }else{ 
            alert("E-Mail is incorrect"); 
            mail.focus();
            return false;
        }
    }else{
        return true;
    }
}

function CheckURL(url){
    url.value = url.value.toLowerCase();
    if(url.value!=''){
        urlRegExp = /^(((ht|f)tp(s?))\:\/\/)([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(\/\S*)?$/
        if(urlRegExp.test(url.value)==false){ 
            alert("URL is incorrect"); 
            url.focus();
            return false; 
        }else{
            return true;
        }
    }else{
        return true;
    }
}


