function TrimString(sString) {
	var oRegTrim = new RegExp('^([ ]*)');
	sString = sString.replace(oRegTrim,'');
	var oRegTrim = new RegExp('([ ]*)$');
	sString = sString.replace(oRegTrim,'');
	return(sString);
}
function OpenPopUpWindow(sUrl) {
	var sWindowName = 'RAD';
	var sAttributes = 'toolbar=yes,location=yes,status=yes,resizable=yes,scrollbars=yes,menubar=yes';

	if (this.OpenPopUpWindow.arguments.length > 1) sWindowName = this.OpenPopUpWindow.arguments[1];
	if (this.OpenPopUpWindow.arguments.length > 2) {
		if (this.OpenPopUpWindow.arguments.length > 3) {
			if (this.OpenPopUpWindow.arguments[3] == true) sAttributes = this.OpenPopUpWindow.arguments[2];
				else sAttributes += ',' + this.OpenPopUpWindow.arguments[2];
		} else sAttributes += ',' + this.OpenPopUpWindow.arguments[2];
	}
	var oWin = window.open(sUrl,sWindowName,sAttributes);
	return(oWin);
}

//T1141 - ifat b - put the google search term in a cookie in order to know what the lead searched for when arriving to the site (temp_google)
if (document.referrer) {	
	var GoogleQueryString_regexp = /^http(s)?:\/\/([^\/]*google\.[^/]+)\/[^?]*\?((.+)&)?q=([^&]*)(&(.*))?$/i;
	var GMiniQueryString_regexp = /^http(s)?:\/\/(rad-google.bsky.net)\/[^?]*\?((.+)&)?q=([^&]*)(&(.*))?$/i;
	//1232 add yahoo + yandex to search term
	var YahooQueryString_regexp = /^http(s)?:\/\/([^\/]*yahoo\.[^/]+)\/[^?]*\?((.+)&)?p=([^&]*)(&(.*))?$/i;
	var YandexQueryString_regexp = /^http(s)?:\/\/([^\/]*yandex\.[^/]+)\/[^?]*\?((.+)&)?text=([^&]*)(&(.*))?$/i;
	
	//1374 add the referrer site for leads coming from Google-Ads in different sites
	var GoogleAdsURLString_regexp =/^http(s)?:\/\/([^\/]*googlesyndication\.[^/]+)\/[^?]*\?((.+)&)?url=([^&]*)(&(.*))?$/i;
	 
	if (document.referrer.match(GoogleQueryString_regexp) && !document.referrer.match(GMiniQueryString_regexp)) {	
		var GoogleQueryString = document.referrer.replace(GoogleQueryString_regexp, '$5');		
		//T1161 - ifat b - add google url to cookie
		var GoogleURLString = document.referrer.replace(GoogleQueryString_regexp, '$2')		
		//T1363 - ib
		if (GoogleQueryString != "") {
			//T1199 - the encoding was not good for utf-8 characters
			//GoogleQueryString = unescape(decodeURIComponent(escape(GoogleQueryString))).replace(";", " ");
			GoogleQueryString = GoogleQueryString.replace(";", "+");
			document.cookie = "GoogleQueryString="+GoogleQueryString+"; path=/";
			document.cookie = "GoogleURLString="+GoogleURLString+"; path=/";
			//alert(location.search);
			var WT_srch_regexp = /\?(.*&)?WT\.srch=1(&.*)?/;
			if (location.search && location.search.match(WT_srch_regexp)) {
				document.cookie = "GooglePaidCampaign=1; path=/";
			} else {
				document.cookie = "GooglePaidCampaign=0; path=/";
			}
		}
	} else if (document.referrer.match(GMiniQueryString_regexp)) {		
		var GMiniQueryString = document.referrer.replace(GMiniQueryString_regexp, '$5');		
		var GMiniURLString = document.referrer.replace(GMiniQueryString_regexp, '$2')		
		if (GMiniQueryString != "") {			
			GMiniQueryString = GMiniQueryString.replace(";", "+");
			document.cookie = "GMiniQueryString="+GMiniQueryString+"; path=/";
			document.cookie = "GMiniURLString="+GMiniURLString+"; path=/";			
		}
	} else if (document.referrer.match(YahooQueryString_regexp)) {		
		var YahooQueryString = document.referrer.replace(YahooQueryString_regexp, '$5');		
		var YahooURLString = document.referrer.replace(YahooQueryString_regexp, '$2')		
		if (YahooQueryString != "") {			
			YahooQueryString = YahooQueryString.replace(";", "+");
			document.cookie = "YahooQueryString="+YahooQueryString+"; path=/";
			document.cookie = "YahooURLString="+YahooURLString+"; path=/";			
		}
	} else if (document.referrer.match(YandexQueryString_regexp)) {		
		var YandexQueryString = document.referrer.replace(YandexQueryString_regexp, '$5');		
		var YandexURLString = document.referrer.replace(YandexQueryString_regexp, '$2')		
		if (YandexQueryString != "") {			
			YandexQueryString = YandexQueryString.replace(";", "+");
			document.cookie = "YandexQueryString="+YandexQueryString+"; path=/";
			document.cookie = "YandexURLString="+YandexURLString+"; path=/";				
			var WT_srch_regexp = /\?(.*&)?WT\.srch=1(&.*)?/;
			if (location.search && location.search.match(WT_srch_regexp)) {
				document.cookie = "YandexPaidCampaign=1; path=/";
			} else {
				document.cookie = "YandexPaidCampaign=0; path=/";
			}		
		}
	//1374 add the referrer site for leads coming from Google-Ads in different sites
	} else if (document.referrer.match(GoogleAdsURLString_regexp)) {	
		var GoogleAdsURLSite = document.referrer.replace(GoogleAdsURLString_regexp, '$5');
		GoogleAdsURLSite = unescape(GoogleAdsURLSite)
		if (GoogleAdsURLSite.lastIndexOf("http") != -1) {
			GoogleAdsURLSite = GoogleAdsURLSite.substring(7);
		}
		if (GoogleAdsURLSite.lastIndexOf("/") == GoogleAdsURLSite.length-1){
			GoogleAdsURLSite = GoogleAdsURLSite.substring(0,GoogleAdsURLSite.length-1);
		}
		GoogleAdsURLSite = escape(GoogleAdsURLSite)
		document.cookie = "GoogleAdsURLSite="+GoogleAdsURLSite+"; path=/";
		var WT_srch_regexp = /\?(.*&)?WT\.srch=1(&.*)?/;
		if (location.search && location.search.match(WT_srch_regexp)) {
			document.cookie = "GooglePaidCampaign=1; path=/";
		} else {
			document.cookie = "GooglePaidCampaign=0; path=/";
		}
	}	
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////


var SecuredCategory = new Array;
function GotoCategory(CategoryID, ExternalUrl,IsNewWindow,Width,Height,IsToolbarDisplayed,isScrollbars,isStatus,isResizable) {
	if (ExternalUrl != null) {
		if ('' == ExternalUrl) {
			document.cookie = "PRMfromRAD=1";
		}
		if(IsNewWindow==1) {
			var sWinName = "" ;
			var overWrite = true ;
			var sAttributes = "width="+Width+",height="+Height ;
			if(IsToolbarDisplayed) sAttributes += ",location=yes,toolbar=yes,menubar=yes,titlebar=yes" ;
			else sAttributes += ",location=yes,toolbar=no" ;
			
			//if(isScrollbars)  sAttributes += ",scrollbars=yes" ;
			//else sAttributes += ",scrollbars=no" ;
			sAttributes += ",scrollbars=yes" ;
			
			//if(isStatus)  sAttributes += ",status=yes" ;
			//else sAttributes += ",status=no" ;
			sAttributes += ",status=yes"
			
			//if(isResizable)  sAttributes += ",resizable=yes" ;
			//else sAttributes += ",resizable=no" ;
			sAttributes += ",resizable=yes"
			
			OpenPopUpWindow(ExternalUrl,sWinName,sAttributes,overWrite) ;
		} else window.location = ExternalUrl;
	} else {
		if(IsNewWindow==1) {
			var sWinName = "" ;
			var overWrite = true ;
			var sAttributes = "width="+Width+",height="+Height ;
			if(IsToolbarDisplayed) sAttributes += ",location=yes,toolbar=yes" ;
			else sAttributes += ",location=yes,toolbar=no" ;
			if(isScrollbars)  sAttributes += ",scrollbars=yes" ;
			else sAttributes += ",scrollbars=no" ;
			if(isStatus)  sAttributes += ",status=yes" ;
			else sAttributes += ",status=no" ;
			if(isResizable)  sAttributes += ",resizable=yes" ;
			else sAttributes += ",resizable=no" ;
			OpenPopUpWindow('/Home/0,6583,' + CategoryID + ',00.html',sWinName,sAttributes,overWrite) ;
		} else {
			window.location = '/Home/0,6583,' + CategoryID + ',00.html';
		}
   }
}

function OpenAlertWindow(sMessage) {
	var oErrorMessage = new Object();
	oErrorMessage.Text = sMessage;
	if (this.OpenAlertWindow.arguments.length > 1) var iAlertType = 2
	else var iAlertType = 1;
	var arrMessage = sMessage.split('<br>');
	sMessage = arrMessage.join('\n');
	alert(sMessage);
	//return (window.showModalDialog('/Api/CdaApiAlertMessages/1,10856,,00.html?LanguageID=' + iCurrentLanguageID + '&winType=' + iAlertType,oErrorMessage,'dialogHeight:300px;dialogWidth:500px;center:yes;help:no;resizable:no;status:no;scroll:0'));
}
function CheckSearchText(sErrorText) {
	var bReturn = true;
	oForm = event.srcElement;
	sString = TrimString(oForm.SearchString.value);
	var oRegTrim = new RegExp('[%\_]+');
	sString = sString.replace(oRegTrim,'');
	if (sString == '') {
		OpenAlertWindow(sErrorText);
		event.returnValue = false;
		bReturn = false;
	}
	return(bReturn);
}
