function getBrowser() {
	var ua=navigator.userAgent.toLowerCase();
	return ua;
}

function isBrowser() {
	return ua.indexOf(t) != -1;
}

function isOpera8() {
	var b = getBrowser();
  if(/opera[/ ](\d)/.test(b))
		if(RegExp.$1 <= 8) {
		  return true;
		}
		else
			return false;
}




function isIE55() {
	var b = getBrowser();
	if (/msie (\d)/.test(b))
		if(RegExp.$1 == 5)
			return true;
		else
			return false;
}

function isIE() {
	var b = getBrowser();
	if(/msie (\d)/.test(b)) {
		return true;
	}
	else
		return false;
}

function isIE_() {
	var b = getBrowser();
	if(/msie (\d)/.test(b)) {
		return true;
	}
	else
		return false;
}
/*
b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('gecko/')? 'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'';
if(b != null) {
	if(b.substr(0,2) == 'op') { // ja ir opera
		if(RegExp.$1 == 8) { // ja ir 8 versija
			document.write('<link href="opera_style.css" rel="StyleSheet" type="text/css" title="Opera style" />');
		}
	}

}
*/
// iel�d�jam speci�lu .css priek� Operas8

//Tarifu kalkulatoram
function openTarCalc(param) {
	switch(param) {
		case "private":
			window.open('/ltc/tarifu_calc/','w1','width=792,height=550,resizable=0,scrollbars=0,status=yes');
			return false;
			break;
		case "business":
			window.open('/ltc/tarifu_calc_biz/','w1','width=792,height=550,resizable=0,scrollbars=0,status=yes');
			return false;
			break;
	}
}

function popup_service(url) {
	window.open(url,'','width=883,height=616,resizable=0,scrollbars=1,status=yes,toolbar=0,location=0,menubar=0');
	return false;
}

function popup_mercury(url) {
	window.open(url,'','width=700,height=590,resizable=0,scrollbars=1,status=yes,toolbar=0,location=0,menubar=0');
	return false;
}

function popup_tariff(url) {
	window.open(url,'','width=793,height=551,resizable=0,scrollbars=0,status=yes,toolbar=0,location=0,menubar=0');
	return false;
}

function popup_service_forms(url) {
	window.open(url,'','width=883,height=618,location=0,toolbar=0,directories=0,menubar=0,resizable=no,status=0,top=0,left=0');
	return false;
}

/* SERVICE AVAILABILITY POPUP */
function openServiceForm(url) {

	if(url.indexOf("?") != -1)
		url = url + "&urlis=1";
	else
		url = url + "?urlis=1";
	if(url.indexOf("service_forms_step" != -1)) {
		window.resizeTo(900,708);
	} else {
		window.resizeTo(701,590);
	}
	document.location = url;
	return false;
}

/*
Created by Girts Gosko, 20.09.2007
Function closes popup window and refreshes it's parent window, for example, if action in popup window is completed and results shown on parent window, just call this function after action is complete on popup window.
*/
function refreshParent() {
	window.opener.location.href = window.opener.location.href;
	if (window.opener.progressWindow) {
		window.opener.progressWindow.close()
	}
	window.close();
}



/* VALIDATE FIELDS FUNCTIONS */

function trim(s)
{
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
}

function validate_field(field_id,alerttxt)
{
	if (document.getElementById(field_id))
	{
		var field = document.getElementById(field_id);
		with (field)
		{
			if (value==null||trim(value)=="")
			  {alert(alerttxt);field.focus();return false}
			else {return true}
		}
	}
}

function validate_radio(field_id)
{
	if (document.getElementById(field_id))
	{
		var field = document.getElementById(field_id);
		with (field)
		{
			if (checked==null||checked==false)
			  {return false}
			else {return true}
		}
	}
}

function check_field(field_id) {
	var field = document.getElementById(field_id);
	with (field)
	{
		if (value==null||trim(value)=="")
		  {return false}
		else {return true}
	}
}

function check_integer(field_id)
{
	if (document.getElementById(field_id))
	{
		var field = document.getElementById(field_id);
		with (field)
		{
			if (value==null||trim(value)==""||trim(value)=='0')
			{
				return false;
			}
			else
			{
				var i;
				for (i = 0; i < value.length; i++)
				{
					// Check that current character is number.
					var c = value.charAt(i);
					if (((c < "0") || (c > "9"))) return false;
				}
				// All characters are numbers.
				return true;
			}
		}
	}
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function validate_phone_number(field_id)
{
	if (document.getElementById(field_id))
	{
		var field = document.getElementById(field_id);
		with (field)
		{
			var digits = "0123456789";
			var phoneNumberDelimiters = "371";
			var validWorldPhoneChars = phoneNumberDelimiters + "+";
			var minDigitsInIPhoneNumber = 7;
			var maxDigitsInIPhoneNumber = 8;
			s=stripCharsInBag(value,validWorldPhoneChars);
			alert(s);
			return (isInteger(s) && s.length >= minDigitsInIPhoneNumber && s.length <= maxDigitsInIPhoneNumber);
		}
	}
}


function validate_email(field_id,alerttxt)
{
	if (document.getElementById(field_id))
	{
		var field = document.getElementById(field_id);
		with (field)
		{
			if (value==null) {
				alert(alerttxt);field.focus();return false;
			}
			if (value.length==0) {
				alert(alerttxt);field.focus();return false;
			}
			if (!allValidChars(value)) {  // check to make sure all characters are valid
				alert(alerttxt);field.focus();return false;
			}
			if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value))) {
				alert(alerttxt);field.focus();return false;
			}
			if (value.indexOf("@") < 1) { //  must contain @, and it must not be the first character
				alert(alerttxt);field.focus();return false;
			} else if (value.lastIndexOf(".") <= value.indexOf("@")) {  // last dot must be after the @
				alert(alerttxt);field.focus();return false;
			} else if (value.indexOf("@") == value.length) {  // @ must not be the last character
				alert(alerttxt);field.focus();return false;
			} else if (value.indexOf("..") >=0) { // two periods in a row is not valid
				alert(alerttxt);field.focus();return false;
			} else if (value.indexOf(".") == value.length) {  // . must not be the last character
				alert(alerttxt);field.focus();return false;
			}
			return true;
		}
	}
}

function allValidChars(email) {
	  var parsed = true;
	  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	  for (var i=0; i < email.length; i++) {
		var letter = email.charAt(i).toLowerCase();
		if (validchars.indexOf(letter) != -1)
		  continue;
		parsed = false;
		break;
	  }
	  return parsed;
}

function print_r( array, return_val ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Michael White (http://crestidg.com)
    // +   improved by: Ben Bryan
    // *     example 1: print_r(1, true);
    // *     returns 1: 1
    
    var output = "", pad_char = " ", pad_val = 4;
 
    var formatArray = function (obj, cur_depth, pad_val, pad_char) {
        if (cur_depth > 0) {
            cur_depth++;
        }
 
        var base_pad = repeat_char(pad_val*cur_depth, pad_char);
        var thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char);
        var str = "";
 
        if (obj instanceof Array || obj instanceof Object) {
            str += "Array\n" + base_pad + "(\n";
            for (var key in obj) {
                if (obj[key] instanceof Array) {
                    str += thick_pad + "["+key+"] => "+formatArray(obj[key], cur_depth+1, pad_val, pad_char);
                } else {
                    str += thick_pad + "["+key+"] => " + obj[key] + "\n";
                }
            }
            str += base_pad + ")\n";
        } else if(obj == null || obj == undefined) {
            str = '';
        } else {
            str = obj.toString();
        }
 
        return str;
    };
 
    var repeat_char = function (len, pad_char) {
        var str = "";
        for(var i=0; i < len; i++) { 
            str += pad_char; 
        };
        return str;
    };
    output = formatArray(array, 0, pad_val, pad_char);
 
    if (return_val !== true) {
        document.write("<pre>" + output + "</pre>");
        return true;
    } else {
        return output;
    }
}

function getAbsolutePosition(elem)
{
	var r = { x: elem.offsetLeft, y: elem.offsetTop };
	if (elem.offsetParent)
	{
		var tmp = getAbsolutePosition(elem.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
} 

function openServiceAvailabilityByGET(){
	var re = /(?:\?|&)service_id=(\d+)/g;
	var arr = re.exec(location.href);
	if(arr!=null){
		var service_id = arr[1];
	}
	else{
		var service_id = '';
	}
	
	//window.open(' /ltc/service_availability/?service_id=' + service_id,'','location=0,status=0,scrollbars=1,width=992px,height=590px');
	window.open(' /ltc/service_availability/?service_id=' + service_id,'');
	return void(0);
}

function readCookie(name) {
	var nameEQ = name + "=";
	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) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function base64_decode(input) {
  var keyStr = "ABCDEFGHIJKLMNOP" +
               "QRSTUVWXYZabcdef" +
               "ghijklmnopqrstuv" +
               "wxyz0123456789+/" +
               "=";
             
  var output = "";
  var chr1, chr2, chr3 = "";
  var enc1, enc2, enc3, enc4 = "";
  var i = 0;
 
  // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  var base64test = /[^A-Za-z0-9\+\/\=]/g;

  input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

  do {
    enc1 = keyStr.indexOf(input.charAt(i++));
    enc2 = keyStr.indexOf(input.charAt(i++));
    enc3 = keyStr.indexOf(input.charAt(i++));
    enc4 = keyStr.indexOf(input.charAt(i++));

    chr1 = (enc1 << 2) | (enc2 >> 4);
    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
    chr3 = ((enc3 & 3) << 6) | enc4;

    output = output + String.fromCharCode(chr1);

    if (enc3 != 64) {
      output = output + String.fromCharCode(chr2);
    }
    if (enc4 != 64) {
      output = output + String.fromCharCode(chr3);
    }

    chr1 = chr2 = chr3 = "";
    enc1 = enc2 = enc3 = enc4 = "";

  } while (i < input.length);

  return unescape(output);
}
function changeBg(obj, add_class, remove_class) {
	if(!obj)
		return false;

	$(obj).addClass(add_class);
	$(obj).removeClass(remove_class);

}

function delayChangeBg(element, add_class, remove_class) {
	setTimeout('changeBg(null, add_class, remove_class)', 1000);
}


function verySpecifikEnkurs(enkurs, article_id) {
	displayItem(article_id);
	var target_offset = $("#"+enkurs).offset();
	var target_top = target_offset.top;
	$('html, body').animate({scrollTop:target_top}, 500);
}
