$.ajaxSetup( {
type : 'POST',	   
 url  : 'http://www.forvault.com/ajax.php'
} ); 

function getserver(){
	if($.trim($('#quick_game_select').val())==''){
		$('#serverlist').html('<select  disabled="disabled" class="textinput" style="width:190px"><option value="">Choose Sever</option></select>');
		$('#numlist').html('<select id="numlist" disabled="disabled" class="textinput" style="width:190px"><option value="">Amount and Price</option></select>');
		$('#pirce_quickgold').val(''); 
		return;	
		}
var gameidArray = $('#quick_game_select').val().split('|');
  gameid = gameidArray[0];
  $("#server").attr('disabled','disabled');
  $("#num").attr('disabled','disabled');
  $("#PriceType").attr('disabled','disabled');
  $("#pirce_quickgold").attr('value','');
  if($('#quick_game_select').val()!='1'){
  $("#load_game_no_rs").attr('style','display:block');
  }else{
  $("#load_game_no_rs").attr('style','display:none');
  }
  $("#load_ajax_quickgoldgamelist").attr('style','display:block');
  
  //document.getElementById("load_ajax_quickgoldgamelist").style.display = 'block';
  //alert($("#load_ajax_quickgoldgamelist").attr('style'));
  $.ajax({
 data : 'game_id='+gameid+'&type=server',
 //dataType : 'script',
 success : function(data){	 
             eval('var serverArray='+data);
			 
			  var str_serverList='<select class="textinput" id="server" name="server" onchange="getproducts();" style="width:190px">';
		           str_serverList+='<option value="">Choose the server</option>';
		      
			for(var key in serverArray){
				 if(key=='add'){continue;}
				str_serverList+='<option value="'+serverArray[key][0]+'|'+serverArray[key][2]+'" >'+serverArray[key][1]+'</option>';
			   }
		   str_serverList+='</select>';
		 $('#serverlist').html(str_serverList);
		 $("#load_ajax_quickgoldgamelist").attr('style','display:none');
				}
	   	 })		

}

function getsalesmanager()
{
var gameidArray = $('#quick_game_select').val().split('|');
  gameid = gameidArray[0];
  $.ajax({
 data : 'game_id='+gameid+'&type=salesmanager',
 //dataType : 'script',
 success : function(data){ 
             eval('var managerArray='+data);
			 
			  var str_managerList='<select class="textinput" id="SalesManager" name="SalesManager" onchange="get_last_price();" style="width:190px">';
		           str_managerList+='<option value="">Sales Manager</option>';
					for(var key in managerArray){
				 if(key=='add'){continue;}
				str_managerList+='<option value="QQ:'+managerArray[key][0]+',name:'+managerArray[key][1]+'" >'+managerArray[key][1]+'</option>';
			   }
		   str_managerList+='</select>';
		 $('#SalesMangers').html(str_managerList);				
				}
	   	 })		

}

function getproducts(){
	if($.trim($('#server').val())==''){
		$('#numlist').html('<select id="num" disabled="disabled" class="textinput" style="width:190px"><option value="">Scegli importo</option></select>');
		$('#pirce_quickgold').val(''); 
		return;	
		}
  $('#MangerTotalPrice').val('Total Price');
  $('#SalesMangerPrice').css('display','none');
  $('#SalesManager').attr('disabled','diabled');
  $("#num").attr('disabled','disabled');
  $("#pirce_quickgold").attr('value','');
  $("#load_ajax_quickgoldgamelist").attr('style','display:block');
var serveridArray = $('#server').val().split('|');
  serverid = serveridArray[0];
  $.ajax({
 data : 'server_id='+serverid+'&type=products',
 //dataType : 'script',
 success : function(data){	 
                   
             eval('var productArray='+data);
			
			 var priceTypeArr = $('#PriceType').val().split('-');

			 switch(priceTypeArr[0])
				{
				 case 'USD':
					 priceTags ='$';
					 break;
				case 'GBP':
					 priceTags ='\u00a3';
					 break;
				case 'EUR':
					 priceTags ='\u20ac';
					 break;

				}
			  var str_productList='<select class="textinput" id="num" onchange="get_last_price();" style="width:190px">';
			for(var key in productArray){
				 if(key=='add'){continue;}
				productarray = productArray[key][0].split('-');
				productnum = $.trim(productarray[productarray.length-1].replace(/[^0-9]/ig,""));
				productnumunit = checknum(productarray[productarray.length-1]);
				productid = productArray[key][2];
				productprice = (productArray[key][1]*priceTypeArr[1]).toFixed(2);
				str_productList+='<option value="'+productnum+'-'+productprice+'-'+productid+' " >'+productnumunit+' - '+priceTags+productprice+'</option>';
			   }
		   str_productList+='</select>';
		 getsalesmanager();
		 $('#MangerTotalPrice').val(parseFloat(productArray[0][1]*priceTypeArr[1]).toFixed(2));
		 $('#numlist').html(str_productList);
		 $("#load_ajax_quickgoldgamelist").attr('style','display:none');
		 $('#PriceType').attr('disabled','');
				}
	   	 })		

}

function checknum(str)
{
	var start; 
	for(i=0;i<str.length;i++)  
	{
		if("0123456789".indexOf(str.substr(i,1))>-1) 
		{
			start = i;
			break;
		}
	}  
	return  str.substr(start);
}


function check_quick()
{   
	if($('#num').val() == '')
	{
		alert('Choose server');
		return false;
	}
	numarray = $("#num").val().split('-');
	ProdunctId = numarray[2];
var GameName = $('#quick_game_select').find('option:selected').text();
var GameIdArray = ($('#quick_game_select').find('option:selected').val()).split('|');
var GameId = GameIdArray[0];
var GameErpId = GameIdArray[1];
var ServerIdsArray = ($('#server').find('option:selected').val()).split('|');
var ServerIds = ServerIdsArray[0];
var ServerErpId = ServerIdsArray[1];
if(method=="" || GameName=="Runescape"){
   var url = "checkout_leveling.html?buy_id="+GameId+"_"+ServerIds+"_"+ProdunctId+"&action=pl_buy_now";
   window.location = url;
   return false;
}
	
	
	
	if($("#CharactorName").val() == 'Character Name')
	{
		alert('Please fill in your Character!');
		$("#CharactorName").focus();
		return false;
	}
	if($('#CustomerEmail').val()=='Email'){
		alert('Enter Email');
		$('#CustomerEmail').focus();
		return false;
	}
	if ($("#CustomerEmail").val().search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
	{
		alert('Email Address Wrong!');
		$("#CustomerEmail").focus();
		return false;
	}
	if ($("#CustomerPhone").val() == 'Phone Number')
	{
		alert('Input Phone Number');
		$("#CustomerPhone").focus();
		return false;
	}
    var re = /^[0-9]*$/;     
     if (re.test($('#CustomerPhone').val()) == false)
    {   
	    alert('Phone Wrong');
	    $('#CustomerPhone').focus();
		return false;
    }
	numtextarray = $('#num').find('option:selected').text().split('-');
	priceTypeArr = $('#PriceType').val().split('-');
	productsnum = "35|1|"+GameErpId+"|"+ServerErpId+"|"+numarray[0];
	productsname = '35|1 x-'+ GameErpId +'- '+ServerErpId+' - '+numarray[0]; 
	os0 = $("#CharactorName").val()+"|0|0"; 
	os1 = $("#CustomerEmail").val()+'|0|'+$("#CustomerPhone").val()+'|0'; 
	
	var method = document.getElementsByName("PayType");
	for(var i=0;i<method.length;i++){
		if(method[i].checked){
			method=method[i].value;
			break;
		}
	}
	if($('#SalesManager').val()=='')
	{
		totalprice = numarray[1];
		os3 = ' |'+$("#CustomerEmail").val()+'|'+$("#CustomerPhone").val();
	}
	else
	{
		totalprice = $('#MangerTotalPrice').val();
		os3 = 'Manager Price:'+$("#SalesMangerPrice").val()+ $("#SalesManager").val()+'|'+$("#CustomerEmail").val()+'|'+$("#CustomerPhone").val();
	}

	switch(method){
		case "1":
			$("#amount").val(totalprice);
			$("#currency_code").val(priceTypeArr[0]);
			$("#item_name").val(productsname);
			$("#item_number").val(productsnum);
			$("#os0").val(os0);
			$("#os1").val(os1);
			$("#os3").val(os3);
			$("#theform").submit();
			return false;
			break;
		case "2":
			$("#price").val(totalprice);
			$("#tm_currency_code").val(priceTypeArr[0]);
			$("#game").val($('#quick_game_select').find('option:selected').text());
			$("#server").val($('#server').find('option:selected').text());
			$("#gold").val(numarray[0]);
			$("#char").val($("#CharactorName").val());
			$("#mail").val($("#CustomerEmail").val());
			$("#phone").val($("#CustomerPhone").val());
			$("#payment_other").submit();
			/*form_eur = document.getElementById("payment_other");
			form_eur.server2.value = $('#server').find('option:selected').text();
			form_eur.game.value = $('#quick_game_select').find('option:selected').text();
			form_eur.price.value = totalprice;
			form_eur.gold.value = numarray[0];
			form_eur.tm_currency_code.value = priceTypeArr[0];
			form_eur.char.value = $("#CharactorName").val();
			form_eur.mail.value = $("#CustomerEmail").val();
			form_eur.phone.value = $("#CustomerPhone").val();
			form_eur.submit();*/
			return false;
			break;
		case "3":
			form_wes = document.getElementById("payment_other_western");
			form_wes.server2.value = $('#server').find('option:selected').text();
			form_wes.game.value = $('#quick_game_select').find('option:selected').text()+" "+$('#server').find('option:selected').text()+" "+numarray[0]+"Gold";
			form_wes.price.value = totalprice;
			form_wes.gold.value = numarray[0];
			form_wes.tm_currency_code.value = priceTypeArr[0];
			form_wes.manger.value = 'Manager Price:'+$("#SalesMangerPrice").val()+priceTypeArr[0]+',name:'+$('#SalesManager').find('option:selected').text();
			form_wes.manger_price.value = $("#SalesMangerPrice").val();
			form_wes.char.value = $("#CharactorName").val();
			form_wes.mail.value = $("#CustomerEmail").val();
			form_wes.phone.value = $("#CustomerPhone").val();
			form_wes.submit();
			return false;
			break;
	}
}

 function get_last_price()
{
	var numarray = $("#num").val().split('-');
	if($('#SalesManager').val()=='')
	{
		$('#MangerTotalPrice').val(numarray[1]);
		$('#SalesMangerPrice').css('display','none');
		return false;
	}
	var gold_num=parseFloat(numarray[0]);
	var percent_0_3000=0.039;
	var percent_3000_10000=0.035;
	var percent_10000_100000=0.032;
	var percent_100000=0.029;
	var usd=0.30;
	var gbp=0.20;
	var eur=0.35;
	var cad=0.55;
	var jpy=40;
	var aud=0.40;
	var priceTypeArr = $('#PriceType').val().split('-');
	var currency_type=priceTypeArr[0];
	if(0 < gold_num <=3000)
	{
		manager_price_percent=percent_0_3000;
	}
	else if(3000 < gold_num <=10000)
	{
		manager_price_percent=percent_3000_10000;
	}
	else if(10000 < gold_num <=100000)
	{
		manager_price_percent=percent_10000_100000;
	}
	else if(gold_num >10000)
	{
		manager_price_percent=percent_100000;
	}
	else
	{
		manager_price_percent=percent_0_3000;
	}

	if(currency_type=='USD'){
		manager_price_add=usd;
	}
	else if(currency_type=='EUR')
	{
		manager_price_add=eur;
	}
	else if(currency_type=='GBP')
	{
		manager_price_add=gbp;
	}
	else if(currency_type=='CAD')
	{
		manager_price_add=cad;
	}
	else if(currency_type=='JPY')
	{
		manager_price_add=jpy;
	}
	else if(currency_type=='AUD')
	{
		manager_price_add=aud;
	}
	else
	{
		manager_price_add=usd;
	}
	$('#SalesMangerPrice').css('display','');
	var salestotalprice = parseFloat(manager_price_add+manager_price_percent*parseFloat(numarray[1])).toFixed(2);
	$('#SalesMangerPrice').val(salestotalprice);
	var MangerTotalPrice = parseFloat(parseFloat(salestotalprice)+parseFloat(numarray[1])).toFixed(2);
	$('#MangerTotalPrice').val(MangerTotalPrice);
//$("totalprice_manager").value=parseFloat(manager_price_add+manager_price_percent*$("totalprice").value).toFixed(2);
//$('manager_control').style.display="";
//$("totalprice_last").value=(parseFloat($("totalprice_manager").value) +  parseFloat($("totalprice").value)).toFixed(2);
	 }


//server start
function selgamefuns()
{
  $("#load_ajax_quickgoldgamelist2").attr('style','display:block');
 var $cpath=(document.getElementById('gamesel').value).toString();
 var $sort=document.getElementById('sort').value;
 var $str_cpath = $cpath.replace("cPath=","");
 var $serverNums = $str_cpath.split("_");
 var $gamename=document.getElementById('gamename').value;
 var $game_name2 = $gamename.replace(" ","-");
 var $game_name3 = $game_name2.replace(" - ","-");
 var $game_name4 = $game_name3.replace(" ","-");
 var $game_name4 = $game_name4.toLowerCase();
 var $server_name = document.getElementById('gamesel').options[document.getElementById('gamesel').selectedIndex].text;
 var $server_name2 = $server_name.replace(" - ","-");
 var $server_name3 = $server_name2.replace(" ","-");
 var $server_name4 = $server_name3.toLowerCase();
 var $currency=document.getElementById('curreys').options[document.getElementById('curreys').selectedIndex].text;
 var $currcury_code="";
	 switch($currency){
		  case 'USD':
			$currcury_code = '$';
			break;
		  case 'EUR':
			$currcury_code = '&euro;';
			break;
		  case 'GBP':
			$currcury_code = '&pound;';
			break;
	 }
 var $currcury_code_ss = document.getElementById('curreys').options[document.getElementById('curreys').selectedIndex].value;

  $.ajax({
// data : 'game_id='+$serverNums[0]+'&server_id='+$serverNums[1]+'&type=products',
 data : 'server_id='+$serverNums[1]+'&type=products',

 success : function(data){	 

             eval('var productArray='+data);
			 var str_productList='<table>';
			for(var key in productArray){
				 if(key=='add'){continue;}
					 if($serverNums[0]=="1"){
						str_productList+='<tr><td width="360" style=" padding-left:15px; text-align:left; padding:10px;" >'+$.trim(productArray[key][0])+'</td><td width="100">'+$currcury_code+(parseFloat(productArray[key][1].replace("\t","")*$currcury_code_ss)).toFixed(2)+'</td><td width="140" style="text-align:center"><a href="checkout_leveling.html?buy_id='+$serverNums[0]+'_'+$serverNums[1]+'_'+$.trim(productArray[key][2])+'&action=pl_buy_now"><img class="listingBuyNowButton" height="17" width="81" title=" Buy Now " alt="Buy Now" src="includes/templates/template_default/buttons/english/button_buy_now.gif"/></a></td></tr>';
					 }else{
				 		str_productList+='<tr><td width="360" style=" padding-left:15px; text-align:left; padding:10px;">'+$.trim(productArray[key][0])+'</td><td width="100">'+$currcury_code+(parseFloat(productArray[key][1].replace("\t","")*$currcury_code_ss)).toFixed(2)+'</td><td width="140" style="text-align:center"><a href="'+$game_name4+'-'+$server_name4+'-gold-'+$str_cpath+'.html?products_id='+$.trim(productArray[key][2])+'&action=buy_now&currency='+$currency+'&sort='+$sort+'"><img class="listingBuyNowButton" height="17" width="81" title=" Buy Now " alt="Buy Now" src="includes/templates/template_default/buttons/english/button_buy_now.gif"/></a></td></tr>';
					 }
			   }
		   str_productList+='</table>';
		 $('#proclist').html(str_productList);
		 $("#load_ajax_quickgoldgamelist2").attr('style','display:none');				
				}
	   	 })		

}
