
// ************************************************************
// Author : Narendra Yalavarthi
// Date : 05/06/2010
//*************************************************************
// populate dropdowns from XMl files.
var dropdownValues;
//initialize the xml objects.
function init(){

	$.ajax({
		type: "POST",
		url: "/ftpc/EOCSAJaxServlet?callType=1&salesOrg=1100",
		dataType: "xml",
		//data: "{}",
		//contentType: "text/xml; charset=utf-8",
		//contentType: "charset=utf-8",
		error: displayError,
		success: parseXML
	});
	
}


function parseXML(xml){
	dropdownValues = $(xml).find('prescription brand[brand_code=PH] power-list power');
	populatePowerList('drop-list1', 'PH', 'd', 'sph');
	
}

function displayError (xhr, ajaxOptions, thrownError){
	 //alert(xhr.getResponseHeader("Content-Type") +" & "+xhr.responseText);
	 //alert(xhr.getResponseHeader("Content-Type"));
	 //alert(thrownError);
	 alert(ajaxOptions);

}

//Sph power
//idname = id of the dropdown to be populated
//errorId = div id of error
// product id 
function populatePowerList(idname, productId, divId, inputName){
	$(dropdownValues).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){
	var result = false;
	$.ajax({
		type: "GET",
		url: "/ftpc/EOCSAJaxServlet?callType=2&salesOrg=1100&brandCode=PH&power="+p_value,
		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";
}
