String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function addEvent(obj, evType, fn, useCapture){
  if (obj == null)
    return false;
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent('on' + evType, fn);
    return r;
  } else {
    //alert('Handler could not be attached');
  }
}

function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent(evType, fn);
    return r;
  } else {
    //alert('Handler could not be removed');
  }
}

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function cancelEvent(e){
	if(!e) var e = window.event;
	try{
		e.returnValue = false;
		e.cancelBubble = true;
	}catch(err){}
	try{
		//e.cancelBubble is supported by IE - this will kill the bubbling process.
		if(e.returnValue){
			e.returnValue = false;
		}
		if(e.cancelBubble){
			e.cancelBubble = true;
		}
	}catch(err){}
	try{
		//e.stopPropagation works only in Firefox.
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
	}catch(err){}
}

function getEventTarget(e){
	var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return null;
    return target;
}

function openPopupWindow(url, features, replace, title) {
    if( features == null ) {
        features = "scrollbars=0";
    }
    if( title == null ) {
        title = "seek4_popup";
    }
	//return window.open(url, "seek4_popup", features, replace);
	var hWnd = window.open(url, title, features, replace);
	return hWnd;
}

function getSelectedValue( htmlSelect )
{
    return htmlSelect.options[htmlSelect.selectedIndex].value;
}

/* XML manipulation */
function parseToXML(text)
{
    var xmlDoc;
    if (window.ActiveXObject) // IE
    {
        var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(text);
    }
    else if (document.implementation && document.implementation.createDocument) // Mozilla, Firefox, Opera, etc.
    {
        var parser = new DOMParser();
        xmlDoc = parser.parseFromString(text,"text/xml");
    }
    
    return(xmlDoc);
}

function getCurrentForm(e){
	var obj=getEventTarget(e);
	while(obj.tagName.toLowerCase() != 'form'){
		if(obj.tagName.toLowerCase() == 'body'){
			return null;
		}
		obj = obj.parentNode;
	}
	if(obj.tagName.toLowerCase() != 'form'){
		obj = null;
	}
	return obj;
}
function submitCurrentForm(e){
	cancelEvent(e);
	var obj = getCurrentForm(e);
	obj.submit();
}

function redirect(url){
	window.location = url;
}

/* String functions */
function trim( str ) {
   return str.replace(/^\s*|\s*$/g,"");
}

// Original: Ryan Sokol.  Modified:  Ronnie T. Moore. Editor Web Site: The JavaScript Source 
// This script and many more are available free online at The JavaScript Source http://javascript.internet.com 
// Begin
function checkInt(str){
	if (!str) return 0;
	var ok = "";
	var i = 0
	var l = str.length;
	for (i; i < l; i++) {
		var ch = str.substring(i, i+1);
		if ((ch < "0" || "9" < ch) && (i==0 && ch != '-' && ch != '+')){ 
			alert("Only integer input is allowed!\n\n" 
				+ parseInt(ok) + " will be used because '" 
				+ str + "' is invalid.\nYou may correct "
				+ "this entry and try again.");
			return parseInt(ok);
		} else {ok += ch;}
	}
	return parseInt(str);
}

function checkDecimal(str) {
	if (!str) return 0;
	var ok = "";
	var i = 0
	var l = str.length;
	for (i; i < l; i++) {
		var ch = str.substring(i, i+1);
		if ((ch < "0" || "9" < ch) && ch != '.' && (i == 0 && ch != '-' && ch != '+')) {
			alert("Only numeric input is allowed!\n\n" 
				+ parseFloat(ok) + " will be used because '" 
				+ str + "' is invalid.\nYou may correct "
				+ "this entry and try again.");
			return parseFloat(ok);
		}else{ok += ch;}
	}
	return str;
}

function makeHours(hour, min, sec){
	return (hour + min/60 + sec/3600);
}

function makeTime(num){
	if(num){
		var hour = parseInt(num);
		num -= parseInt(num); 
		num *= 60;
		var min = parseInt(num);
		num -= parseInt(num); 
		num *= 60;
		var sec = parseInt(num);
		return {Hour:hour, Minute:min, Second:sec};
	}
}
//  End -->
function loadIframe(id,url,useTimeout){
	if(useTimeout == true){
		setTimeout("loadIframe('"+id+"','"+url+"',false);",200);
	}else{
		try{
			$(id).src=url;
		}catch(e){}
	}
}
function loadContent(id,content,useTimeout){
	if(useTimeout == true){
		content = new String(content);
		content = content.replace(/'/g,"\\'");
		setTimeout("loadContent('"+id+"','"+content+"',false);",200);
	}else{
		try{
			$J('#' + id).append(content);
		}catch(e){}
	}
}

// querystring
function Querystring(qs) {
	this.params = {};
	if (qs == null){
	    qs = location.search.substring(1, location.search.length);
	    this.url = location.pathname;
	}else{
	    var args = qs.split('?');
	    this.url = args[0];
	    qs = args[1] || "";
	}
	if (qs.length == 0) return;
	qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&');
    for (var i = 0; i < args.length; i++){
	    var pair = args[i].split('=');
	    var name = decodeURI(pair[0]);
	    var value = (pair.length==2)
		    ? decodeURI(pair[1])
		    : name;
	    this.params[name] = value;
    }
}
Querystring.prototype.get = function(key, default_){
	var value = this.params[key];
	return (value != null) ? value : default_;
}
Querystring.prototype.set = function(key, value){
	this.params[key] = value;
}
Querystring.prototype.toString = function(flag){
    var res="";
    for (var i in this.params){res += i + "=" + this.params[i] + "&";}
	if (!flag) res=this.url+"?"+res;
	return res.substring(0, res.length - 1);
}
function AjustHeight(jQueryObj1, jQueryObj2){
	if(jQueryObj1 != null && jQueryObj2 != null){
		if(jQueryObj1.height() < jQueryObj2.height()){
			jQueryObj1.height(jQueryObj2.height());
		} else {
			jQueryObj2.height(jQueryObj1.height());
		}
	}
}

function getScroll(){
		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		return {left: scrOfX, top: scrOfY };

    }
    
function setScrollX(left){
	if( typeof( window.pageXOffset ) == 'number' ) {
		window.pageXOffset = left;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		document.body.scrollLeft = left;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		document.documentElement.scrollLeft =left;
	}
}
function setScrollY(top){
	if( typeof( window.pageYOffset ) == 'number' ) {
		var x = getScroll().left;
		window.scrollTo(x, top);
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		document.body.scrollTop = top;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		document.documentElement.scrollTop = top;
	}
}

function getClientSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return {width : myWidth, height: myHeight};
}

function IsNumeric(sText){
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++){ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1){IsNumber = false;}
	}
	return IsNumber;
}

function initExternalLinks() { //find all elements <a class="external"> for start PopWin()
	$J("a.external").bind("click", function (e){
		e.preventDefault();
		doTrack('/redirect/'+ $J(this).attr("href"));
		var newwin = window.open($J(this).attr("href"));
		return !newwin;
	});
//	$J("a.external-sex-blogs").bind("click", function (e){
//		e.preventDefault();
//		doTrack('/redirect/sex-blogs/'+ $J(this).attr("href"));
//		var newwin = window.open($J(this).attr("href"));
//		return !newwin;
//	});
}

var checkChars = {
    illegalSymbols: ['~', '=', ',', '.', ';', ':', '?', '+', '{', '}', '[', ']', '(', ')', '<', '>', '!', '@', '#', '$', '%', '^', '&', '*', '\'', '\\', '/', '|', '"', '£', '½', '§', '`', '´', '`', '№'], // "~=,.;:?+{}[]()<>!@#$%^&*'\\|/\"£½§`´`№"
    isCharValid : function(string) {
        for (ch in checkChars.illegalSymbols){
            if (string.indexOf(checkChars.illegalSymbols[ch]) > -1) {
                return false;
            }
        }
        return true;
    },
    
    checkDeleteSymbols: function(keyCode){
        if (keyCode == 46 // Del
            || keyCode == 8 // Backspace
            ) { return true; } 
        return false;
    },
    
    checkControlSymbols: function(keyCode){
        if (keyCode == 37 // Arrow
            || keyCode == 38 // Arrow 
            || keyCode == 39 // Arrow
            || keyCode == 40 // Arrow
            || keyCode == 27 // Esc
            || keyCode == 13 // Enter
            || keyCode == 18 // Alt
            || keyCode == 17 // Ctrl
            || keyCode == 16 // Shift
            || keyCode == 9 // 
            || keyCode == 35 // End
            || keyCode == 36 // Home
            || keyCode == 33 // PageUp
            || keyCode == 34 // PageDown
            || keyCode == 45 // Insert
            || keyCode == 112 // F1
            || keyCode == 113 // F2
            || keyCode == 114 // F3
            || keyCode == 115 // F4
            || keyCode == 116 // F5
            || keyCode == 117 // F6
            || keyCode == 118 // F7
            || keyCode == 119 // F8
            || keyCode == 120 // F9
            || keyCode == 121 // F10
            || keyCode == 122 // F11
            || keyCode == 123 // F12
            || keyCode == 44 // PrtScr
            || keyCode == 144 // NumLock
            || keyCode == 145 // ScrollLock
            || keyCode == 19 // Pause/Break
            ) { return true; } 
        return false;
    }
}

//$J(document).ready(function(){initExternalLinks();});

    /**
    *
    *  URL encode / decode
    *  http://www.webtoolkit.info/
    *
    **/
     
    var Url = {
     
	    // public method for url encoding
	    encode : function (string) {
		    return escape(this._utf8_encode(string));
	    },
     
	    // public method for url decoding
	    decode : function (string) {
		    return this._utf8_decode(unescape(string));
	    },
     
	    // private method for UTF-8 encoding
	    _utf8_encode : function (string) {
		    string = string.replace(/\r\n/g,"\n");
		    var utftext = "";
     
		    for (var n = 0; n < string.length; n++) {
     
			    var c = string.charCodeAt(n);
     
			    if (c < 128) {
				    utftext += String.fromCharCode(c);
			    }
			    else if((c > 127) && (c < 2048)) {
				    utftext += String.fromCharCode((c >> 6) | 192);
				    utftext += String.fromCharCode((c & 63) | 128);
			    }
			    else {
				    utftext += String.fromCharCode((c >> 12) | 224);
				    utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				    utftext += String.fromCharCode((c & 63) | 128);
			    }
     
		    }
     
		    return utftext;
	    },
     
	    // private method for UTF-8 decoding
	    _utf8_decode : function (utftext) {
		    var string = "";
		    var i = 0;
		    var c = c1 = c2 = 0;
     
		    while ( i < utftext.length ) {
     
			    c = utftext.charCodeAt(i);
     
			    if (c < 128) {
				    string += String.fromCharCode(c);
				    i++;
			    }
			    else if((c > 191) && (c < 224)) {
				    c2 = utftext.charCodeAt(i+1);
				    string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				    i += 2;
			    }
			    else {
				    c2 = utftext.charCodeAt(i+1);
				    c3 = utftext.charCodeAt(i+2);
				    string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				    i += 3;
			    }
     
		    }
     
            var lsRegExp = /\+/g;
            // Return the decoded string
            return String(string).replace(lsRegExp, " ");
		    //return string;
	    }
    }