function deleteNode(element){
    if (element) {
		while( element.hasChildNodes() ) {
			element.removeChild( element.lastChild );
		}
    }
}

function loadCT(title,url,div) {
	document.getElementById(div).innerHTML = '<form action=""><fieldset><legend>' + title + '</legend><div id="' + div + '_inner"><img src="all/images/throbber.gif" style="line-height: 20pt; height: 8pt;" /> Loading...</div></fieldset></form>';
	loadC(url,div + '_inner');
}

function checkAll() {
	check = document.getElementById('check_result');

	if (check.childNodes.length > 0) {
	    deleteNode(check);
	}

	checkJavaScript(check);
	checkUserAgent(check);
	checkTimeZone(check);
	checkFlash(check);
	checkNSV(check);
	checkSpeed(check);
}

function addTempCheckRow(table, description, tempText, tempID) {
	var row = document.createElement('tr');
	var desc = document.createElement('th');
	var test = document.createElement('td');
			
	desc.appendChild(document.createTextNode(description));
	test.appendChild(document.createTextNode(tempText));

	test.className = 'pending';

	row.appendChild(desc);
	row.appendChild(test);
	row.id = tempID;
	
	table.appendChild(row);
}

function addCheckRow(check, description, itemID, resultCode) {
	var item = document.createElement('div');
	var result = document.createElement('span');
	var desc = document.createElement('span');

	desc.appendChild(document.createTextNode(description));
	desc.className = 'desc';

	if (resultCode == 1) {
		result.innerHTML = '&#10003;';
		result.className = 'symbol result ok';
	} else if (resultCode == 0) {
		result.innerHTML = '&#10007;';
		result.className = 'symbol result error';
	} else if (resultCode == 3) {
		result.innerHTML = '<img src="all/images/throbber.gif" />';
		result.className = 'result';
	} else {
		result.innerHTML = '?';
		result.className = 'result pending';
	}

	item.appendChild(result);
	item.appendChild(desc);
	item.id = itemID;

	check.appendChild(item);
}

function addCheckRowHTML(check, description, itemID, resultCode) {
	var item = document.createElement('div');
	var result = document.createElement('span');
	var desc = document.createElement('span');

	desc.innerHTML = description;
	desc.className = 'desc';

	if (resultCode == 1) {
		result.innerHTML = '&#10003;';
		result.className = 'symbol result ok';
	} else if (resultCode == 0) {
		result.innerHTML = '&#10007;';
		result.className = 'symbol result error';
	} else if (resultCode == 3) {
		result.innerHTML = '<img src="all/images/throbber.gif" />';
		result.className = 'result';
	} else {
		result.innerHTML = '?';
		result.className = 'result pending';
	}

	item.appendChild(result);
	item.appendChild(desc);
	item.id = itemID;

	check.appendChild(item);
}

function checkUserAgent(table) {
	BrowserDetect.init();
	addCheckRow(table, 'Your Operating System is ' + BrowserDetect.OS, 'os_check', true);
	addCheckRow(table, 'You are using ' + BrowserDetect.browser + ' version ' + BrowserDetect.version + ' web browser', 'browser_check' , true);
}

function checkTimeZone(table) {
    if (table) {
		try {
			now = new Date();
			var offset = now.getTimezoneOffset();
			offset = offset / 60 * (-1);
			if (offset > 0) { offset = '+' + offset; }
			addCheckRow(table, 'Your Time Zone is GMT ' + offset, 'timezone_check', true);
	    } catch (e) {
			addCheckRow(table, 'Failed to detect Your time zone, TV Schedule times would be incorrect.', 'timezone_check', failed);
	    }
	}
}

function checkJavaScript(table) {
	if (table) {
		addCheckRow(table, 'Javascript is supported by Your web browser and we will be able to perform all tests.', 'js_check', true);
	}
}

function checkNSV(table) {
	if (table) {
		if (nsv.installed) {
			addCheckRow(table, 'NSV Plugin was succesfully detected.', 'nsv_check', true);
		} else {
			addCheckRowHTML(table, 'NSV Plugin was not found on Your system and You will be unable to watch our TV. <a href="http://www.nullsoft.com/nsv/embed/nsvmoz_vp3_mp3.xpi"><strong>Click here to download and install plugin.</strong></a>', 'nsv_check', false);
		}
	}
}

function checkFlash(table) {
	if (table) {
		if (flash.ver[8]) {
			addCheckRow(table, 'Macromedia Flash ' + flash.version +  ' was found.', 'flash_check', true);
		} else if (flash.installed) {
			addCheckRow(table, 'Macromedia Flash ' + flash.version + ' was found on Your system. You need version at least 8.0 to watch our VoD content.', 'flash_check', false);
		} else {
			addCheckRow(table, 'Macromedia Flash 8.0+ was not found.', 'flash_check', false);
		}
	}
}

function checkSpeed(table) {
    date = new Date();
    var startTime = date.getTime();
    var page_request = false;
    var url = '128k.bin?' + startTime;
    var datasize = 128000;

    addCheckRow(table, 'We are currently checking Your connection speed. Please be patient.', 'speed_check_tmp', 3);

    try {
        page_request = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try {
            page_request = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
	    	if (window.XMLHttpRequest) {
    			page_request = new XMLHttpRequest();
	    	} else {
				return false;
	    	}
		}
    }

    page_request.onreadystatechange=function(){
		loadpage(page_request);
    }

	var startTime = date.getTime();
    page_request.open('GET', url, true);
    page_request.send(null);

    function loadpage(page_request) {
		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
		    tempRow = document.getElementById('speed_check_tmp');
		    table.removeChild(tempRow);

			date = new Date();
			var endTime = date.getTime();

			var diffTimeMilliseconds = endTime - startTime;
			var diffTimeSeconds = diffTimeMilliseconds/1000;
			var bits = (datasize*8);
			var kbits = bits/1024;
			var throughput = kbits/(diffTimeSeconds);
			throughput = Math.round(throughput * .93);

			addCheckRow(table, 'Your connection speed is ' + throughput + 'kbps', 'speed_check_max', 1);

			speed = '';
			if (throughput >= 200) {
				if (nsv.installed) {
					addCheckRow(table, 'Your connection speed is at least 200kbps and You should be able to watch Armbets.TV Streaming', 'speed_check_tv', 1);
				} else {
					if (BrowserDetect.browser == 'Firefox') {
						addCheckRowHTML(table, 'Your connection speed is at least 200kbps, but You have no NSV plugin. <a href="http://www.nullsoft.com/nsv/embed/nsvmoz_vp3_mp3.xpi"><strong>Click here to download and install plugin.</strong></a>', 'speed_check_tv', 0);
					} else {
						addCheckRowHTML(table, 'Your connection speed is at least 200kbps, but You have no NSV plugin. It should be automatically installed when You will try to watch Armbets.TV Streaming. <a href="http://www.nullsoft.com/nsv/embed/nsvplayx_vp3_mp3.cab">Click here to install it manually.</a>', 'speed_check_tv', 0);
					}
				}
				connection = '200kbps+';
			} else {
				addCheckRow(table, 'Your connection speed is below 200kbps. You will be unable to watch Armbets.TV Streaming.', 'speed_check_tv', 0);
			}

			if (throughput >= 300) {
				if (flash.ver[8]) {
					addCheckRow(table, 'Your connection speed is at least 300kbps and You should be able to watch Low Quality VoD', 'speed_check_lowq', 1);
				} else {
					addCheckRow(table, 'Your connection speed is at least 300kbps, but You have no Macromedia Flash 8.0+ and You will be unable to watch Low Quality VoD.', 'speed_check_lowq', 0);
				}
				connection = '300kbps+';
			} else {
				addCheckRow(table, 'Your connection speed is below 300kbps and You would not be able to watch Low Quality VoD', 'speed_check_lowq', 0);
			}

			if (throughput >= 500) {
				if (flash.ver[8]) {
					addCheckRow(table, 'Your connection speed is at least 500kbps and You should be able to watch Medium Quality VoD', 'speed_check_medq', 1);
				} else {
					addCheckRow(table, 'Your connection speed is at least 500kbps, but You have no Macromedia Flash 8.0+ and You will be unable to watch Medium Quality VoD.', 'speed_check_medq', 0);
				}
				connection = '500kbps+';
			} else {
				addCheckRow(table, 'Your connection speed is below 500kbps and You would not be able to watch Medium Quality VoD', 'speed_check_medq', 0);
			}

			if (throughput >= 800) {
				if (flash.ver[8]) {
					addCheckRow(table, 'Your connection speed is at least 800kbps and You should be able to watch High Quality VoD', 'speed_check_highq', 1);
				} else {
					addCheckRow(table, 'Your connection speed is at least 800kbps, but You have no Macromedia Flash 8.0+ and You will be unable to watch High Quality VoD.', 'speed_check_highq', 0);
				}
				connection = '800kbps+';
			} else {
				addCheckRow(table, 'Your connection speed is below 800kbps and You would not be able to watch High Quality VoD', 'speed_check_highq', 0);
			}

			if (throughput < 200) {
				connection = 'Lower then 200kbps';
			}

		}
    }
}

function sImg(obj, on, name, lang) {
    if (on == 1) {
        obj.src = lang + "/images/on/" + name + ".gif";
    } else {
        obj.src = lang + "/images/off/" + name + ".gif";
    }
}

function sFlag(obj, on, name) {
    if (on == 1) {
        obj.src = "all/images/flags/on/" + name + ".gif";
    } else {
        obj.src = "all/images/flags/off/" + name + ".gif";
    }
}


function confirmGo(url)
{
	msg = 'Are You sure You want to perform this action (Movie which is currently playing will be pernamently stopped and You will not be able to watch it again without paying for it).';

	if (confirm(msg)) {
		document.location = url;
	}
}
function loadC(url,div) {
    now = new Date();
    offset = now.getTimezoneOffset();
    
    var page_request = false;

    try {
        page_request = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try {
            page_request = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
	    if (window.XMLHttpRequest) {
    		page_request = new XMLHttpRequest();
	    } else {
		return false;
	    }
	}
    }

    page_request.onreadystatechange=function(){
	loadpage(page_request,div);
    }

    if (url.indexOf("?")==-1) {
	newurl = url + '?offset=' + offset;
    } else {
	newurl = url + '&offset=' + offset;
    }

    page_request.open('GET', newurl, true);
    page_request.send(null);

    function loadpage(page_request,div) {
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
	    document.getElementById(div).innerHTML=page_request.responseText;
	}
    }
}

function initPage() {
    loadC('dynamic/menu.php','menu');
    loadC('dynamic/coming_soon.php','dynamic_left');
    movieLoad();
    if (document.location.indexOf('register_ok')!=-1) {
		loadC('dynamic/home.php','dynamic');
    }
}

function initCheck() {
	str = 'No tests were performed yet. Please click on "Perform all checks" button to check if Your web browser fully supports our page.'
	check = document.getElementById('check_result');
	if (check) {
		if (check.childNodes.length > 0) {
			deleteNode(check);
		}

		addCheckRow(check, str, 'js1_check', 2);
		checkButton = document.getElementById('check_button');
		if (check) {
			checkButton.className = 'submit block';
		}
	}
}

function initBasket() {
	var str = '' + document.location;
	if (str.indexOf("vodplay") > -1) {
		var links = document.getElementsByTagName("a");
		for (i=0; i<links.length; i++) {
			if (links[i]) {
				links[i].href = "javascript:confirmGo('" + links[i].href + "')";
			}
		}
	}
}

function setCookie(name,value) {
	howlong=new Date();
	howlong.setTime(howlong.getTime() + 10000);
	document.cookie = name + '=' + value + '; expires=' + howlong.toGMTString();
}

