﻿
/*  ISC Original Scripts modified to work in SharePoint */


/* ParcelPicture.js
---------------------------------------------------------------*/
var sErrorMsg = '';

function CalculateRate( sRequestType )
{
	var serviceOption = GetServiceOption( );
	var area = GetArea( );
	var numCount = GetParcelCount( );

	if ( sRequestType == "picture" )
	{
		CalculateParcelPictureCost( serviceOption, area, numCount );
	}
	else if ( sRequestType == "mineral" )
	{
		CalculateMineralCertificationCost( serviceOption, area, numCount );
	}
}; // CalculateRate


/*********************************
    Helper Methods
 *********************************/

// Calculates the rates for parcel pictures
function CalculateParcelPictureCost( sServiceType, sArea, iParcelCount )
{	
	var iRate = 0;
	
	// made dynamic so that prices can be updated on the future
	var regParcelPrice = $( ".jq_regParcelPrice" )[0] != null ? $( ".jq_regParcelPrice" ).val( ) : 0;
	var regQuarterNoBundlePrice = $( ".jq_regQuarterNoBundlePrice" )[0] != null ? $( ".jq_regQuarterNoBundlePrice" ).val( ) : 0
	var expParcelPrice = $( ".jq_expParcelPrice" )[0] != null ? $( ".jq_expParcelPrice" ).val( ) : 0;
	var expQuarterNoBundlePrice = $( ".jq_expQuarterNoBundlePrice" )[0] != null ? $( ".jq_expQuarterNoBundlePrice" ).val( ) : 0;
	var priParcelPrice = $( ".jq_priParcelPrice" )[0] != null ? $( ".jq_priParcelPrice" ).val( ) : 0;
	var priQuarterNoBundlePrice = $( ".jq_priQuarterNoBundlePrice" )[0] != null ? $( ".jq_priQuarterNoBundlePrice" ).val( ) : 0;

	//alert("Calculating parcel picture count: " + iParcelCount + "\n" + "service type " + sServiceType + "\nsArea " + sArea);
	
	if ( sServiceType == "regular" )
	{
		if ( sArea == "parcel" ) { iRate = iParcelCount * regParcelPrice; }
		else if ( sArea == "quarter" ) { iRate = iParcelCount * regQuarterNoBundlePrice; }
	}
	else if ( sServiceType == "express" )
	{
		if ( sArea == "parcel" ) { iRate = iParcelCount * expParcelPrice; }
		else if ( sArea == "quarter" ) { iRate = iParcelCount * expQuarterNoBundlePrice; }
	}	
	else if ( sServiceType == "priority" )
	{
		if( sArea == "parcel" ) { iRate = iParcelCount * priParcelPrice; }
		else if ( sArea == "quarter" ) { iRate = iParcelCount * priQuarterNoBundlePrice; }
	}
	
	SetRateField( "$ " + iRate );

}; // CalculateParcelPicture


// Calculates the rates for mineral certification
function CalculateMineralCertificationCost( sServiceType, sArea, iParcelCount )
{
	var iRate = 0;
	
	// made dynamic so that prices can be updated on the future
	var regParcelPrice = $( ".jq_regParcelPrice" )[0] != null ? $( ".jq_regParcelPrice" ).val( ) : 0;
	var regQuarterBundlePrice = $( ".jq_regQuarterBundlePrice" )[0] != null ? $( ".jq_regQuarterBundlePrice" ).val( ) : 0;
	var regQuarterNoBundlePrice = $( ".jq_regQuarterNoBundlePrice" )[0] != null ? $( ".jq_regQuarterNoBundlePrice" ).val( ) : 0
	var expParcelPrice = $( ".jq_expParcelPrice" )[0] != null ? $( ".jq_expParcelPrice" ).val( ) : 0;
	var expQuarterBundlePrice = $( ".jq_expQuarterBundlePrice" )[0] != null ? $( ".jq_expQuarterBundlePrice" ).val( ) : 0;
	var expQuarterNoBundlePrice = $( ".jq_expQuarterNoBundlePrice" )[0] != null ? $( ".jq_expQuarterNoBundlePrice" ).val( ) : 0;
	var priParcelPrice = $( ".jq_priParcelPrice" )[0] != null ? $( ".jq_priParcelPrice" ).val( ) : 0;
	var priQuarterBundlePrice = $( ".jq_priQuarterBundlePrice" )[0] != null ? $( ".jq_priQuarterBundlePrice" ).val( ) : 0;
	var priQuarterNoBundlePrice = $( ".jq_priQuarterNoBundlePrice" )[0] != null ? $( ".jq_priQuarterNoBundlePrice" ).val( ) : 0;
	
	var bIsBundle = $( ".jq_Bundle :checked" )[0] != null;

	//alert("Calculating mineral picture count: " + iParcelCount + "\n" + "service type " + sServiceType + "\nsArea " + sArea);
	
	if ( sServiceType == "regular" )
	{
		if ( sArea == "parcel" ) { iRate = iParcelCount * regParcelPrice; }
		else if ( sArea == "quarter" )
		{
			// calculate the bundle rate for a quarter section
			if ( bIsBundle ) { iRate = iParcelCount * regQuarterBundlePrice; }
			else { iRate = iParcelCount * regQuarterNoBundlePrice; }
		}
	}
	else if ( sServiceType == "express" )
	{
		if ( sArea == "parcel" ) { iRate = iParcelCount * expParcelPrice; }
		else if ( sArea == "quarter" )
		{
			// calculate the bundle rate for a quarter section
			if ( bIsBundle ) { iRate = iParcelCount * expQuarterBundlePrice; }
			else { iRate = iParcelCount * expQuarterNoBundlePrice; }
		}
	}
	else if ( sServiceType == "priority" )
	{
		if( sArea == "parcel" ) { iRate = iParcelCount * priParcelPrice; }
		else if ( sArea == "quarter" )
		{
			// calculate the bundle rate for a quarter section
			if ( bIsBundle ) { iRate = iParcelCount * priQuarterBundlePrice; }
			else { iRate = iParcelCount * priQuarterNoBundlePrice; }
		}
	}
	
	SetRateField( "$ " + iRate );

}; // CalculateMineralCertificationCost


// returns the number of parcel #'s entered
function GetParcelCount( )
{
	var iParcelCount = 0;
    
    // get collection of input fields
    var inputArray = $( ".txt0 input.jq_TXT" );
    if ( inputArray != null && $( inputArray ).length > 0 )
    {
    	// loop through all of the input fields to see if there are any items entered
	    for( var i = 0; i < 10; i++ )
	    {
		    var oParcelField = $( inputArray ).eq( i );
		
		    // check if there has been a value entered
		    if( oParcelField != null )//&& $( oParcelField ).val( ).length > 0 )
		    {
		        var value = $( oParcelField ).val( );
			    var sTrimmedString = adxTrim( $( oParcelField ).val( ) );
    			
			    if ( sTrimmedString != null && sTrimmedString.length > 0 )
				    iParcelCount++;
		    }
	    } // for
    }

	//alert("parcel count: " + iParcelCount);
	
	return iParcelCount;
	
}; // GetParcelCount


// checks for any parcel numbers that are not 9 digits or a number
function GetBadParcelNumbers( )
{ 
	var iBadNumbers = 0;	
	
	// get collection of input fields
    var inputArray = $( ".txt0 input.jq_TXT[fieldType='parcel']" );
    if ( inputArray != null && $( inputArray ).length > 0 )
    {
	    // loop through all of the parcel fields to see if there are any items entered
	    for( var i = 0; i < 10; i++ )
	    {
		    var oParcelField = $( inputArray ).eq( i );
    		
		    // check if there has been a value entered
		   	if( oParcelField != null )//&& $( oParcelField ).val( ).length > 0 )
		    {
			    var value = $( oParcelField ).val( );
			    var sTrimmedString = adxTrim( value );

			    if ( sTrimmedString != null && sTrimmedString.length > 0 )
			    {
		            if( sTrimmedString.length != 9 )
				    {
					    iBadNumbers++;
				    }
				    // length ok, check to see if it is a number
				    else if ( sTrimmedString.length == 9 && isNaN( sTrimmedString ) )
					{
						iBadNumbers++;
				    }
			    }
		    }
	    } // for
	}
				
	return iBadNumbers;
	
}; // GetBadParcelNumbers


/*********************************
    End Helper Methods
 *********************************/


/*********************************
    Utility Methods
 *********************************/

// get the service option from the service option radio buttons
function GetServiceOption( )
{
    var sServiceType = "";
	
	var rblServiceOption = $( ".jq_ServiceOption :radio:checked" );
	if ( rblServiceOption != null )
	{
	    if ( $( rblServiceOption ).val( ) == "1" )
	        sServiceType = "regular";
	    else if ( $( rblServiceOption ).val( ) == "2" )
	        sServiceType = "express";   // originally coded as direct
	    else if ( $( rblServiceOption ).val( ) == "3" )
	        sServiceType = "priority";
	    else if ( $( rblServiceOption ).val( ) == "4" )
	        sServiceType = "custom";
	}

	return sServiceType;
	
}; // GetServiceOption


// retrieves the area - either parcel or entire quarter
function GetArea( )
{
	var sArea = "";
	
	var rblArea = $( ".jq_Area :radio:checked" );
	if ( rblArea != null && $( rblArea ).length > 0 )
	{
	    if ( $( rblArea ).val( ) == "1" )
	        sArea = "parcel";
	    else if ( $( rblArea ).val( ) == "2" )
	        sArea = "quarter";
	}
	
	return sArea;
	
}; // GetArea


// Sets the rate field on the form
function SetRateField( sAmount )
{
	var oRateBox = $( ".jq_CalculatedRate" );
	
	if (oRateBox != null)
		$( oRateBox ).text( sAmount );

}; // SetRateField


// Trims leading and trailing spaces from strText.
function adxTrim(sText)
{	
	//Trims leading and trailing spaces from strText.
	//leading spaces 
	if (sText != null && sText != '')
	{
		while (sText.substring(0,1) == ' ') 
		sText = sText.substring(1, sText.length);

		// trailing spaces 
		while (sText.substring(sText.length-1,sText.length) == ' ')
		sText = sText.substring(0, sText.length-1);
	}
	return sText;
};

/*********************************
    End Utility Methods
 *********************************/


/* End ParcelPicture.js
---------------------------------------------------------------*/