// Global Variables
var zipform_scheme = "http:";
var zipform_hostname = "mis.oranges88.com";
var zipform_path = "sm";
var zipform_format = "json";
var zipform_maxcount = "5";
var zipform_page = "zipform.aspx";
var zipform_src = "";
var zipform_debugjs = "no";
var zipform_feedcalled = "no";
var zipform_siteid = "157"
var zipform_gfeed;


// Calls the partner ppc feed executes the resulting JSON object
function zf_showAds() {

    if (zf_checkParams()) {
        zf_callFeed();
    }
    return false;
}


function zf_renderAds() {
    a_debug("Render Form");

    if (zipform_gfeed.catid == null || zipform_gfeed.catid == "undefined" || zipform_gfeed.catid == "") {
        if (document.getElementById("zip-box"))
            document.getElementById("zip-box").style.display = "none";
        if (document.getElementById("zip-box2"))
            document.getElementById("zip-box2").style.display = "none";
    }
    else {

        try {

            if (typeof zipform_gfeed != "undefined" && zipform_gfeed != null) {

                var strB = new Array(); // Equivalent to a string builder in other languages
                var second_form = new Array(); // Equivalent to a string builder in other languages

                strB.push('<form id="zipform" name="zipform" action="/results.aspx" method="GET">');
                if (zipform_gfeed.formtype && zipform_gfeed.formtype == "Category" && zipform_gfeed.catid && zipform_gfeed.catid != "") {
                    strB.push('<input id="cid" name="cid" type="hidden" value="' + zipform_gfeed.catid + '"></input>');
                    strB.push('<input id="ept" name="ept" type="hidden" value="' + zipform_gfeed.ept + '"></input>');
                    strB.push('<input id="aid" name="aid" type="hidden" value="' + zipform_gfeed.adid + '"></input>');
                    strB.push('<input id="ft" name="ft" type="hidden" value="' + zipform_gfeed.formtype + '"></input>');
                }
                if (zipform_gfeed.formtype && zipform_gfeed.formtype == "Task" && zipform_gfeed.taskid && zipform_gfeed.taskid != "") {
                    strB.push('<input id="cid" name="cid" type="hidden" value="' + zipform_gfeed.catid + '"></input>');
                    strB.push('<input id="tid" name="tid" type="hidden" value="' + zipform_gfeed.taskid + '"></input>');
                    strB.push('<input id="ept" name="ept" type="hidden" value="' + zipform_gfeed.ept + '"></input>');
                    strB.push('<input id="aid" name="aid" type="hidden" value="' + zipform_gfeed.adid + '"></input>');
                    strB.push('<input id="ft" name="ft" type="hidden" value="' + zipform_gfeed.formtype + '"></input>');

                }
                strB.push('<input id="serviceName" name="serviceName" type="hidden" value="' + zipform_gfeed.description + '"></input>');
                strB.push('<input id="pmkeyword" name="pmkeyword" type="hidden" value="' + zipform_gfeed.description + '"></input>');
                strB.push('<div id="zipfields">');
                strB.push('<input type="text" name="zipcode" id="zip" value="Enter Zipcode" align="center" class="cleardefault"/>');
                strB.push('<input type="image" value="" id="zipbutton" src="/i/servicemagic/zipbutton.gif"/>');
                strB.push('</div>');
                strB.push('</form>');
                strB.push('<span id="find-local">' + 'Find Local:' + '</span><div id="h5"><h5 id="zipText">' + zipform_gfeed.description + '</h5></div>');

                //second form
                second_form.push('<div id="text"><span id="find-local">' + 'Find Local:' + '</span><div id="h5"><h5 id="zipText">' + zipform_gfeed.description + '</h5></div></div>');

                second_form.push('<form id="zipform2" name="zipform2" action="/results.aspx" method="GET">');
                if (zipform_gfeed.formtype && zipform_gfeed.formtype == "Category" && zipform_gfeed.catid && zipform_gfeed.catid != "") {
                    second_form.push('<input id="cid" name="cid" type="hidden" value="' + zipform_gfeed.catid + '"></input>');
                    second_form.push('<input id="ept" name="ept" type="hidden" value="' + zf_ept_test + '"></input>');
                    second_form.push('<input id="aid" name="aid" type="hidden" value="' + zipform_gfeed.adid + '"></input>');
                    second_form.push('<input id="ft" name="ft" type="hidden" value="' + zipform_gfeed.formtype + '"></input>');
                }
                if (zipform_gfeed.formtype && zipform_gfeed.formtype == "Task" && zipform_gfeed.taskid && zipform_gfeed.taskid != "") {
                    second_form.push('<input id="cid" name="cid" type="hidden" value="' + zipform_gfeed.catid + '"></input>');
                    second_form.push('<input id="tid" name="tid" type="hidden" value="' + zipform_gfeed.taskid + '"></input>');
                    second_form.push('<input id="ept" name="ept" type="hidden" value="' + zf_ept_test + '"></input>');
                    second_form.push('<input id="aid" name="aid" type="hidden" value="' + zipform_gfeed.adid + '"></input>');
                    second_form.push('<input id="ft" name="ft" type="hidden" value="' + zipform_gfeed.formtype + '"></input>');

                }
                second_form.push('<input id="serviceName" name="serviceName" type="hidden" value="' + zipform_gfeed.description + '"></input>');
                second_form.push('<input id="pmkeyword" name="pmkeyword" type="hidden" value="' + zipform_gfeed.description + '"></input>');
                second_form.push('<div id="zipfields">');
                second_form.push('<input type="text" name="zipcode" id="zip" value="Enter Zipcode" align="center" class="cleardefault"/>');
                second_form.push('<input type="image" value="" id="zipbutton" src="/i/servicemagic/zipbutton.gif"/>');
                second_form.push('</div>');
                second_form.push('</form>');
                // /second form

                document.getElementById("zip-box2").innerHTML = second_form.join("");
                
                a_debug("Render this: " + strB.join(""));

                var mylist = document.getElementsByTagName("div");
                var divcount = 0;
                for (i = 1; i < mylist.length; i++) {
                    if (mylist[i].id == "zip-box") {
                        divcount += 1;
                        //alert(mylist[i].id);
                        mylist[i].style.display = 'block';
                        mylist[i].innerHTML = strB.join("");
                        a_debug("InnerHtml: " + mylist[i].innerHTML);
                    }
                }

            } else {
                // Handle no feed			
                return;
            }
        } catch (err) { }
    }
    return;
}

function zf_callFeed() {
    a_debug("zf_callFeed - zipform_feedcalled: " + zipform_feedcalled);
    if (zipform_feedcalled == 'no') {
        a_debug("zf_callFeed - calling feed now");
        var headID = document.getElementsByTagName("head")[0];
        var newScript = document.createElement('script');
        newScript.type = 'text/javascript';
        newScript.src = zipform_src;
        headID.appendChild(newScript);
        zipform_feedcalled = "yes";
    } 
}

// Renders the feed from returned JSON object
function zipForm(feed) {
    zipform_gfeed = eval(feed);
    zf_renderAds();
}

function zf_getUrlAndPath(withpath) {
    var returl = "";
    var cur_scheme = document.location.protocol;
    if (cur_scheme == 'https:')
        zipform_scheme = cur_scheme;

    if (typeof zf_host != "undefined" && zf_host != null) zipform_hostname = zf_host;

    if (typeof zf_path != "undefined" && zf_path != null) zipform_path = zf_path;

    returl = zipform_scheme + '//';
    returl += zipform_hostname;
    if (withpath) returl += '/' + zipform_path;
    return returl;

}

var add_params = "";
function zf_checkParams() {
    if (zipform_feedcalled == "no") {
        var url_params2 = new Array();
	url_params2.push('format=' + zipform_format);
	if (typeof zf_siteid != "undefined" && zf_siteid != null) zipform_siteid = zf_siteid;
	url_params2.push('siteid=' + zipform_siteid);
        var cur_url = window.location.href;
        var serve_url = cur_url;
	if (cur_url.indexOf('?') > 0) serve_url = cur_url.substr(0, cur_url.indexOf('?'));
	url_params2.push('serveUrl=' + encodeURIComponent(serve_url));
	if (url_params2.length != undefined && url_params2.length > 0) add_params = url_params2.join("&");
	if (add_params != "") {
		zipform_src = zf_getUrlAndPath(true) + '/';
                zipform_src += zipform_page + '?';
                zipform_src += add_params;
                a_debug('Debug feed url: \n' + zipform_src + '\n');
        }
    } // end if havefeed = no
    return true;
}


// If JS Debug
if (zipform_debugjs == 'yes') {
    document.write('<form id="zf_jsdebug"><textarea id="zf42out" rows="10" cols="100"></textarea></form>');
    var f = document.forms["zf_jsdebug"];
}

function a_debug(str) {
    if (zipform_debugjs == 'yes') f.elements["zf42out"].value += str + "\n";
}


// Call function to show Ads
zf_showAds();

