// JavaScript Document for classifieds

/* Change the sort for hitlist */
function sortHits(obj) {
	var url = obj.options[obj.selectedIndex].value;

	if (url != '')
		window.location = url;
}


/* Validet email */
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
		return false

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	   return false
	   
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false

	 if (str.indexOf(at,(lat+1))!=-1)
		return false

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false

	 if (str.indexOf(dot,(lat+2))==-1)
		return false

	 if (str.indexOf(" ")!=-1)
		return false

	 return true					
}

function ValidateForm(emailID) {

	if ((emailID.value==null)||(emailID.value=="")) {
		emailID.focus()
		return false
	}

	if (echeck(emailID.value)==false) {
		emailID.focus()
		return false
	}
		
	return true
}

function displayVolviaTopLayer() {
		// check if cookies are enabled
		document.cookie="testcookie"
		cookieEnabled = (document.cookie.indexOf("testcookie") != -1);
		
		// Check if cookie exists
		var nameEQ = "volvia=";
		var ca = document.cookie.split(';');
		
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) var layer = c.substring(nameEQ.length,c.length);
		}
		 
		if (cookieEnabled && layer != 1) {
			new topLayer('volvia', 620, 'Volvia', '<iframe border="0" frameborder="0" src="/inc/ads_layer_google.php?keyword=Volvia" style="margin: 0; padding: 0" scroll="no" scrolling="no" width="600" height="500"></iframe>', 1);
			// Get current time
			var today = new Date();
			today.setTime( today.getTime() );
			
			expires = 7 * 1000 * 60 * 60 * 24;
	
			var expires_date = new Date(today.getTime() + (expires));
			
			// Set cookie
			document.cookie = "volvia" + "=" + escape("1") + ";expires=" + expires_date.toGMTString()
		}
			

}

function displayLayer() {
	if ($("bottomLayer") && $("topLayer")) {
		var top = 0;
		
		obj = $("checkHeight");
		
		while (obj) {
			top += obj.offsetTop;
			
			obj = obj.offsetParent;
		}
		
		// check if cookies are enabled
		document.cookie="testcookie"
		cookieEnabled = (document.cookie.indexOf("testcookie") != -1);
		
		// Check if cookie exists
		var nameEQ = "volvia=";
		var ca = document.cookie.split(';');
		
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) var layer = c.substring(nameEQ.length,c.length);
		}
		
		if (cookieEnabled && layer != 1) {
			var plus = false;
			
			// Some offset haxxing if you dont have fullscreen etc. 
			// If toplayer is heigher than current viewheight we have to do some more digging
			
			var layerheight = 540;
			
			if(layerheight >= document.viewport.getHeight()) {
				
				// Check if toplayer + scrolloffset is lower than total height of webpage then just put it at the top of your screen
				if((layerheight+document.viewport.getScrollOffsets().top) <= document.viewport.getHeight()) {
					
					var topvalue = document.viewport.getScrollOffsets().top + "px";
					var topmargin = 0;
					
				}else{
					
					// Else put it at the bottom of the page
					var topmargin = (layerheight+document.viewport.getScrollOffsets().top)-document.viewport.getHeight();
					var topvalue = document.viewport.getScrollOffsets().top + "px";
					
					// Check so div doesnt get put outside the screen
					if((document.viewport.getScrollOffsets().top - topmargin) < 0) {
						topmargin = 0;	
					}
				}
				
			} else {
				// Else just put it centered on the screen
				var topvalue = "50%";
				var topmargin = Math.round(layerheight / 2) - document.viewport.getScrollOffsets().top;
				
				if(topmargin < 0) {
					topmargin = topmargin * -1;
					plus = true;
				}
			}
			
			// Display layer
			$("bottomLayer").setStyle({ display: "block", width: document.viewport.getWidth() + 'px' });
			$("topLayer").setStyle({ display: "block", top: topvalue, marginTop: (plus == false ? '-' : '') + topmargin + 'px', width: "610px" });
			$("topLayer").setStyle({ left: '50%', marginLeft: '-' + Math.round($("topLayer").offsetWidth / 2) + 'px' });
			$("contentLayer").setStyle({ width: "620px" });
			
			// Set site height on layer
			$("bottomLayer").setStyle({ height: top + "px" });
			
			// Set content
			$("contentLayer").update('<iframe border="0" frameborder="0" src="/inc/ads_layer_google.php?keyword=Volvia" scroll="no" scrolling="no" width="600" height="500"></iframe>');
			
			// Get current time
			var today = new Date();
			today.setTime( today.getTime() );
			
			expires = 7 * 1000 * 60 * 60 * 24;
	
			var expires_date = new Date(today.getTime() + (expires));
			
			// Set cookie
			document.cookie = "volvia" + "=" + escape("1") + ";expires=" + expires_date.toGMTString()
		}
	}
	
	// If close link exists
	if ($("closeLayer")) {
		// Observe link for click
		$("closeLayer").observe("click", function() {
			// Hide layers when clicking
			$("bottomLayer").setStyle({ display: "none" });
			$("topLayer").setStyle({ display: "none" });
		});
	}
}