var WS  = "http://"+document.location.host+"/get.php";
var WSP = "http://"+document.location.host+"/pl.php";
function check(str)
{
	var len = document.getElementById("sltGames").options.length;
	for(var i=0; i<len; i++)
	{
		if(document.getElementById("sltGames").options[i].value == str)
			document.getElementById("sltGames").options[i].selected = true;
	}
}
shq={
	ex:function(main,ex){for(var k in (ex||{}))main[k]=ex[k];return main}
	,each:function(list,fn){for(var i=0;i<list.length;i++)fn.call(list[i],i,list)}
	,chk:function $chk(obj){return !!(obj || obj === 0)}
	,find:function(selector,box){
		if(typeof(selector)!='string')return selector;
		if(selector.charAt(0)=='.')return (function(className,box){
			var list=[];
			shq.each(shq.find('*',box),function (i){
				if(this.className.indexOf(className)!=-1)list.push(this)
			})
			return list;
		})(selector.replace('.',''),shq.find(box));
		if(selector.charAt(0)=='#')return document.getElementById(selector.replace('#',''));
		return (shq.find(box)||document).getElementsByTagName(selector);
	}
}
function chk(str,htm)
{
	var len = document.getElementById(htm).options.length;
	for(var i=0; i<len; i++)
	{
		if(document.getElementById(htm).options[i].value == str)
			document.getElementById(htm).options[i].selected = true;
	}
}
function addLoadEvent(fn){
        var oldonload=window.onload;

        if(typeof window.onload != 'function'){
                window.onload=fn;
        }else{

                window.onload=function(){
                        oldonload();
                        fn();
                }
        }

}

function xmlhttp()
{
	if ( window.XMLHttpRequest )		// Gecko
		return new XMLHttpRequest() ;
	else if ( window.ActiveXObject ){	// IE
	var MSXML = ['MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
            for(var n = 0; n < MSXML.length; n ++)
            {
                try
                {
                    var objXMLHttp = new ActiveXObject(MSXML[n]);
					return objXMLHttp;
                    break;
                }
                catch(e)
                {
                }
			}

	}
}
$=shq.find;
function getP(val)
{
	var url=WS+"?action=price&code="+val;
      var XML=xmlhttp();
      XML.open("GET",url,true);
      XML.onreadystatechange=function(){

          if(XML.readyState == 4){

            if(XML.status==200){
              var Xd=parseXML(XML.responseXML);
              var Prices = Xd['prices'][0];
              var Price  = Prices['price'];
              if(sP)sP.length=0
              for(var i=0;o=Price[i];i++){
                sP.options[i]=new Option(o['qt']+' $'+Math.round(o['_text']*100)/100,Math.round(o['_text']*100)/100);
                sP.options[i].setAttribute('title',o['discount']);
                sP.options[i].setAttribute('class',o['unit_price']);
			    sP.options[i].setAttribute('proguid',o['guid']);
				sP.options[i].setAttribute('proname',o['qt']);
				//sp.options[i].setAttribute('provalue',o['number']);
                tP.rows[i+1].cells[0].innerHTML=o['qt'];
                tP.rows[i+1].cells[1].innerHTML=Math.round(o['_text']*100)/100;
              }

          tPrice.innerHTML=$('#amount').value=sP.value
          $('#trade_num').value = sP.options[sP.selectedIndex].text.replace(/\s.*$/i,'');
          $('#unit_price').value  = sP.options[sP.selectedIndex].getAttribute('class');
          $('#discount').value    = sP.options[sP.selectedIndex].title;
          $('#server_id').value    = sS.value;//.split(",").pop();alert(sS.value.split(",").pop());alert(sS.value);
          $('#servername').value    = sS.options[sS.selectedIndex].text;





              $('#loadserver').src="../images/spacer.gif";

            }
          }
      }
      $('#loadserver').src="../images/loading.gif";
      XML.send(null);
}
var BG ={
   tmparr:[],
   init:function(){
     sK = $('#sltGames'),sP = $('#sltProducts'),tP = $('#tabproducts'),tPrice=$('#tboxPrice'),sS=$('#sltServers')
	 sK.onchange=BG.getServer;
     sS.onchange=BG.getPrice;
     tP.onclick=BG.setProduct;
     sP.onchange=BG.setProduct;



   },
   getServer:function(){
	   var url=WS+"?action=server&code="+this.value;
	   var XML=xmlhttp();
	   XML.open("GET",url,true);
		XML.onreadystatechange=function(){
			if(XML.readyState == 4){
				if(XML.status==200){
					var Xf=parseXML(XML.responseXML);
					var Prices = Xf['servers'][0];
					var Price  = Prices['server'];
					if(sS)sS.length=0
					  for(var i=0;o=Price[i];i++){
						sS.options[i]=new Option(o['_text'],o['code']);
					  }
					$("#gamename").value = sK.options[sK.selectedIndex].text;
					$("#gameid").value = sK.options[sK.selectedIndex].value;
					getP(sS.value);
					$('#loadserver').src="../images/spacer.gif";
				}
			}
		}
		$('#loadserver').src="../images/loading.gif";
		XML.send(null);
   },
   getPrice:function(){
      var url=WS+"?action=price&code="+this.value;
      var XML=xmlhttp();
      XML.open("GET",url,true);
      XML.onreadystatechange=function(){

          if(XML.readyState == 4){

            if(XML.status==200){
              var Xd=parseXML(XML.responseXML);
              var Prices = Xd['prices'][0];
              var Price  = Prices['price'];
              if(sP)sP.length=0
              for(var i=0;o=Price[i];i++){
                sP.options[i]=new Option(o['qt']+' $'+Math.round(o['_text']*100)/100,o['_text']);
                sP.options[i].setAttribute('title',o['discount']);
                sP.options[i].setAttribute('class',o['unit_price']);
				sP.options[i].setAttribute('proguid',o['guid']);
				sP.options[i].setAttribute('proname',o['qt']);
				//sp.options[i].setAttribute('provalue',o['number']);
                tP.rows[i+1].cells[0].innerHTML=o['qt'];
                tP.rows[i+1].cells[1].innerHTML=Math.round(o['_text']*100)/100;
              }
          tPrice.innerHTML=$('#amount').value=Math.round(sP.value*100)/100;
          $('#trade_num').value = sP.options[sP.selectedIndex].text.replace(/\s.*$/i,'');
          $('#unit_price').value  =('#amount').value;
          $('#discount').value    = sP.options[sP.selectedIndex].title;
          $('#server_id').value    = sS.value;//.split(",").pop();alert(sS.value.split(",").pop());alert(sS.value);
          $('#servername').value    = sS.options[sS.selectedIndex].text;
		  $('#proguid').value=sP.options[sP.selectedIndex].proguid;
		  $('#proname').value=sP.options[sP.selectedIndex].proname;
	

              $('#loadserver').src="../images/spacer.gif";

            }
          }
      }
      $('#loadserver').src="../images/loading.gif";
      XML.send(null);

   },
   setProduct:function(e){

     var setBg=function(e){
        BG.tmparr.unshift(e)
        BG.tmparr.length=2;
        e.style.backgroundColor ='#FCFEB7';
        try{BG.tmparr[1].style.backgroundColor='';}catch(e){}
     }
    switch(this.id.toLowerCase()){
      case 'tabproducts':
          var ev= (window.event||e).srcElement||e.target
          setBg(ev)
          var v = ev.innerHTML;
          if(/\.\d{2}$/i.test(v)){
           // sP.value=v;
          //  tPrice.innerHTML=$('#amount').value=Math.round(v*100)/100;
			//alert(sP.selectedIndex);
          //  $('#trade_num').value = sP.options[sP.selectedIndex].text.replace(/\s.*$/i,'');

          //  $('#unit_price').value =sP.options[sP.selectedIndex].getAttribute('class');
          //  $('#discount').value =sP.options[sP.selectedIndex].title;
			// $('#proguid').value=sP.options[sP.selectedIndex].getAttribute('proguid');
		 // $('#proname').value=sP.options[sP.selectedIndex].getAttribute('proname');
shq.each(sP.options,
                function(i,o){
                 if(this.value.indexOf(v)!=-1){
                     o.selectedIndex=i;tPrice.innerHTML=$('#amount').value=Math.round(sP.value*100)/100;
                     $('#trade_num').value = sP.options[sP.selectedIndex].text.replace(/\s.*$/i,'');
                     $('#unit_price').value =sP.options[sP.selectedIndex].getAttribute('class');
                     $('#discount').value =sP.options[sP.selectedIndex].title;
					 $('#proguid').value=sP.options[sP.selectedIndex].getAttribute('proguid');
		  			 $('#proname').value=sP.options[sP.selectedIndex].getAttribute('proname');
                     }

                })
          }else{
            shq.each(sP.options,
                function(i,o){
                 if(this.text.indexOf(v)!=-1){
                     o.selectedIndex=i;tPrice.innerHTML=$('#amount').value=Math.round(sP.value*100)/100;
                     $('#trade_num').value =sP.options[sP.selectedIndex].text.replace(/\s.*$/i,'');
                     $('#unit_price').value =sP.options[sP.selectedIndex].getAttribute('class');
                     $('#discount').value =sP.options[sP.selectedIndex].title;
					 $('#proguid').value=sP.options[sP.selectedIndex].getAttribute('proguid');
		  			 $('#proname').value=sP.options[sP.selectedIndex].getAttribute('proname');
                     }

                })
          }
      break;
      case 'sltproducts':
          tPrice.innerHTML=$('#amount').value=Math.round(sP.value*100)/100;
          $('#trade_num').value = sP.options[sP.selectedIndex].text.replace(/\s.*$/i,'');
		  $('#proguid').value=sP.options[sP.selectedIndex].getAttribute('proguid');
		  $('#proname').value=sP.options[sP.selectedIndex].getAttribute('proname');
		  var selIndex = sP.selectedIndex;
          var unitPrice = sP.options[selIndex].className;          
	      if( unitPrice == '' || unitPrice == null)
		 	$('#unit_price').value = 1;
		 else
	          $('#unit_price').value = unitPrice;
			  
         $('#discount').value =sP.options[sP.selectedIndex].title;

      break;

    }
    //sP.value=v;
   }

}
addLoadEvent(BG.init)


function parseXML(xml /* req.responseXML */) {
	var obj = { _text : "" };
	var child = xml.firstChild;
	while (child) {
		if ((child.nodeName == "#text") || (child.nodeName =="#cdata-section")) {
			obj._text += child.nodeValue;
		} else if (child.nodeType == 1) {
			if (typeof obj[child.nodeName] == "undefined") {
				obj[child.nodeName] = [];
			}
            obj[child.nodeName][obj[child.nodeName].length] = parseXML(child);

		}
		child = child.nextSibling;
	}

	var att = xml.attributes;
	var i = 0;
	if (att) { // there are attributes
		for (var i = 0; i < att.length; i++) {
			obj[att[i].name] = att[i].value;
		}
	}
	if (obj._text.match(/^\s*$/)) {
		delete obj._text;
	}

	return obj;
}






function checkpost(f){

   if(f.amount.value==""||f.amount.value==0){
	  alert('Please select Quantity');
	  return false;
    }
    if(/\W|^$/g.test(f.character_to.value)){
	  alert('Please input correct character name!');
          f.character_to.focus();
	  return false;
    }
    if(f.customer_full_name.value==""){
	  alert('Please input correct full name!');
          f.customer_full_name.focus();
	  return false;
    }
   var emailv=f.customer_email.value;
   if(!/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(emailv)){
    alert("Please Type correct email");
    f.customer_email.focus();
    return false;
    }

   if(f.customer_phone2.value=="" && (f.phone1.value.length !=3 || f.phone2.value.length !=3 || f.phone3.value.length!=4)){
	  alert('Please Type correct US /Non-US phone number. e.g xxx-xxx-xxxx');
      f.phone1.focus();
	  return false;
    }
	if(f.phone1.value.length =="")
   if((f.phone1.value.length ==0 && f.phone2.value.length == 0 && f.phone3.value.length == 0) && (f.customer_phone2.value.length<5 || f.customer_phone2.value.length>16)){
	  alert('Please type correct  Non-US phone number. e.g xxx-xxxxxxx');
      f.customer_phone2.focus();
	  return false;
    }
    var server = f.servercode
    if(/aegwynn/i.test(server.options[server.selectedIndex].text)){
     var r = window.confirm('Are you sure your server is '+server.options[server.selectedIndex].text+'?');
     if(r){
        return true;
     }else{
       return false;
     }
    }
    return true
}


function checkGoldPost(f){

   if(f.amount.value==""||f.amount.value==0){
	  alert('Please select Quantity');
	  return false;
    }
    if(/\W|^$/g.test(f.character_to.value)){
	  alert('Please input correct character name!');
          f.character_to.focus();
	  return false;
    }
    if(f.customer_full_name.value==""){
	  alert('Please input correct full name!');
          f.customer_full_name.focus();
	  return false;
    }
   var emailv=f.customer_email.value;
   if(!/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(emailv)){
    alert("Please Type correct email");
    f.customer_email.focus();
    return false;
    }

   if(f.customer_phone2.value=="" && (f.phone1.value.length !=3 || f.phone2.value.length !=3 || f.phone3.value.length!=4)){
	  alert('Please Type correct US /Non-US phone number. e.g xxx-xxx-xxxx');
      f.phone1.focus();
	  return false;
    }
	if(f.phone1.value.length =="")
   if((f.phone1.value.length ==0 && f.phone2.value.length == 0 && f.phone3.value.length == 0) && (f.customer_phone2.value.length<5 || f.customer_phone2.value.length>16)){
	  alert('Please type correct  Non-US phone number. e.g xxx-xxxxxxx');
      f.customer_phone2.focus();
	  return false;
    }
	 var addZero=function(n){if(n.length!=2&&((n-0)<10)){return '0'+n}else{return n}}
 	var d=new Date();
	var ordernum=""+d.getFullYear()+addZero(d.getMonth()+1)+addZero(d.getDate())+addZero(d.getHours())+addZero(d.getMinutes())+addZero(d.getSeconds())+addZero(d.getMilliseconds()); 
	 	document .getElementById ("order_number").value=ordernum;
 
	
		
    return true
}