/**
* @desc Function DtManager() - Used to create objects which manipulates the download tokens
* DtManager is mainly used to make different kinds of calculations based on Download tokens
*
* @access public
* @param usrDts int - Default download tokens for the new object being created.
* @return return Object
*/
var DT_VALUE =  0.25 // download tokens value constant

function DtManager(usrDts){
	//this.usrDts = this.priceFormat(usrDts);
    this.usrDts = usrDts * DT_VALUE; 
}
	/**
	* @desc Function priceFormat() - Returns given number in price/decimal format. i.e for 2, it will return 2.00
	*
	* @access public
	* @param 
	* @return return float
	*/
	DtManager.prototype.priceFormat = function(p){
		return p.toFixed(2);
	}

	/**
	* @desc Function predictBalanceAfterUse() - It is used to calculate the amount which will be left
	* after subtracting the DTs (corresponding to this object) from the given amount.
	*
	* @access public
	* @param 
	* @return return float
	*/
	DtManager.prototype.predictBalanceAfterUse = function(purAmt){  
    
     
		var t;
		if(this.usrDts > purAmt)
			t = (this.usrDts - purAmt);
		else
			t = 0;
		//return this.priceFormat(t);        
        return this.priceFormat(t/DT_VALUE);
	}

	/**
	* @desc Function getDts() - Returns download tokens corresponding to this object
	*
	* @access public
	* @param 
	* @return return int
	*/
	DtManager.prototype.getDts = function()
    {
       
		return this.usrDts/DT_VALUE;
	}

/**
* @desc Function getSelectedPaymentMode() - Returns the payment mode opted by the user
*
* @access public
* @param 
* @return return string
*/
function getSelectedPaymentMode(){
    
	if($('pm_cc') && $('pm_cc').checked) return $('pm_cc').value;
	if($('pm_pp') && $('pm_pp').checked) return $('pm_pp').value;
    if($('pm_dt') && $('pm_dt').checked) return $('pm_dt').value;     
   
}


/**
* @desc Function checkUserExistence() - It checks if the user with the entered email/uname exists or not.
* If the user exists, it prompts for the password confirmation else moves the user to the next section.
*
* @access public
* @param 
* @return return NULL
*/
function checkUserExistence()
{
	/**
		no email id present, so no user existence check. Go to the next page directly
	*/
	if(!$('useremail') || userIsLoggedIn)
	{
		toggleDisplay($('formDetails')); 
		toggleDisplay($('reviewDetails'));
		toggleDisplay($('errorDiv')); //added by madan for #0000651
		return;
	}

	new Ajax.Updater('ue_resp', '/checkout/checkUserExistence/',
						{
							asynchronous:true,
							evalScripts:true,
							onComplete:function(request,json)
									   {
											turnOffProcessIndicator();
									   },
							onFailure:function(request,json)
									  {
										    turnOffProcessIndicator();
											smdAlert('Request Failed', '<div class=formerror>Sorry, could not complete the request.<br />Please try again.</div>')
									  },
							parameters:'em='+ $('useremail').value
						 }
					); 
	turnOnProcessIndicator("<div style='width: 200px; text-align: center;font-weight: bold;'>Please wait....</div>");
}

/**
* @desc Function userExistenceCheckRequired() - It determines if the user existence check is to be done or not
* User existence check is normally done on the CC input form. For the billing info review section, check is not done
*
* @access public
* @param 
* @return return bool
*/
function userExistenceCheckRequired()
{
	/**
		Check if this is the proper section to check the user existence.
		User existence is to be checked on the input section only.
		For review section, let the user submit the form.
	*/
	if($('formDetails').style.display == 'none')
		return false;//user existence check not required

	checkUserExistence();
	return true;//user existence check required
}


function initScrollLayer(layerHolder, layerToScroll, dragBarElement, trackElement, scrollDirection)
{ 
  if($(layerHolder) && $(layerToScroll) && $(dragBarElement) && $(trackElement))
  {	  
	  var wndo = new dw_scrollObj(layerHolder, layerToScroll);
	  var scrollScope = $(layerToScroll).clientHeight - $(layerHolder).clientHeight;
	  if(scrollScope > 0)
	  {
		  wndo.setUpScrollbar(dragBarElement, trackElement, scrollDirection, 1, 1);
		  dw_scrollObj.GeckoTableBugFix(layerHolder); 
	  }
	  
	  $('scr_up').style.display = (scrollScope > 0 )?'':'none';
	  $('scr_dn').style.display = $('scr_up').style.display;
	  
  }
}

function initContensPopUpHeight(layerHolder, layerToScroll, contentId)
{
	var scrollScope = $(layerToScroll).clientHeight - $(layerHolder).clientHeight;
	if(scrollScope <= 0 ){
		
	if(isIE)
         {
            $(contentId).style.paddingTop = '20px';            
         }
		$(contentId).style.height = $(layerToScroll).clientHeight + 'px';
		centerDiv('smdAlert');
	}
}


function nonMemberManager(newUserRadioId, existingUserRadioId)
{
	this.newUserRadioId = newUserRadioId;
	this.existingUserRadioId = existingUserRadioId;
}

nonMemberManager.prototype.doSubmit = function()
{
	if($(this.existingUserRadioId).checked)
		this.doLogin();
	else
	if($(this.newUserRadioId).checked)
		this.doRegister();
}

nonMemberManager.prototype.doLogin = function()
{
	var redirectUrl = document.location.href;

   if(onlyPromotional==1)
    {
       var redirectUrl = document.location.href + '/promotional/yes';   
    }

	var strUrl = '/user/login';
	new Ajax.Updater('ajx_rsp',strUrl,
									 {
									   asynchronous:true, 
									   evalScripts:true, 
									   onLoading:function(request, json)
												{
													$('ajx_rsp').innerHTML = 'Verifying login credentials...please wait...';
												},
										onComplete: function(request, json){
														if($('smdAlert')){centerDiv('smdAlert');}
													},
									    method: "POST",
										parameters: 'uname='+$('em').value+'&password='+$('pw').value+'&noFrgtPwdLnk=1&submit=login&redirectUrl='+redirectUrl
									 }
                     );  
}

nonMemberManager.prototype.doRegister = function()
{
	var redirectUrl = document.location.href;

   if(onlyPromotional==1)
    {
       var redirectUrl = document.location.href + '/promotional/yes';   
    }
	
	if($('chkproducerAgreement').checked == false)
	{
		var chkAgreement = '0';
	}
	else
	{
		var chkAgreement = '1';
	}

	var strUrl = '/cart/registerUser';
	new Ajax.Updater('ajx_rsp',strUrl,
									 {
									   asynchronous:true, 
									   evalScripts:true, 
									   onLoading:function(request, json)
												{
													$('ajx_rsp').innerHTML = 'Processing request..please wait..';
												},
										onComplete: function(request, json){
														if($('smdAlert')){centerDiv('smdAlert');}
													},
									    method: "POST",
										parameters: 'uname='+$('email').value+'&newpw='+$('newpw').value+'&cnfpw='+$('cnfpw').value+'&chkAgree='+chkAgreement+'&submit=profile&redirectUrl='+redirectUrl
									 }
                     );  
}

nonMemberManager.prototype.hidePwdInput = function()
{
	$('loggedInAccount').style.display = 'none';
	$('newReg').style.display = '';
}

nonMemberManager.prototype.showPwdInput = function()
{
	$('loggedInAccount').style.display = '';
	$('newReg').style.display = 'none';
}