function createRequestObject() {
    var tmpXmlHttpObject;
    if (window.XMLHttpRequest) { 
        tmpXmlHttpObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) { 
        tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return tmpXmlHttpObject;
}

//call the above function to create the XMLHttpRequest object
var http = createRequestObject();

function checkIp() {
    http.open('get', '/assets/ip_country_test.php');
    http.onreadystatechange = doForward;
    http.send(null);
}

function doForward() {
    if(http.readyState == 4){
        var response = http.responseText;
		// if it's a jp ip, check the cookies
		if(response==1){
			printJpMsg();
			checkCookies();			
		}		

		if(response==2){
			printDeMsg();
			checkCookies();
		}
//no dont do this
//		if(response==3){
//			printUkMsg();
//			checkCookies();
//		}
	}
}

function printJpMsg() {
	var jpblk = document.getElementById('ccijpnotice2');
	if(jpblk) {
		nm = readCookie('nm');	
		if(nm!='true') {
			jpblk.innerHTML = 'We noticed that you are visiting CamcorderInfo.com from Japan. We have a Japanese language sister publication at <a href="http://www.camcorderinfo.jp">CamcorderInfo.jp</a>. <a href="javascript:hideJpMsg()"><em>Click here to hide this message.</em></a>';
			jpblk.style.display = 'block';
		}
	}
}

function printDeMsg() {
	var jpblk = document.getElementById('ccijpnotice2');
	if(jpblk) {
		nm = readCookie('nm');	
		if(nm!='true') {
			jpblk.innerHTML = 'We noticed that you are visiting CamcorderInfo.com from Germany. We have a German language sister publication at <a href="http://www.camcorderinfo.de">CamcorderInfo.de</a>. <a href="javascript:hideDeMsg()"><em>Click here to hide this message.</em></a>';
			jpblk.style.display = 'block';
		}
	}
}

function printUkMsg() {
	var jpblk = document.getElementById('ccijpnotice2');
	if(jpblk) {
		nm = readCookie('nm');	
		if(nm!='true') {
			jpblk.innerHTML = 'We noticed that you are visiting CamcorderInfo.com from the UK. We have a sister publication at <a href="http://www.camcorderinfo.co.uk">CamcorderInfo.co.uk</a>. <a href="javascript:hideUkMsg()"><em>Click here to hide this message.</em></a>';
			jpblk.style.display = 'block';
		}
	}
}

function hideJpMsg() {
	var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	
	document.getElementById('ccijpnotice2').style.display = 'none';
	document.cookie = 'nm=true'+expires;
}

function hideDeMsg() {
	var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	
	document.getElementById('ccijpnotice2').style.display = 'none';
	document.cookie = 'nm=true'+expires;
}

function hideUkMsg() {
	var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	
	document.getElementById('ccijpnotice2').style.display = 'none';
	document.cookie = 'nm=true'+expires;
}

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 checkCookies() {
	var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();

	// look to see if we have a nb cookie
	var nb = readCookie('nb');
	var loco = window.location.pathname;

	// show the message, if they are coming back from cci.jp, cookie them..	
  	if(document.referrer.indexOf('camcorderinfo.jp') >= 0) {
		document.cookie = 'nb=true'+expires;
		nb = 'true';
  	}

	if(document.referrer.indexOf('camcorderinfo.de') >= 0) {
		document.cookie = 'nb=true'+expires;
		nbde = 'true';
	}

	if(document.referrer.indexOf('camcorderinfo.co.uk') >= 0) {
		document.cookie = 'nb=true'+expires;
		nbuk = 'true';
	}	
	
	// now, worry about forwarding
	if(nbde!='true'){
		// if they have an null or false, forward them IF
		// they are on one of the pages we forward
		if(loco == '/index.php' || loco=='/' || loco.indexOf('/ratings.php') >= 0){
			window.location = 'http://www.camcorderinfo.de'+loco;
		}	
		else if(loco.indexOf('/d/Reviews&level_b=Camcorder.htm') >= 0){
			window.location = 'http://www.camcorderinfo.de/d/reviews.htm';			
		}
	}

	if(nbuk!='true'){
		// if they have an null or false, forward them IF
		// they are on one of the pages we forward
		if(loco == '/index.php' || loco=='/' || loco.indexOf('/ratings.php') >= 0){
			window.location = 'http://www.camcorderinfo.co.uk'+loco;
		}	
		else if(loco.indexOf('/d/Reviews&level_b=Camcorder.htm') >= 0){
			window.location = 'http://www.camcorderinfo.co.uk/d/reviews.htm';			
		}
	}

}

function checkBrowser(){
/*	if (navigator.appName == 'Microsoft Internet Explorer')
	{
	 document.getElementById('menu').className = 'iemargin';
	 document.getElementById('mainbody_alt').style.width = '100%';
	}*/
	
	checkIp();
}


