
// ************************************************************
// 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=CYP]');
	dropdownValues = xml;
	populateCylinderList('drop-list1', 'CYP', 'cyl-drop', 'cyl');
	populateAxisList('drop-list2', 'CYP', 'axis-drop', 'axis');
	populatePowerList('drop-list3', 'CYP', 'diameter-drop', 'diameter');
	
}

function displayError(xml){
	alert("error"+xml);
}


//populated List object ( customozed dropdown)
//idname = id of the dropdown to be populated
//errorId = div id of error
// product id 
function populateCylinderList(idname, productId, divId, inputName){
	var temp = $(dropdownValues).find('prescription brand[brand_code=CYP] cylinder-list cylinder');
	$(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>");
	});
}

//Sph power
//idname = id of the dropdown to be populated
//errorId = div id of error
// product id 
function populatePowerList(idname, productId, divId, inputName){
	var temp = $(dropdownValues).find('prescription brand[brand_code=CYP] power-list 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>");
	});
	
}
//Add power - populate power values in LIST dropdown
//idname = id of the dropdown to be populated
//errorId = div id of error
//product id 
function populateAxisList(idname, productId, divId, inputName){
	var temp = $(dropdownValues).find('prescription brand[brand_code=CYP] axis-list axis');
	$(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, axis, cylinder){
	var result = false;
	$.ajax({
		type: "GET",
		url: "/ftpc/EOCSAJaxServlet?callType=2&salesOrg=1100&brandCode=CYP&power="+p_value+"&cylinder="+cylinder+"&axis="+axis,
		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";
}
