/*
$(document).ready(function(){       
	$('#invitation_code').bind("cut copy paste",function(e) {           
		e.preventDefault();       
	});   
	 
	jQuery.fn.idle = function(time){
		var i = $(this);
		i.queue(function(){
			setTimeout(function(){
				i.dequeue();
			}, time);
		});
	}; 
}); 
*/

function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i< activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
   	alert(e);
    //suppress error
   }
  }
 }
 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  return new XMLHttpRequest()
 else
  return false
}

//Sample call:
//var myajaxrequest=new ajaxRequest()

function makeAjaxRequest(responseId){
	
	var mypostrequest = new ajaxRequest()
	mypostrequest.onreadystatechange = function(){
	 if (mypostrequest.readyState==4){
	 	alert('ready');
	  if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
	  	var response = mypostrequest.responseText;
	  	alert('response:'+response);
	   	document.getElementById(responseId).innerHTML = response;
	  }
	  else{
	   alert("An error has occured making the request")
	  }
	 }
	}
	
	//var fieldata=encodeURIComponent(document.getElementById(req).value);
	//var parameters = req+'='+fieldata;
	//mypostrequest.open("POST", url, true);
	//mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	//mypostrequest.send(parameters);
}

function CheckInvitationCode(obj,req,len,action,responseId,extras){
	
	//alert('event report->'+$.eventReport(req)); 
	//alert (obj.id);
	//return;
	
	//var inputVis = inputObj.style.display;
	//alert(responseId);
	
	
	if(obj.id == 'invitation_code'){
		var invalidTxt = "<span style=\"color: #FF6347;\">This is not a valid invitation code</span>";
		var invCodeRadioRowObj = document.getElementById('row_no_invitation_code'); 
		
		var inputObj = document.getElementById(req);
		var inputTxt = inputObj.value;
		var responseObj = document.getElementById(responseId);
		var responseTxtId = responseId+'txt';
		var responseTxtObj = document.getElementById(responseTxtId);
		var extrasObj = document.getElementById(extras);
		var check_len = inputTxt.length;
		if(check_len < len){
			//alert(responseObj.style.display);
			if(responseObj.value != ''){
				responseObj.value = '';
			}
			responseObj.style.display = 'none';
			return;
		}
		if(extrasObj.style.display == 'none'){
			extrasObj.style.display = 'block';
		}
		if(check_len == len){
			var mypostrequest = new ajaxRequest()
			mypostrequest.onreadystatechange = function(){
				if (mypostrequest.readyState==4){
				 	//alert('ready');
				 	if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
				 		responseObj.style.display = 'block';
				  		var serveResponse = mypostrequest.responseText;
				  		//alert('response:'+server);
				  		responseTxtObj.style.display = 'block';
				  		if(serveResponse != 'invalid'){
				  			//alert(serveResponse);
				  			if(serveResponse == 'NoCV'){
				  				invCodeRadioRowObj.style.display = 'none';
				   				responseTxtObj.innerHTML = serveResponse;
				  			}
				  		}
				  		else{
				  			responseTxtObj.style.color = '#FF6347';
				  			responseTxtObj.innerHTML = invalidTxt;
				  			//setTimeout(responseObj.style.display = 'none', respHide);
				  		}
				  		
				  	}
				 	else{
				   		//alert("An error has occured making the request")
				  	}
				}
			}
			var fieldata=encodeURIComponent(inputTxt);
			var parameters = 'action=ajax&'+req+'='+fieldata;
			mypostrequest.open("POST", action, true);
			mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			mypostrequest.send(parameters);
		}
		else if(check_len > len){
			//var response = invalidTxt;
			responseObj.style.display = 'block';
			responseObj.innerHTML = invalidTxt;
			//setTimeout(responseObj.style.display = 'none', respHide);
		}
	}
	if(obj.id == 'no_invitation_code'){
		//alert(obj.id);
		var invCodeVis = document.getElementById('invcode').style.display;
		if(invCodeVis != 'none'){
		//var acceptObj = document.getElementById('accepterms');
			document.getElementById('apterms').style.display = 'block';
			document.getElementById('invcode').style.display = 'none';
		}
		else{
			obj.checked = false;
			document.getElementById('apterms').style.display = 'none';
			document.getElementById('invcode').style.display = 'block';
			
		}
	}
}

function initAPAvailability(){
	AP = {};
	AP['avail'] = {};
	AP['avail'] = 'Not checked';
}


function CheckAPAvailability(obj,action){
	if(obj.name == 'country'){
		var countryIndex = obj.selectedIndex;
		var country = obj.options[countryIndex].value;
	}
	else if(obj.name == 'partnership'){
		var countryIndex = document.getElementById('country').selectedIndex;
		//alert(countryIndex);
		var country = document.getElementById('country').options[countryIndex].value;
		//alert(country);
		if(country == 'Please select...'){
			return;
		}
	}
	if(document.getElementById('territory') != undefined){
		var checkTP = document.getElementById('territory').checked;
		var checkAP = document.getElementById('associate').checked;
		if(checkTP == true){
			var partnership = 'territory';
		}
		else{
			var partnership = 'associate';
		}
		var responseObj = document.getElementById('row_response');
		var responseTxtObj = document.getElementById('response_text');
		var inputTxt = 'partnership='+partnership+'&country='+country;
		//alert('tp:'+checkTP+' country:'+country);
	}
	
	var mypostrequest = new ajaxRequest()
	mypostrequest.onreadystatechange = function(){
		if (mypostrequest.readyState==4){
			//alert('ready');
			if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
				responseObj.style.display = 'block';
				var serveResponse = mypostrequest.responseText;
				//alert('response:'+serveResponse);
				if(serveResponse != 'invalid'){
					//alert(serveResponse);	
				   	responseTxtObj.innerHTML = serveResponse;
				}
				else{
				   	//alert("An error has occured making the request")
				  }
			}
			else{
				//alert("An error has occured L 187");
			}
		}
	}
	//var fieldata=encodeURIComponent(inputTxt);
	//alert('fieldata->'+fieldata);
	var parameters = 'action=ajax&'+inputTxt;
	mypostrequest.open("POST", action, true);
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	mypostrequest.send(parameters);

}


function CheckAPAvailExpress(obj,action){
	if(obj.name == 'country'){
		//document.getElementById('formsubmit').style.display = 'none';
		var countryIndex = obj.selectedIndex;
		var country = obj.options[countryIndex].value;
		document.getElementById('country_of_interest').value = country;
		if(document.getElementById('express').style.display == 'block'){
			var row_moretp = 'no';
		}
		else{
			var row_moretp = 'yes';
		}
	}
	else if(obj.name == 'partnership'){
		var countryIndex = document.getElementById('country').selectedIndex;
		//alert(countryIndex);
		var country = document.getElementById('country').options[countryIndex].value;
		//alert(country);
		if(country == 'Please select...'){
			return;
		}
	}
	
	var responseObj = document.getElementById('row_response');
	var responseTxtObj = document.getElementById('response_text');
	var inputTxt = '&country='+country+'&return_tp_summary='+row_moretp;
		//alert('tp:'+checkTP+' country:'+country);

	
	var mypostrequest = new ajaxRequest()
	mypostrequest.onreadystatechange = function(){
		if (mypostrequest.readyState==4){
			//alert('ready');
			if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
				responseObj.style.display = 'block';
				var serveResponse = mypostrequest.responseText;
				//alert('response:'+serveResponse);
				if(serveResponse != 'invalid'){
					//alert(serveResponse);	
				   	responseTxtObj.innerHTML = serveResponse;
				   	if(serveResponse.search('express_vis=no') != -1){
				   		document.getElementById('express').style.display = 'none';
				   		document.getElementById('tdcontent').style.background = "url('images/associates_bg.jpg') no-repeat;";
				   	}
				}
				else{
				   	//alert("An error has occured making the request")
				  }
			}
			else{
				//alert("An error has occured L 187");
			}
		}
	}
	//var fieldata=encodeURIComponent(inputTxt);
	//alert('fieldata->'+fieldata);
	var parameters = 'action=ajax&'+inputTxt;
	mypostrequest.open("POST", action, true);
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	mypostrequest.send(parameters);

}

