/**
 * Init form server
 */
var IAFS_Error = '';
var IAFS_FormServerDomain = 'forms.innovationads.com';
try{
  //required
  if(IAFS_Id === false || IAFS_Id == undefined){
    var IAFS_Id = '';
  }
  if(IAFS_Signature === false || IAFS_Signature == undefined){
    var IAFS_Signature = '';
  }
  if(IAFS_Width === false || IAFS_Width == undefined){
    var IAFS_Width = '';
  }
  if(IAFS_Height === false || IAFS_Height == undefined){
    var IAFS_Height = '';
  }
  //optional
  if(IAFS_Replace === false || IAFS_Replace == undefined){
    var IAFS_Replace = '';
  }
  if(IAFS_Prepopulate === false || IAFS_Prepopulate == undefined){
    var IAFS_Prepopulate = '';
  }
  if(IAFS_Command === false || IAFS_Command == undefined){
    var IAFS_Command = '';
  }
  if(IAFS_Redirect === false || IAFS_Redirect == undefined){
    var IAFS_Redirect = '';
  }
  if(IAFS_Template === false || IAFS_Template == undefined){
    var IAFS_Template = '';
  }
  if(IAFS_Path === false || IAFS_Path == undefined){
    var IAFS_Path = '';
  }
  //default auto
  if(IAFS_AutoWidth === false || IAFS_AutoWidth == undefined){
    var IAFS_AutoWidth = 500;
  }
  if(IAFS_AutoHeight === false || IAFS_AutoHeight == undefined){
    var IAFS_AutoHeight = 800;
  }

  //process
  if(IAFS_Id == ''){
    IAFS_Error = IAFS_Error + '<li>Invalid IAFS_Id</li>';
  }
  if(IAFS_Signature == ''){
    IAFS_Error = IAFS_Error + '<li>Invalid IAFS_Signature</li>';
  }
  if((IAFS_Width != '' && IAFS_Width != 'Auto' && !isNaN(IAFS_Width)) || (IAFS_Width != '' && IAFS_Width == 'Auto' && IAFS_AutoWidth != '' && !isNaN(IAFS_AutoWidth))){
    if(IAFS_Width == 'Auto'){
      //default
      IAFS_Width = IAFS_AutoWidth;
      //custom
      if(IAFS_Template.indexOf('.css') != -1){
        try{
          var IAFS_CustomTemplateIdentifier = IAFS_Template.replace('.','_');
          var IAFS_AutoWidth_custom = eval('IAFS_AutoWidth_'+IAFS_CustomTemplateIdentifier);
          if(IAFS_AutoWidth_custom!='' && !isNaN(IAFS_AutoWidth_custom)){
            IAFS_Width = IAFS_AutoWidth_custom;
          }
        }
        catch(error){
        }
      }
      //shared
      if(IAFS_Template != ''&& IAFS_Template.indexOf('shared/') == 0){
        if(IAFS_Template.indexOf('cne') == 7){
          IAFS_Width = IAFS_AutoWidth_cne;
        }
        else if(IAFS_Template.indexOf('fsg') == 7){
          IAFS_Width = IAFS_AutoWidth_fsg;
        }
        else if(IAFS_Template.indexOf('10k') == 7){
          IAFS_Width = IAFS_AutoWidth_10k;
        }
        else if(IAFS_Template.indexOf('offer') == 7){
          IAFS_Width = IAFS_AutoWidth_offer;
        }
        else if(IAFS_Template.indexOf('search') == 7){
          IAFS_Width = IAFS_AutoWidth_search;
        }
        else if(IAFS_Template.indexOf('register') == 7){
          IAFS_Width = IAFS_AutoWidth_register;
        }
      }
    }
    IAFS_Width = new Number(IAFS_Width);
    //alert('IAFS_Width:'+IAFS_Width);
    if(isNaN(IAFS_Width)){
      IAFS_Error = IAFS_Error + '<li>Invalid IAFS_Width</li>';
    }
  }
  else{
    IAFS_Error = IAFS_Error + '<li>Invalid IAFS_Width</li>';
  }
  if((IAFS_Height != '' && IAFS_Height != 'Auto' && !isNaN(IAFS_Height)) || (IAFS_Height != '' && IAFS_Height == 'Auto' && IAFS_AutoHeight != '' && !isNaN(IAFS_AutoHeight))){
    if(IAFS_Height == 'Auto'){
      //default
      IAFS_Height = IAFS_AutoHeight;
      //custom
      if(IAFS_Template.indexOf('.css') != -1){
        try{
          var IAFS_CustomTemplateIdentifier = IAFS_Template.replace('.','_');
          var IAFS_AutoHeight_custom = eval('IAFS_AutoHeight_'+IAFS_CustomTemplateIdentifier);
          if(IAFS_AutoHeight_custom!='' && !isNaN(IAFS_AutoHeight_custom)){
            IAFS_Height = IAFS_AutoHeight_custom;
          }
        }
        catch(error){
        }
      }
      //shared
      if(IAFS_Template != ''&& IAFS_Template.indexOf('shared/') == 0){
        if(IAFS_Template.indexOf('cne') == 7){
          IAFS_Height = IAFS_AutoHeight_cne;
        }
        else if(IAFS_Template.indexOf('fsg') == 7){
          IAFS_Height = IAFS_AutoHeight_fsg;
        }
        else if(IAFS_Template.indexOf('10k') == 7){
          IAFS_Height = IAFS_AutoHeight_10k;
        }
        else if(IAFS_Template.indexOf('offer') == 7){
          IAFS_Height = IAFS_AutoHeight_offer;
        }
        else if(IAFS_Template.indexOf('search') == 7){
          IAFS_Height = IAFS_AutoHeight_search;
        }
        else if(IAFS_Template.indexOf('register') == 7){
          IAFS_Height = IAFS_AutoHeight_register;
        }
      }
    }
    IAFS_Height = new Number(IAFS_Height);
    //alert('IAFS_Height:'+IAFS_Height);
    if(isNaN(IAFS_Height)){
      IAFS_Error = IAFS_Error + '<li>Invalid IAFS_Height</li>';
    }
  }
  else{
    IAFS_Error = IAFS_Error + '<li>Invalid IAFS_Height</li>';
  }

  //prepare global pre-population
  var globalPrepopulationArray = new Array();
  if(window.location.href){
    var locationHref = window.location.href;
    if(locationHref.indexOf("?") > -1 ){
      var queryString = locationHref.substr(locationHref.indexOf("?")+1);
      var subQueryArray = queryString.split("&");
      for(var iParam = 0; iParam < subQueryArray.length; iParam++ ){
        var parameterArray = subQueryArray[iParam].split("=");
        if(parameterArray[0]!=''&&parameterArray[1]!=''){
          //alert(parameterArray[0]+'=>'+parameterArray[1]);
          globalPrepopulationArray[parameterArray[0]] = parameterArray[1];
        }
      }
    }
  }
  //prepare manual pre-population
  if(IAFS_Prepopulate != ''){
    var manualPrepopulationArray = eval('('+unescape(IAFS_Prepopulate)+')');
  }
  else{
    var manualPrepopulationArray = new Array();
  }
  //prepare combined pre-population
  var combinedPrepopulationArray = new Array();
  var mappingArray = new Array();
  mappingArray['ia_firstname']='FirstName';
  mappingArray['firstname']='FirstName';
  mappingArray['ia_lastname']='LastName';
  mappingArray['lastname']='LastName';
  mappingArray['ia_address']='Address';
  mappingArray['address']='Address';
  mappingArray['ia_address2']='Address2';
  mappingArray['address2']='Address2';
  mappingArray['ia_city']='City';
  mappingArray['city']='City';
  mappingArray['ia_state']='State';
  mappingArray['state']='State';
  mappingArray['ia_zip']='Zip';
  mappingArray['zip']='Zip';
  mappingArray['ia_phone']='Phone';
  mappingArray['phone']='Phone';
  mappingArray['ia_phone2']='Phone2';
  mappingArray['phone2']='Phone2';
  mappingArray['ia_country']='Country';
  mappingArray['country']='Country';
  mappingArray['ia_email']='Email';
  mappingArray['email']='Email';
  //add global prepopulation first
  for(key in globalPrepopulationArray){
    if(key!='' && globalPrepopulationArray[key]!=''){
      if(mappingArray[key.toLowerCase()]&&mappingArray[key.toLowerCase()]!=''){
        //alert('exists key=>'+key+' value=>'+globalPrepopulationArray[key]);
        combinedPrepopulationArray[mappingArray[key.toLowerCase()]] = globalPrepopulationArray[key];
      }
      else{
        //alert('not exists key=>'+key+' value=>'+globalPrepopulationArray[key]);
        combinedPrepopulationArray[key] = globalPrepopulationArray[key];
      }
    }
  }
  //add manual prepopulation next
  for(key in manualPrepopulationArray){
    if(key!='' && manualPrepopulationArray[key]!=''){
      //alert('key=>'+key+' value=>'+manualPrepopulationArray[key]);
      combinedPrepopulationArray[key] = manualPrepopulationArray[key];
    }
  }
  //prepare combined prepopulation string
  var combinedPrepopulationString = '';
  for(key in combinedPrepopulationArray){
    if(key!='' && combinedPrepopulationArray[key]!=''){
      //alert('key=>'+key+' value=>'+combinedPrepopulationArray[key]);
      if(combinedPrepopulationString==''){
        combinedPrepopulationString = '"'+key+'":"'+combinedPrepopulationArray[key]+'"';
      }
      else{
        combinedPrepopulationString = combinedPrepopulationString+',"'+key+'":"'+combinedPrepopulationArray[key]+'"';
      }
    }
  }
  //alert(combinedPrepopulationString);
  //replace IAFS_Prepopulate
  if(combinedPrepopulationString!=''){
    combinedPrepopulationString = '{'+combinedPrepopulationString+'}';
    IAFS_Prepopulate = escape(combinedPrepopulationString);
  }
  if(IAFS_Prepopulate!=''){
    IAFS_Prepopulate = IAFS_Prepopulate.substring(0,1000);
  }
  //alert(unescape(IAFS_Prepopulate));

  //prepare protocol & path
  if(IAFS_Path == ''){
    var IAFS_Protocol = 'HTTP';
    var IAFS_Path = 'http://' + IAFS_FormServerDomain;
    var IAFS_Script = '';
    if(document.getElementById('IAFS_Script')){
      IAFS_Script = document.getElementById('IAFS_Script').src;
      IAFS_Script = IAFS_Script.toLowerCase();
      if(IAFS_Script.indexOf('https://') == 0){
        IAFS_Protocol = 'HTTPS';
        IAFS_Path = 'https://' + IAFS_FormServerDomain;
      }
    }
  }
  else{
    var IAFS_Path = new String(IAFS_Path);
    if(IAFS_Path.toLowerCase().indexOf('http://') != -1 && IAFS_Path.toLowerCase().indexOf('.innovationads.com') != -1){
      var IAFS_Protocol = 'HTTP';
    }
    else if(IAFS_Path.toLowerCase().indexOf('https://') != -1 && IAFS_Path.toLowerCase().indexOf('.innovationads.com') != -1){
      var IAFS_Protocol = 'HTTPS';
    }
    else{
      var IAFS_Protocol = 'HTTP';
      var IAFS_Path = 'http://' + IAFS_FormServerDomain;
    }
  }
}
catch(e){
  IAFS_Error = IAFS_Error + '<li>' + e + '</li>';
}
//alert(IAFS_Error);
if(IAFS_Error==''){
  //init
  var IAFS_Domain = '';
  var isFormBuilderCalled = false;
  if(window.location){
    var windowLocation = new String(''+window.location);
    if(windowLocation.indexOf('?') == -1){
      IAFS_Domain = windowLocation;
    }
    else{
      IAFS_Domain = windowLocation.substr(0,windowLocation.indexOf('?'));
    }

    if(windowLocation.indexOf('/formbuilder/?page=Script.GenerateScript') != -1){
      isFormBuilderCalled = true;
    }
  }
  var IAFS_Panel = 'IAFS_Panel_' + IAFS_Id;
  var IAFS_IframeSource = IAFS_Path + '/formserver/?IAFS_Id=' + IAFS_Id + '&IAFS_Signature=' + IAFS_Signature + '&IAFS_Domain=' + escape(IAFS_Domain) + '&IAFS_Protocol=' + IAFS_Protocol + '&IAFS_Width=' + IAFS_Width + '&IAFS_Height=' + IAFS_Height + '&IAFS_Replace=' + IAFS_Replace + '&IAFS_Prepopulate=' + IAFS_Prepopulate + '&IAFS_Command=' + IAFS_Command + '&IAFS_Redirect=' + IAFS_Redirect + '&IAFS_Template=' + IAFS_Template + '&IAFS_Path=' + IAFS_Path + '&rand=' + Math.random();
  //alert(IAFS_IframeSource);
  document.write('<div id="' + IAFS_Panel + '" style="display:block; visibility:visible; width:' + IAFS_Width + 'px;"></div>');
  if(!isFormBuilderCalled){
    document.getElementById(IAFS_Panel).innerHTML = '<iframe id="IAFS_Iframe" name="IAFS_Iframe" src="' + IAFS_IframeSource + '" frameborder="0" scrolling="no" align="top" width="' + IAFS_Width + '" height="' + IAFS_Height + '" marginheight="0" marginwidth="0" allowTransparency="true" style="background-color:transparent; margin:0px; padding:0px; width:' + IAFS_Width + 'px; height:' + IAFS_Height + 'px;"></iframe><br/><img src="' + IAFS_Path + '/formserver/images/spacer.gif" border="0" width="' + IAFS_Width + '" height="1"/><br/>';
  }
  else{
    var debugText = 'Resolution: '+IAFS_Width+'(W)X'+IAFS_Height+'(H)<br/>';
    if(IAFS_Template==''){
      debugText = debugText + 'Template: [default]<br/>';
    }
    else{
      debugText = debugText + 'Template: '+IAFS_Template+'<br/>';
    }
    document.getElementById(IAFS_Panel).innerHTML = '<span style="font-size:8pt;">'+debugText+'</span><br/><iframe id="IAFS_Iframe" name="IAFS_Iframe" src="' + IAFS_IframeSource + '" frameborder="0" scrolling="no" align="top" width="' + IAFS_Width + '" height="' + IAFS_Height + '" marginheight="0" marginwidth="0" allowTransparency="true" style="border:1px solid #8C8A8C; background-color:transparent; margin:0px; padding:0px; width:' + IAFS_Width + 'px; height:' + IAFS_Height + 'px;"></iframe><br/><img src="' + IAFS_Path + '/formserver/images/spacer.gif" border="0" width="' + IAFS_Width + '" height="1"/><br/>';
  }
}
else{
  document.write('<span style="font-family:Arial; font-size:9pt; color:#FF0000;"><b>Invalid JavaScript Call!</b><br/>'+IAFS_Error+'</span>');
}
