// JavaScript Document
// POP 15-12-2009
// 18-12-2009 add checkRewrite(), popUpWindow()
// 21-12-2009 add 

var monthList = new Array(13)
monthList[0] = 'Month List Start at Index 1';
monthList[1] = 'January';
monthList[2] = 'February';
monthList[3] = 'March';
monthList[4] = 'April';
monthList[5] = 'May';
monthList[6] = 'June';
monthList[7] = 'July';
monthList[8] = 'August';
monthList[9] = 'September';
monthList[10] = 'October';
monthList[11] = 'November';
monthList[12] = 'December';

var dayList = new Array(7);
dayList[0] = "Sunday";
dayList[1] = "Monday";
dayList[2] = "Tuesday";
dayList[3] = "Wednesday";
dayList[4] = "Thursday";
dayList[5] = "Friday";
dayList[6] = "Saturday";


function numbersonly(e) {
	var unicode = e.charCode ? e.charCode : e.keyCode;
	if (unicode != 8 && unicode !=9 ) {
		if(unicode < 45 || unicode > 57) {
			return false
		}
	}
}

function chkMail(mail) {
	var mailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (mailFilter.test(mail)) {
		return true;
	}
	else {
		return false;
	}
}

function dateToFullDate(val) {
	var temp = val.split("-");
	return temp[2] + " " + monthList[parseInt(temp[1])] + " " + temp[0];
}

function checkRewrite(val) {
	var c = false;
	var rewriteFilter = /^[a-zA-Z0-9_\.]+$/;
	
	if (val.value.length == 0) {
		alert("Name rewrite is empty!!");
		c = false;
	}
	else if(!rewriteFilter.test(val.value)) {
		alert("Special charactor is not allow for rewriting");
		c = false;
	}
	else {
		$.ajax({
			type: "post",
			url: "ajax/ajax.php",
			async: false,
			data: "file=rewrite&value=" + val.value,
			complete: function(response) {
				if(response.responseText == 'N'){
					alert("Name rewrite is conflict!!!");
					c = false;
				}
				else {
					c = true;
				}
			}
		});
	}
	val.focus();
	return c;
}


 function checkmodRewrite(val,oldval) {

	var c = false;
	var rewriteFilter = /^[a-zA-Z0-9_\.]+$/;
	
	if (val.value.length == 0) {
		alert("Name rewrite is empty!!");
		c = false;
	}
	else if(!rewriteFilter.test(val.value)) {
		alert("Special charactor is not allow for rewriting");
		c = false;
	}
	else {
		$.ajax({
			type: "post",
			url: "ajax/ajax_checkmodrewrite.php",
			async: false,
			data: "file=rewrite&value=" + val.value+"&oldvalue="+oldval.value,
			complete: function(response) {
				if(response.responseText == 'N'){
					alert("Name rewrite is conflict!!!");
					c = false;
				}
				else {
					c = true;
				}
			}
		});
	}
	val.focus();
	return c;
}
function popUpWindow(sURL, sName, width, height, menu, resize, scrollbar, status, title, toolbar) {
        // detect value
        menu = typeof(menu) == 'undefined'?0:menu;
        resize = typeof(resize) == 'undefined'?0:resize;
        scrollbar = typeof(scrollbar) == 'undefined'?0:scrollbar;
        status = typeof(status) == 'undefined'?0:status;
        title = typeof(title) == 'undefined'?0:title;
        toolbar = typeof(toolbar) == 'undefined'?0:toolbar;

        screenwidth = window.screen.width;
        screenheight = window.screen.height;

        var left = (screenwidth/2) - (width/2);
        var top = (screenheight/2) - (height/2);

        var newwin = window.open(sURL, sName, "top=" + top + ", left=" + left + ", height="+ height + ", width=" + width + ", menubar=" + menu + ", resizable=" + resize + ", scrollbars=" + scrollbar + ", status=" + status + ", titlebar=" + title + ", toolbar=" + toolbar + "");
        newwin.focus();
} 

function number_format (number, decimals, dec_point, thousands_sep) {
		//number_format(20000,2,'.',',');	
        var exponent = "";
        var numberstr = number.toString ();
        var eindex = numberstr.indexOf ("e");
        if (eindex > -1) {
                exponent = numberstr.substring (eindex);
                number = parseFloat (numberstr.substring (0, eindex));
        }
                 
        if (decimals != null) {
                var temp = Math.pow (10, decimals);
                number = Math.round (number * temp) / temp;
        }
        var sign = number < 0 ? "-" : "";
        var integer = (number > 0 ? Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
         
        var fractional = number.toString ().substring (integer.length + sign.length);
        dec_point = dec_point != null ? dec_point : ".";
        fractional = decimals != null && decimals > 0 || fractional.length > 1 ? (dec_point + fractional.substring (1)) : "";
        if (decimals != null && decimals > 0) {
                for (i = fractional.length - 1, z = decimals; i < z; ++i) {
                        fractional += "0";
                }
        }
                 
        thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? thousands_sep : null;
        if (thousands_sep != null && thousands_sep != "") {
                for (i = integer.length - 3; i > 0; i -= 3){
                        integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
                }
        }  
        return sign + integer + fractional + exponent;
}

function getFullDateFromTimestamp(unixtime) {
	var current = new Date(unixtime * 1000);
	return current.getDate() + " " + monthList[current.getMonth() + 1] + " " + current.getFullYear();
}
	
function getDateFromTimestamp(unixtime) {
	var current = new Date(unixtime * 1000);
	return current.getFullYear() + "-" + current.getMonth() + "-" + current.getDate();
}
	
function getTimeFromTimestamp(unixtime) {
	var current = new Date(unixtime * 1000);
	return current.getHours() + ":" + current.getMinutes() + ":" + current.getSeconds();
}
function getYearFromTimestamp(unixtime) {
	var current = new Date(unixtime * 1000);
	return current.getFullYear();
}
function getMonthFromTimestamp(unixtime) {
	var current = new Date(unixtime * 1000);
	return current.getMonth();
}
function getDayFromTimestamp(unixtime) {
	var current = new Date(unixtime * 1000);
	return current.getDate();
}

//Set Top menu  Add by Ohm 20091216
function MM_showHideLayers(DivName,ar,action){
		$("#"+DivName).css("visibility",action);
}
   //Set Top menu  Add by Ohm 20091216
 function showHideLayers(DivName,ar,action){
		$("#"+DivName).css("visibility",action);
 }
 function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function myFixedTo(val, precision){
    if(val>0){
        if(String(val).indexOf('.')!=-1){
            s=String(val).split('.');                             
            k=precision - (s[1]).length;
            for(i=0;i<k;i++){
                s[1]=s[1]+'0';
            }
            return String(s[0]+'.'+s[1]);
        }else{
            s=val+'.';
            for(i=0;i<precision;i++){
                s=s+'0';
            }
            return s;
        }
    }else{
        z='';
        for(i=1;i<=precision;i++){
            z=z+'0';
        }
        return '0.'+z;
    };
}
function RoundToNdp(X, N){
    var T=Math.pow(10, N);
    T=Math.round(X*T)/T;
    return myFixedTo(T, N);
}
	//Logout
	function MemberLogout(){
		ajaxPath = $("#ajaxpath").val();	
		if(confirm("Confirm logout?")==true){
			$.get(ajaxPath+"ajax_memberlogout.php",{},function(data){
				if(data){
					alert(data);
					location.href = "http://www.e-condom.org";
					//location.href = "/e-condom/";
				}
			});
		}
	}
	
$(document).ready(function(){
			var objKey="#edit-query"; // id ของ ช่องค้นหา 
			if($(objKey).val()==""){
					$(objKey).addClass("watermark");
			}	
			$(objKey).focus(function(){	 
				$(objKey).removeClass("watermark");			 
			}).blur(function(){
				if($(objKey).val()==""){
					$(objKey).addClass("watermark");
				}	
			});	

		
	
	
	leftheight = $(".leftmenu").height();
 	bodyheight =$(".bodycontent ").height();
	bodytoleft= bodyheight+12*1;
	//alert(bodytoleft)
	if(leftheight > bodyheight){
		$(".bodycontent").addClass('bodyfix');
	}	if(leftheight < bodyheight){
		$(".leftmenu").css('height',bodytoleft);
		$(".rightmenu").css('height',bodytoleft);
	}
	
	$("#quickloginbutton").bind("click",function(){
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;		
		ajaxPath = $("#ajaxpath").val();	
		emLlogin = $("#emlquick").val();
		Pwdlogin = $("#pwdquick").val();
			if (!filter.test(emLlogin)){
				alert("Please provide a valid email address.");
				$("#emlquick").select();
			}else if (Pwdlogin == ""){
				alert("Enter your password.");
				$("#pwdquick").focus();
			}else{
				$.get(ajaxPath+"ajax_loginshoppingart.php",{inputeml:emLlogin,inputpwd:Pwdlogin},
				function(data){
					//alert(data)
					st1 = data.split("::");
					if(st1[1]==0){
						alert(st1[0]);
					}else {
						alert('Thank you for logging in!\n Please continue shopping.')
						$("#setinfo").html(st1[0]);
						$(".quick-login").slideUp();
					}

			})	
			}//End else
	})
	
	
	
	

});

	function shoppingChklogin(){
		ajaxPath = $("#ajaxpath").val();	
		emLlogin = $("#memberemail1").val();
		mempwd = $("#memberpwd1").val();	
		//alert(emLlogin)
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;		
			if (!filter.test(emLlogin)){
				alert("Please provide a valid email address.");
				$("#memberemail1").select();
			}else if (mempwd == ""){
				alert("Enter your password.");
				$("#memberpwd1").focus();
			}else{
				$.get(ajaxPath+"ajax_loginshoppingart.php",{inputeml:emLlogin,inputpwd:mempwd},
				function(data){
					//alert(data)
					st1 = data.split("::");
					if(st1[1]==0){
						alert(st1[0]);
					}else {
						$("#setinfo").html(st1[0]);
						$("#loginline").slideUp();
						$("#loginlineform").slideUp();
						$(".quick-login").slideUp();
					//	alert(st1[0])
					}

			})	
			}//End else
	
		
	}
	
	function LoginNow(){
				//	$("#chklogin").bind('click',function(){
				if($("#chklogin").is(':checked') ==true){
					$("#loginshoppingcart").slideDown(500)
				}else{
					$("#loginshoppingcart").slideUp(500)
				}
			//})
	
	}

