/* JQuery */




// Side slide menu

$(document).ready(function($) {
	
	var cookie= $.cookie("menu");
	
	$('ul#category > li.sub > ol').hide(); // close all
	
	if(cookie != null){
		document.getElementById(cookie).className= "open";
	} else {
		$("ul#category .sub:first-child > a.close").addClass("open");
	}
	
	$("ul#category > li.sub > a.open").parent().find("ol").show();
	
	$("ul#category .sub > a").click(function() {
		if(this.className.indexOf('open') != -1) {
			animate("up");
			$(this).addClass('close');
			$(this).removeClass('open');
			
			$.cookie("menu", null);
		} else {
			animate("up");
			$('.open').removeClass('open');
			$(this).addClass('open');
			$('.close').removeClass('close');
			animate("down");
			
			$.cookie("menu", $('.open').attr("id"));
		}
		
		return false;
		
	}); 
	
	var tryoncookie=$.cookie("tryoncookie");
	
	if (tryoncookie==null) {
		$("#tryon").width(0);
		$("#tryon").height(0);
		$.cookie("tryoncookie", "close");
	} else {
		if (tryoncookie=='open') { 
			$("#tryon").width(757);
			$("#tryon").height(250);			
		} else {
			$("#tryon").width(0);
			$("#tryon").height(0);
		}
	}
	
	$("#trybtn").click(function() {
		
		var tryoncookie=$.cookie("tryoncookie");
		if (tryoncookie==null) {
			$.cookie("tryoncookie", "close");
			document.getElementById('tryon').style.width='0px';
			document.getElementById('tryon').style.height='0px';			
		} else {
			if (tryoncookie=='open') {	
				$.cookie("tryoncookie", "close");
				document.getElementById('tryon').style.width='0px';
				document.getElementById('tryon').style.height='0px';				
			} else {
				$.cookie("tryoncookie", "open");
				document.getElementById('tryon').style.width='757px';
				document.getElementById('tryon').style.height='250px';				
			}
		
		}
		
		return false;
			
	});

});


/* end JQuery */

function animate( way ){
	if(way == "up"){
		$('.open').parent().find("ol").slideUp("fast");
	} else {
		$('.open').parent().find("ol").slideDown("fast");
	}
}


function limitline(textarea,limit){
var val=textarea.value.replace(/\r/g,'').split('\n');
	if(val.length>limit) {
		textarea.value=val.slice(0,-1).join('\n')
		return false;
	}
}


function subscribe_newsletter() {
	
	var name=$("#subscribe_name").attr('value');
	var email=$("#subscribe_email").attr('value');
	
	var fail=0; error="";
	
	if ((name=="") || (name=="Enter your name")) { error='Please enter your name\n'; fail=1; }
	if ((email=="") || (email=="Enter your email address")) { error+='Please enter your Email Address'; fail=1; }
	if ((!isValidEmail(email)) && (email!="Enter your email address")) { error+='Please enter correct Email Address'; fail=1; }

	if (fail==0) {
		var dataString="pg=subscribe&name=" + name + "&email=" + email;
		
		$.ajax({  
		   type: "GET",  
		   url: "index.php",  
		   data: dataString,  
		   success: function(msg) {  
				document.getElementById('newsletter').innerHTML='<span class="message-text">' + msg + '</span>';
		   }  
		});  
	} else {
		alert(error);
	}
     
}

function search_focus(id,obj) {

	switch (id) {
		case 1: if (obj.value=="product search") { obj.value=''; } break;
		case 2: if (obj.value=="") { obj.value='product search'; } break;
	}
}



/* Switch on/off to textfield background texture */
function smarttip_focus(obj) {
		
	obj.style.background='none';  obj.style.backgroundColor='#FFFFFF';
}

function smarttip_blur(obj,pic) {
	if (obj.value=="") { obj.style.background="url(/images/" + pic + ") no-repeat 4px center";  obj.style.backgroundColor='#FFFFFF'; }
}

/* Realtime price calculate update for glass-order */
function cal_price() {
	var total=0;
	
	frm=document.getElementById('order');
	total+=parseFloat(document.getElementById('frame_price').value);
	//total+=parseFloat(document.getElementById('len_price').value);
	
	value='';
	for (var i=0; i < frm.extra.length; i++) {
		if (frm.extra[i].checked) { value=frm.extra[i].value; }
	}
	
	if (value!='') {
		tmp=value.split("|");
		total+=parseFloat(tmp[1]);
	}
	
	
	//document.getElementById('total').innerText=formatCurrency(total);
	//var tar=document.getElementById('total');
	//tar.innerHTML=formatCurrency(total);
	
	$("#total").html(formatCurrency(total));
	
	frm.total.value=total;
	
	
}

/* switch lense extra options */
function switch_lense(lenid,title,price) {
	
	document.getElementById('label_title').innerHTML=title;
	document.getElementById('label_price').innerHTML=formatCurrency(price);
	document.getElementById('len_price').value=price;
	
	
	var dataString="pg=ajax&action=lenseextra-action&lenid=" + lenid;
		
		$.ajax({  
		   type: "GET",  
		   url: "index.php",  
		   data: dataString,  
		   success: function(msg) {  
				document.getElementById('extra').innerHTML=msg;
				cal_price();

		   }  
		});  
	
}
	
function isObject(obj) {
	
   if (obj.constructor.toString().indexOf("Object") == -1)
      return false;
   else
      return true;
}	




function DoDel(iserial)
{
	if (confirm("Are you sure?"))
	{
		window.location.href=iserial;
	}
}

function DoQuit()
{
	if (confirm("Exit without save changes?"))
	{
		window.location.href='my_refarm.php';
	}
}

function show_window(sid,h,w)
{

  var popup = window.open("aa", "News",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=' + h + ',height=' + w);
  popup.location = sid;

}

function show_bar_window(sid,h,w)
{

  var popup = window.open("", "News",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=' + h + ',height=' + w);
  popup.location = sid;

}


function checkUncheckAll(theElement) {
	 //documentation for this script at http://www.shawnolson.net/a/693/
    var theForm = theElement.form, z = 0;
    while (theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') {
     theForm[z].checked = theElement.checked;
     z++;
    }
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

// Cart-Step 3 toggle billing form
function toggle_form() {
	var frm=document.getElementById('billing-table');
	
	if (frm.className=='') { frm.className='close'; } else { frm.className=''; }
	
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}


function getFlashMovie( movieName ){
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function tryOnFrame( frameID ){

		if (document.getElementById('tryon').style.width=="0px") {
			
			document.getElementById('tryon').style.width='757px';
			document.getElementById('tryon').style.height='250px';
			
		}
		getFlashMovie("tryout").tryOnFrame( frameID );
		$.cookie("tryoncookie", "open");

} 
			

function try_this_form(frameId) {
	document.getElementById('trythison').style.width='753px';
	document.getElementById('trythison').style.height='250px';	
	getFlashMovie("tryout").tryOnFrame( frameId );  
	$.cookie("tryoncookie", "open");	
	
}

	
