/**************************************/
// $Revision: 20 $
// $Author: Jack.feng $
// $Date: 11/15/06 10:06a $
/**************************************/
function GetTheForm()
{
    var theForm;
    theForm = document.forms[0];
    return theForm;
}

function SetTheForm(theForm)
{    
    theForm.u.value = theForm.username.value;
    theForm.p.value = theForm.password.value;
    theForm.method = 'get';
    theForm.target = "_new";
}

function GetSiteSearchResults(action)
{    
    var theForm = GetTheForm();
    theForm.action = action;
    theForm.target = "_self";
    theForm.method = "post";
    theForm.submit();
}

function FindSalesReps(action)
{
    var theForm =GetTheForm();
    theForm.action = action + "FindSalesReps.aspx";
    theForm.target = "_self";
    theForm.method = "post";  
    theForm.submit();
}

function EnterSubmitFindReps(event,action) 
{
    var theForm = GetTheForm();
    theForm.action = action + "FindSalesReps.aspx"; 
    theForm.target = "_self";
    theForm.method = "post";
    EnterSubmit(theForm);
}

function EnterSubmitSearch(event, action) 
{
  var theForm = GetTheForm(); 
  theForm.action = action;
  theForm.method = "post";
  theForm.target = "_self";

  if(theForm.radioLogin != null)
  {
     theForm.username.value = "";
     theForm.password.value = "";
     HideErrorMsg();    
  }
  
  EnterSubmit(theForm);
}

function EnterSubmit(theForm)
{
  if(event && event.keyCode == 13)
  {
    var spanElements = document.getElementsByTagName("span");
    var divElements = document.getElementsByTagName("div");
    HideControlErrorMsg(spanElements);
    HideControlErrorMsg(divElements);
    theForm.submit();

  }
  else
  {
    return true;
  }
}

function HideControlErrorMsg(elements)
{
  var start = 0;
  for (i=start; i<elements.length; i++)
  {
      if(elements[i].id != null)
      {        
        if(elements[i].id.indexOf("_ctl0") != -1)
        {
              if(elements[i].id.indexOf("ShoppingCart") == -1)
              {
                var spanErrorMsg = document.getElementById(elements[i].id);
                if(spanErrorMsg != null)
                {
                        spanErrorMsg.style.visibility="hidden";                 
                }
              }         
        }
        else
        {
          var spanErrorMsg = document.getElementById("errorMsg");
          if(spanErrorMsg != null)
          {
            spanErrorMsg.style.visibility="hidden";                 
          }           
        }
      }
    }
}

function SubmitToSelectedSubscription(language, event) 
{  
  	var theForm = GetTheForm();
	
	myOption = -1;
	for (i=theForm.radioLogin.length-1; i > -1; i--) 
	{
		if (theForm.radioLogin[i].checked) 
		{
			myOption = i;
		}
	}	

	if(myOption != -1)
 	{    	
		if(ValidLogin(language))
    	{       			
			SwitchSubscription(language, theForm, theForm.radioLogin[myOption].value, event);
    	}
    	else
    	{
      		return false;
    	}    
  	}
  	else
  	{
    	var spanErrorMsg = document.getElementById("errorMsg");
    	if(errorMsg != null)
    	{
      	if (language.toUpperCase() == "EN")
        	spanErrorMsg.innerText = "* Please select an account to login.";
      	else
        	spanErrorMsg.innerText = "* Veuillez sélectionner le compte où vous désirer vous connecter.";

      	spanErrorMsg.style.display="";                   
    	}

    	return false;
  	}  
}

function SwitchSubscription(language, theForm, selectedValue, event)
{		
	switch (selectedValue)
    {
      case "myaccount": 		       
          LogonMyAccount(theForm, language, event);
          break;
      case "online":        
          LogonCCHOnline(theForm, language, event);
          break;
      case "hranswers":    
          LogonHRAnswerNow(theForm, event);
          break;
      case "prac":
          LogonPractitioners(theForm, language, event);
          break;
       case "pulse":
          LogonLegislativePulse(theForm, event);
          break;
     }    
}
/* Change Tkt. No : #
   Changed Date: 04/22/08
   Changed By: Biruntha Sasikumar */  
function ChangeLoginText(selectedValue,language)
{	
    	
	var choiceButton = document.getElementById("spanUserName");
	 
	
	   	      
	    if (selectedValue == "myaccount")
	     {
	      
	      if (language.toUpperCase() == "EN")  
            choiceButton.innerText="Email:";
          else
            choiceButton.innerText="Courriel:"; 
            }
        else
        {
          if (language.toUpperCase() == "EN")  
            choiceButton.innerText="Username:";
          else
             choiceButton.innerText="Nom d\'utilisateur:";
          }
          
        
}
/* Change End */

function LogonMyAccount(theForm, language, event)
{
    var url = "https://";
    var host = document.location.host;          
    var spath = document.location.pathname.toLowerCase();
    var epath = "MyAccount/Login.aspx";
	var fpath = "MonCompte/Connexion.aspx";
    
    if(language.toUpperCase() == "EN")
    {
      if(spath.indexOf("index.aspx") > 0)
      {
        url += host + spath.replace("index.aspx", epath);
      }
      else
      {
        url += host + spath + epath;
      }
    }
    else
    {
      if(spath.indexOf("accueil.aspx") > 0)
      {
        url += host + spath.replace("accueil.aspx", fpath);
      }
      else
      {
        url += host + spath + fpath;
      }
    }
	
    theForm.action = url;
    theForm.method = "post";
    theForm.target = "_self";
    theForm.submit();
}

 

function SubmitTheForm(theForm, event)
{
    if(event != null)
    {     
      theForm.username.value = "";
      theForm.password.value = "";
      HideErrorMsg();
      EnterSubmit(theForm);      
    }
    else
    {
      theForm.submit();
    }
}

function LogonCCHOnline(theForm, language, event)
{
    SetTheForm(theForm);    
    if(language.toUpperCase() == "EN")
    {
      theForm.action = "http://www.cchonline.ca"; 
    }
    else
    {
      theForm.action = "http://www.cchenligne.ca";
    }

    SubmitTheForm(theForm, event);
}


function LogonPractitioners(theForm, language, event)
{
    SetTheForm(theForm);
   
    theForm.action = "http://www.cchpractitioners.ca";
    SubmitTheForm(theForm, event);
}


function LogonHRAnswerNow(theForm, event)
{
    SetTheForm(theForm);
    theForm.vid.value = "nextpage:hranswersnow";           
    theForm.action = "http://www.hranswersnow.ca/NXT/gateway.dll";
    SubmitTheForm(theForm, event);
}

function LogonLegislativePulse(theForm, event)
{
    theForm.method = 'post';
    theForm.target = "_new";
    theForm.action = "http://pulse.cch.ca/login.asp";
    theForm.submit();
}

function SubmitFrom(arg)
{    
    var fromButton = arg.name;    
    if(fromButton == "imageHRAnswersNowButton")
    {
      if(ValidLogin("EN"))
      {        
        return LogonHRAnswersNow();
      }
      else
      {
        return false;
      }
    }    
    else if(fromButton == "imageCCHOnlineButton")
    {
      if(ValidLogin("EN"))
      {
        return LogonNewCCHOnline();
      }
      else
      {
        return false;
      }
    }
    else
    { 
      theForm = GetTheForm();
      var url = "http://";
      var host = document.location.host;          
      var spath = document.location.pathname.toLowerCase();

      if(spath.indexOf(".aspx") == -1)
      {
        url += host + spath + "index.aspx";
      }
      else
      {
        url = document.location;
      }

      theForm.action = url;
      theForm.method = 'post';
      theForm.target = "_self";            
      return ValidateMultiMediaProductQuantity();
    }
}

function ValidLogin(language)
{  
  var theForm = GetTheForm();
  var errstr = "";
	
  if (theForm.username.value == "" )
  {
    errstr += "user name,";
    theForm.username.focus();
  }
  
  if (theForm.password.value == "" )
  {
        errstr= errstr + "password";                  
        if (theForm.username.value == "" )
        {
          theForm.username.focus();
        }
        else
        {
          theForm.password.focus();
        }
  }

  if(errstr != "")
  {
    var spanErrorMsg = document.getElementById("errorMsg");
	
    if(spanErrorMsg != null)
    {  		      
	  if (language.toUpperCase() == "FR")
	  {		
        spanErrorMsg.innerHTML = "Le nom d'utilisateur et le mot de passe sont requis.";		
      }
      else
      {
        spanErrorMsg.innerHTML = "A valid username and password are required.";
      }
       //innerText is not compatible with Firefox for some reason   
	  spanErrorMsg.style.display = "";                   
    }

    return false;
  }

  return true;
}

function LogonNewCCHOnline()
{
  var theForm = GetTheForm();
  SaveUserInfo();      
  ResetQuantity();
  HideErrorMsg();
  theForm.method = 'get';
  theForm.target = "_new";
  theForm.u.value = theForm.username.value;
  theForm.p.value = theForm.password.value;
  theForm.savePassword.checked = GetCookie("savePassword");  
  theForm.action = "http://www.cchonline.ca";
  SubmitTheForm(theForm, event);
}

function HideErrorMsg()
{
    var spanErrorMsg = document.getElementById("errorMsg");
    if(spanErrorMsg != null)
    {
      spanErrorMsg.style.display="none";               
    }

    var spanCtlErrorMsg = document.getElementById("_ctl0_errorMsg");
    if(spanCtlErrorMsg != null)
    {
      spanCtlErrorMsg.style.display="none";                   
    }    
}

function LogonHRAnswersNow()
{
    var theForm = GetTheForm();
    SaveUserInfo();      
    ResetQuantity();
    HideErrorMsg()
    theForm.method = 'get';
    theForm.target = "_new";
    theForm.u.value = theForm.username.value;
    theForm.p.value = theForm.password.value;
    theForm.savePassword.checked = GetCookie("savePassword");  
    theForm.vid.value = "nextpage:hranswersnow";           
    theForm.action = "http://www.hranswersnow.ca/NXT/gateway.dll";
    SubmitTheForm(theForm, event);
}

function ResetQuantity()
{
  var inputElements = document.getElementsByTagName("input");
  var start = 0;
  for (i=start; i<inputElements.length; i++)
  {
    if(inputElements[i].id != null)
    {        
      if(inputElements[i].id.indexOf("ctl1_Quantity") != -1)
      {
        if(inputElements[i].value != "" && inputElements[i].value != "0")
        {
          inputElements[i].value = "";
        }
      }  
    }
   } 
}

function GetCookie(name) 
{
  var tempArray = new Array();
  var cookiesStr = document.cookie;
  var cookiesArray = cookiesStr.split("; ");

  for (var i=0; i<cookiesArray.length; i++) 
  {
    tempArray = cookiesArray[i].split("=");
    if (tempArray[0] == name)
      return tempArray[1];
  }

  return "";
}

function SaveUserInfo(thForm) 
{
  if (document.form.savePassword.checked) 
  {
    var expiryDate = new Date(2010, 00, 01);
    expiryDate = expiryDate.toGMTString();
    document.cookie = "username=" + document.form.username.value + "; expires=" + expiryDate;
    document.cookie = "password=" + document.form.password.value + "; expires=" + expiryDate;
    document.cookie = "savePassword=" + document.form.savePassword.checked + "; expires=" + expiryDate;
  }
  else 
  {
    var expiryDate = new Date();
    expiryDate = expiryDate.toGMTString();
    document.cookie = "username=; expires=" + expiryDate;
    document.cookie = "password=; expires=" + expiryDate;
    document.cookie = "savePassword=; expires=" + expiryDate;
  }

  return true;
}

function ExpandCollapse(root, obj, theImage)
{  
    if (obj.style.display=="none") 
    {
      obj.style.display = ''
      document.images[theImage].src = root + "_images/icons/minus.gif"
    }
    else 
    {
      obj.style.display = "none"
      document.images[theImage].src = root + "_images/icons/plus.gif"
    }
}

function Expand(root, obj, theImage)
{  
      obj.style.display = ''
      document.images[theImage].src = root + "_images/icons/minus.gif"
}

function Collapse(root, obj, theImage)
{  
      obj.style.display = 'none'
      document.images[theImage].src = root + "_images/icons/plus.gif"
}

function ValidatorFocus()
{
    var i;
    for (i = 0; i < Page_Validators.length; i++) 
    {
      if (!Page_Validators[i].isvalid) 
      {
          document.getElementById(Page_Validators[i].controltovalidate).focus();
          break;
      }
    }
}

function RedirectTo(location, language)
{
    if(language.toUpperCase() == "EN")
    {
      if (confirm('You are about to leave CCH website.'))
        window.open(location);
    }
    else
    {
      if (confirm('Vous êtes sur le point de quitter notre site web CCH.'))
        window.open(location);
    }
}


function ShowHideCreditCard(flag) 
{
	var invite = document.getElementById("tabCreditCard");
  if (invite!=null) 
  {
   if(flag == 0)
   {
    cmCreateManualLinkClickTag('radio/creditcard', 'creditcard');
    EnablePaymentValidators();
    invite.style.display = "";
   } 
   else
   {
    cmCreateManualLinkClickTag('radio/cchaccount', 'cchaccount');
    DisablePaymentValidators();
    invite.style.display = "none"; 
   }
  }
}

function ShowNewShippingAddress()
{
	var e = document.getElementById("ckbShipToAnotherAddress");
	
	if (e.checked)                                  
	{ 
		divNewShippingAddress.style.display = 'inline';
		EnableValidatorsOrder();
	}
	else
	{
		divNewShippingAddress.style.display = 'none';
		DisableValidatorsOrder();
	}  	
}

function Hide(buttonID) 
{
 if (navigator.appName == "Microsoft Internet Explorer")
  {
  ButtonsTable.style.display = "none";
  ProcessingMessageTable.style.display = "";
}
else
  {
    ProcessingMessageTable.style.display = "";
    document.getElementById(buttonID).src = "../_images/spacer.gif";  
  }
}
