/*
function fblogin() {
	FB.login(function(response) {
		checkLogin();
	}, {scope:'email,publish_stream'});
}
*/

function checkLogin(lng) {
	FB_autologin = true;
	// if fbs_ cookies exist redirect to mall
	if (getFbCookie()) {
		setCookie("checkFbLogin", "true", 30);
		window.open("http://www.trillenium.com/" + lng + "/facebook_login", "_top");
	}          
}

function getFbCookie()
{
	var allCookies = document.cookie.split("; ");
	for (i = 0; i < allCookies.length; i++)
	{
		var key = allCookies[i].substr(0, allCookies[i].indexOf("="));
		if (key == "fbsr_179589412088184")
		{                    
			return true;
		}
	}

	return false;
}

function setCookie(c_name, value, exdays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
	document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
	var i, x, y, ARRcookies = document.cookie.split(";");
	for (i = 0; i < ARRcookies.length; i++) {
		x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
		y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
		x = x.replace(/^\s+|\s+$/g, "");
		if (x == c_name) {
			return unescape(y);
		}
	}
}

function mjenjaj(){
	if ($('zemlje_choose'))	{
		if ($('zemlje_choose').style.display=='none'){
			$('zemlje_choose').style.display='block';
		}else{
			$('zemlje_choose').style.display='none';
		}
	}
}

window.addEvent("domready",function() {
	if ($("contactForm")) {
    	new FormCheck('contactForm', {
    		display : {
    			errorsLocation : 1,
    			indicateErrors : 2,
    			showErrors : 0
    		}
    	});
    }
    
    if ($("loginForm")) {
    	new FormCheck('loginForm', {
    		display : {
    			errorsLocation : 1,
    			indicateErrors : 2,
    			showErrors : 0
    		}
    	});
    }
});

function generatePassword(length, noPunction, randomLength) {

    if (parseInt(navigator.appVersion) <= 3) {
        alert("Sorry this only works in 4.0+ browsers");
        return '';
    }

    var sPassword = "";

    if (randomLength) {
        length = Math.random();

        length = parseInt(length * 100);
        length = (length % 7) + 6
    }


    for (i=0; i < length; i++) {

        numI = getRandomNum();
        if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }

        sPassword = sPassword + String.fromCharCode(numI);
    }

    return sPassword;
}

function getRandomNum() {

    // between 0 - 1
    var rndNum = Math.random()

    // rndNum from 0 - 1000
    rndNum = parseInt(rndNum * 1000);

    // rndNum from 33 - 127
    rndNum = (rndNum % 94) + 33;

    return rndNum;
}

function checkPunc(num) {

    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }

    return false;
}

function registrationPasswordGenerator() {
	sPassword = generatePassword(8, true, false);
	$('password').value = sPassword;
	$('password2').value = sPassword;
	$('password2').setStyle('display', 'none');
	$('password4').setStyle('display', 'block');
	setText("password4", sPassword);
}

function showHiddenPassword() {
	$('password2').setStyle('display', 'block');
	$('password4').setStyle('display', 'none');
}

function resetHiddenPassword() {
	$('password').setStyle('display', 'block');
	$('password2').setStyle('display', 'block');
	$('password3').setStyle('display', 'none');
	$('password4').setStyle('display', 'none');
	$('password').value='';
	$('password2').value='';
	$('password').focus()
}

function setText(id, txt) {
	var elem;
	
	if( document.getElementById  && (elem=document.getElementById(id)) ) {
		if( !elem.firstChild )
			elem.appendChild( document.createTextNode( txt ) );
		else 
			elem.firstChild.data = txt;
	}
	
	return false; 
}

function lngChange(url) {
	document.frmLng.action = url;
	document.frmLng.submit();
}

function countdown() {
	if (sec == 0) {
		window.clearTimeout(SD);
		$('theTime').setStyle('display', 'none');
		$('theTimeMsg').setStyle('display', 'none');
		$('thePINMsg').setStyle('display', 'block');
	} else {
		sec--;
		minutes = (sec/60).toInt();
		seconds = sec % 60;;
		time = (minutes<=9 ? "0" + minutes : minutes) + ":" + (seconds<=9 ? "0" + seconds : seconds);
		$('theTime').innerHTML = time;
		SD = window.setTimeout("countdown();", 1000);
	} 
}
// Unity
	function GetUnity() {
		if (typeof unityObject != "undefined") {
			return unityObject.getObjectById("unityPlayer");
		}
			return null;
		}
		if (typeof unityObject != "undefined") {			
			var params = {
			backgroundcolor: "FFFFFF",
			bordercolor: "FFFFFF",
			textcolor: "000000",
			logoimage: "logo.png",
			progressbarimage: "progressbar.png",
			progressframeimage: "progressframe.png",
			disableContextMenu: "true"
		};
		unityObject.embedUnity("unityPlayer", "Mall.unity3d", "100%", "100%", params);
	}
	function GetParam() {
		GetUnity().SendMessage("Application", "ProcessParam", parent.param);
	}
	
	function AppReady(appId) {
		// TODO: Check why this funcion is called from Unity in production build!
	}
		
// Epay
// Start epay
function createiFrame(OrderID, Guid) {
	$('cashier-bg').setStyle('visibility', 'visible');
	$('iFrameUnity').setStyle('visibility', 'hidden');
	$('iCheckoutWrapper').src = 'epay.aspx?OrderID=' + OrderID + '&Guid=' + Guid;
}
// End epay
function endPayment(result) {
	$('iFrameUnity').setStyle('visibility', 'visible');
	$('cashier-bg').setStyle('visibility', 'hidden');
	var unity = GetUnity();
	unity.SendMessage('CheckoutEvenetListener', 'OnEPayEnd', ''+result); 
}
