/** userside ajax **/
//some variables to help handle states
var READY_STATE_UNINITIALIZED 	= 0;
var READY_STATE_LOADING			= 1;
var READY_STATE_LOADED			= 2;
var READY_STATE_INTERACTIVE		= 3;
var READY_STATE_COMPLETE		= 4;
var req;
var params;

var addNewAction_f				= 0;

//generalForm val used with ajax
var validForm 					= 0;

//commonUrls
var ladderUserURL					= "/php/ajaxscripts/getLadderUser.php";
var strLadderUserParams				= "";
var ladderUserPath					= "getLadderUser";

var SubTypeValidateUrl				= "/php/ajaxscripts/validPurchaseType.php";

//checks for borwser compatability and sets appropriatly
function getXMLHTTPRequest(){
 var xRequest	= null;
 if(window.XMLHttpRequest){
  xRequest = new XMLHttpRequest;
 }else if(typeof ActiveXObject){
  xRequest = new ActiveXObject('Microsoft.XMLHttp');
 }

 return xRequest;
}


/**
  * reqType determins action
  */
function sendServerRequest(reqType,HttpMethod){
 if(!HttpMethod){
  HttpMethod = "POST";
 }
 
 req  	= getXMLHTTPRequest();
 
 if(req){
  switch(reqType){
  	case 'getLadderUser':
  		url						= ladderUserURL;
  		userNameValue			= getDiv('ladderUser').value;
  		pathValue				= document.frmGift.ddlLadders.options[document.frmGift.ddlLadders.selectedIndex].value;
  		params					= "str_LadderUser="+userNameValue+"&ddlLadders="+pathValue;
  		
  		req.onreadystatechange  = validate;
	break;
	case 'ValidSubPurchase':
		url						= SubTypeValidateUrl;
		subType					= document.frmGift.ddlSub_type.options[document.frmGift.ddlSub_type.selectedIndex].value;
		userNameValue			= getDiv('ladderUser').value;
  		pathValue				= document.frmGift.ddlLadders.options[document.frmGift.ddlLadders.selectedIndex].value;
  		params					= "str_LadderUser="+userNameValue+"&ddlLadders="+pathValue+"&ddlSub_type="+subType;
  		
  		req.onreadystatechange  = validPurchase;
	break;
  }	
  req.open(HttpMethod,url,true);
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  req.send(params);
 }
 
 if(validForm < 1){ 
  return false;
 }else{
  return true;
 }
}

function displayErrorDiv(divID,strMessage){
	oDivMsg	= getDiv(divID);
	oDivMsg.innerHTML	= strMessage;
	show(divID);
}

//checks status of user of ladder check
function validate(){
  var ready      = req.readyState;
  var data		 = null;
  
  oDivMsg	= getDiv('idMsg');
  
    if(ready == READY_STATE_COMPLETE && req.status == 200){
     data = req.responseText;
     
	     if(data >= 1){
	     	oDivMsg.innerHTML	= "Valid User Found";
	     	validForm			= 1;

	     	gift_Submit();
	     	
		document.frmGift.submit();		

	     	//check mem type 
	     	//sendServerRequest('ValidSubPurchase');
	     }else{
	     	validForm			= 0;
	     	displayErrorDiv('idMsg','WARNING: InValid User');
	     	gift_Submit();
	     }

    }else{
     //add loading text here	
     	oDivMsg.innerHtml	= "Loading...";
     	show('idMsg');
    }
}

/** sees if user can ourchase account type **/
function validPurchase(){
  var ready      = req.readyState;
  var data       = null;

  oDivMsg	= getDiv('idMsg');
  
    if(ready == READY_STATE_COMPLETE && req.status == 200){
     data = req.responseText;
     
	     if(data == 1){
	     	oDivMsg.innerHTML	= "User Found!";
	     	show('idMsg');
	     	validForm			= true;
	     }else{
	     	validForm			= false;
	     	oDivMsg.innerHTML	= data;
	     	show('idMsg');
	     }
    }else{
     //add loading text here	
     	oDivMsg.innerHtml	= "Loading...";
     	show('idMsg');
    }
}


/** ofunctions associated with ajaxScripts above **/
function gift_Submit(){
	//check for valid gift submit form
	//misleeading name this gets any id object
	
	//sendServerRequest('getLadderUser');
	
	oUser = getDiv('ladderUser').value;
	
	//make sure we are good uptill here
	if(validForm > 0){
		if(oUser.value == ""){
			return false;
		}else{
			document.frmGift.btn_Next.disabled = true;
		}
	}else{
		return false;
	}
}