﻿//Vars
/// <reference path="CSClientLib.js" />

var CFRAME_NEWS_REG = 'CSCRNewsFrame';
var CCSCR_NEWS_REG_URL = '/Components/CR/NewsRegister.aspx';
var CCSCR_NEWS_REG_POST_URL = '/Components/CR/NewsRegister.aspx';

var newsCallBackFunction;
var newsRegFrame;
var arrNewsCatItems = new Array();

//End Vars

function ValidateNewsRequiredFields(formName)
{
  if(formName == '') return false;
  
  var hasEmail = false;
  var hasFirstName = false;
  var hasLastName = false;
  
  aForm = document.forms[formName];
  if(aForm)
  {
    //"[\w\.-]+(\+[\w-]*)?@([\w-]+\.)+[\w-]+";
    //([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}
    var re = new RegExp("[A-Za-z0-9_\.-]+([A-Za-z0-9_-]*)?@([A-Za-z0-9_-]+\.)+[A-Za-z0-9_-]+");
    
    for (e = 0; e < aForm.elements.length; e++) 
    {
      if (aForm.elements[e].name == 'email') 
      {
        if(ValidateEmailAddress(aForm.elements[e].value)) hasEmail = true;
        //if(aForm.elements[e].value.match(re)) hasEmail = true;
        //if(aForm.elements[e].value != '') hasEmail = true;
      }
      else if(aForm.elements[e].name == 'first_name')
      {
        if(aForm.elements[e].value != '') hasFirstName = true;
      }
      else if(aForm.elements[e].name == 'last_name')
      {
        if(aForm.elements[e].value != '') hasLastName = true;
      }
    }
  }
  //If all required fields are set then return true
  if(hasEmail && hasLastName && hasFirstName) return true;

  var msg = '';
  if(!hasEmail) msg = "'Email Address'";
  else if(!hasFirstName) msg = "'First Name'";
  else if(!hasLastName) msg = "'Last Name'";
  
  alert('Required field ' + msg + ' is missing or incorrectly formatted.');
  
  return false;
}

function BuildNewsSubQueryString(aFrameObj)
{
  var subQuery = '';
  
  for(var i = 1; i <= arrNewsCatItems.length; i++)
  {
    if(subQuery != '') subQuery += '&';
    subQuery += 'cat_item_' + i.toString() + '=' + escape(arrNewsCatItems[i-1]);
  }
  
  return subQuery
}

function ClearNewsCategories()
{
  arrNewsCatItems.length = 0;
}

function AddNewsCategory(categoryCode)
{
  //checkCode the catch code does not already exist
  for(var i = 0; i < arrNewsCatItems.length; i++)
  {
    if(arrNewsCatItems[i] == categoryCode) return;
  }
  
  //Add a new cat code to the end of the array
  arrNewsCatItems[arrNewsCatItems.length] = categoryCode;
}

function ShowNewsRegistrationSize(divContainer, aWidth, aHeight, callBack)
{
  var divObj = document.getElementById(divContainer);
            
  if(!divObj)
  {
    alert('Cannot find DIV \'' + divContainer + '\'. Please check you have set the HTML div id tag as <div id=”' + divContainer + '”/>.');
    return;
  }
  
  //Create the new frame if it does not already exist
  if(!newsRegFrame)
  {
    newsRegFrame = CreateFrameComponentSize(CFRAME_NEWS_REG, divObj, aWidth, aHeight);
  }

  //newsCallBackFunction = callBack;
  AddNewsRegCallBack(callBack);
  //Display the registration component page
  CallServerParams(newsRegFrame, GetCommerceServer() + CCSCR_NEWS_REG_URL, '', BuildNewsSubQueryString);
}

function ShowNewsRegistration(divContainer, callBack)
{
  var divObj = document.getElementById(divContainer);
            
  if(!divObj)
  {
    alert('Cannot find DIV \'' + divContainer + '\'. Please check you have set the HTML div id tag as <div id=”' + divContainer +'”/>.');
    return;
  }
  
  //If not already created then create the new frame
  if(!newsRegFrame)
  {
    newsRegFrame = CreateFrameComponentSize(CFRAME_NEWS_REG, divObj, divObj.style.width, divObj.style.height);
  }

  //newsCallBackFunction = callBack;
  AddNewsRegCallBack(callBack);
  //divContainer.display = 'block';
  //Display the registration component page
  CallServerParams(newsRegFrame, GetCommerceServer() + CCSCR_NEWS_REG_URL, '', BuildNewsSubQueryString);
}

function SubmitNewsRegistration(formName)
{
  if(!ValidateNewsRequiredFields(formName)) return false;
  
  //Creates the form as hidden if it has not already created
  if(!newsRegFrame) newsRegFrame = CreateFrameComponentHidden(CFRAME_NEWS_REG);
  
  //Calls the server to register the new details
  CallServer(newsRegFrame, GetCommerceServer() + CCSCR_NEWS_REG_POST_URL, formName);
}

function SubmitNewsRegistrationCallBack(formName, callBack)
{
  if(!ValidateNewsRequiredFields(formName)) return false;
  
  //Creates the form as hidden if it has not already created
  if(!newsRegFrame) newsRegFrame = CreateFrameComponentHidden(CFRAME_NEWS_REG);

  //newsCallBackFunction = callBack;
  AddNewsRegCallBack(callBack);
  //Calls the server to register the new details
  CallServer(newsRegFrame, GetCommerceServer() + CCSCR_NEWS_REG_POST_URL, formName);
}

function AddNewsRegCallBack(callBack) {
    if (callBack) {
        enqCallBackFunction = callBack;
        //Register event listner in the core lib (CSClient.js)
        AddMessageQueueCallBack("newsRegCallBack", msgNewsRegCallBack);
    }
}
function msgNewsRegCallBack(msgName, msgParams) {
    switch (msgName) {
        case "newsRegCallBack": NewsRegistrationCallBack(msgParams); break;
    }
}
function NewsRegistrationCallBack(msgParams) {
    if (newsCallBackFunction) {
        var strCompleted = messageManager.GetParamValueDef(msgParams, "c", "false");
        var regCompleted = strCompleted.toLowerCase() == "true";
        //If the call back function is assigned then call it
        newsCallBackFunction(regCompleted);
    }
}
/*function NewsRegistrationCallBack(regCompleted)
{
  //If the call back function is assigned then call it
  if(newsCallBackFunction) newsCallBackFunction(regCompleted);
}*/
