var countryCode = "";
var CountryName = '';
var Currency = '';

if (document.cookie && document.cookie.indexOf('Country')>-1){

	countryCode = readCookie('Country');

	if(! countryCode.indexOf('none'))countryCode='';

	if(countryCode != '')
		document.write("<SCRIPT LANGUAGE='JavaScript' SRC='http://www.oag.com/oag/resources/js/"+countryCode + ".js'><\/SCRIPT>");
}



function checkDef(versionId){

price = '' ; 



var n = itemList.findItem(versionId);

if(n != null){
	price =  n.getPrice();
}	



/*if(typeof pid[versionId] != 'undefined')
 price = pid[versionId];
 */

 
 
return price ; 
}


function getPriceType(versionId){

var priceType = -1; 

var n = itemList.findItem(versionId);

if(n != null) return n.getPriceType();

/*if(typeof pid[versionId] != 'undefined')
 price = pid[versionId];
 */

 
 
return priceType ; 
}



function showTable(TableId,hideTableId){

	if(document.all){ 
	 document.all[TableId].style.display="";
	 document.all[hideTableId].style.display="none";
	}
	else if (document.getElementById)
	{
		document.getElementById(TableId).style.display="";
		document.getElementById(hideTableId).style.display="none";
	}
	
}

function setCountry (fieldName) {
var expires = new Date();
expires.setTime(expires.getTime() + (24 * 60 * 60 * 1000) * 365 );

var c_str = "Country="+fieldName+ ";expires="+expires.toGMTString() ;
c_str+="; path=/; domain=.oagcargoflights.com;";

document.cookie= c_str;
}


function getValidPriceCount(editionData){

	var versionArray=editionData.split('#');

	var validPriceCount = 0 ; 
	
	for (i=0; i < versionArray.length; i++){
	
	var editionArray=versionArray[i].split('~');
	versionId=editionArray[1];
	show=editionArray[2];
	group=editionArray[4];

		if(group =='WorldMate' || group == 'VisualIT' || group == 'PriorityPass') validPriceCount++;

		if(show == 'Y' && checkDef(versionId) != '') validPriceCount++;
	
	}
	
	return validPriceCount ; 
}



function displayEditions(editionData) {

var containsMonthlyBilling = false ;


Currency = itemList.getCurrency();
var OAGRegion = itemList.getOAGRegion();



// if any variables not set do not do table
if(Currency == '' || editionData == '' || countryCode =='' ) return ;
//loop through editions and check there is a valid price, if not - return

var validPriceCount = getValidPriceCount(editionData);



if(validPriceCount == 0) return;

//Start table

var entryList = new items();
entryList.setOAGRegion(OAGRegion);
entryList.setCurrency(currency);



//break into records on #, then break into fields on ~
var versionArray=editionData.split('#');

for (i=0;i<versionArray.length;i++){

var editionArray=versionArray[i].split('~');
versionDesc=editionArray[0];
versionId=editionArray[1];
show=editionArray[2];
showBuy=editionArray[3];
group=editionArray[4];
prod=editionArray[5];



//look up price and if any errors set no show
price = checkDef(versionId);
priceType = getPriceType(versionId) ; 

var editionItem = new item();
editionItem.setEdition(versionDesc);
editionItem.setId(versionId);
editionItem.setPriceType(priceType);
editionItem.setPrice(price);
editionItem.setShowBuyButton(showBuy);
editionItem.setGroup(group);


if(showBuy == 'N'){
	editionItem.setShowBuyButton(false);
}

if(priceType == 0 || priceType == 2) containsMonthlyBilling = true ; 

if(price == ""){
	show = "N"
	
}
//set temp value for currency so it is not overwritten by WorldMate excpetion, this allows USD and other currency on same page
displayCurrency=Currency;
//set ordering urls
// old url -var OAGUrl = "https://secure.oag.com/oag_com/new_version/Product_Catalog/buy_now.asp?product_id=" + versionId + "&ctyorg=" + countryCode;
var OAGUrl = "https://safe.oag.com/cart/OAGAddItemToShoppingCart.do?regionCode=" + OAGRegion + "&subscriptionDefId=" + versionId + "&country=" + countryCode;
var worldmateUrl = "http://www.handango.com/minibrand/basket.jsp?siteId=783&addItem=" + versionId + "&bNav=1&continueUrL=http://www.oag.com&logo=http://www.mobimate.com/pics/logo_oag.gif";
// -- when ff is in shoppping cart un rem the next line and remove marked sections
var flightfinderUrl="/cart/OAGAddItemToShoppingCart.dosubscriptionDefId="+versionId+ "&country="+countryCode+ "&regionCode=" + OAGRegion;
var priorityUrl = 'http://scripts.affiliatefuture.com/AFClick.asp?affiliateID=30900&merchantID=340&programmeID=3301&mediaID=0&tracking=&url=';


//set url based on group
switch(group)
{

case "PriorityPass":
//orderUrl="<a href='" + priorityUrl + "' class='blacklink' target='new' title='Buy'>Buy</a>";
orderUrl=priorityUrl;
show = 'Y';

switch(versionId)
{
case "000001"://standard

price="99.00";
break;

case "000002"://standard
price="249.00";
break;

case "000003"://standard
price="399.00";
break;


default:
price="POA";
break;
}

displayCurrency="USD";
break;


case "WorldMate" :
//orderUrl="<a href='" + worldmateUrl + "' class='blacklink' target='new' title='Buy'>Buy</a>";
orderUrl=worldmateUrl;
show = 'Y';
switch(versionId)
{
case "68905"://palm
price="74.95";
break;

case "93538"://pocket pc
price="74.95";
break;

case "124358"://UIQ symbian
price="69.95";
break;



case "164356"://Blackberry
price="59.95";
break;

default:
price="POA";
break;
}

displayCurrency="USD";
break;


case "VisualIT":
show = 'Y';
displayCurrency="GBP";
visualitUrl =  'http://store.eSellerate.net/s.asp?';
switch(versionId)
{
case "000001"://palm visual it
price="10.34";
visualitUrl+='s=STR915703045&AFFIDC=AFF5815299349&Cmd=CATALOG&CategoryID=4045';  
orderUrl=visualitUrl;
break;

case "000002"://Pocket PCe visual it
price="10.34";
visualitUrl+='s=STR915703045&AFFIDC=AFF5815299349&Cmd=CATALOG&CategoryID=4044';
orderUrl=visualitUrl;
break;
                  
case "000003"://Sony Ericsson P series visual it
price="10.94";
visualitUrl+='s=STR915703045&AFFIDC=AFF5815299349&Cmd=CATALOG&CategoryID=4946';
orderUrl=visualitUrl;
break;

case "000004"://Series 60 Phones Symbian visual it
price="10.94";
visualitUrl+='s=STR915703045&AFFIDC=AFF5815299349&Cmd=CATALOG&CategoryID=5679';
orderUrl=visualitUrl;
break;

case "000005"://MS Smartphone visual it
price="10.94";
visualitUrl+='s=STR915703045&AFFIDC=AFF5815299349&Cmd=CATALOG&CategoryID=4046';
orderUrl=visualitUrl;
break;

default:
break;
}//end switch 
displayCurrency="GBP";
break;


default:
orderUrl= OAGUrl;
break;
}


editionItem.setPrice(price);
editionItem.setUrl(orderUrl);
editionItem.setCurrency(displayCurrency);

	if(show == 'Y')
		entryList.add(editionItem);

	
}//end loop		

	createPricingTable(entryList) ; 
	//entryList.clear();
	


}

function createPricingTable(entries){

//startPricingTable();

var containsMonthlyBilling = false ; 

var lst = new Array();

lst = entries.getItemsList(); 

for(i=0;i<lst.length;i++){
	
	var tmpitem = lst[i]; 
	
	if(tmpitem.getPriceType() == 0 || tmpitem.getPriceType() == 2) {
		containsMonthlyBilling = true  ; 
	}
	
	var useMonthlyLabel = true; 
	
	
	if(tmpitem.getPriceType() == 2){
		useMonthlyLabel = false ; 
	}
	
}


 addPricingTableHeader(containsMonthlyBilling,useMonthlyLabel);

	for(i=0;i<lst.length;i++){
		
		var monthlyItem = lst[i] ; 
		
		var annualItem = entries.findEdition(monthlyItem.getEdition() , monthlyItem.getId());
		
		if(annualItem != null){
			entries.remove(entries.findItemIndex(annualItem.getId));
		}
		
		if(monthlyItem.getShowBuyButton()) {
			addpricing(monthlyItem,annualItem) ; 
		}

	}

endPricingTable(containsMonthlyBilling);	
	
	lst.clear();
	
}


function addPricingTableHeader(containsMonthlyBilling,useMonthlyLabel){

	document.write('<table width="313" border="0" align="center" cellpadding="0" cellspacing="0" summary="Header detailing editions and price" bgcolor="#e6eef7">');
	document.write('<tr><td width="10"><IMG SRC="'+img_bhl+'" WIDTH="10" HEIGHT="23" BORDER="0" ></td>');
	document.write('<td nowrap="nowrap" width="153" background="'+img_bhm+'" class="whiteboldstandard">Edition</td>');


	if(containsMonthlyBilling){
		document.write('<td nowrap="nowrap" width="77" background="'+img_bhm+'" class="whiteboldstandard">');
		
		if(! useMonthlyLabel){
		document.write('Per Issue');
		}
		else{
		document.write('Monthly*');
		}
		document.write('</td>');
		document.write('<td nowrap="nowrap" background="'+img_bhm+'" class="whiteboldstandard" align="left">Annual</td>');
	}
	else{
	document.write('<td nowrap="nowrap" width="77" background="'+img_bhm+'" class="whiteboldstandard">&nbsp;</td>');
		document.write('<td nowrap="nowrap" background="'+img_bhm+'" class="whiteboldstandard">')
		document.write('Price');
		document.write('</td>');
	}

	document.write('<td width="10"><IMG SRC="'+img_bhr+'" WIDTH="10" HEIGHT="23" BORDER="0" ></td>');
	document.write('</tr><tr height="10"><td></td></tr></table>');

}


function addpricing(monthlyItem,annualItem){

var tmp ; 

if(annualItem != undefined && annualItem.getPriceType() == 2) tmp = annualItem; 

if(monthlyItem.getPriceType() == 1){
	annualItem = monthlyItem ; 
}

if(tmp != undefined && tmp.getPriceType() == 2) monthlyItem = tmp ; 


document.write('<table width="313" cellpadding="0" cellspacing="0" border="0" bgcolor="#e6eef7"><tr>');
document.write('<td class="blacksmall" width="165">');
document.write('<span style="padding-left:5px">');

/*if(monthlyItem == null){
 document.write('&nbsp;');
}*/
document.write(monthlyItem.getEdition() + '</span>') ; 
document.write('</td>');		

document.write('<td');

if(annualItem != null) document.write(' width="65" ');

document.write(' align="left">');

if(monthlyItem.getPriceType() != 1){
	addBuyButton(monthlyItem);
}else{
document.write('&nbsp;');
}


document.write('</td>');
		
document.write('<td width=10><IMG SRC="'+xspacer+'" WIDTH="10" BORDER="0" height="25"></td>');
		
			if(annualItem != null){
				document.write('<td align="left" width="65"">');
				addBuyButton(annualItem);
				document.write('</td>');
			}
			else{
				document.write('<td align="left" width="65"><IMG SRC="'+xspacer+'" WIDTH="20" BORDER="0" ></td>'); 
			}
			
document.write('<td width=10><IMG SRC="'+xspacer+'" WIDTH="10" BORDER="0" height="25"></td>');
document.write('</tr><tr height="10"><td colspan="5"><IMG SRC="'+xspacer+'" height="10" BORDER="0" ></td></tr></table>');


}


function addBuyButton(item){

document.write('<table width="65" border="0" cellpadding="0" cellspacing="0" summary="buy button"><tr>');
		document.write('<td width="5" height="25"><IMG SRC="'+img_li+'" WIDTH="5" HEIGHT="25" BORDER="0" ></td>');
		document.write('<td width="55" align="center" nowrap="nowrap" background="'+img_mi+'">');
		document.write('<a title="Buy" class="blacklink" href="' + item.getUrl() + '">'+getCurrencySymbol(item.getCurrency())  + item.getPrice()+'</a>');
		document.write('<td width="5" height="25"><IMG SRC="'+img_ri+'" WIDTH="5" HEIGHT="25" BORDER="0" ></td>');
		document.write('</tr></table>');

}





function endPricingTable(containsMonthlyBilling){


//document.write('</td></tr>');
if(containsMonthlyBilling){

document.write('<table width="313" bgcolor="#E6EEF7"><tr><td   align="center" style="font-size: 9px;padding-bottom:5px;color:grey;">* Payable by credit card only for a minimum period of twelve months</td></tr></table>');
}
//document.write('</td></tr></table>');
}



function startPricingTable(){

document.write('<table width="313" border="1" cellpadding="0" cellspacing="0" summary="Product Editions pricing table"><tr><td>');

document.write('<table width="313" border="1" cellpadding="0" cellspacing="0" summary="Product Editions pricing table"><tr><td>');
document.write('<table width="313" border="0" align="center" cellpadding="0" cellspacing="0" summary="Header detailing editions and price">');
document.write('<tr><td width="10"><IMG SRC="'+img_bhl+'" WIDTH="10" HEIGHT="23" BORDER="0" ></td>');
document.write('<td nowrap="nowrap" width="125" background="'+img_bhm+'" class="whiteboldstandard"> Edition</td>');
document.write('<td nowrap="nowrap" width="168" background="'+img_bhm+'" class="whiteboldstandard">Price</td>');
document.write('<td width="10"><IMG SRC="'+img_bhr+'" WIDTH="10" HEIGHT="23" BORDER="0" ></td>');
document.write('</tr></table>');
document.write('</td></tr><tr>');
document.write('<td><img src="'+xspacer+'" width="14" height="3" alt=""></td>');
document.write('</tr><tr>');
document.write('<td bgcolor="#E6EEF7"><img src="'+xspacer+'" width="14" height="5" alt=""></td>');
document.write('</tr><tr><td bgcolor="#E6EEF7" class="blackbody">');
}
