// JavaScript Document

$(function(){
    var dates = new Date();
    var times = dates.getTime();
    var error_wrap = "div.error_info";
    $("#usernames").livequery("blur",function(){
        var user = $(this).val();
        if(!user){
            $("#userstatus").removeClass("valid").addClass("invalid").text("ยังไม่ได้กรอกชื่อผู้ใช้");
            $("#CheckUser").val("1");
            return;
        }
        $.ajax({
            type: "get",
            url: "/requests/valid_user_shop.php",
            data: "user="+user,
            cache: false,
            success: function(msg){ 
                if( msg == 1 ){
                    var error_t = "จะต้องขึ้นต้นด้วยตัวอักษร<br>ใช้ ตัวเลข ตัวอักษร ขีดกลาง (-) ได้เท่านั้น แต่ไม่สามารถจบด้วย underscore (-) ได้<br>จำนวนอักษรต้องอยู่ระหว่าง 6-30 ตัวอักษรเท่านั้น";
                    $("#userstatus").removeClass("valid").addClass("invalid").html(error_t);
                    $("#CheckUser").val("1");
                }else
                if( msg == 2 ){
                    $("#userstatus").removeClass("valid").addClass("invalid").html("ชื่อผู้ใช้นี้มีอยู่ในระบบแล้ว");
                    $("#CheckUser").val("2");
                }else{
                    $("#userstatus").removeClass("invalid").addClass("valid").html("ชื่อผู้ใช้นี้สามารถใช้ได้");
                    $("#CheckUser").val("3");
                }
            }
        });
    });
});

function checkaddtent(f){ 
	if(f.tent.value==""){
		alert("กรุณากรอกชื่อเต้นท์รถ");
		f.tent.focus();
		return(false);
	}
	if(f.tent_detail.value==""){
		alert("กรุณากรอกสโลแกนของเต้นท์");
		f.tent_detail.focus();
		return(false);
	}
	if(f.name.value==""){
		alert("กรุณากรอกชื่อเจ้าของเต้นท์");
		f.name.focus();
		return(false);
	}
	if(f.lastname.value==""){
		alert("กรุณากรอกนามสกุลเจ้าของเต้นท์");
		f.lastname.focus();
		return(false);
	}
	if(f.id_state.value=="") {
		alert('กรุณาเลือกจังหวัด');
		f.id_state.focus();
		return(false);
	}
	if(f.id_ampher.value=="") {
		alert('กรุณาเลือกอำเภอ');
		f.id_ampher.focus();
		return(false);
	}
	if(f.usernames.value=="") {
		alert('กรุณากรอกชื่อผู้ใช้');
		f.usernames.focus();
		return(false);
	}
	/*if(f.password.value=="") {
		alert('กรุณากรอกรหัสผ่าน');
		f.password.focus();
		return(false);
	}*/
	if(f.userstatus.value=='ชื่อผู้ใช้นี้มีอยู่ในระบบแล้ว') {
		alert('ชื่อผู้ใช้นี้มีอยู่ในระบบแล้ว');
		f.userstatus.focus();
		return(false);
	}
	return(true);
}

function Confrims() {
	if(confirm("Are you sure?")) {
		return(true);
	}
	return(false);
}

function checknum(){
	var f=document.getElementById('telsms');
	if(f.value) {
		if((isNaN(f.value))==true) {
			alert("กรุณากรอกเบอร์โทรศัพท์เป็นตัวเลขเท่านั้น");
			f.select();
			return(false);
		}
		if(f.value.length<10) {
			alert("กรุณากรอกเบอร์โทรศัพท์ให้ครบ 10 หลัก");
			f.select();
			return(false);
		}
	}
	return(true);
}

$(document).ready(function(){
    var time = Math.random();
    $("#id_group").livequery("change",function(){
		 var id_group = $(this).val();
		  $.ajax({
            type: "GET",
            url: "/requests/group.php",
            data: "id_group="+id_group+"&time="+time,
            success: function(req){
                $("#show").empty().html(req);
            }
        }); 
	});
});

$(document).ready(function(){
    var time = Math.random();
    $("#super_sale_id").livequery("change",function(){
		 var super_sale_id = $(this).val();
		  $.ajax({
            type: "GET",
            url: "/requests/sale.php",
            data: "super_sale_id="+super_sale_id+"&time="+time,
            success: function(req){
                $("#sale_id").empty().html(req);
            }
        }); 
	});
});

$(document).ready(function(){
    var time = Math.random();
    $("#id_state").livequery("change",function(){
		 var state_id = $(this).val();
		  $.ajax({
            type: "GET",
            url: "/requests/select_province.php",
            data: "state_id="+state_id+"&time="+time,
            success: function(req){
                $("#id_ampher").empty().html(req);
            }
        }); 
	});
});

function allcheck(obj,main){
    for(var i=0; i < obj.elements.length ; i++){
        if(main.checked==true){
            obj[i].checked=true;
        }else{
            obj[i].checked=false;
        }
    }
}



function number_format (number, decimals, dec_point, thousands_sep) {
    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 maxsize(url,id){
    newwin=window.open(url,'fullsize'+id, 'width='+screen.width+', top=0, left=0, height='+screen.height+', fullscreen=yes, location=0');
    if (window.focus){
        newwin.focus();
    }
    return false;
}

function delc(obj){
    var status=false;
    for(var i=0 ; i < obj.elements.length ; i++ ){
        if(obj[i].name=='del[]'){
            if(obj[i].checked==true){
                status=true;
            }
        }
    }
    if(status==false){
        alert('Warning!!!\n\nPlease select at least one item to delete!!!');
        return false;
    }else{
        if(confirm('Warning!!!\n\n Confirm Press OK, Cancel Press Cancel.')){
            return true;
        }else{
            return false;
        }
    }
}


function delc_str(obj, var_str){
    var status=false;
    for(var i=0 ; i < obj.elements.length ; i++ ){
        if(obj[i].name=='del[]'){
            if(obj[i].checked==true){
                status=true;
            }
        }
    }
    if(status==false){
        alert('Warning!!!\n\nPlease select at least one item to delete!!!');
        return false;
    }else{
        if(confirm('Warning!!!\n\n '+var_str)){
            return true;
        }else{
            return false;
        }
    }
}




function newwin(URLStr,width, height, usescrollbar,resize)
{
    var popUpWin=0;
    var left = (screen.width/2) - width/2;
    var top = (screen.height/2) - height/2;
    if(popUpWin)
    {
        if(!popUpWin.closed) popUpWin.close();
    }
    popUpWin = open(URLStr, 'popUpWin', 'toolbar=0,location=0,directories=0,status=0,menub ar=0,scrollbars='+usescrollbar+',resizable='+resize+',copyhistory=0,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}


function isEmail(email){ 
	
	
    var emailRE = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	
    if (!emailRE.test(email)){
        return false;
    }else{
        return true;
    }

}

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("z-index","20000");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


function isUrl(s) {
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(s);
}

function insertAtCursor(myField, myValue) {
    //IE support
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)+myValue+myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
}

//Jump menu
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
