$(document).ready(function(){	
	 	$('#buy_credits').change(function(){ 
			page_total_update();
			});	
		$('#feu_input_username').defaultValue('Username');
		$('#feu_input_password').defaultValue('Password');
		$('#buy_credits').change();	
		$('.color_selector').change();
	});
var PayPalItemList = ''+
'{"m01":{"Price":"9.95","paypalName":"Linkrealms Store 1 Month Claim","ItemTypeID":"1233","Quantity":"1","Bonus":{"Credits":"20","Bank":1}},'
+'"m03":{"Price":"26.95","paypalName":"Linkrealms Store 3 Month Claim","ItemTypeID":"1507","Quantity":"1","Bonus":{"Credits":"54","Bank":1}},'
+'"m06":{"Price":"47.95","paypalName":"Linkrealms Store 6 Month Claim","ItemTypeID":"1508","Quantity":"1","Bonus":{"Credits":"96","Bank":1}},'
+'"m12":{"Price":"83.95","paypalName":"Linkrealms Store 12 Month Claim","ItemTypeID":"1509","Quantity":"1","Bonus":{"Credits":"168","Bank":1}},'
+'"c033":{"Price":"9.95","paypalName":"Linkrealms Store 33 credits","ItemTypeID":"1","Quantity":"33","Bonus":{"Credits":"33","Bank":1}},'
+'"c071":{"Price":"19.95","paypalName":"Linkrealms Store 71 credits","ItemTypeID":"1","Quantity":"71","Bonus":{"Credits":"71","Bank":1}},'
+'"c134":{"Price":"34.95","paypalName":"Linkrealms Store 134 credits","ItemTypeID":"1","Quantity":"134","Bonus":{"Credits":"134","Bank":1}},'
+'"c208":{"Price":"49.95","paypalName":"Linkrealms Store 208 credits","ItemTypeID":"1","Quantity":"208","Bonus":{"Credits":"208","Bank":1}},'
+'"c375":{"Price":"74.95","paypalName":"Linkrealms Store 375 credits","ItemTypeID":"1","Quantity":"375","Bonus":{"Credits":"375","Bank":1}}}'

var Y = YUI();
var iList;
Y.use('json', function(Y) { 
	try {	
		iList=Y.JSON.parse(PayPalItemList);	
		PickedItem="m01";	
		ItemData=iList[PickedItem];
		}
		catch(e) {	alert("Sorry, we are experiencing a small problem. We will retun as soon as possible.");	}
		});
/*$.getJSON("js/usrinfo.php?action=pId-list",
		function(resp){	player_list=resp.data;	}
		);
$.getJSON("js/usrinfo.php?action=pId-ls",
		function(resp){	player_ls=resp.data;	}
		);
*/
function change_player_id() {
		giftID=document.getElementById('gift_id').value.substr(2);
		msgnme=document.getElementById('msgname');
		url='/js/name.php?id='+giftID;
		$.get(url,function(data) {
				if((data=="null\n")||(data==null)){
					alert('No valid user!');
				}else{
					msgnme.innerHTML = data;
//					alert('Load was performed - Result:'+data);
				}
			}
			);



//		msgnme.innerHTML=PlayerName;
	}
function togglegiftline(obj)	{
		var el = document.getElementById(obj);
		var choice = document.getElementById('gift_choice');
		var disp= (choice.checked == true ? 'none' : 'block' );
		el.style.display = disp;
	}
function imagebox(a, b,c )	{
		temp= a.options[a.selectedIndex].value;
		image="images/store/CreditStake/"+c+temp+".gif";
		image_holder= document.getElementById(b);
		image_holder.src=image;
	}
function page_total_update()	{
    var ItemForm=document.forms['buy_credits'];
    var PickedItem = $("input[@name='item_choice']:checked").val();
	ItemData=iList[PickedItem];	
   	var amount 	= document.getElementById('amount');
	var iname 	= document.getElementById('item_name');
	var inumber = document.getElementById('item_number');
	
	amount.value	=ItemData.Price;
    iname.value		=ItemData.paypalName;
    inumber.value	=PickedItem;
}

function submit_form()
{		
    var not_loggedin=false;
    sID=false;
	var loop=0;
    var el = (document.getElementById('gift_player_options').style.display == 'none' ? 'Self' : 'Gift');
	try { sID=document.getElementById('sender_id').value; } catch (e) {  not_loggedin=true;    }	
	try { storeStatus=document.getElementById('store_id').value; } catch (e) {  not_loggedin=true;    }	
	if ( ( not_loggedin==true ) || ( sID == false) || (sID<0) ) {	alert("Sorry, you must be logged in to a valid, active Linkrealms game account to proceed");        return;    }

	if ( storeStatus=="1"){		alert("Your store account is currently suspended, please contact support to resolve this issue:  store@linkrealms.com");	return;	} 

	var cMessage =document.getElementById('custom');
	var form=document.forms['buy_credits'];
	var inumber = document.getElementById('item_number');

	if(inumber.value[0]="m") { type="stake"; } else if  (inumber.value[0]="c"){ type="credits";} else {alert("Bad Product type chosen");return false;}
	
	if(el=="Gift") {
		 
		var temp=document.getElementById('gift_id').value;
		var rID=temp.slice(2);
		var serverID=temp.slice(0,1);
		var PlayerName=msgnme=document.getElementById('msgname').innerHTML;
		var custom_message=new Object();
		custom_message.gift_message     =document.getElementById('package[message]').value;
		custom_message.gift_package     =document.getElementById('gift_package_select').selectedIndex;
		custom_message.gift_cardsel     =document.getElementById('gift_card_select').options[document.getElementById('gift_card_select').selectedIndex].value;	
		custom_message.sID              =sID;
		custom_message.rID              =rID;
		custom_message.serverID         =serverID;
		var gift_package_text			=document.getElementById('gift_package_select').options[document.getElementById('gift_package_select').selectedIndex].text;  
		var gift_cardsel_text           =document.getElementById('gift_card_select').options[document.getElementById('gift_card_select').selectedIndex].text;		
		cMessage.value=Y.JSON.stringify(custom_message);
		Message							="You have chosen to give '"+ItemData.paypalName+"' worth $"+ItemData.Price+" to\n"+PlayerName+"\nin a "+gift_package_text+" box with a "+gift_cardsel_text
											+" card and the message:"
											+"\n==========================\n"
											+custom_message.gift_message
											+"\n==========================\n"
											+"Please choose Ok to proceed, or cancel to make changes."; 
												
	}else {
		var custom_message              =new Object();
		custom_message.gift_package     =0;
		custom_message.gift_message     =0;
		custom_message.gift_cardsel     =0;
		custom_message.rID              =-1;
		custom_message.sID              =sID;
		custom_message.serverID         =1;
		cMessage.value                  =Y.JSON.stringify(custom_message);
		Message                         ="You have chosen to purchace '"+ItemData.paypalName+"' worth $"+ItemData.Price+"\nPlease choose Ok to proceed, or cancel to make changes.";
	}
	
var formRet=document.getElementById('pplRtnVal');
formRet.value="http://www.linkrealms.com/index.php?page=paypal-success-"+type+"-"+el+"&gift=PlayerName";
var r=confirm(Message);
if (r==true) {
        document.forms['buy_credits'].submit();
    } else {
		}
}
function ajax_load(url,dest)
{  
    var tmp = url + "&showtemplate=false";
    var tmp2 = tmp.replace(/amp;/g,'');
    $(dest).load(tmp2);
// alert('Attempted to load '+tmp2+' to ' + dest);
}
function doLogin() {
    ajax_load('http://www.linkrealms.com/index.php?page=loginpage','#LoginBox');
}
function selectUpdate(id){
    var newUrl=null;
    var cId="color_id"+id;
    var apID="add_product"+id;
    var select = document.getElementById(cId);
    var selOpt = select.options[select.selectedIndex];
    select.style.Color=selOpt.style.Color;
    select.style.backgroundColor=selOpt.style.backgroundColor;
    var cart_link=document.getElementById(apID); //   ([?&])color=([^&]*)([ &])
    var url=cart_link.href;
if(url.indexOf('option')==-1){
        newUrl=url+"&option="+selOpt.value;
    }else {
       newUrl=url.replace(/option=[\w]*/ig,"option="+selOpt.value);
    }
    if(newUrl==url) {
        result="no change";
    }else {
        result="change";
    }
    cart_link.href=newUrl;
}

