﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="../Wcf/SDProxy.svc" />
/// <reference path="Lib/jquery-1.2.6-vsdoc.js" />
/// <reference path="beetservices-0.0.js" />
/// <reference path="carousel-0.0.js" />
/// <reference path="common.js" />
/// <reference path="eventmanager-0.0.js" />
/// <reference path="lunchcartservices-0.0.js" />
/// <reference path="popupwindow-0.0.js" />
/// <reference path="productservices-0.0.js" />
/// <reference path="userservices-0.0.js" />

var cgMgr = {
  maxId: 1,
  increment: function(objName, u) {
    var obj = $("#" + objName);
    if (isNaN(parseInt(obj.val()))) {
      obj.val("1");
      return;
    }
    var iQty = parseInt(obj.val());
    iQty = iQty + u;
    if (iQty < 1 || iQty > 9999) iQty = 1;
    //set text field
    obj.val(iQty);
  },

  anonymousToggled: function(cId, nId, eId) {
    if (document.getElementById(cId).checked) {
      document.getElementById(eId).value = 'anonymous@specialtys.com';
      document.getElementById(eId).readOnly = true;
      document.getElementById(nId).value = 'Anonymous';
      document.getElementById(nId).readOnly = true;
    }
    else {
      document.getElementById(eId).value = '';
      document.getElementById(eId).readOnly = false;
      document.getElementById(nId).value = '';
      document.getElementById(nId).readOnly = false;
    }
  },

  newFieldReqCheck: function() {
    var containsContent = true;
    for (var i = 1; i <= cgMgr.maxId; i++) {
      if (document.getElementById('newemail' + i).value.length == 0) {
        containsContent = false;
      } else {
         document.getElementById('newemail' + i).value = document.getElementById('newemail' + i).value.replace("<", "");
         document.getElementById('newemail' + i).value = document.getElementById('newemail' + i).value.replace(">", "");
      }
    }
    if (containsContent == true) cgMgr.addExtraEmail();
  },

  addExtraEmail: function() {
    var eDiv = document.getElementById("extraemails");
    cgMgr.maxId++;
    var div = document.createElement('div');
    div.setAttribute('class', 'frmStandAloneInput');
    var inp = document.createElement('input');
    inp.setAttribute('class', 'text');
    inp.setAttribute('id', 'newemail' + cgMgr.maxId);
    inp.setAttribute('type', 'text');
    inp.setAttribute('maxlength', '100');
    inp.setAttribute('size', '40');
    inp.setAttribute('name', 'newemail');
    inp.onkeypress = cgMgr.newFieldReqCheck;
    div.appendChild(inp);
    eDiv.appendChild(div);
  },

  previewThemeWithSampleData: function(theme) {
    var themeId = $("#" + theme).val()
    specialtys.com.SDProxy.GetCookieGramTemplateSampleData(themeId, function(d) {
      popupWindowMgr.content(cgMgr.getCgHtmlWrapper(d));
      popupWindowMgr.showLargeWindow();
    });
  },
  previewThemeWithCartData: function(lId) {
    specialtys.com.SDProxy.GetCookieGramTemplateCartData(lId, function(d) {
      popupWindowMgr.content(cgMgr.getCgHtmlWrapper(d));
      popupWindowMgr.showLargeWindow();
    });
  },
  previewThemeWithFinalData: function(cId) {
    specialtys.com.SDProxy.GetCookieGramTemplateFinalData(cId, function(d) {
      popupWindowMgr.content(cgMgr.getCgHtmlWrapper(d));
      popupWindowMgr.showLargeWindow();
    });
  },
  getCgHtmlWrapper: function(iT) {
    return '<div style="padding: 20px;"><div style="width: 630px; height: 480px; padding-left: 65px; padding-right: 65px; overflow: auto;">' + iT + '</div></div>';
  }
}
