 	function validateEmail(email)
		  	{
							if (email == ""){return false;}
							
							
							badStuff = ";:/,' \"\\";
							for (i=0; i<badStuff.length; i++)
							{
							badCheck = badStuff.charAt(i)
							if (email.indexOf(badCheck,0) != -1){return false;}
							}
							
							posOfAtSign = email.indexOf("@",1)
							if (posOfAtSign == -1){return false;}
							
							
							if (email.indexOf("@",posOfAtSign+1) != -1){return false;}
							
							posOfPeriod = email.indexOf(".", posOfAtSign)
							if (posOfPeriod == -1){return false;}
							
							if (posOfPeriod+2 > email.length){return false;}
							
							return true;
					}
function trimString(str)
{
	  while (str.charAt(0) == ' ')
	    str = str.substring(1);
	  while (str.charAt(str.length - 1) == ' ')
	    str = str.substring(0, str.length - 1);
	  return str;
}


function UserForm()
{   
  
        if(document.userReg.forename.value=="")
        {
            document.userReg.forename.select();
            alert("Please enter firstname");
            return false;
        }
        
        if(document.userReg.surname.value=="")
        {
            document.userReg.surname.select();
            alert("Please enter lastname");
            return false;
        }
        if(!validateEmail(document.userReg.email.value))
        {
            document.userReg.email.select();
            alert("Not a valid Email");
            return false;
        }
        if(document.userReg.password.value=="")
        {
           document.userReg.password.select();
            alert("Please enter password");
            return false;
        }
           if(document.userReg.password2.value=="")
        {
           document.userReg.password2.select();
            alert("Please enter confirmation password");
            return false;
        }
        if(document.userReg.password.value!=document.userReg.password2.value)
        {
            document.userReg.password2.select();
            alert("Password and confirmation password are not same");
            return false;
        }
}

function LoginForm()
{
    if(!validateEmail(document.userReg.username.value))
    {
        document.userReg.email.select();
        alert("Not a valid Email");
        return false;
    }
    if(document.userReg.password.value=="")
    {
        document.userReg.password.select();
        alert("Please enter password");
        return false;
    }
}

function ProfileForm()
{   
  
        if(document.userReg.forename.value=="")
        {
            document.userReg.forename.select();
            alert("Please enter firstname");
            return false;
        }
        
        if(document.userReg.surname.value=="")
        {
            document.userReg.surname.select();
            alert("Please enter lastname");
            return false;
        }
        
}

function PasswordForm()
{
    if(document.userReg.password.value=="")
        {
           document.userReg.password.select();
            alert("Please enter password");
            return false;
        }
           if(document.userReg.password2.value=="")
        {
           document.userReg.password2.select();
            alert("Please enter confirmation password");
            return false;
        }
        if(document.userReg.password.value!=document.userReg.password2.value)
        {
            document.userReg.password2.select();
            alert("Password and confirmation password are not same");
            return false;
        }
    
}


function single_double_pd(cntrl)
{
	var obj_dist=document.getElementById('spec_distance');
	var obj_near=document.getElementById('spec_near');
	var obj_single=document.getElementById('spec_pd');
	
	var dist_tr=document.getElementById('distpd');
	var near_tr=document.getElementById('nearpd');
	var single_tr=document.getElementById('singlepd');
	
	if(cntrl.checked)	
	{
		obj_dist.disabled=true;
		obj_dist.value="";
		dist_tr.style.visiblity="hidden";
		dist_tr.style.display="none";
		
		obj_near.disabled=true;
		obj_near.value="";
		near_tr.style.visibility="hidden";
		near_tr.style.display="none";
		
		obj_single.disabled=false;
		single_tr.style.visiblity="visible";
		single_tr.style.display="";
	}
	else
	{
		obj_dist.disabled=false;
		dist_tr.style.visiblity="visible";
		dist_tr.style.display="";
		
		obj_near.disabled=false;
		near_tr.style.visibility="visible";
		near_tr.style.display="";
		
		obj_single.disabled=true;
		obj_single.value="";
		single_tr.style.visiblity="hidden";
		single_tr.style.display="none";
	}
}
function markAllRows(container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

	    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
                         checkbox.checked = true;
              
           
        }
    }

    return true;
}

function applyformula(val)
{
	var flag=false;
	if(val>=2)
	{
		var rows = document.getElementById('frm').getElementsByTagName('tr');
		var checkbox;

	    for ( var i = 0; i < rows.length; i++ ) 
	    {

	        checkbox1 = rows[i].getElementsByTagName( 'input' )[0];
	
	        if ( checkbox1 && checkbox1.type == 'radio' && checkbox1.name=="lens_type") 
	        {
	             if(checkbox1.checked== true)
	             	flag=true;
	        }
    	    }
    	    if(!flag)
    	    {
    	    	alert("Please select lens type first");
    	    	return false;
    	    }
	}
	var flag=false;
	if(val==3)
	{
		var rows = document.getElementById('frm').getElementsByTagName('tr');
		var checkbox;

	    for ( var i = 0; i < rows.length; i++ ) 
	    {

	        checkbox1 = rows[i].getElementsByTagName( 'input' )[0];
	
	        if ( checkbox1 && checkbox1.type == 'radio' && checkbox1.name=="lens_design") 
	        {
	             if(checkbox1.checked== true)
	             	flag=true;
	        }
    	    }
    	    if(!flag)
    	    {
    	    	alert("Please select lens design first");
    	    	return false;
    	    }
	}
	var obj=document.getElementById('Formula');
	obj.value=val;
	var frm=document.getElementById('frm');
	frm.action="instant_selection.php#1";
	frm.submit();
}

function chk_instant(thisform)
{
	with(thisform)
	{
		if((isNaN(spec_right_axis.value)) || (spec_right_axis.value < 0 || spec_right_axis.value > 180))
		{
			alert("Right Axis should be between 0 and 180");
			spec_right_axis.focus();
			return false;
		}
		if((isNaN(spec_left_axis.value)) || (spec_left_axis.value < 0 || spec_left_axis.value > 180))
		{
			alert("Left Axis should be between 0 and 180");
			spec_left_axis.focus();
			return false;
		}
		if(one_pd2.checked)
		{
	/*
		   if(trimString(spec_pd.value)=="" || (isNaN(spec_pd.value)))
		  {
		 	alert("Test Prob");	
		 	spec_pd.focus();
		 	return false;
		 	}
	 */
			if(spec_pd.value < 0 || spec_pd.value > 76)
			{
				alert("Valid range for PD is between 50 - 76");	
				spec_pd.focus();
				return false;
			}
		}
		else
		{
		/*
		 	if(trimString(spec_distance.value)=="" || (isNaN(spec_distance.value)))
       	{
				alert("Valid range for disatance PD is between 50 - 76");	
				spec_distance.focus();
				return false;
		 	}
	 */
			if(spec_distance.value < 0 || spec_distance.value > 76)
			{
				alert("Valid range for distance PD is between 50 - 76");	
				spec_distance.focus();
				return false;
			}
	/*
			if(trimString(spec_near.value)=="" || (isNaN(spec_near.value)))
			{
				alert("Valid range for near PD is between 50 - 76");	
				spec_near.focus();
				return false;
			}
  */
			if(spec_near.value < 0 || spec_near.value > 76)
			{
				alert("Valid range for near PD is between 50 - 76");	
				spec_near.focus();
				return false;
			}
		}
		/*
		if(trimString(pres_name.value)=="")
		{
			alert("Please Enter the name of Prescribing Optometrist or Ophthalmologist");
			pres_name.focus();
			return false;
		}
		if(trimString(pres_phone.value)=="")
		{
			alert("Please Enter the Phone Number of Prescribing Optometrist or Ophthalmologist");
			pres_phone.focus();
			return false;
		}	
		if(pres_date.selectedIndex==0)
		{
			alert("Please selcct the date of prescription");
			pres_date.focus();
			return false;
		}
		if(pres_month.selectedIndex==0)
		{
			alert("Please select the month of prescription");
			pres_month.focus();
			return false;
		}
		if(pres_year.selectedIndex==0)
		{
			alert("Please select the year of prescription");	
			pres_year.focus();
			return false;
		}
		*/
	}
}

function chk_selection()
{
	var type_checked=false;
	var design_checked=false;
	var tint_checked=false;
	var rows = document.getElementById('frm').getElementsByTagName('tr');
	var checkbox;

	    for ( var i = 0; i < rows.length; i++ ) 
	    {
	        checkbox1 = rows[i].getElementsByTagName( 'input' )[0];
	        if(checkbox1 && checkbox1.type == 'radio')
	        {
		        if(checkbox1.name=="lens_type") 
		        {
		             if(checkbox1.checked== true)
		             	type_checked=true;
	        	}
	        	if(checkbox1.name=="lens_design") 
		        {
		             if(checkbox1.checked== true)
		             	design_checked=true;
	        	}
	        	if(checkbox1.name=="features") 
		        {
		             if(checkbox1.checked== true)
		             	tint_checked=true;
	        	}
	        }
        }
        if(type_checked==false)
        {
        	alert("Please Select a Lens Type");	
        	return false;
        }
        if(type_checked==false)
        {
        	alert("Please Select a Lens Design");	
        	return false;
        }
        if(tint_checked==false)
        {
        	alert("Please Select a Tints and Coatings");	
        	return false;
        }
}

function chk_getquote(thisform)
{
	var type_checked=false;
	var tint_checked=false;
	var rows = document.getElementById('frm').getElementsByTagName('tr');
	var checkbox;

	    for ( var i = 0; i < rows.length; i++ ) 
	    {
	        checkbox1 = rows[i].getElementsByTagName( 'input' )[0];
	        if(checkbox1 && checkbox1.type == 'radio')
	        {
		        if(checkbox1.name=="used_for") 
		        {
		             if(checkbox1.checked== true)
		             	type_checked=true;
	        	}
	        	
	        	if(checkbox1.name=="features") 
		        {
		             if(checkbox1.checked== true)
		             	tint_checked=true;
	        	}
	        }
        }
        if(type_checked==false)
        {
        	alert("Please Select What will you be using the glasses for?");	
        	return false;
        }
       
        if(tint_checked==false)
        {
        	alert("Please Select a Features");	
        	return false;
        }
	
	
	with(thisform)	
	{
		/*if(trimString(used_for.value)=="")
		{
			alert("Please Enter What will you be using the glasses for?");
			used_for.focus();
			return false;
		}
		if(trimString(design.value)=="")
		{
			alert("Please Enter What Lens Design would you like on the lenses?");
			design.focus();
			return false;
		}
		if(trimString(features.value)=="")
		{
			alert("Please Enter What features would you like on the lenses?");
			features.focus();
			return false;
		}
		if(trimString(tint_colour.value)=="")
		{
			alert("Please Enter Tint Colour?");
			tint_colour.focus();
			return false;
		}*/
		
		if(trimString(firstname.value)=="")
		{
			alert("Please Enter FirstName");	
			firstname.focus();
			return false;
		}
		if(trimString(surname.value)=="")
		{
			alert("Please Enter SurName");
			surname.focus();
			return false;
		}
		if(trimString(address.value)=="")
		{
			alert("Please Enter Address");
			address.focus();
			return false;
		}
		if(trimString(city_town.value)=="")
		{
			alert("Please Enter City-town");
			city-town.focus();
			return false;
		}
		if(trimString(state.value)=="")
		{
			alert("Please Enter State");
			state.focus();
			return false;
		}
		
		if(trimString(postcode.value)=="")
		{
			alert("Please Enter Postcode");
			postcode.focus();
			return false;
		}
		
		if(!validateEmail(email.value))
		{
			alert("Please Enter Valid Email");
			email.focus();
			return false;
		}
		if(bdate.selectedIndex==0)
		{
			alert("Please select date of your birth");
			bdate.focus();
			return false;
		}
		if(bmonth.selectedIndex==0)
		{
			alert("Please select the month of your birth");
			bmonth.focus();
			return false;
		}
		if(byear.selectedIndex==0)
		{
			alert("Please Enter the year of your month");
			byear.focus();
			return false;
		}
	}
}


//Function to check quote number
function chk_quote_no()
{
	var obj=document.getElementById('pricing3');
	var cntrl=document.getElementById('quote');
	if(obj.checked)	
	{
		if(trimString(cntrl.value)=="")
		{
			alert("Please Enter Quote number");
			cntrl.focus();
			return false;
		}
		if(isNaN(cntrl.value))
		{
			alert("Pleas Enter a number for Quote number");
			cntrl.focus();
			return false;
		}
	}
}

function pd_check(cntrl)
{
	if(cntrl.value!="" && !isNaN(cntrl.value))
	{
		if(parseFloat(cntrl.value)>=25 && parseFloat(cntrl.value)<=38)
		{
			cntrl.value=parseFloat(cntrl.value)*2;
		}
	}
}

function chk_quoteno(thisform)
{
	with(thisform)	
	{
		if(bdate.selectedIndex==0)
		{
			alert("Please Select Your Date of Birth");
			bdate.focus();
			return false;
		}
		if(bmonth.selectedIndex==0)
		{
			alert("Please Select Your Month of Month");	
			bmonth.focus();
			return false;
		}
		if(byear.selectedIndex==0)
		{
			alert("Please Select Your Year of Birth");
			byear.focus();
			return false;
		}
		if(!validateEmail(email.value))
		{
			alert("Please Enter a valid Email address");	
			email.focus();
			return false;
		}
	}
}


function final_submit()
{
	var type_checked=false;
	var design_checked=false;
	var tint_checked=false;
	var clr_checked=false;
	var rows = document.getElementById('frm').getElementsByTagName('tr');
	var tint_clr=document.getElementById('tint_colour');
	var checkbox;

	    for ( var i = 0; i < rows.length; i++ ) 
	    {
	        checkbox1 = rows[i].getElementsByTagName( 'input' )[0];
	        if(checkbox1 && checkbox1.type == 'radio')
	        {
		        if(checkbox1.name=="lens_type") 
		        {
		             if(checkbox1.checked== true)
		             	type_checked=true;
	        	}
	        	
	        	if(checkbox1.name=="lens_design") 
		        {
		             if(checkbox1.checked== true)
		             	design_checked=true;
	        	}
	        	
	        	if(checkbox1.name=="features") 
		        {
		             if(checkbox1.checked== true)
		             	tint_checked=true;
	        	}
	        }
        }
        if(type_checked==false)
        {
        	alert("Please Select a Lens Type.");	
        	return false;
        }
       
       	if(design_checked==false)
       	{
       		alert("Please Select a Lens Design");	
       		return false;
       	}
       	
        if(tint_checked==false)
        {
        	alert("Please Select a Tints & Coatings");	
        	return false;
        }
        
        if(tint_clr.length > 1)
        {
        	if(tint_clr.value=='0')
        	{
        		alert("Please Select a Tint Colour");
        		return false;
        	}
        }
        document.frm.action="cart.php";
	
}

function same(e)
{
	if(e.chk.checked)
	{
		e.sfristname.value = e.bfristname.value;
		e.slastname.value = e.blastname.value;		
		e.saddress.value = e.baddress.value;
		e.spostcode.value = e.bpostcode.value;
		e.scity.value = e.bcity.value;	
		e.sregion.value = e.bregion.value;
		e.stelephone.value = e.btelephone.value;
		e.scountry.selectedIndex = e.bcountry.selectedIndex ;
	}
}

function chk_checkout(thisform)
{
	with(thisform)	
	{
		if(trimString(bfristname.value)=="")	
		{
			alert("Please Enter First Name for billing information");	
			bfristname.focus();
			return false;
		}
		if(trimString(blastname.value)=="")	
		{
			alert("Please Enter Last Name for billing information");	
			blastname.focus();
			return false;
		}
		if(trimString(baddress.value)=="")	
		{
			alert("Please Enter billing address");	
			baddress.focus();
			return false;
		}
		if(trimString(btelephone.value)=="")	
		{
			alert("Please Enter Telephone for billing information");	
			btelephone.focus();
			return false;
		}
		if(trimString(bcity.value)=="")	
		{
			alert("Please Enter city/town for billing information");	
			bcity.focus();
			return false;
		}
		if(trimString(bregion.value)=="")	
		{
			alert("Please Enter Region/State/Province for billing information");	
			bregion.focus();
			return false;
		}
		if(trimString(bpostcode.value)=="")	
		{
			alert("Please Enter postcode for billing information");	
			bpostcode.focus();
			return false;
		}
		
		
		if(trimString(sfristname.value)=="")	
		{
			alert("Please Enter First Name for shipping information");	
			sfristname.focus();
			return false;
		}
		if(trimString(slastname.value)=="")	
		{
			alert("Please Enter Last Name for shipping information");	
			slastname.focus();
			return false;
		}
		if(trimString(saddress.value)=="")	
		{
			alert("Please Enter shipping address");	
			saddress.focus();
			return false;
		}
		if(trimString(stelephone.value)=="")	
		{
			alert("Please Enter Telephone for shipping information");	
			stelephone.focus();
			return false;
		}
		if(trimString(scity.value)=="")	
		{
			alert("Please Enter city/town for shipping information");	
			scity.focus();
			return false;
		}
		if(trimString(sregion.value)=="")	
		{
			alert("Please Enter Region/State/Province for shipping information");	
			sregion.focus();
			return false;
		}
		if(trimString(spostcode.value)=="")	
		{
			alert("Please Enter postcode for shipping information");	
			spostcode.focus();
			return false;
		}
	}
}

function chk_forgot(thisform)
{	
	with(thisform)
	{
		if(!validateEmail(username.value))	
		{
			alert("Please Enter a valid Email Address");
			username.focus();
			return false;
		}
	}
}

function chk_payonline(thisform)
{	return true;	/*
	with(thisform)	
	{
		if(!terms.checked)	
		{
			alert("You must accept the terms and conditions to continue");
			return false;
		}
	}	*/
	
}

function change_details(cntrl)
{
	var obj=document.getElementById('state');
	var obj1=document.getElementById('city');
	if(cntrl.value=="AUS")
	{
		obj.innerHTML="State/Territory";
		obj1.innerHTML="City/Town";
	}
	else if(cntrl.value=="NZ")
	{
		obj.innerHTML="Town or City";
		obj1.innerHTML="Suburb or RD No.";
	}	
	else if(cntrl.value=="US")
	{
		obj.innerHTML="State";
		obj1.innerHTML="Suburb/City";
	}
	else if(cntrl.value=="UK")
	{
		obj.innerHTML="Post Town";
		obj1.innerHTML="Locality";
	}	
	else if(cntrl.value=="CA")
	{
		obj.innerHTML="Town or City";
		obj1.innerHTML="Suburb/City";
	}	
}

function change_quotedetails(cntrl)
{
	var obj=document.getElementById('state');
	var obj1=document.getElementById('city');
	if(cntrl.value=="AUS")
	{
		obj.innerHTML="State/Territory*";
		obj1.innerHTML="City/Town*";
	}
	else if(cntrl.value=="NZ")
	{
		obj.innerHTML="Town or City*";
		obj1.innerHTML="Suburb or RD No.*";
	}	
	else if(cntrl.value=="UK")
	{
		obj.innerHTML="Post Town*";
		obj1.innerHTML="Locality*";
	}	
	else if(cntrl.value=="US")
	{
		obj.innerHTML="State*";
		obj1.innerHTML="Suburb/City*";
	}	
	else if(cntrl.value=="CA")
	{
		obj.innerHTML="State*";
		obj1.innerHTML="Suburb/City*";
	}	
}


function chk_opthinfo(thisform)
{
	with(thisform)
	{
		if(trimString(pres_name.value)=="")
		{
			alert("Please Enter the name of Prescribing Optometrist or Ophthalmologist");
			pres_name.focus();
			return false;
		}
		if(trimString(pres_phone.value)=="")
		{
			alert("Please Enter the Phone Number of Prescribing Optometrist or Ophthalmologist");
			pres_phone.focus();
			return false;
		}	
		if(pres_date.selectedIndex==0)
		{
			alert("Please selcct the date of prescription");
			pres_date.focus();
			return false;
		}
		if(pres_month.selectedIndex==0)
		{
			alert("Please select the month of prescription");
			pres_month.focus();
			return false;
		}
		if(pres_year.selectedIndex==0)
		{
			alert("Please select the year of prescription");	
			pres_year.focus();
			return false;
		}
	}
}

//Function for checking balance entry in sphere drop down
function chk_BAL(cntrl)
{
	
	var cnindex=cntrl.selectedIndex;
	var cnval=cntrl.options[cnindex].value;
	
	var rold=document.getElementById('sp_r_sph');
	var lold=document.getElementById('sp_l_sph');
	
	
	if(cnval != "bal")
	{
		if(cntrl.id=="spec_right_sphere")
			rold.value=cnval;
		else if(cntrl.id=="spec_left_sphere")
			lold.value=cnval;
	}
	
	var left_sphere=document.getElementById('spec_left_sphere');
	var left_cyl = document.getElementById('spec_left_cyl');
	var left_axis = document.getElementById('spec_left_axis');
	
	var right_cyl = document.getElementById('spec_right_cyl');
	var right_axis = document.getElementById('spec_right_axis');
	var right_sphere=document.getElementById('spec_right_sphere');

	
	if(cnval=="bal")
	{
		if(cntrl.id == "spec_right_sphere")
		{
			index=left_sphere.selectedIndex;
			var vl=left_sphere.options[index].value;
			
			if(vl=="bal")
			{
				alert("Sphere for both the eyes cannot be BAL");
				//cntrl.focus();
				if(rold.value!="")
					cntrl.value=rold.value;
				else
					cntrl.selectedIndex=32;
				return false;
			}
			else
			{
				
				right_cyl.selectedIndex=24;
				right_cyl.disabled=true;
				
				right_axis.value="0";
				right_axis.disabled=true;
				
				return true;
			}
		}
		else if(cntrl.id=="spec_left_sphere")
		{
			
			index=right_sphere.selectedIndex;
			var vl=right_sphere.options[index].value;
			if(vl=="bal")
			{
				alert("Sphere for both the eyes cannot be BAL");
				if(lold.value!="")
					cntrl.value=lold.value;
				else
					cntrl.selectedIndex=32;
				//cntrl.focus();
				return false;
			}
			else
			{
				
				
				left_cyl.selectedIndex=24;
				left_cyl.disabled=true;
				
				left_axis.value="0";
				left_axis.disabled=true;
				
				return true;
			}
			
		}
	}
	else
	{
		if(cntrl.id == "spec_right_sphere")
		{
			
			right_cyl.disabled=false;
			right_axis.disabled=false;
			
			return true;

		}
		else if(cntrl.id == "spec_left_sphere")
		{
			
			left_cyl.disabled=false;
			left_axis.disabled=false;
			
			return true;
			
		}
	}
}
