
// ************************************************************
// Author : Narendra Yalavarthi
// Date : 05/06/2010
//*************************************************************
// populate dropdowns from XMl files.
var dropdownValues;
//initialize the xml objects.
function init(){
	$.ajax({
		type: "GET",
		url: "/ftpc/EOCSAJaxServlet?callType=1&salesOrg=1100",
		dataType: "xml",
		error: displayError,
		success: parseXML
	});

}



function parseXML(xml){
	//dropdownValues = $(xml).find('prescription brand[brand_code=BAC]');
	dropdownValues = xml;
	populatePowerList('drop-list1', 'BAC', 'd', 'sph');
	populateAddPowerList('drop-list2', 'BAC', 'add-drop', 'add');
	
}

function displayError(xml){
	alert("error"+xml);
}


//Sph power
//idname = id of the dropdown to be populated
//errorId = div id of error
// product id 
function populatePowerList(idname, productId, divId, inputName){
	var dropdown = document.getElementById(idname);
	var temp = $(dropdownValues).find('prescription brand[brand_code=BAC] power-list power');
	$(temp).each(function() {
	        var item = $(this).text();
	        var oNewNode = document.createElement("LI");
		dropdown.appendChild(oNewNode);
		$(oNewNode).html("<a href=javascript:toggle('"+divId+"');clearMsg();changeValue("+item+",'"+inputName+"')>"+item+"</a>");
	       
	});
	
}
//Add power - populate power values in LIST dropdown
//idname = id of the dropdown to be populated
//errorId = div id of error
//product id 
function populateAddPowerList(idname, productId, divId, inputName){
	var temp = $(dropdownValues).find('prescription brand[brand_code=BAC] add-power-list add_power');
	$(temp).each(function() {
		var item = $(this).text();
		var oNewNode = document.createElement("LI");
		document.getElementById(idname).appendChild(oNewNode);
		$(oNewNode).html("<a href=javascript:toggle('"+divId+"');clearMsg();changeValue('"+item+"','"+inputName+"')>"+item+"</a>");
	});
}


/***********************************************************************************
// validate prescription parameters.
// p_value 	= selected prescription value from dropdown.
************************************************************************************/
function validate(p_value, addPower){
	var result = false;
	$.ajax({
		type: "GET",
		url: "/ftpc/EOCSAJaxServlet?callType=2&salesOrg=1100&brandCode=BAC&power="+p_value+"&addPower="+addPower,
		dataType: "xml",
		error: displayError,
		success: function(xml) {
		    result = $(xml).find('VALID').text();
		    if(result == 'true'){
		    	displayYesMessage();
		    }else{
		    	displayNoMessage();
		}
		}

	});
}


/**
* Clear the prescription error / success message when user makes a new selection.
*/
function clearMsg(){
	document.getElementById("prescription-validation").className = "prescription-validation";
}
