﻿var Site=new Hash({
	init:function () {

		// finds each element in the page with class="blank" and makes it a link to a new window
		$$(".pageWrap a.blank").each(
				function(item){
					item.addEvent('click', function(e){
						e = new Event(e);
						window.open(item.href);
						e.stop();
						});
				}
		)
		
		//Builds the dropdown menu
		if($("PageTop")){
			var menu = $("PageTop").getElements(".menu-category");
			menu.each(function(menuCat){
				var dropdown = menuCat.getElement(".dropdown");
				if(dropdown){
					menuCat.addEvent("mouseenter",function(e){
						menuCat.addClass("hover");
					});
					menuCat.addEvent("mouseleave",function(e){
						menuCat.removeClass("hover");
					});
					var container = dropdown.getParent('li');
					var size = dropdown.getSize().x;
					var contSize = container.getSize().x;
					if(contSize + 10 > size) size = contSize + 10;
					
					dropdown.getElement('.tab').setStyle('width',contSize + "px");
					dropdown.setStyle('width',size+'px');
					
					var height = dropdown.getSize().y;
					if(height%2==1) height = height + 1;
					dropdown.setStyle('height',height+'px');
				}
			});
		}

		if (typeof($("PageMiddle").hideFocus)!="undefiend"){
			// test for ie property
			$$(".pageWrap .hidefocus").each(
				function(item){
					item.hideFocus = "hideFocus";
				}
			)
		}

		if(typeof(ValidationSummaryOnSubmit)!="undefined" && ValidationSummaryOnSubmit.toString){
			// disable scroll and rename the function to avoid scope problems
			var rep = ValidationSummaryOnSubmit.toString().replace(/window.scrollTo/g,"//window.scrollTo").replace(/ValidationSummaryOnSubmit/,"ValidationSummaryOnSubmit_NEW");
			eval(rep);
			ValidationSummaryOnSubmit=ValidationSummaryOnSubmit_NEW;
			//alert(ValidationSummaryOnSubmit);

		}

		if (typeof(breadcrumbAddition)!="undefined" && $("Breadcrumbs")){
			//$("Breadcrumbs").innerHTML += " : " + breadcrumbAddition;
		}

		// there are pages without the header
		if($("txtTopSearch")){
			$("txtTopSearch").addEvent("keydown",function(e){
				e = new Event(e);
				if (e.key=="enter"){
					e.stop();
					SendSearch();
				}
			});
			if (typeof(searchedWord)=="undefined"){
				$("txtTopSearch").value=  "Search";
				$("txtTopSearch").addEvent("focus",function(e){
					if ($("txtTopSearch").value=="Search")$("txtTopSearch").value="";
				});
				$("txtTopSearch").addEvent("blur",function(e){
					if ($("txtTopSearch").value=="")$("txtTopSearch").value="Search";
				});
			}else{
				$("txtTopSearch").value=  searchedWord;
			}

			$("btTopSearch").addReplacingEvent("click", SendSearch);

		}
		if (location.search.indexOf("debug")>-1)AutoFillForm();
	},
	QS:function(name){
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
		var regexS = "[\\?&]"+name+"=([^&#]*)";  
		var regex = new RegExp( regexS ); 
		var results = regex.exec( window.location.href );  
		if( results == null ) return "";  else  return results[1];
	}
});
$DL(Site.init);

function SendSearch(){
	var value = $("txtTopSearch").value.replace(/^\s*/, "").replace(/\s*$/, "");	
	self.location = "search.aspx?q="+encodeURIComponent(value);
}

function AutoFillForm(){

	$$("input[type=text]").each(function(input){
		input.value = input.id.substr(input.id.lastIndexOf("_")+1);
		if (input.name.toLowerCase().indexOf("email")>-1)input.value="test@test.com";
	});

	$$("textarea").each(function(input){
		input.value = input.id.substr(input.id.lastIndexOf("_")+1) + "\nline2";
	});


}

var Validator = {
	IsEmail:function(s){
		var result = /^[a-z0-9\-\._]*@{1}[a-z0-9\-_]+\.+[a-z0-9\._-]{2,}$/i.test(s);
		return result;
	},

	IsPassword:function(s){
		return /^[a-z0-9-_]{8,15}$/i.test(s);
	},

	IsAlphaNumeric:function(s){
		return /^[a-z0-9-_ \.]+$/i.test(s);
	},

	IsDigits:function(s){
		return /^[0-9]*$/.test(s);
	}
}

function DownloadFile(url, ext, requiresLogin) {
   

	var downloadUrl = "http://" + location.hostname + Config.RootUrl + "DownloadFile.aspx?file="+encodeURIComponent(url);
	//only protected files are encoded, so before check if the user is logged	
	if (requiresLogin && !UserManager.IsLogged){
	    UserManager.ShowRegisterForm(downloadUrl);
		Mantis.Optier.Services.PublicService.RegisterDownloadFileClick(url);
		return;//stop the download
	}
	switch(ext){
		case "ZIP":
		case "PNG":
		case "JPG":
		case "GIF":
		case "DOC":
		case "XLS":
		case "PPT":
				location.href=downloadUrl;
			break;
		default:	
			var downloadWin = window.open(downloadUrl);	
	}
		
}

function scrollTop(){

	window.scrollTo(0,0);

}
