// display decision alert box
function decision(message, url){
	if(confirm(message)) location.href = url;
}

// open browser window
function openPopUp(url, windowName, w, h, scrollbar) {

           var winl = (screen.width - w) / 2;
           var wint = (screen.height - h) / 2;
           winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scrollbar ;
		   win = window.open(url, windowName, winprops);
           if (parseInt(navigator.appVersion) >= 4) { 
              	win.window.focus(); 
           } 
}

function jumpMenu(target,object,restore){ 
  eval(target+".location='"+object.options[object.selectedIndex].value+"'");
  if (restore) object.selectedIndex=0;
}

function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function getImage(imageName)
{
		findObj('img').src = imageName;
}

function submitDoc(formName) { 
  var obj;
 
	if (obj=findObj(formName)!=null) 
	{
		findObj(formName).submit(); 
	}
	else 
	{
		alert('The form you are attempting to submit called \'' + formName + '\' couldn\'t be found. Please make sure the submitDoc function has the correct id and name.');
	}

}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function getCheckedValue(radioObj) {
	var radioLength = radioObj.length;
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

var CONST_BI_FOCAL = "Bi-focal (With line) Multi vision";
var CONST_PROGRESSIVE_SLIM = "Slim progressive (No-line) Multi vision";
var CONST_PROGRESSIVE_REGULAR = "Progressive (No-line) Multi vision";


function fGetLensUpgrade(vIndex){
    		valMad="";
			valRec="";
			bCustom = false;
    		if (strLensType == "Distance (Single vision)" || strLensType == "Reading (Single vision)"){
    			if (Math.abs(sCYL_R)>2 || Math.abs(sCYL_L)>2){
    				bCustom = true;
    			}
	    		
    			if (parseFloat(sSPH_R) < -10 && isIndexHigher(getCheckedValue(vIndex),"1.67 custom")==false){
    				if (!(valMad > "1.67 custom")){
    					valMad = "1.67 custom";
    					bCustom = true;
    				}
    			}
	    		
    			if (parseFloat(sSPH_L) < -10 && isIndexHigher(getCheckedValue(vIndex),"1.67 custom")==false){
    				if (!(valMad > "1.67 custom")){
    					valMad = "1.67 custom";
    					bCustom = true;
    				}
    			}
	    			
    			if (parseFloat(sSPH_R) < -8 && parseFloat(sSPH_R) >= -10){
    				if (Math.abs(sCYL_R)>2 && isIndexHigher(getCheckedValue(vIndex),"1.67 custom")==false){
    					if (!(valMad > "1.67 custom")){
    						valMad = "1.67 custom";
    						bCustom = true;
    					}
    				}else if(isIndexHigher(getCheckedValue(vIndex),"1.67")==false){
    					if (!(valMad > "1.67")){
    						valMad = "1.67";
    					}
    				}
	    				
    			}
	    		
    			if (parseFloat(sSPH_L) < -8 && parseFloat(sSPH_L) >= -10){
    				if (Math.abs(sCYL_L)>2 && isIndexHigher(getCheckedValue(vIndex),"1.67 custom")==false){
    					if (!(valMad > "1.67 custom")){
    						valMad = "1.67 custom";
    						bCustom = true;
    					}
    				}else if(isIndexHigher(getCheckedValue(vIndex),"1.67")==false){
    					if (!(valMad > "1.67")){
    						valMad = "1.67";
    					}
    				}
    			}
	    			    			
    			if (parseFloat(sSPH_R) < -6 && parseFloat(sSPH_R) >= -8){
    				if (Math.abs(sCYL_R)>2 && isIndexHigher(getCheckedValue(vIndex),"1.61 custom")==false){
    					if (!(valMad > "1.61 custom")){
    						valMad = "1.61 custom";	
    						bCustom = true;
    					}
    				}else if (isIndexHigher(getCheckedValue(vIndex),"1.61")==false){
    					if (!(valMad > "1.61")){
    						valMad = "1.61";
    					}
    				}
	    				
    			}
	    		
    			if (parseFloat(sSPH_L) < -6 && parseFloat(sSPH_L) >= -8){
    				if (Math.abs(sCYL_L)>2 && isIndexHigher(getCheckedValue(vIndex),"1.61 custom")==false){
    					if (!(valMad > "1.61 custom")){
    						valMad = "1.61 custom";
    						bCustom = true;
    					}
    				}else if(isIndexHigher(getCheckedValue(vIndex),"1.61")==false) {
    					if (!(valMad > "1.61")){
    						valMad = "1.61";
    					}
    				}
	    				
    			}
	    			
    			if (Math.abs(sCYL_R)>2 && isIndexHigher(getCheckedValue(vIndex),"1.56 custom")==false){
    				if (!(valMad > "1.56 custom")){
    					valMad = "1.56 custom";
    					bCustom = true;
    				}
    			}
	    			
    			if (Math.abs(sCYL_L)>2 && isIndexHigher(getCheckedValue(vIndex),"1.56 custom")==false){
    				if (!(valMad > "1.56 custom")){
    					valMad = "1.56 custom";
    					bCustom = true;
    				}
    			}
	    			
    			if (parseFloat(sSPH_R) < 0)
	    		{
	    			var dCYL;
	    			if (sCYL_R=="none")
	    			{
	    				dCYL=0;
	    			}
	    			else
	    			{
	    				dCYL=parseFloat(sCYL_R);
	    			}
		    		
	    			var dDiff = Math.abs(parseFloat(sSPH_R)+dCYL);
	    			if(dDiff>5.75 && isIndexHigher(getCheckedValue(vIndex), "1.67")==false)
	    			{
	    				if (!(valRec > "1.67")){
	    					if (Math.abs(sCYL_L)>2 || Math.abs(sCYL_R)>2){
    							valRec = "1.67 custom";
    							bCustom = true;
    						}
	    					else
	    					{
		    					valRec="1.67";
		    				}
	    				}
		    				
	    			}else if (dDiff >= 3.75 && dDiff <=5.75 && isIndexHigher(getCheckedValue(vIndex), "1.61")==false){
	    				if (!(valRec > "1.61")){
	    					if (Math.abs(sCYL_L)>2 || Math.abs(sCYL_R)>2){
    							valRec = "1.61 custom";
    							bCustom = true;
    						}
	    					else
	    					{
		    					valRec="1.61";
		    				}
	    				}
	    			}
		    			
	    		}
		    		
	    		if (parseFloat(sSPH_L) < 0)
	    		{
	    			var dCYL;
	    			if (sCYL_L=="none")
	    			{
	    				dCYL=0;
	    			}
	    			else
	    			{
	    				dCYL=parseFloat(sCYL_L);
	    			}
		    			
	    			var dDiff = Math.abs(parseFloat(sSPH_L)+dCYL);
	    			if(dDiff>5.75 && isIndexHigher(getCheckedValue(vIndex),"1.67")==false)
	    			{
	    				if (!(valRec > "1.67")){
	    					if (Math.abs(sCYL_L)>2 || Math.abs(sCYL_R)>2){
    							valRec = "1.67 custom";
    							bCustom = true;
    						}
	    					else
	    					{
		    					valRec="1.67";
		    				}
	    				}	
		    				
	    			}
	    			else if (dDiff >= 3.75 && dDiff <=5.75 && isIndexHigher(getCheckedValue(vIndex), "1.61")==false){
	    				if (!(valRec > "1.61")){
	    					if (Math.abs(sCYL_L)>2 || Math.abs(sCYL_R)>2){
    							valRec = "1.61 custom";
    							bCustom = true;
    						}
	    					else
	    					{
		    					valRec="1.61";
		    				}
	    				}
	    			}
		    			
	    		}
    		}
    		else if (strLensType == CONST_PROGRESSIVE_REGULAR){
    			if (parseFloat(sCYL_R)>-0.25 || parseFloat(sCYL_L)>-0.25 || parseFloat(sCYL_R)<-3.00 || parseFloat(sCYL_L)<-3.00){
    				bCustom = true;
    			}
	    		if ((parseFloat(sSPH_R) < -6 && parseFloat(sSPH_R) >= -8)||(parseFloat(sSPH_L) < -6 && parseFloat(sSPH_L) >= -8)){
    				if (bCustom && isIndexHigher(getCheckedValue(vIndex),"1.61 custom")==false){
    					if (!(valMad > "1.61 custom")){
    						valMad = "1.61 custom";	
    						
    					}
    				}else if (isIndexHigher(getCheckedValue(vIndex),"1.61")==false){
    					if (!(valMad > "1.61")){
    						valMad = "1.61";
    					}
    				}
	    				
    			}else{
    				if (bCustom == true && isIndexHigher(getCheckedValue(vIndex),"1.56 custom")==false){
    					valMad = "1.56 custom";
    				}
    			}
    			if (parseFloat(sSPH_R) < 0)
	    		{
	    			var dCYL;
	    			if (sCYL_R=="none")
	    			{
	    				dCYL=0;
	    			}
	    			else
	    			{
	    				dCYL=parseFloat(sCYL_R);
	    			}
		    		
	    			var dDiff = Math.abs(parseFloat(sSPH_R)+dCYL);
	    			//alert (isIndexHigher(getCheckedValue(vIndex), "1.61"));
	    			if(dDiff>3.75 && isIndexHigher(getCheckedValue(vIndex), "1.61")==false)
	    			{
	    				if (!(valRec > "1.61")){
	    					if (bCustom){
    							valRec = "1.61 custom";
    						}
	    					else
	    					{
		    					valRec="1.61";
		    				}
	    				}
		    				
	    			}
		    			
	    		}
		    		
	    		if (parseFloat(sSPH_L) < 0)
	    		{
	    			var dCYL;
	    			if (sCYL_L=="none")
	    			{
	    				dCYL=0;
	    			}
	    			else
	    			{
	    				dCYL=parseFloat(sCYL_L);
	    			}
		    			
	    			var dDiff = Math.abs(parseFloat(sSPH_L)+dCYL);
	    			if(dDiff>3.75 && isIndexHigher(getCheckedValue(vIndex),"1.61")==false)
	    			{
	    				if (!(valRec > "1.61")){
	    					if (bCustom){
    							valRec = "1.61 custom";
    						}
	    					else
	    					{
		    					valRec="1.61";
		    				}
	    				}
	    			}
		    			
	    		}
    		}
    		else if (strLensType == CONST_PROGRESSIVE_SLIM){
    			if (parseFloat(sCYL_R)>-0.25 || parseFloat(sCYL_L)>-0.25 || parseFloat(sCYL_R)<-3.00 || parseFloat(sCYL_L)<-3.00){
    				bCustom = true;
    			}
	    		if (bCustom == true && isIndexHigher(getCheckedValue(vIndex),"1.56 custom")==false){
    				valMad = "1.56 custom";
    			}
    		}
    		else if (strLensType == CONST_BI_FOCAL){
    			if (parseFloat(sCYL_R)>-0.5 || parseFloat(sCYL_L)>-0.5 || parseFloat(sCYL_R)<-2.00 || parseFloat(sCYL_L)<-2.00){
    				bCustom = true;
    			}
	    		if (bCustom == true && isIndexHigher(getCheckedValue(vIndex),"1.49 custom")==false){
    				valMad = "1.49 custom";
    			}
    		}
    	} 
    	
    	function isIndexHigher(objValue, compareValue){
    	//debugger;
    	if (objValue==""){
    		return false;
    	}
    	switch(compareValue){
    		case "1.49 custom":
    			if (objValue == "1.49"){
    				return false;
    			}else{
    				return true;
    			}
    		break;
    		case "1.56 custom":
    			if (objValue !== "1.49" && objValue !== "1.49 custom" && objValue !== "1.56" && objValue !=="1.50 (for polarized only)"){
    				return true;
    			}else{
    				return false;
    			}
    		break;
    		case "1.61":
    			if (objValue !== "1.49" && objValue !== "1.49 custom" && objValue !== "1.56" && objValue !== "1.56 custom" && objValue !=="1.50 (for polarized only)"){
    				return true;
    			}else{
    				return false;
    			}
    		break;
    		case "1.61 custom":
    			if (objValue !== "1.49" && objValue !== "1.49 custom" && objValue !== "1.56" && objValue !== "1.56 custom" && objValue !== "1.61" && objValue !=="1.50 (for polarized only)"){
    				return true;
    			}else{
    				return false;
    			}
    		break;
    		case "1.67":
    			if (objValue !== "1.67" && objValue !== "1.67 custom"){
    				return false;
    			}else{
    				return true;
    			}
    		break;
    		case "1.67 custom":
    			if (objValue !== "1.67 custom"){
    				return false;
    			}else{
    				return true;
    			}
    		break;
    	}
    }
    
    //alex comment: not the best solution, cause needs to change with the DB, but,,, no time to implement jscript hashtable...sigh.
    function estimateIndexPrice(vIndex){
        //debugger;
        switch(vIndex){
            case "":
                return "No extra cost";
            break;
            case "1.56":
                return "No extra cost";
            break;
            case "1.56 custom":
                return "$12.00";
            break;
            case "1.61":
                return "$18.00";
            break;
            case "1.61 custom":
                return "$30.00";
            break;
            case "1.67":
                return "$38.00";
            break;
            case "1.67 custom":
                return "$50.00";
            break;
        }
    }
    
    function IsNumeric(sText){
        var ValidChars = "01234567890.";
        var Char;
        
        for (i=0;i<sText.length;i++)
        {
            Char=sText.charAt(i);
            if (ValidChars.indexOf(Char)==-1){
                return false;
            }
        }
        
        return true;
    }