// set your AFFILIATE IDs here. Leave blank any you do not have an account for.
var arrAffiliates = {
	'co.uk' : '',
	'com'	: '',
	'de'	: '',
	'fr'	: '',
	'ca'	: '',
	'jp'	: ''
}

/**
 * search for any AMAZON product links and replace with a referral link to the user's local Amazon site
 * @author Pete Williams
 * @url http://petewilliams.info/blog/2009/07/javascript-amazon-associate-link-localiser/
 */
//AJAX
function aaGetXmlHttpRequest() {
   var xmlHttpObj;
   if(window.XMLHttpRequest) {
      xmlHttpObj= new XMLHttpRequest();
   }else {
      try {
         xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
      }catch(e) {
         try {
            xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
         }catch(e) {
            xmlHttpObj=false;
         }
      }
   }
   return xmlHttpObj;
}
//-----------------------------------
function testip() {
xmlhttp = aaGetXmlHttpRequest();
if(!xmlhttp) return;
gt='http://'+document.location.hostname+'/ip/ip.php';
  //            alert(gt);
                xmlhttp.open('GET',gt,true);
                xmlhttp.onreadystatechange = doneip;
                xmlhttp.send(null);

}
//var ipresponse;

function doneip() {
        if(xmlhttp.readyState==4 && xmlhttp.status==200) {
           var response=xmlhttp.responseText;
		checkAmazonLinks(response);
        }
}


function checkAmazonLinks(ipresponse) {
	// set required VARIABLES
	var strTld;
	var objRegexAsin	= new RegExp( '\/([A-Z0-9]{10})' );
	var objRegexSearch	= new RegExp( 'field-keywords=[A-Za-z0-9+-]+' );
//		alert(ipresponse);
		//var strCountry		= google.loader.ClientLocation.address.country_code;
		var strCountry		= ipresponse;

	// get domain TLD from country code
	switch ( strCountry ) {
		case 'GB':
		case 'IE':
			strTld = 'co.uk';
			break;
		case 'CA':
			strTld = 'ca';
			break;
		case 'FR':
			strTld = 'fr';
			break;
		case 'JP':
			strTld = 'co.jp';
			break;
		case 'AT':
		case 'DE':
			strTld = 'de';
			break;
		default:
			strTld = ( arrAffiliates[strCountry.toLowerCase()] ? strCountry.toLowerCase() : 'com' );
			break;
	}
//alert(strCountry);

	// get all LINKS
	var arrLinks = document.getElementsByTagName( 'a' );

	// get appropriate aFFILIATE ID
	strAffiliateId = ( arrAffiliates[strTld] ? arrAffiliates[strTld] : arrAffiliatesSpares[strTld] );

	// LOOP through
	for ( i=0; i<arrLinks.length; i++ ) {

		// is it an AMAZON link
		var intIndex = arrLinks[i].href.toLowerCase().indexOf( 'amazon.' );

		if ( intIndex > 0) {
//alert(arrLinks[i].href);
			// find ASIN
			var arrResults = objRegexAsin.exec( arrLinks[i].href );
			if ( arrResults ) {
				// REPLACE URI
				var strAsin = arrResults[1];
				arrLinks[i].href  = '/og.php?url=http://www.amazon.' + strTld + '/exec/obidos/ASIN/' + strAsin + '/?tag=' + strAffiliateId;
			}

			// find Search
			var arrResults = objRegexSearch.exec( arrLinks[i].href );
			if ( arrResults ) {
				// REPLACE URI
				var strSearch = arrResults[0];
//				alert(strSearch);
				arrLinks[i].href  = '/og.php?url=http://www.amazon.' + strTld + '/s?index=blended&tag=' + strAffiliateId + '&' + strSearch;
			}


		}
	}
}

// initialise on page LOAD
if (window.addEventListener){
    window.addEventListener('load',testip,false); //W3C
} else{
    window.attachEvent('onload',testip); //IE
}

// these are the author's affiliate IDs. They will only be used for localities you do not provide an ID for.
var arrAffiliatesSpares = {
	'co.uk' : '011080-21',
	'com'	: '011080-20',
	'de'	: '011080de-21',
	'fr'	: '011080fr-21',
	'ca'	: '011080ca-20',
	'jp'	: '011080-22'
}

