/**
 * @version: 1.0
 * @date: 26-06-2009
 * @category: Javascript Custom Component for Bahnhof privet site
 * @copyright KN Telecom Ukraine
 */

function openUrl(url) {
	document.location.href = url;
}

function ifConfirm(url) {
	if (confirm('Are you sure?')) {
		document.location.href = url;
	}
}

function changeEvent(SelectorID) {
	if (SelectorID == "dropdownObj") {
		if ($('#'+SelectorID).val() != '') document.location.href = $('#'+SelectorID).val();
	}
}

function InstallPngFix() {
	if (($.browser.msie)&&(parseInt(jQuery.browser.version) < 7)){
		$('img[src*=png], .png').each(function(){
			$(this).css('behavior', 'url(js/iepngfix.htc)');
		});
	}
}

function InstallZebra(){
	$('.zebra tr:even').addClass('even');
	$('.zebra tr:odd').addClass('odd');
	$('.zebra tr').hover(
		function(){
			$(this).addClass('hovered');
		},
		function(){
			$(this).removeClass('hovered');
		}
	)
}
function InslallBoxBg(){
	$('.box').each(function(){
		if ($(this).height() < 160) {
			$(this).removeClass('middle').addClass('small');
		}
		if (!$.browser.safari) {
			if ($(this).height() > 400){
				$(this).removeClass('middle').addClass('big');
			}
		}
	})
}
function destroyTBWindow(){
	$('.TBwindow').hide();
	$('.TBmessage').hide();
}

function showTBWindow(){
	var topMargin=(($(window).height() - ($('.TBmessage').height()))/3 + document.getElementsByTagName('body')[0].scrollTop);
	$('.TBwindow').height($(document).height()).show();
	$('.TBmessage').css('top', topMargin).show();
	$('.TBwindow').click(function(){
		destroyTBWindow();
	})
}

function setTBWindowContentElement(id) {
	var el = '#'+id;
//	alert(el);
	$('#pnlSplash').append($(el));
}

document.onkeydown = function(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	}
	else { // mozilla
		keycode = e.which;
	}
	if (($('.TBwindow')) && (keycode == 27)) { // close
		destroyTBWindow();
	}
}

function InstallCustomSelect(){
return;
    $('.all select:not("#activateYearTelefoni,#activateMonthTelefoni,#activateDayTelefoni")').each(function(){
        $(this).wrap(document.createElement("div"));
        if ($(this).css('width')=='auto'){
            tWidth = $(this).width()+'px';
        } else {
            tWidth = $(this).css('width');
        }
        $(this).parent('div').iSelectbox({selectbox_id:$(this).attr('id'), width:tWidth}); 
    })
    $('#activateYearTelefoni').wrap(document.createElement("div")).parent('div').iSelectbox({selectbox_id:'activateYearTelefoni', width:'55px'}); 
    $('#activateMonthTelefoni').wrap(document.createElement("div")).parent('div').iSelectbox({selectbox_id:'activateMonthTelefoni', width:'40px'}); 
    $('#activateDayTelefoni').wrap(document.createElement("div")).parent('div').iSelectbox({selectbox_id:'activateDayTelefoni', width:'40px'}); 
}
function HideCustomSelect(SelectID) {
return;
 if (!(($.browser.msie)&&(parseInt(jQuery.browser.version) < 7))){
  var CurrentSelect=$('#'+SelectID);
  CurrentSelect.hide();
  tWidth = CurrentSelect.next('div.iSelectbox_main').css('width');
  var selectHeight = '18px';
  var selectMargin = '-20px';
  CurrentSelect.next('div.iSelectbox_main').after('<div class="waitLoad" style="width:'+tWidth+'; height:'+selectHeight+'; margin-top:'+selectMargin+';"/>');
 }
}

function RefreshCustomSelect(SelectID){
	if (!(($.browser.msie)&&(parseInt(jQuery.browser.version) < 7))){
		$('div.waitLoad').remove();
		var CurrentSelect=$('#'+SelectID);
		CurrentSelect.next('div.iSelectbox_main').remove();
		if (CurrentSelect.show().css('width')=='auto'){
			tWidth = CurrentSelect.width()+'px';
		}
		else {
			tWidth = CurrentSelect.css('width');
		}
		//CurrentSelect.parent('div').iSelectbox({selectbox_id:SelectID, width:tWidth});
	}
}

$(document).ready(function(e) {
	try {
		$("div[rel='hidden']").hide();
		$('input:text, input:password, textarea').addClass('inText');
		$('input:checkbox').checkbox();
		$('input:radio').checkbox({cls:'jquery-radio-checkbox'});

		InstallPngFix();
		InstallZebra();
		InslallBoxBg();
		
		
		if (!sum) {
			var sum = 0;
			for (i = 0; i < $('.intop .navi a').length; i++) {
				sum = sum + $('.intop .navi a')[i].offsetWidth;
			}
			var lastLength = 880;
			$('.intop .navi a').css('margin-right',((lastLength - sum) / $('.intop .navi a').length));
			$('.intop .navi a:last').css('margin-right', 0);
		}
		if (!(($.browser.msie)&&(parseInt(jQuery.browser.version) < 7))){
			//InstallCustomSelect();
		}
	} catch(e) {
		//alert(e);
	}
	
	if (($.browser.msie)&&(parseInt(jQuery.browser.version) == 8)){
		$('input[type=radio]').css('margin','2px 0 -2px 0');
	} else {
		if (!($.browser.safari) && !($.browser.opera) && !($.browser.msie))
			$('input[type=radio]').css('margin','3px 0 -4px 0');
		if ($.browser.msie) {}
	}
});

function getElementsByLang(tagName, value) {
	ccollect = new Array();
	var inc=0;
	var alltags = document.all ? document.all : document.getElementsByTagName(tagName);

	for (i = 0; i < alltags.length; i++) {
		if (alltags[i].lang==value) {
			ccollect[inc++]=alltags[i];
		}
	}

	return ccollect;
}

function GetDataCreditSafe(phNumber) {
// /privat/ only live
	var urlCreditSafe = "/public/creditsafe/getdatacreditsafe/"+phNumber;
	var request = mk_xmlhttpreq();
	request.open('GET', urlCreditSafe, true);
	request.onreadystatechange = function () {
		if (request.readyState != 4) { return; }
		var response = request.responseText;
		eval(response);
		SetDataCreditSafe(dataCreditSafe);
	}
	request.send(null);
}

function SetDataCreditSafe(dataCreditSafe) {
	form = document.getElementById('dataForm').elements;
	for (var i = 0; form.length > i; i++) {
		switch(form[i].name) {
			case "varFirstName":
					form[i].value = dataCreditSafe.varFirstName;
				break;
			case "varLastName":
					form[i].value = dataCreditSafe.varLastName;
				break;
			case "varPersonnNummer":
					form[i].value = dataCreditSafe.varPersonnNummer;
				break;
			case "varAddress":
					form[i].value = dataCreditSafe.varAddress;
				break;
			case "varZip":
					form[i].value = dataCreditSafe.varZip;
				break;
			case "varCity":
					form[i].value = dataCreditSafe.varCity;
				break;
			case "scoring":
					form[i].value = dataCreditSafe.scoring;
				break;
			default :
				break;
		}
	}
}

function mk_xmlhttpreq () {
	/*var xh = GXmlHttp.create();
	if (xh != null) { return xh; }*/
	// Std IExploder:
	try {
		xh = new ActiveXObject("Msxml2.XMLHTTP");
		return xh;
	} catch (e) { }
	try {
		xh = new ActiveXObject("Microsoft.XMLHTTP");
		return xh;
	} catch (e) { }
	try {
		xh = new XMLHttpRequest();
		return xh;
	} catch (e) { }
	// Only other way I could find...
	try {
		xh = window.createRequest();
		return xh;
	} catch (e) { }
	return undefined;
}
