var divCloserName = 'DivCloser';
var IFRAMEmasterName = 'IFRAMEmaster';
var divMainVisibilityName = 'ucALFA_IFrame';
var divSpinnerName = 'DivSpinner';
var griName = 'gri';

function showForm(cM) {
    var codModel = "";
    if (cM != null) { codModel = cM; }

    InitNMForm();
    griVisibility('block');
    var DivMain = document.getElementById(divMainVisibilityName);
    if (DivMain != null) {
        if (DivMain.style.display == "block") { divMainVisibility("none"); }
        else {
            divMainVisibility("block");
            var IFRAMEmaster = document.getElementById(IFRAMEmasterName);
            if (IFRAMEmaster != null) {
                IFRAMEmasterVisibility("block");
                //var indexOf = -1;
                //if ((IFRAMEmaster.src != null) && (IFRAMEmaster.src != "")) { indexOf = IFRAMEmaster.src.indexOf("nmFiatForm.aspx", 0); }
                //if ((IFRAMEmaster.src == null) || (IFRAMEmaster.src == "") || (indexOf < 0)) {
                    divSpinnerVisibility("block");
                    IFRAMEmaster.src = 'http://selfcare.fiat.com/nmFiatForm.aspx?BRAND=00&selfclose=yes&MMVS=' + codModel + "&nmCurrentPage=" + window.location.href;
                //}
                //else {
                    divCloserVisibility("block");
                //}
            }
            else { alert(divMainVisibilityName + " not found"); }
        }
    }
    else { alert(divMainVisibilityName + " not found"); }
}

function divCloserVisibility(attribute)    { divGenericVisibility(divCloserName,attribute); }
function IFRAMEmasterVisibility(attribute) { divGenericVisibility(IFRAMEmasterName,attribute); }
function divMainVisibility(attribute)      { divGenericVisibility(divMainVisibilityName,attribute); }
function divSpinnerVisibility(attribute)   { divGenericVisibility(divSpinnerName,attribute); }
function griVisibility(attribute)          { divGenericVisibility(griName, attribute); }

function divGenericVisibility(objectName, attribute) {
    var DivGeneric = document.getElementById(objectName);
    if (DivGeneric != null) { DivGeneric.style.display = attribute; }
}

function functionToCallAfterLoading(fromHow) {
    var iframe = document.getElementById(IFRAMEmasterName);
    if (iframe != null) {
        var indexOf = -1;
        if ((iframe.src != null) && (iframe.src != "")) {
            indexOf = iframe.src.indexOf("nmFiatForm.aspx", 0);
            if ((indexOf > 0)) {
                divCloserVisibility("block");
                divSpinnerVisibility("none");
            }
        }
    }
}

function CloseIframe() {
    IFRAMEmasterVisibility("none");
    divCloserVisibility("none");
    divMainVisibility("none");
    griVisibility('none');
}
 
document.write("<style  type=\"text/css\">");
document.write("    .divGray{");
document.write("        position:absolute;");
//document.write("		left:-198px;");
//document.write("		top:-20px;");
//document.write("		width:" + screen.width + ";");
//document.write("		height:" + screen.height + ";");
document.write("		z-index:900;");
document.write("		background:none repeat scroll 0 0 #454545;");
document.write("		filter:alpha(opacity=90);v-moz-opacity:.90;opacity:.90");
document.write("	}");
document.write("</style>");

document.write("<div id=\"gri\" class=\"divGray\" style=\"display:none;\"></div>");
document.write("<div id=\"DivSpinner\" style=\"display:none;position:absolute;left:300px;top:300px;background-color:#ffffff;font-family:Verdana;font-size:medium;z-index:7;\">");
document.write("    <div>");
document.write("        Loading TestDrive Form ...&nbsp;");
//document.write("        <img style=\"vertical-align:middle\" src=\"http://www.fiat.it/FIAT_ITALIA/NMForms/img/spinnerSmall.gif\" alt=\"\" />");
document.write("    </div>");
document.write("</div>");
document.write("<div id=\"ucALFA_IFrame\" style=\"display:none;position:absolute;z-index:1000;background-color:#fff;margin: 0pt auto; top:20px;\">");
document.write("    <div id=\"DivCloser\" style=\"display:none;position:absolute;left:560px;z-index:900;width:50px;height:50px;\" onClick=\"CloseIframe();\" >");
//document.write("        <img  src=\"http://www.fiat.it/FIAT_ITALIA/NMForms/img/spaziatore.gif\" style=\"display:none;padding-left:0px;padding-top:25px;width:60px;height:19px;\"/>");
document.write("    </div>");
document.write("    <iframe id=\"IFRAMEmaster\" style=\"z-index:1000\" onLoad=\"functionToCallAfterLoading();\" width=\"621\" height=\"461\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"\"></iframe>");
document.write("</div>");


//_spBodyOnLoadFunctionNames.push("InitNMForm"); //SP version
function InitNMForm() {
    var formWidth = 621;
    var formHeight = 461;
    var totalWidth = $(document).width();
    var totalHeight = $(document).height();
    var winHeight = $(window).height();
    //alert(totalWidth + ', ' + totalHeight + ', ' + winHeight);

    var halfWidth = totalWidth / 2;
    var halfFormWidth = formWidth / 2;
    var dx = halfWidth - halfFormWidth;

    var halfHeight = winHeight / 2;
    var halfFormHeight = formHeight / 2;
    var top = halfHeight - halfFormHeight;

    $('#DivSpinner').css('left', dx);
    //$('#DivSpinner').css('top', dx);

    $('#ucALFA_IFrame').css('left', dx);
    $('#ucALFA_IFrame').css('top', top);

    $('#gri').css('width', totalWidth);
    $('#gri').css('height', totalHeight);
};

