//
// xCore 2 main.js File
//
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,yPos;

String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};

String.prototype.repeat = function(l){
	return new Array(l+1).join(this);
};

String.prototype.change = function(indeks, znak){
	 return this.substr(0, indeks) + znak + this.substr(indeks + 1);
};

//
// Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/
//
function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 						= "an unknown operating system";
	}
}

//
// Helper function for getBrowserInfo()
//
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

getBrowserInfo(); // get browser & OS name

//
// Set page scroll to x,y
//
function setScroll(x, y) {
	window.scrollTo(x, y); 
}

// 
// Get HTML object by ID
//
function getId(id) {
	itm = null;
	
	if (document.getElementById) {
		itm = document.getElementById(id);
	} else if (document.all) {
		itm = document.all[id];
	} else if (document.layers) {
		itm = document.layers[id];
	}
	return itm;
}


// 
// Get Cookie
// 
function getCookie( name )
{
	cname = x_cookie_prefix + name + '=';
	cpos  = document.cookie.indexOf( cname );
	
	if ( cpos != -1 ) {
		cstart = cpos + cname.length;
		cend   = document.cookie.indexOf(";", cstart);
		if (cend == -1) {
			cend = document.cookie.length;
		}
		return unescape( document.cookie.substring(cstart, cend) );
	}
	
	return null;
}

// 
// Set cookie 
// 
function newCookie( name, value, sticky )
{
	name 	= x_cookie_prefix + name;
  	domain 	= "";
	path   	= "";
	
	if ( x_cookie_path != "" ) {
		path = x_cookie_path;
	}
  	if ( x_cookie_domain != "" ) {
		domain =  x_cookie_domain;
	}
	
  var cookie = (name + "=" + escape(value));
  if (sticky) {
    cookie += "; expires=Wed, 1 Jan 2020 00:00:00 GMT"
  }
  cookie += (path    ? "; path="+path : "");
  cookie += (domain  ? "; domain="+domain : "");
  document.cookie = cookie;
}

// 
// Create new PopUp window
// 
function newPopWindow(url, name, width,height,center,resize,scroll,posleft,postop)
{
	showx = "";
	showy = "";
	
	if (posleft != 0) { X = posleft }
	if (postop  != 0) { Y = postop  }
	
	if (!scroll) { scroll = 1 }
	if (!resize) { resize = 1 }
	
	if ((parseInt (navigator.appVersion) >= 4 ) && (center)) {
		X = (screen.width  - width ) / 2;
		Y = (screen.height - height) / 2;
	}
	
	if ( X > 0 ) {
		showx = ',left='+X;
	}
	
	if ( Y > 0 ) {
		showy = ',top='+Y;
	}
	
	if (scroll != 0) { scroll = 1 }
	
	var Win = window.open( url, name, 'width='+width+',height='+height+ showx + showy + ',resizable='+resize+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no');
}

// 
// Invert ALL checkboxes selection in form
// 
function invertCheckboxes(form) {
    form = document.forms[form];
    
    for(i=0; i< form.elements.length; i++) {
        if(form.elements[i].type=='checkbox') {
            if(form.elements[i].checked == true)
            form.elements[i].checked = false;
            else
            form.elements[i].checked = true;
        }
    }
}

//
// Select ALL checkboxes in form
//
function selectAllCheckboxes(form) {
    form = document.forms[form];
    
    for(i=0; i< form.elements.length; i++) {
        if(form.elements[i].type=='checkbox') form.elements[i].checked = true;
    }
}

//
// Unselect ALL checkboxes in form
//
function unselectAllCheckboxes(form) {
    form = document.forms[form];
    
    for(i=0; i< form.elements.length; i++) {
        if(form.elements[i].type=='checkbox') form.elements[i].checked = false;
    }
}

//
// change Image id=objectID src value to obj
//
function changeSrc(src, obj)
{
	var object = getId( obj );

	if (object) {
		object.src = src;
	}
}

//
// switch Block visibility
//
function switchBlock ( token )
{
	switchBox( token );

	var block = getCookie( token );

	if ( block == 1 ) {
		newCookie(token, 0, 1);
		changeSrc(block_close, token + '_icon');
	} else {
		newCookie(token, 1, 1);
		changeSrc(block_open, token + '_icon');
	}
}

//
// check Block visibility
//
function checkBlock ( token )
{
	var block = getCookie( token );
	var lock  = 0;
	
	if ( block == 1 ) {
		switchBox( token , true );
		changeSrc(block_open, token+'_icon');
	} else {
		if(lock == 0)
			changeSrc(block_close, token+'_icon');
	}
}

//
// switch Div visibility
//
function switchBox(obj, nojquery) {
	if(nojquery) {
		// At this time we dont have loaded jquery
		// pure JS
		var curr = getId(obj).style.display;
		if(curr == 'block') {
			getId(obj).style.display = 'none';
		} else {
			getId(obj).style.display = 'block';
		}
	} else {
		if(!$j("#" + obj)) {
			alert('Structure error: #' + obj + ' undefined');
			return;
		}
		$j("#" + obj).slideToggle("slow");
	}
}

//
// show Div
//
function showBox(obj)
{
	if(!$j("#" + obj)) {
		alert('Structure error: #' + obj + ' undefined');
		return;
	}
	$j("#" + obj).show("slow");
}

//
// hide Div
//
function hideBox(obj)
{
	if(!$j("#" + obj)) {
		alert('Structure error: #' + obj + ' undefined');
		return;
	}
	$j("#" + obj).hide("slow");
}

//
// Points add/remove JS ( USR editor FORMS)
//
function pointsMinus( id ) {
	var POLE = document.getElementById( id );

	if(parseInt(POLE.value) > 0) {
		POLE.value = parseInt(POLE.value)-1;
	}
}
//
// Point + 1
//
function pointsPlus( id ) {
	var DODAJ 	= 1;
	var POLE 	= document.getElementById( id );
	POLE.value 	= parseInt(POLE.value)+DODAJ;
}

//
// Point Check is int ?
//
function pointsCheck( id ) {
	var value = document.getElementById( id ).value;
	if(value != '') {
		var result = parseInt(value);
		if(isNaN(result)) {
			alert(x_locale_main_int);
			return false;
		}
		
	}
	return;
}

//
// Confirm info ?
//
function confirmInfo (obj, INFO) {
	INFO = INFO.replace(/\<br\/\>/gi, "\n");
	INFO = INFO.replace(/\<br \/\>/gi, "\n");
	INFO = INFO.replace(/\<br\>/gi, "\n");

	
	if (confirm(INFO)) {
		window.location.href=obj.href;
	} else {
		return false;
	}
}

function confirmInfoMulti(form, INFO1, INFOM) {
	
	form = document.forms[form];
	selected = 0;
	
    for(i=0; i< form.elements.length; i++) {
        if(form.elements[i].type=='checkbox') {
            if(form.elements[i].checked == true)
            	selected++;
        }
    }
    if(selected > 1) {
		INFO = INFOM.replace(/\<br\/\>/gi, "\n");
		INFO = INFO.replace(/\<br \/\>/gi, "\n");
		INFO = INFO.replace(/\<br\>/gi, "\n");
    } else {
    	INFO = INFO1.replace(/\<br\/\>/gi, "\n");
		INFO = INFO.replace(/\<br \/\>/gi, "\n");
		INFO = INFO.replace(/\<br\>/gi, "\n");
    }
 
	if (selected > 0 && confirm(INFO)) {
		return true;
	} else {
		return false;
	}
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
 	var de = document.documentElement;
  	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;

	arrayPageSize = new Array(w,h,windowWidth,windowHeight) 
	return arrayPageSize;
}

function doAJAXdstCorrection() {
	$j.ajax({
	type: "POST",
	url: x_base_url + '' + x_module_access + "=mcp&" 
			 + x_action_access + "=dstautocorrect&ajax=1",
	cache: false,
	success: function(html){
		
	}});
}

