
// **************************************************************
// launched from event
var fCheckAccount = function (strInputId) {	
	
	// build up the post string when passing variables to the server side page
	var PostStr = "tmpCode=" + document.getElementById(strInputId).value;	
	// use the generic function to make the request
	doAJAXCall('../../../Ko-Includes/f_CheckBuyerCUCode.asp', 'GET', PostStr, showMessageResponse_CheckAccount);

}

// The function for handling the response from the server
var showMessageResponse_CheckAccount = function (oXML) {
	// get the response text, into a variable
    var response = oXML.responseText;
	var result;
	var fResult;
	
	cssBackColour = '#000000'
	cssFontColour = '#FFFFFF'
		
	if (response == '0') {
			cssFontColour = document.getElementById('hBadBCFontColour').value;
			cssBackColour = document.getElementById('hBadBCBackColour').value;
			result = document.getElementById('hBadBCResult').value;
			
			var PostStr = '';
			fResult = badResponse;	
			doAJAXCall('../../../Ko-Includes/f_BuildAddress.asp', 'GET', PostStr, fResult );
					
	} else if (response == '1') {
			cssFontColour = document.getElementById('hGoodBCFontColour').value;
			cssBackColour = document.getElementById('hGoodBCBackColour').value;
			result = document.getElementById('hGoodBCResult').value;
			
			var PostStr = '';
			fResult = okResponse;
			doAJAXCall('../../../Ko-Includes/f_BuildAddress.asp', 'GET', PostStr, fResult );
			
	} else if (response == '-1') {
			cssFontColour = '#FFFFFF';
			cssBackColour = '#FFFFFF';
			//result = 'Too many attempts!';
			//fResult = badAttempts;
			//alert('Too many attempts!');
			window.location = '../../../Ko-bespoke/ajxmsg.asp';
			//doAJAXCall('../../../Ko-Includes/f_ValMsgViaAjax.asp', 'POST', PostStr, fResult );
	}
	
	//doAJAXCall('../../../Ko-Includes/f_BuildAddress.asp', 'GET', PostStr, fResult );
	
	
	//else {
	//	cssBackColour = '#000000';
	//		result = response; // prints out the invalid response
	//}
	
    // update the Div to show the result from the server
	document.getElementById("divAccountValidationResponse").style.backgroundColor=cssBackColour;
	document.getElementById("divAccountValidationResponse").style.color=cssFontColour;
	document.getElementById("divAccountValidationResponse").style.width=document.getElementById('hBCWidth').value;
	document.getElementById("divAccountValidationResponse").style.fontFamily=document.getElementById('hBCFont').value;
	document.getElementById("divAccountValidationResponse").style.fontWeight=document.getElementById('hBCFontWeight').value;
	document.getElementById("divAccountValidationResponse").style.fontSize=document.getElementById('hBCFontSize').value;
	document.getElementById("divAccountValidationResponse").style.padding=document.getElementById('hBCPadding').value;
	document.getElementById("divAccountValidationResponse").style.textAlign=document.getElementById('hBCTextAlign').value;
		
	document.getElementById("divAccountValidationResponse").innerHTML = result;
	
};

var controlId = 'RegisterBuyer93'
var FormId = 'RegisterBuyer'


var okResponse = function (oXML) {
	
	// array to hold value passed 
	var recValue;
	// value of responseText is purely that, a string, we cannot pass an array
	// and expect it to deal with it. So we have to take the string and
	// redefine it here
	recValue=oXML.responseText;	
	// split the string
	recValue = recValue.split("|");
	
	var n;
	var v;
	// increment set to '1' so we can work out where we are in the string and then
	// create the 'value' and the 'text' for each option
	var inc = 1;

	// remove any existing options
	removeSelectOptions(controlId);
	// add a default option
    createOption(FormId,controlId,'def','Please select','selected');
	
	// loop through string of addresses
	for (n in recValue)	
	{				
			if (isEven(inc)) 
				{ // even: so we have hit the 2nd element (text of option), apply 'v' created below and current string value				
				createOption(FormId,controlId,v,recValue[n],'');
				}
			else
				{ // odd: so we are on the first element (value of option) , set 'v' to current string value
				v = recValue[n];
				}
			inc++
	}
			
	document.getElementById(controlId).style.width='auto';
		
	
};

function isEven(num) {
  return !(num % 2);
}

//var badAttempts = function (valURL) {
	
//	alert('Too many attempts!');
	//window.location = 'http://upg.neb.uk.com/';

//};

var badResponse = function (vArr) {
	// used if no addresses are returned (ie; account does not exist)	
	removeSelectOptions(controlId) 
    createOption(FormId,controlId,'0','None','selected')
	document.getElementById(controlId).style.width='75px';
};

function removeSelectOptions(controlId) {

	var theDropDown = document.getElementById(controlId)   
	var numberOfOptions = theDropDown.options.length   
	for (i=0; i<numberOfOptions; i++) {   
	//Note: Always remove(0) and NOT remove(i)   
	theDropDown.remove(0)   
	}   

}

function createOption(f,e,newValue,newText,selValue){

var objSelect=document.forms[f].elements[e];

//objSelect.onchange= function(){alert(objSelect.value);};

var objOption = document.createElement("option");
objOption.text = newText
objOption.value = newValue
objOption.selected = selValue

if(document.all && !window.opera)
  {objSelect.add(objOption);}
 else
  {objSelect.add(objOption, null);};
}
// *******************************************



