﻿function writeActionForm() {
	var res = getData();
	var formStr = '<form action="'+res['url']+'" name="jsDownload" id="jsDownload" method="post">';
	formStr += '<input type="hidden" name="pData" value="'+res['pData']+'" />';                                               
	formStr += '<input type="hidden" name="lang" value="'+res['lang']+'" />';
	formStr += '<input type="hidden" name="skin_id" value="'+res['skin_id']+'" />';
	formStr += '</form>';     

	$("body").append(formStr);
}

function initIframeSrc(iframeEL) {
	var res = getData();
	iframeEL.src = res['url']+"?pData="+res['pData']+"&lang="+res['lang'];
}

//gets all the cookies from the current page an sets them in a new window specified sent as a parameter
var setchild = function(_win){
	//get all the cookies and split them into a array
	var cookies = document.cookie.split(";");

	for (k in cookies){
		//split the current string into a new array
		try{
			var rr_keyval = cookies[k].split("=");
			for (m in rr_keyval)
				//cut the spaces from the beginning of the string
				while (rr_keyval[m].charAt(0)==' ') rr_keyval[m] = rr_keyval[m].substring(1,rr_keyval[m].length);
				//set a new cookie in the new window
				_win.document.cookie = rr_keyval[0] + "=" + rr_keyval[1] + "; path=/";
		}catch (err){}
	}
}


function getData(){
	var res = Array();
	res['url'] = 'http://online.rummyroyal.com/bo/RummyRoyal.php';
	res['skin_id'] = 7;
	var brs = browserDetection();
	res['pData'] = '3580^6668^1^75130^1^0^'+brs+'^0';
	res['lang'] = 'it';
	return res;
}

function browserDetection(){
	var browser = jQuery.browser;
	var curBrowser = '';
	for (br in browser){
		if (browser[br] == true)
			curBrowser = br;
	}
	switch (curBrowser) {
		case 'safari':
			return 's';
		break;
		case 'opera':
			return 'o';
		break;
		case 'msie':
			return 'i';
		break;
		case 'mozilla':
			return 'f';
		break;
		case 'chrome':
			return 'c';
		break;
		default:
			return 'u';
		break;
	}
}

$(document).ready(function() {
	$("#noDownloadLink").click(function(event){                                  
		document.jsDownload.target = "win";
		var win = window.open('/blankPage.html?'+getUnixTime(),'win','width=800,scrollbars=0,height=635,left=100,top=50,resizable=1');
		setchild(win);    
		win.focus();                                           
		if (browserDetection() == 'f') document.jsDownload.submit();
		else jsDownload.submit();          
	});
	writeActionForm();
});

function getUnixTime()
{
	var foo = new Date; // Generic JS date object
	var unixtime_ms = foo.getTime(); // Returns milliseconds since the epoch
	var unixtime = parseInt(unixtime_ms / 1000);       
	return unixtime;
}