/* THIS FILE CONTAINS CONFIDENTIAL TRADE SECRET INFORMATION
 * Any use without the expressed written approval of * Mervyn L. Cohn or Peter W. Pedrotti, Jr. is prohibited.
 *
 * e-Praise Diamond Appraisal Engine
 * Copyright (C) 2002-2010 Mervyn L. Cohn and Peter W. Pedrotti, Jr.
 * All Rights Reserved
 */

//COOKIE FUNCTIONS

function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60;
		//expires = expires * 1000 * 60 * 60 * 24;
	}
	
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) 
	{
		return null;
	}
}

//END COOKIE FUNCTIONS

function goback(){ //BACK BUTTON SCRIPT
history.back();
} //check

function CutScoreIt(){ // MAIN eCS FUNCTION

/* Input characteristic data used to determine value, assigning default values
 * for certs that don't record particular values. Populate form fields
 * in Session.asp for transfer to database and to PDF appraisal template.
 */
	var CertNumber = document.FrontPage_Form1.CertNumber.value;

sessvars.CertNumber=(CertNumber);				

	var Weight = parseFloat(document.FrontPage_Form1.Weight.value);

sessvars.Weight=(Weight);		

	var Shape = document.FrontPage_Form1.Shape.value;

sessvars.Shape=(Shape);		
	var Cut = document.FrontPage_Form1.Cut.value;

sessvars.Cut=(Cut);		
	var Width1 = parseFloat(document.FrontPage_Form1.Width1.value);

sessvars.Width1=(Width1);		
	var Width2 = parseFloat(document.FrontPage_Form1.Width2.value);

sessvars.Width2=(Width2);		
	var Depth = parseFloat(document.FrontPage_Form1.Depth.value);

sessvars.Depth=(Depth);
	var DimensionsString = (Width1 + " x " + Width2 + " x " + Depth);

/* SHAPEFACTORS: These are the retail customer desirability factors
 * relative to a Round Brilliant diamond, for diamonds of different shapes. Also
 * gives the constants used in calculating the approximate weights of the
 * different shapes from their dimensions.
 */
	var ShapeFactors = function(Shape){ // func1
	var ShapeArray = ["Round",1,0.0017005,"Oval",0814,0.0019050,"Rectangular",0.755,0.0023955,"Square",0.835,0.0022975,"Marquise",0.846,0.0015230,"Navette",0.846,0.0015230,"Boat",0.856,0.0015230,"Pear",0.792,0.0018585,"Teardrop",0.792,0.0018585,"Heart",0.771,0.0015184,"Cushion",0.99,0.00180,"Trillion",0.846,0.00272,"Trilliant",0.846,0.00272,"",1,0.00174];
	for (j = 0 ; j < ShapeArray.length; j++){ // for,
	  if (ShapeArray[j] == Shape){ // if
	  ShapeDesireFactor = ShapeArray[j+1];
	  ShapeWeightFactor = ShapeArray[j+2];
} 
} 
	ShapeFactorsArray = [ShapeWeightFactor,ShapeDesireFactor];
	return ShapeFactorsArray;
}
	ShapeFactorsArray = new ShapeFactors(Shape);
	ShapeWeightFactor = ShapeFactorsArray[0];
	ShapeDesireFactor = ShapeFactorsArray[1];

/* IDEALIZED WEIGHT: This determines whether the subject is too tall
 * or too wide to have a Girdle Percent within the range commonly accepted
 * as representing the "Tolkowsky Ideal Range" of 57.3% to 61.9%.
 * If the stone is too tall, a new height is calculated to bring its ratio
 * to 61.9%; if too wide, a new mean girdle diameter is calculated to bring
 * its ratio to 57.3%. The weight of a stone with the idealized dimensions
 * is calculated using the standard appraiser's formulas.
 * In principle, this causes the appraisal to be based on the largest "Ideal"
 * diamond that can be reground from the subject diamond. From time to time,
 * if in the opinion of the supervising appraiser the definition of the
 * "ideal range" changes, these values may simply be edited to reflect
 * current opinion.
 */
	WtCalc = function(Width1, Width2, Depth, ShapeWeightFactor){ 
	
var Divisor, Multiplier;
	if (Shape == "Round" && Cut == "Brilliant"){Divisor = 0.57; Multiplier = 0.63;}

	else if (Shape == "Round" && Cut == "Modified Brilliant"){Divisor = 0.57; Multiplier = 0.63;}//		
	else if (Shape == "Round" && Cut == "Mine"){Divisor = 0.603; Multiplier = 0.687;}
	else if (Shape == "Round" && Cut == "Old Mine"){Divisor = 0.603; Multiplier = 0.687;}
	else if (Shape == "Round" && Cut == "Premium"){Divisor = 0.57; Multiplier = 0.63;}
	else if (Shape == "Round" && Cut == "European"){Divisor = 0.603; Multiplier = 0.687;}
	else if (Shape == "Round" && Cut == "Old European"){Divisor = 0.603; Multiplier = 0.687;}
			
	else if (Shape == "Square" && Cut == "Radiant"){Divisor = 0.65; Multiplier = 0.76;}
	else if (Shape == "Square" && Cut == "Princess"){Divisor = 0.65; Multiplier = 0.76;}
	else if (Shape == "Square" && Cut == "Modified Brilliant"){Divisor = 0.65; Multiplier = 0.76;}
	else if (Shape == "Square" && Cut == "Asscher"){Divisor = 0.65; Multiplier = 0.76;}
	else if (Shape == "Square" && Cut == "Emerald"){Divisor = 0.60; Multiplier = 0.73;}
	else if (Shape == "Square" && Cut == "Princess Plus"){Divisor = 0.65; Multiplier = 0.760;}
	else if (Shape == "Square" && Cut == "Premium"){Divisor = 0.634; Multiplier = 0.700;}
	
	else if (Shape == "Cushion" && Cut == "Mine"){Divisor = 0.603; Multiplier = 0.687;}	
	else if (Shape == "Cushion" && Cut == "Old Mine"){Divisor = 0.603; Multiplier = 0.687;}		
	else if (Shape == "Cushion" && Cut == "Brilliant"){Divisor = 0.603; Multiplier = 0.687;}	
	else if (Shape == "Cushion" && Cut == "Modified Brilliant"){Divisor = 0.603; Multiplier = 0.687;}

	else if (Shape == "Rectangular" && Cut == "Radiant"){Divisor = 0.65; Multiplier = 0.760;}
	else if (Shape == "Rectangular" && Cut == "Princess"){Divisor = 0.65; Multiplier = 0.760;}
	else if (Shape == "Rectangular" && Cut == "Modified Brilliant"){Divisor = 0.653; Multiplier = 0.760;}
	else if (Shape == "Rectangular" && Cut == "Asscher"){Divisor = 0.653; Multiplier = 0.760;}
	else if (Shape == "Rectangular" && Cut == "Emerald"){Divisor = 0.60; Multiplier = 0.73;}	
	else if (Shape == "Rectangular" && Cut == "Step"){Divisor = 0.60; Multiplier = 0.73;}
	else if (Shape == "Rectangular" && Cut == "Mixed"){Divisor = 0.60; Multiplier = 0.73;}	
	else if (Shape == "Rectangular" && Cut == "Asscher"){Divisor = 0.653; Multiplier = 0.760;}		
		
	else if (Shape == "Oval"){Divisor = 0.60; Multiplier = 0.720;}//
								
	else if (Shape == "Pear"){Divisor = 0.48; Multiplier = 0.75;}//
	
	else if (Shape == "Marquise"){Divisor = 0.48; Multiplier = 0.65;}//
	
	else if (Shape == "Heart"){Divisor = 0.557; Multiplier = 0.600;}//
	
	else if (Shape == "Trillion" || Shape == "Trilliant")
	{Divisor = 0.557; Multiplier = 0.680;
	}
	
	else 
	{alert("Sorry, our formula cannot analyse that shape/cut combination.");
	}

	var GirdleMean = ((Width1 + Width2) / 2);
	var DepthPctCalc;
	DepthPctCalc = ((100 * Depth) / GirdleMean);
	
	var NewGirdle = GirdleMean;
	var NewDepth = Depth;
	var RawRatio;
	
	if (DepthPctCalc < (100 * Divisor))
		{ // i.e., for RB, if D% <57, div width by .57
		RawRatio = "shallow";	
		NewGirdle = (Depth / Divisor);
		}
	else if (DepthPctCalc > (100 * Multiplier))
		{ // i.e., if D% >63, mult depth by .63
		RawRatio = "deep";		
		NewDepth = (GirdleMean * Multiplier);
		}
	else{	
		RawRatio = "ideal";
		}
	
	sessvars.RawRatio=(RawRatio);		
	
	WtCalcRaw = (Math.pow(GirdleMean, 2) * Depth * 3.52 * ShapeWeightFactor);
	WtCalcRawTemp = (WtCalcRaw * 1000);
	WtCalcRaw = parseInt(WtCalcRawTemp) / 1000;
	
	var WtCalc = Math.pow(NewGirdle, 2) * NewDepth * 3.52 * ShapeWeightFactor;
	return WtCalc;
	}

		WtCalc = WtCalc(Width1, Width2, Depth, ShapeWeightFactor);	

	var WtCalcTemp;
	WtCalcTemp = (WtCalc * 1000);
	WtCalc = parseInt(WtCalcTemp) / 1000;
	
var WtCalcNF = new NumberFormat();
WtCalcNF.setNumber(WtCalc);
WtCalcNF.setPlaces(3);
WtCalc$ = String(WtCalcNF.toFormatted());

	var GScore;
	var GScoreRaw;
	GScore = (WtCalc / Weight);
	GScoreRaw = GScore;
	
	if (GScoreRaw >= 0.995 && GScoreRaw < 1.005) {
		GScore = 1.000;
		}
	
	if (GScoreRaw >= 1.000){
		GScore = (1 / GScore);
		}

	var GScoreNF = new NumberFormat();
	GScoreNF.setNumber(GScore);
	GScoreNF.setPlaces(3);
	GScore = String(GScoreNF.toFormatted());

sessvars.GScore=(GScore);		
	document.FrontPage_Form1.GScore.value = (GScore);
	var GScoreRawNF = new NumberFormat();
	GScoreRawNF.setNumber(GScoreRaw);
	GScoreRawNF.setPlaces(3);
	GScoreRaw = String(GScoreRawNF.toFormatted());	

sessvars.GScoreRaw=(GScoreRaw);			
	
	document.FrontPage_Form1.GScoreRaw.value = (GScoreRaw);
	
}
