function applyCountDown()
{
	$('#defaultCountdown').countdown({until: endDay});
	$('#year').text(endDay.getFullYear());	
}

function menuHandler()
{
	$("ul.ecd_contact li.ecd_contact_method ul").hide(); // Hide all sub menus
		$("ul.ecd_contact li.ecd_first_method").find("ul").slideToggle("slow"); // Slide down the current menu item's sub menu
		// When a top menu item is clicked...
		$("ul.ecd_contact li.ecd_contact_method h3").click( 
			function () {
				$(this).parent().siblings().find("ul").slideUp("normal"); // Slide up all sub menus except the one clicked
				$(this).parent().find("ul").slideToggle("normal"); // Slide down the clicked sub menu
				return false;
			}
		);	
	Cufon.replace('.ecd_harabara , h1 , h2 , h3, h4 , h5 , h6');	
	Cufon.replace('#defaultCountdown .countdown_unit');		
}

function gmapFormValidation()
{
	$("#form1").validate({
	 		submitHandler: function(form) 
	 		{
		   		$('#form1').ajaxSubmit({
				url: 'ec_countdown/includes/php/ajax.php',
				type: 'post', 
				data: {task: 'send_email'}, 
				success: function(responseText){
					$('#notice_box').html(responseText);
					document.getElementById('form1').reset();
					genCaptcha();
				}
				});
	   		},
	   		invalidHandler:function(form)
	   		{
		   		$('#notice_box').html('<div class="alert">Wrong information</div>');	
	   		},
		     messages: 
		     {
			     name: "<span class='ecd_error'>Missing</span>",
			     email: {
			       required:"<span class='ecd_error'>Missing</span>",
			       email: "<span class='ecd_error'>(invalid email)</span>"
			     },
			     
			     url: {
			       
			       url: "<span class='ecd_error'>(invalid url)</span>"
			     },
			     subject: "<span class='ecd_error'>Missing</span>",
			     message: "<span class='ecd_error'>Missing</span>",
			     ver_code: "<span class='eecd_rror'>Missing</span>"
			       
			  }
		    
		});
}

function applyGoogleMap()
{
	var map = null;
  	var geocoder = null;
	  if (GBrowserIsCompatible()) {
	    map = new GMap2(document.getElementById("ecd_map_canvas"));
	
	    map.setUIToDefault();
	    geocoder = new GClientGeocoder();
	    address = gmap_address;
	    
	    geocoder.getLatLng(
	      	address,
	      		function(point) 
	      		{
	        		if (!point) 
	        		{
	          			alert(address + " not found");
	        		}
	        		else
	        		{
	        		 	map.setCenter(point, 15);
	          			var marker = new GMarker(point, {draggable: true});
	          			map.addOverlay(marker);
	          			GEvent.addListener(marker, "dragend", function() {
	            		marker.openInfoWindowHtml(marker.getLatLng().toUrlValue(6));
	          			});
	          			     GEvent.addListener(marker, "click", function() {
	                marker.openInfoWindowHtml(address);
	              });
		      		
	          		}
	        	}
	      	)
	  	}
  	
  	
}

function saveGuestEmail()
{
	var guest_email = $('#guest_email').val();
	
	
	$('#keep_in_touch_input_li').fadeOut('fast', function() {});
	$.post("ec_countdown/includes/php/ajax.php", { task:'save_guest_email',guest_email:guest_email},  
				function(data, textStatus){
					if(data==1)
					{
						$('#guest_email').val('');
						$('#keep_in_touch_input_li').html('<span class="ecd_sys_msg">'+thank_you_email+'</span>');
						$('#keep_in_touch_input_li').fadeIn('fast', function() {});
					}
				}
	);
}

function intval(v) {
    v = parseInt(v);
    return isNaN(v) ? 0 : v;
}

function getPos(e) {
    var l = 0;
    var t  = 0;
    var w = intval(e.style.width);
    var h = intval(e.style.height);
    var wb = e.offsetWidth;
    var hb = e.offsetHeight;
    while (e.offsetParent){
        l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0);
        t += e.offsetTop  + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0);
        e = e.offsetParent;
    }
    l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0);
    t  += e.offsetTop  + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0);
    return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
}

function getScroll(){
    var t, l, w, h;
    if (document.documentElement && document.documentElement.scrollTop) {
        t = document.documentElement.scrollTop;
        l = document.documentElement.scrollLeft;
        w = document.documentElement.scrollWidth;
        h = document.documentElement.scrollHeight;
    } else if (document.body) {
        t = document.body.scrollTop;
        l = document.body.scrollLeft;
        w = document.body.scrollWidth;
        h = document.body.scrollHeight;
    }
    return { t: t, l: l, w: w, h: h };
}

function scroller(el, duration) {
    if(typeof el != 'object') {
        el = document.getElementById(el);
    }
    if(!el) return;
    var z = this;
    z.el = el;
    z.p = getPos(el);
    z.s = getScroll();
    z.clear = function(){
        window.clearInterval(z.timer);
        z.timer=null
    };
    z.t=(new Date).getTime();
    z.step = function(){
        var t = (new Date).getTime();
        var p = (t - z.t) / duration;
        if (t >= duration + z.t) {
            z.clear();
            window.setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
            } else {
                st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
                sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
                z.scroll(st, sl);
            }
        };
        z.scroll = function (t, l){window.scrollTo(l, t)};
        z.timer = window.setInterval(function(){z.step();},13);
}
function genCaptcha()
{
 	$.post("ec_countdown/includes/php/ajax.php", { task:'gen_image'},  
				function(data, textStatus){
					document.getElementById('image_td').innerHTML = data;
					fixBase64();
				}
	);
}
function fixBase64(img) 
{
	var BASE64_DATA = /^data:.*;base64/i;
	var base64Path = "ec_countdown/includes/php/base64.php";  
	img = document.getElementById('ver_code_image');
	if (BASE64_DATA.test(img.src)) 
	{    
		img.src = base64Path + "?" + img.src.slice(5);  
	}
}

function loadGuestEmailGrid(pageNo)
{
	var record_per_page = $('#record_per_page').val();
	if(typeof(record_per_page)=='undefined')
			record_per_page = 10;
	
	$('#content_div').fadeOut('fast', function() {});
	
	$.post("includes/php/ajax.php", { task:'gen_guest_email_grid',record_per_page:record_per_page,pageNo:pageNo},  
				function(data, textStatus){
						$('#content_div').html(data);
						$('#content_div').fadeIn('fast', function() {});
				}
	);
}

function updateEmailTextboxStatus(boxStatus)
{
	if(boxStatus==true)
	{
		$('#test_email').show();
		$('#test_email_desc_span').show();
	}
	else
	{
		$('#test_email').hide();	
		$('#test_email_desc_span').hide();	
	}
}

function checkAll(obj)
{
	var checked_status = obj.checked;
		
	var checkbox_name = obj.name;
	
	$("input[name=" + checkbox_name + "[]]").each(function() {
		this.checked = checked_status;
	});	
}

function removeGuestEmail()
{
	if(typeof($("input:checkbox[name='guest_email[]']:checked").val())=='undefined')
	{
		alert(please_select_a_checkbox);
		return false;
	}
	if(confirm(confirm_remove_guest_email))
	{
		$('#form1').ajaxSubmit({
				url: 'includes/php/ajax.php', 
				type: 'post', 
				data: {task: 'remove_guest_email'}, 
				success: function(responseText){
					loadGuestEmailGrid(1);
				}
				});		
	}	
}

function CKupdate(){
    for ( instance in CKEDITOR.instances )
        CKEDITOR.instances[instance].updateElement();
}


function sendGuestEmail()
{
	if(validateForm()==true)
	{
		$('#send_email_div').fadeOut('fast', function() {});
		$('#action_btn_div').fadeOut('fast', function() {});
		$('#form1').ajaxSubmit
		({
			url: 'includes/php/ajax.php', 
			type: 'post', 
			data: {task: 'send_guest_email'}, 
			success: function(responseText){
			$('#send_email_div').fadeIn('fast', function() {});
			$('#send_email_div').html(responseText);
			}
		});	
	}
}

function toggleGuestEmailList()
{
	var is_visible = $('#view_list_btn_div').is(":visible");
	
	if(is_visible)
	{
		$('#view_list_btn_div').hide();
		$('#hide_list_btn_div').show();
		loadGuestEmailGrid(1);	
	}
	else
	{
		$('#view_list_btn_div').show();
		$('#hide_list_btn_div').hide();
		$('#content_div').fadeOut('fast', function() {});
	}
}

function isValidEmail(emailAddress) 
{
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
