$(document).ready(function() { 
	$(".frmcart").bind("submit", function() {
	
		$.ajax({
			type		: "POST",
			cache	: false,
			url		: "/external.php",
			data		: $(this).serializeArray(),
			success: function(data) {
				$.fancybox({'autoDimensions':false,'type':'iframe','href': 'external.php?pg=cart-result','showCloseButton':true,'width':760,'height':490 }); 
				//$.fancybox(data,{'autoDimensions':false,'type':'inline','showCloseButton':true,'width':760,'height':490 }); 
			}
		});
	
		return false;
	});
	
	$("a#btnlogin").fancybox({'padding':20, 'width':400,'height':320});
	$("a#btnlogin1").fancybox({'padding':20, 'width':400,'height':320});
	$("a#btnlogin2").fancybox({'padding':20, 'width':400,'height':320});
	$("a#btnlogin3").fancybox({'padding':20, 'width':400,'height':320});
	$("a#btnlogin4").fancybox({'padding':20, 'width':400,'height':320});
	$("a.watchlogin").fancybox({'padding':20, 'width':400,'height':320});
	$("a.btnvoucher").fancybox({'padding':20, 'width':710,'height':382, 'scrolling':'no'});
	
	$("a#button-ordernow").fancybox({'padding':20, 'width':510,'height':475});
	$("a#icon1").fancybox({'padding':20, 'width':510,'height':475});
	$("a#icon4").fancybox({'padding':20, 'width':510,'height':475});
	$("a.map").fancybox({'padding':0, 'width':600,'height':450});
	
}); 

function show_menu() {
	$('#menu').slideDown('fast');
}

function hide_menu() {
	$('#menu').slideUp('fast');
}


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 change_store(store,total) {
	for (i=1; i<=total; i++) {
		document.getElementById('store' +i).style.display='none';
	}

	document.getElementById('store' +store).style.display='';
	
}


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='<div class="message-text">' + msg + '</div>';
		   }  
		});  
	} 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 update_price() {
	
	var total=0;
	normal=document.getElementById('pce').value;
	val=document.getElementById('opt').value;
	qty=document.getElementById('qty').value;

	tmp=val.split("|");
		
	
	total=parseFloat(normal)+parseFloat(tmp[0]);
	
	total=parseFloat(total * qty);
	

	
	$("#price").html(formatCurrency(total));
	
	document.frmcart.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 add_watchlist(id) {
	
	var dataString="pg=watchlist-add&id=" + id;
	
	$.ajax({  
	   type: "GET",  
	   url: "index.php",  
	   data: dataString,  
	   success: function(msg) { 

			$('#icon2').text('Watchlist Saved');
			$('#icon2').attr('href','javascript:;');
			$('#icon2').attr('class','disable');
	   }  
	});  
     
}
	
	
function remove_watchlist(id) {
	
	var dataString="pg=watchlist-delete&id=" + id;
	
	$.ajax({  
	   type: "GET",  
	   url: "index.php",  
	   data: dataString,  
	   success: function(msg) { 
			$('#row' +id).fadeOut('fast');
	   }  
	});  
     
}


function switch_pic(target) {
	
	s=document.getElementById('image1');
	t=document.getElementById(target);
	
	surl=s.src;
	turl=t.src;
	
	tmp=surl.split("?f="); tmp=tmp[1].split("&"); ssrc=tmp[0];
	tmp=turl.split("?f="); tmp=tmp[1].split("&"); tsrc=tmp[0];
	
	
	var t_pic="crop.php?f=" + tsrc + "&w=303&h=218";
	var s_pic="crop.php?f=" + ssrc + "&w=143&h=103";
	
	$("#image1").attr("src",t_pic);
	$("#" + target).attr("src",s_pic);


}
	

function warning(act,obj) {
	
	if (act=="show") {
		$("#warning").css('top',top);
		$("#warning").fadeIn("fast");
	} else{
		$("#warning").fadeOut("fast");
	
	}
}

function remove_cart(url) {
	if (confirm("Remove this item from shopping cart?")) {
		window.location.href=url;
	}
}

function close_window() {
	parent.$.fancybox.close();
}

function redeem_voucher() {
	var code=$("#code").attr('value');
	
	var fail=0; error="";
	
	if (code=="") { error='Please enter your voucher code'; fail=1; }

	if (fail==0) {
		var dataString="pg=voucher_check&code=" + code;
		
		$.ajax({  
		   type: "GET",  
		   url: "index.php",  
		   data: dataString,  
		   success: function(msg) {  
		   		document.getElementById('result').style.display='';
				document.getElementById('result').innerHTML=msg;
		   }  
		});  
	} else {
		alert(error);
	}

}

function redeem_now(code) {

		var dataString="pg=voucher_redeem&code=" + code;
		
		$.ajax({  
		   type: "GET",  
		   url: "index.php",  
		   data: dataString,  
		   success: function(msg) {  
		   		document.getElementById('result').style.display='';
				document.getElementById('result').innerHTML=msg;
		   }  
		});  

}


function branch_member(id) {
	
	switch (id) {
		case "1": 
			document.getElementById('member-search').style.display='none';
			document.getElementById('panel2').style.display='none';
			document.getElementById('send').disabled='disabled';
			
			break;
		case "2": 
			document.getElementById('member-search').style.display='';
			document.getElementById('panel2').style.display='none';
			document.getElementById('send').disabled='';
			
			break;
		case "3": 
			document.getElementById('member-search').style.display='none';
			document.getElementById('panel2').style.display='';
			
			document.getElementById('memberid').value=0;
			document.getElementById('fname').value='';
			document.getElementById('lname').value='';
			document.getElementById('email').value='';
			document.getElementById('dobd').selectedIndex=0;
			document.getElementById('dobm').selectedIndex=0;
			document.getElementById('doby').selectedIndex=0;
			document.getElementById('dphone').value='';
			document.getElementById('nphone').value='';
			document.getElementById('send').disabled='';
			
			break;
	
	
	}

}


function load_member() {
	
	$.getJSON(
		"index.php",
		{ pg: 'search-member', email: $("#searchword").val() },
		function(data)
		{
			if (data[0].result==true) {
				document.getElementById('memberid').value=data[0].id;
				document.getElementById('fname').value=data[0].fname;
				document.getElementById('lname').value=data[0].lname;
				document.getElementById('email').value=data[0].email;
				document.getElementById('dobd').selectedIndex=data[0].day;
				document.getElementById('dobm').selectedIndex=data[0].month;
				document.getElementById('doby').selectedIndex=data[0].year;
				document.getElementById('dphone').value=data[0].phone;
				document.getElementById('nphone').value=data[0].mobile;
				
				document.getElementById('panel2').style.display='';
			} else {
				alert("Invalid email, please try again.");
			
			}
			
		}
	);
}


function process_purchase() {

	option=document.getElementById('option').value;
	fname=document.getElementById('fname').value;
	lname=document.getElementById('lname').value;
	email=document.getElementById('email').value;
	dphone=document.getElementById('dphone').value;

	var dataString="pg=validatemember&option=" + option + "&fname=" + fname + "&lname=" + lname + "&email=" + email + "&dphone=" + dphone;
	
	$.ajax({  
	   type: "GET",  
	   url: "index.php",  
	   data: dataString,  
	   success: function(msg) {  
	   		if (msg=="success") {
	   			document.frmbranch.submit();
	   		} else {
	   			document.getElementById('validate').innerHTML=msg;
	   			document.getElementById('validate').style.display='';	
	   		}
	   }  
	});  

}



