// <!--
//////////////////////////////////////////////////////////////////////////////////////////////////
// file: categories.js
// This file has functions to display overlay for categories to show lead fees for each service.
// Uses YUI library
////////////////////////////////////////////////////////////////////////////////////////////////////

// call init() on page load
// YAHOO.util.Event.onDOMReady(init, base);

// this function is called by the main page to show categories
var categoryId;
function categoryList(categoryId,actionVal) {
	serviceId = -1;
	zip = "";
	actionValue=actionVal;
	this.categoryId = categoryId;
	dispOverlay(categoryId,zip,serviceId);
}

//ajax success handler
var succ = function(o){
	dwr.util.setValue('show-categories', o, { escapeHtml:false });
	document.getElementById("loader").style.visibility = "hidden";
	document.getElementById("loader").style.display = "none";
} 

//ajax failure handler
var fail = function(o){
	dwr.util.setValue('show-categories', 'Error: '+ o, { escapeHtml:false });
}

// init() method: creates dialog object with "categoriesOverlay" div's contents
// base - base url (note: when calling $base to pass value here, place it in quotes like '$base')
function init(base) {
	myOverlay = new YAHOO.widget.Dialog("categoriesOverlay", 
			{ 
			  fixedcenter : true,
			  modal : true,
			  visible : false, 
			  constraintoviewport : true,
			  draggable:false, 
			  close:false
			});
	// render the dialog
	myOverlay.render();

	// get arguments passed to the handler
	if(arguments.length > 2) { 
	   base = arguments[2]; 
	}
	// set base url used by ajax functions
	baseURL = base;
}

// ajax call for getting list of all categories
function callAjaxGetCategories(categoryId,zip,serviceId) {
	if(zip == undefined){
		zip = "" ;
	}
	if(serviceId == undefined){
		serviceId = -1 ;
	}
	document.getElementById("show-categories").innerHTML = "";
	document.getElementById("show-errors").innerHTML = "";
   
    // url on server
    var sUrl = baseURL + "/exclude/showCategoryServices.ajax?categoryId=" +categoryId+ "&zip="+zip+ "&serviceId="+serviceId;
	
	// ajax call to url from DWR
	DWRUtil.getData(sUrl, {callback:succ, errorHandler:fail});
}

//this is called when category is clicked to see services and fees
function showServicesList() {
	
    // url on server
    var sUrl = baseURL + "/exclude/showZipsForm.ajax";
    
    // ajax call to url from DWR
	DWRUtil.getData(sUrl, {callback:succ, errorHandler:fail});
}

//this function is called to display overlay
function dispOverlay(categoryId,zip,serviceId) {
	document.getElementById("zip-errors").innerHTML = "";
	callAjaxGetCategories(categoryId,zip,serviceId);
	document.getElementById("loader").style.visibility = "visible";
	document.getElementById("loader").style.display = "inline";
	myOverlay.show();	
}

//////////////////////////////////////////////////////////////////////////////////////////////////
//This file has functions to display overlay for locating zips.
//The page that wants to locate the zip has to call: locateZips(textBoxId) function.
//textBoxId is the "id" of the text input box where the zip code has to be populated.
////////////////////////////////////////////////////////////////////////////////////////////////////

//this function is called by the main page to locate zip
function locateZips(textBoxId) {
	divTextBox = document.getElementById(textBoxId);
	dispOverlay1();
}

//this function is called by the main page to locate zip
var zipsHidden;
function locateZipsOverlay(textBoxId,actionVal) {
	document.getElementById("show-errors").innerHTML = "";
	serviceId = getCheckedValue(document.forms['serviceForm'].elements['serviceId']);
	if(serviceId == "" || serviceId == undefined){
		serviceId = -1;
	}
	actionValue = actionVal;
	divTextBox = document.getElementById(textBoxId);
	dispOverlay1();
}

function cityTextBoxKeyEvent() {
	var charfield=document.getElementById("loczip-city");
	charfield.onkeydown=function(e){
		var e=window.event || e
		if (e.keyCode == 13) callAjaxGetZips();
	}
}

function zipCodeTextBoxKeyEvent(actionValue) {
	var zipcode=document.getElementById("zipcode");
	zipcode.onkeydown=function(e){
		var e=window.event || e
		if (e.keyCode == 13) onGo(actionValue);
	}
}

var actionValue=""; 
var serviceId; 
//baseURL - base url (note: when calling $base to pass value here, place it in quotes like '$base')
var baseURL = "/";

//text box element where the selected zip code has to be populated
var divTextBox;

//dialog footer to show status messages
//ajax success handler - locate 
var handleSuccess = function(o){ 
	var diagFooter = document.getElementById("zip-errors");
	dwr.util.setValue('loczip-ziplist', o, { escapeHtml:false });
	if (o == "") {
		//myOverlay.setFooter("There are no matching zip codes.");
		diagFooter.innerHTML = "There are no matching zip codes.";
	} else {
		//myOverlay.setFooter("");
		diagFooter.innerHTML = "";
	}
} 

//ajax failure handler - locate
var handleFailure1 = function(o){ 
	//myOverlay.setFooter("Status code message: " + o.statusText);
} 
var handleSuccess1 = function(o){
	if(o.length > 1){
		dwr.util.setValue('show-errors', o, { escapeHtml:false });	
	}else{
		post(actionValue , {
			serviceId:serviceId,
			zip:zip,
			categoryId:categoryId
		});
		myOverlay.cancel();
	}
} 

//ajax failure handler - locate
var handleFailure2 = function(o){ 
} 
var handleSuccess2 = function(o){
	if(o.length > 1){
		callAjaxGetCategories(categoryId,divTextBox.value);
		dwr.util.setValue('show-errors', o, { escapeHtml:false });	
	}else{
		post(actionValue , {
			serviceId:serviceId,
			zip:zip,
			categoryId:categoryId
		});
		myOverlay.cancel();
	}
} 

//ajax failure handler - locate
var handleFailure = function(o){ 
	myOverlay.setFooter("Status code message: " + o.statusText);
} 
//ajax callback object - locate
var callback = 
{ 
		success: handleSuccess, 
		failure: handleFailure 
}; 

//this is called after user selects the zip code from list
function selectZipAndClose(zip) {
	divTextBox.value = zip;
	if(serviceId != -1 && zip != "" && categoryId != ""){
		post(actionValue , {
			serviceId:serviceId,
			zip:zip,
			categoryId:categoryId
		});
	}
	else{
		callAjaxCheckCategoriesWithServicesAndZip(categoryId,zip,serviceId);
	}
}

//call ajax for getting zip codes
  
function callAjaxGetZips() {	
	var diagFooter = document.getElementById("zip-errors");
	var jsCity = document.getElementById("loczip-city").value;
	var jsStateCode = document.getElementById("loczip-stateName").value;
	// clear zip list	
	document.getElementById("loczip-ziplist").innerHTML = "";
	// set status
	diagFooter.innerHTML = "Fetching zip codes. Please wait..";
	
	// url on server	
	var sUrl = baseURL + "/exclude/locateZips.ajax?stateCode=" + jsStateCode + "&city=" + jsCity; 
	
	// ajax call to url using DWR
	DWRUtil.getData(sUrl, {callback:handleSuccess, errorHandler:handleFailure});
}

//this function is called to display overlay
function dispOverlay1() {
	showServicesList();
	myOverlay.show();
}
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function onGo(actionValue){
	serviceId = getCheckedValue(document.forms['serviceForm'].elements['serviceId']);
	if(serviceId == "" || serviceId == undefined){
			serviceId = -1;
	}
	this.actionValue =  actionValue;
	zip=document.getElementById("zipcode").value;
	categoryId=document.getElementById("categoryId").value;
	// url on server	
	var sUrl = baseURL + "/processJobCategoryServiceCheck.pub?serviceId=" + serviceId + "&zip=" + zip; 
	// ajax call to url using DWR
	DWRUtil.getData(sUrl, {callback:handleSuccess1, errorHandler:handleFailure1});
}

//ajax call for getting list of all categories
function callAjaxCheckCategoriesWithServicesAndZip(categoryId,zip,serviceId) {
	if(zip==undefined && (serviceId == "" || serviceId == undefined)){
		zip="";
		serviceId="";
	}
	document.getElementById("show-categories").innerHTML = "";
	document.getElementById("show-errors").innerHTML = "";
   
    var sUrl = baseURL + "/processJobCategoryServiceCheck.pub?serviceId=" + serviceId + "&zip=" + zip; 
	// ajax call to url using DWR
	DWRUtil.getData(sUrl, {callback:handleSuccess2, errorHandler:handleFailure2});
    
}
//Post the form
function post(URL, params) {
	var serviceForm=document.createElement("form");
	serviceForm.action=URL;
	serviceForm.method="POST";
	serviceForm.style.display="none";
	for(var x in params) {
		var opt=document.createElement("textarea");
		opt.name=x;
		opt.value=params[x];
		serviceForm.appendChild(opt);
	}
	document.body.appendChild(serviceForm);
	serviceForm.submit();
	return serviceForm;
}
var serviceSucc = function(data){ 
    dwr.util.setValue("showServices", data, { escapeHtml:false });
};

// ajax failure handler
var serviceFail = function(data){
	dwr.util.setValue("showServices", "", { escapeHtml:false });
}; 

function showServices(categoryId,serviceId){
	if(serviceId == undefined){
		serviceId = -1 ;
	}
	if(categoryId == undefined){
		categoryId = -1 ;
	}
		var sUrl = "/showServices.pub?categoryId=" + categoryId + "&serviceId=" + serviceId;
		// ajax call to url using DWR
		DWRUtil.getData(sUrl,{callback:serviceSucc, errorHandler:serviceFail});
}

function showFindProServices(categoryId,serviceId){
	if(serviceId == undefined){
		serviceId = -1 ;
	}
	if(categoryId == undefined){
		categoryId = -1 ;
	}
		var sUrl = "/showFindProServices.pub?categoryId=" + categoryId + "&serviceId=" + serviceId ; 
		// ajax call to url using DWR
		DWRUtil.getData(sUrl,{callback:serviceSucc, errorHandler:serviceFail});
}

function showAffiliateServices(categoryId,serviceId){
	if(serviceId == undefined){
		serviceId = -1 ;
	}
	if(categoryId == undefined){
		categoryId = -1 ;
	}
		var sUrl = "/showAffiliateServices.pub?categoryId=" + categoryId + "&serviceId=" + serviceId ; 
		// ajax call to url using DWR
		DWRUtil.getData(sUrl,{callback:serviceSucc, errorHandler:serviceFail});
}

function showAffiliateForm(){
	var category = document.getElementById("categoryId").selectedIndex;
	var categoryValue = document.getElementById("categoryId").options[category].value;
	var service = document.getElementById("serviceId").selectedIndex;
	var serviceValue = document.getElementById("serviceId").options[service].value;
	var rid = 85;
	if(categoryValue == -1){
		document.getElementById("error").innerHTML = '<div style="padding:0px;margin-bottom:10px;" class="err-alert-box">\
			<div class="err-header" style="padding-left:28px;">Please Correct the Errors Below</div>\
			<ul class="tleft" style="padding-left: 20px;"><li>Please select proper category</li></ul></div>';
	}else if(serviceValue == -1){
		document.getElementById("error").innerHTML = '<div style="padding:0px;margin-bottom:10px;" class="err-alert-box">\
			<div class="err-header" style="padding-left:28px;">Please Correct the Errors Below</div>\
			<ul class="tleft" style="padding-left: 20px;"><li>Please select proper service</li></ul></div>';
	}else{
		document.getElementById("error").innerHTML = '';
		window.open('/processAdCategoryLandingService.pub?categoryId=' + categoryValue + "&serviceId=" + serviceValue+ "&rid=" + rid);

	}
}

function showAffLandingServices87_1(categoryId,serviceId){
	if(serviceId == undefined){
		serviceId = -1 ;
	}
	if(categoryId == undefined){
		categoryId = -1 ;
	}
		var sUrl = "/showAffLandingServices87_1.pub?categoryId=" + categoryId + "&serviceId=" + serviceId ; 
		// ajax call to url using DWR
		DWRUtil.getData(sUrl,{callback:serviceSucc, errorHandler:serviceFail});
}

function showAffLandingServices87_2(categoryId,serviceId){
	if(serviceId == undefined){
		serviceId = -1 ;
	}
	if(categoryId == undefined){
		categoryId = -1 ;
	}
		var sUrl = "/showAffLandingServices87_2.pub?categoryId=" + categoryId + "&serviceId=" + serviceId ; 
		// ajax call to url using DWR
		DWRUtil.getData(sUrl,{callback:serviceSucc, errorHandler:serviceFail});
}

function showAffLandingServices87_3(categoryId,serviceId){
	if(serviceId == undefined){
		serviceId = -1 ;
	}
	if(categoryId == undefined){
		categoryId = -1 ;
	}
		var sUrl = "/showAffLandingServices87_3.pub?categoryId=" + categoryId + "&serviceId=" + serviceId ; 
		// ajax call to url using DWR
		DWRUtil.getData(sUrl,{callback:serviceSucc, errorHandler:serviceFail});
}

function showAff88Services(categoryId,serviceId){
	if(serviceId == undefined){
		serviceId = -1 ;
	}
	if(categoryId == undefined){
		categoryId = -1 ;
	}
	var affId = 88;
		var sUrl = "/showAff88Services.pub?categoryId=" + categoryId + "&serviceId=" + serviceId + "&affId=" + affId ; 
		// ajax call to url using DWR
		DWRUtil.getData(sUrl,{callback:serviceSucc, errorHandler:serviceFail});
}
//-->