// JavaScript Document
//Top Menu
function jsddm_open()
{	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');}

function jsddm_close()
{	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{	closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{	if(closetimer)
	{	window.clearTimeout(closetimer);
		closetimer = null;}}
		
//General
function checkNumber(val){
	//var my_string = prompt("Please enter a number","");
	//document.write(my_string)
	if(isNaN(val)){
		//document.write ("this is not a number ");
		return 0;
	}
}
function removeString(text){
	text=text.replace(/[^0-9]/g, '');
	return text;
}
function keypressed(e, action){
	//alert(e.keyCode)
	if(e.keyCode=='13' && action=="login") getLogin();
} 

//Ajax
function sendActionJSON(action, $url, data){
    var a = $.manageAjax({manageType: 'abortOld', maxReq: 2, blockSameRequest: true});
    var dpObj="workStat";

    $('#'+dpObj).html('Loading...');
    $url=$url+"?action="+action;
    //alert($url);
    a.add({
        data: "&key="+ss_key+"&data="+data,
        error: function() {
            $('#'+dpObj).html('Page Not Found !');
        },
        success: function(json) {
            //alert(json);
            $('#'+dpObj).html("");
            //alert(json.error);
            if(typeof(json.error) != 'undefined'){
                if(json.error != ''){
                    alert(json.error);
                }
                else if(action=="getCartInfo"){
                    showCartItem(json.item,json.price);
                }
            }
        },
        dataType: "json",
        type:"POST",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        cache: false,
        url: $url
    });
}

//Send Action then Alert or Do something
function sendAction(action, $url, data){
	//$(resultLoc).html(data);
	var a = $.manageAjax({manageType: 'abortOld', maxReq: 2, blockSameRequest: true});
	var dpObj="workStat";

	$('#'+dpObj).html('Loading...');
	$url=$url+"?action="+action;
	//alert($url);
	a.add({
		data: "&key="+ss_key+"&data="+data,
		error: function() {
			$('#'+dpObj).html('Page Not Found !');
		},
		success: function(html) {
                    $('#'+dpObj).html("");

                    //alert(html);
                    if(html.substr(0,5)!="Error") {
                        if(action=="addCustomer"){
                             alert(html);
                             window.location="index.php";
                        }
                        else if(action=="logout"){
                            setCookie("pata_username","",-1);
                            setCookie("pata_password","",-1);
                            setCookie("pata_discount","",-1);
                            alert(html);
                            window.location="index.php";
                        }
                        else if(action=="editCustomer"){
                            alert(html);
                            window.location="profile_edit.php";
                        }
                        else if(action=="add2Cart"){
                            //alert(html);
                            sendActionJSON("getCartInfo", "share/shopping.php", "");
                        }
                        else if(action=="removeFromCart") window.location="viewcartitem.php";
                        else if(action=="cancelOrder"){
                            alert(html);
                            window.location="orderhistory.php";
                        }
                        else if(action=="notifyPayment"){
                            alert(html);
                            window.location="index.php";
                        }
                        else alert(html);
                    }
                    else alert(html);
		},
		dataType: "html",
		type:"POST",
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		cache: false,
		url: $url
	});
}

//Get data with 1 valiable
function getForm1Val(action, $url, resultLoc, data){
	//$(resultLoc).html(data);
	var a = $.manageAjax({manageType: 'abortOld', maxReq: 2, blockSameRequest: true});
	var dpObj="workStat";
	
	$('#'+dpObj).html('Loading...');
	$url=$url+"?action="+action;
	
	a.add({
		data: "&key="+ss_key+"&data="+data,
		error: function() {
			$('#'+dpObj).html('Page Not Found !');
		},
		success: function(html) {
			$('#'+dpObj).html("");
			$(resultLoc).html(html);
		},
		dataType: "html",
		type:"POST",
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		cache: false,
		url: $url
	});
}

//Get data with No valiable
function getForm(action, $url, resultLoc){
	var a = $.manageAjax({manageType: 'abortOld', maxReq: 2, blockSameRequest: true});
	var dpObj="workStat";
	
	$('#'+dpObj).html('Loading...');
	$url=$url+"?action="+action;
	
	a.add({
		data: "&key="+ss_key,
		error: function() {
			$('#'+dpObj).html('Page Not Found !');
		},
		success: function(html) {
			$('#'+dpObj).html('');
			$(resultLoc).html(html);
		},
		dataType: "html",
		type:"POST",
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		cache: false,
		url: $url
	});
}
//Shopping Cart
function notifyPayment(frm){
    var conf=confirm("Please make sure all data is correct before send ?");

    if(conf==true){
        var checked="Incorrect Input,\n";
        if(!frm.pMoneyValue.value) checked+="\n-Please enter value that you paid ";
        if(!frm.oid.value) checked+="\n-Please enter Order No. ";
         
        if(checked!="Incorrect Input,\n") alert(checked);
        else{
            data="&pName="+frm.pName.value;
            data=data+"&oid="+frm.oid.value;
            data=data+"&payment="+frm.pPayment.value;
            data=data+"&date="+frm.pDay.value+"/"+frm.pMonth.value+"/"+frm.pYear.value;
            data=data+"&time="+frm.pHrs.value+":"+frm.pMin.value;
            data=data+"&value="+frm.pMoneyValue.value;
            data=data+"&note="+frm.pNote.value;

            //alert(data);
            sendAction("notifyPayment", "share/shopping.php", data);
        }
    }
}

function cancelOrder(data){
    var conf=confirm("Are you sure want to cancel this order ?");
    //Check Old Password
    if(conf==true){
         sendAction("cancelOrder", "share/shopping.php", data);
    }
}
function validateSendOrder(frm){
    var conf=confirm("Confirm Placing this order ?");
    //Check Old Password
    if(conf==true){
        var checked="Incorrect Input,\n";
        //Check Delivery Address
        if(!frm.cAddress_del.value) checked+="\n-Delivery Address cannot be Null ";
        if(!frm.cProvince_del_alt.value && frm.cProvince_del.value==0) checked+="\n-Please enter Delivery Province ";
        if(!frm.cDistrict_del_alt.value && frm.cDistrict_del.value==0) checked+="\n-Please enter Delivery Amphur ";
        if(!frm.cSubDistrict_del_alt.value && frm.cSubDistrict_del.value==0) checked+="\n-Please enter Delivery Tumbon ";
        if(!frm.cPostCode_del.value) checked+="\n-Delivery Address cannot be Null \n";

        if(!frm.cSameBillDel.checked){
            if(!frm.cAddress_bil.value) checked+="\n-Billing Address cannot be Null ";
            if(!frm.cProvince_bil_alt.value && frm.cProvince_bil.value==0) checked+="\n-Please enter Billing Province ";
            if(!frm.cDistrict_bil_alt.value && frm.cDistrict_bil.value==0) checked+="\n-Please enter Billing Amphur ";
            if(!frm.cSubDistrict_bil_alt.value && frm.cSubDistrict_bil.value==0) checked+="\n-Please enter Billing Tumbon ";
            if(!frm.cPostCode_bil.value) checked+="\n-Billing Address cannot be Null \n";
        }
        if(checked!="Incorrect Input,\n") alert(checked);
        else return true;
    }
    return false;
}
function removeFromCart(data){
    var conf=confirm("Remove this item from Cart?");
    if(conf){
        sendAction('removeFromCart', 'share/shopping.php', data)
    }
}
function showCartItem(item,price){
    price=parseFloat(price);
    price=price.toFixed(2);
    $('#cartItem').html(item);
    $('#cartPrice').html(price);
}
function add2Cart(pid, pname, pPrice){
    pPrice=parseFloat(pPrice);

    var pType=$('#pType'+pid).val();
    var pAmount=$('#pAmount'+pid).val();
    var pTotalPrice=pPrice*pAmount;
    pTotalPrice=pTotalPrice.toFixed(2);
    var text= "Add to Cart? \n\n";
    text= text+"Item Name : "+pname+" \n";
    text= text+"Type : "+pType+" \n";
    text= text+"Amount : "+pAmount+" \n";
    text= text+"Total Price : "+pTotalPrice+" Bahts \n";

    data = "&pid="+pid+"&amount="+pAmount+"&type="+pType+"&price="+pTotalPrice;
    var conf=confirm(text);
    if(conf){
        sendAction("add2Cart", "share/shopping.php", data);
    }
}

//Login
function getCookie(c_name){
    if (document.cookie.length>0){
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1){
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

function setCookie(c_name,value,expiredays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie(){
    username=getCookie('username');
    if (username!=null && username!=""){
        alert('Welcome again '+username+'!');
    }
    else{
        username=prompt('Please enter your name:',"");
        if (username!=null && username!=""){
            setCookie('username',username,365);
        }
    }
}
function getLogin(){
    var user=$('#username').val();
    var password=$('#password').val();
    //password=$.md5(password);
    var remember=$('#remember').attr('checked');
    //alert(remember);
    var dpObj="workStat";
    var a = $.manageAjax({manageType: 'abortOld', maxReq: 2, blockSameRequest: true});
    //var $this = $(this);

    $('#'+dpObj).html('Checking...');
    var $url="share/login.php";
    $url=$url+"?action=getLogin";

    a.add({
        data: "&user="+user+"&pass="+password,
        error: function() {
            $('#'+dpObj).html('Page Not Found !');
        },
        success: function(html) {
            if(html=="1" || html=="2"){
                if(html=="1") alert("Incorrect Username or Password!");
                else alert("Account is not active!, Please activate you account");
            }
            else{
                    //lStatus="Loging in...";
                    if(remember==true){
                        setCookie("pata_username",user,30);
                        setCookie("pata_password",html,30);
                    }
                    else{
                        setCookie("pata_username","",-1);
                        setCookie("pata_password","",-1);
                        setCookie("pata_discount","",-1);
                    }
                    window.location="index.php";
            }
            //alert(html);
            $('#'+dpObj).html("");
        },
        dataType: "html",
        type:"POST",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        cache: false,
        url: $url
    });
    //getAllProduct();
}

//Register Form
function editCustomer(frm){
	var pAddPost="";

  	pAddPost += "&cID="+frm.cID.value;
	pAddPost += "&cName="+frm.cName.value;
	pAddPost += "&cLastname="+frm.cLastname.value;
	pAddPost += "&cBirthdate="+frm.cBirthYear.value+"-"+frm.cBirthMonth.value+"-"+frm.cBirthDay.value;
	pAddPost += "&cSex="+frm.cSex.value;
	pAddPost += "&cEmail="+frm.cEmail.value;
	pAddPost += "&cIDCard="+frm.cIDCard.value;
	pAddPost += "&cPhone="+frm.cPhone.value;
	pAddPost += "&cMobile="+frm.cMobile.value;
	pAddPost += "&cFax="+frm.cFax.value;
	pAddPost += "&cKnowPata="+frm.cKnowPata.value;

	pAddPost += "&cAddress_del="+frm.cAddress_del.value;
	if(frm.cProvince_del_alt.value) pAddPost += "&cProvince_del="+frm.cProvince_del_alt.value;
	else pAddPost += "&cProvince_del="+frm.cProvince_del.options[frm.cProvince_del.selectedIndex].text;
	if(frm.cDistrict_del_alt.value) pAddPost += "&cDistrict_del="+frm.cDistrict_del_alt.value;
	else pAddPost += "&cDistrict_del="+frm.cDistrict_del.options[frm.cDistrict_del.selectedIndex].text;
	if(frm.cSubDistrict_del_alt.value) pAddPost += "&cSubDistrict_del="+frm.cSubDistrict_del_alt.value;
	else pAddPost += "&cSubDistrict_del="+frm.cSubDistrict_del.options[frm.cSubDistrict_del.selectedIndex].text;
	pAddPost += "&cPostCode_del="+frm.cPostCode_del.value;

	if(frm.cSameBillDel.checked==false){
		pAddPost += "&cAddress_bil="+frm.cAddress_bil.value;
		if(frm.cProvince_bil_alt.value) pAddPost += "&cProvince_bil="+frm.cProvince_bil_alt.value;
		else pAddPost += "&cProvince_bil="+frm.cProvince_bil.options[frm.cProvince_bil.selectedIndex].text;
		if(frm.cDistrict_bil_alt.value) pAddPost += "&cDistrict_bil="+frm.cDistrict_bil_alt.value;
		else pAddPost += "&cDistrict_bil="+frm.cDistrict_bil.options[frm.cDistrict_bil.selectedIndex].text;
		if(frm.cSubDistrict_bil_alt.value) pAddPost += "&cSubDistrict_bil="+frm.cSubDistrict_bil_alt.value;
		else pAddPost += "&cSubDistrict_bil="+frm.cSubDistrict_bil.options[frm.cSubDistrict_bil.selectedIndex].text;
		pAddPost += "&cPostCode_bil="+frm.cPostCode_bil.value;
	}
	else pAddPost += "&cAddress_bil=&cProvince_bil=&cDistrict_bil=&cSubDistrict_bil=&cPostCode_bil=";

	var action="editCustomer";
	var $url= "share/user.php"; /// ---- improtant
	var data=pAddPost;

	//alert(data);
	sendAction(action, $url, data);
}
function addCustomer(frm){
	var pAddPost="";
		
	pAddPost += "&cName="+frm.cName.value;
	pAddPost += "&cLastname="+frm.cLastname.value;
	pAddPost += "&cBirthdate="+frm.cBirthYear.value+"-"+frm.cBirthMonth.value+"-"+frm.cBirthDay.value;
	pAddPost += "&cSex="+frm.cSex.value;
	pAddPost += "&cEmail="+frm.cEmail.value;
	pAddPost += "&cIDCard="+frm.cIDCard.value;
	pAddPost += "&cPhone="+frm.cPhone.value;
	pAddPost += "&cMobile="+frm.cMobile.value;
	pAddPost += "&cFax="+frm.cFax.value;
	pAddPost += "&cKnowPata="+frm.cKnowPata.value;
	pAddPost += "&cLevel="+frm.cLevel.value;
	pAddPost += "&cActive=false"
	pAddPost += "&cApprove=false"

	pAddPost += "&cAddress_del="+frm.cAddress_del.value;
	if(frm.cProvince_del_alt.value) pAddPost += "&cProvince_del="+frm.cProvince_del_alt.value;
	else pAddPost += "&cProvince_del="+frm.cProvince_del.options[frm.cProvince_del.selectedIndex].text;
	if(frm.cDistrict_del_alt.value) pAddPost += "&cDistrict_del="+frm.cDistrict_del_alt.value;
	else pAddPost += "&cDistrict_del="+frm.cDistrict_del.options[frm.cDistrict_del.selectedIndex].text;
	if(frm.cSubDistrict_del_alt.value) pAddPost += "&cSubDistrict_del="+frm.cSubDistrict_del_alt.value;
	else pAddPost += "&cSubDistrict_del="+frm.cSubDistrict_del.options[frm.cSubDistrict_del.selectedIndex].text;
	pAddPost += "&cPostCode_del="+frm.cPostCode_del.value;

	if(frm.cSameBillDel.checked==false){
		pAddPost += "&cAddress_bil="+frm.cAddress_bil.value;
		if(frm.cProvince_bil_alt.value) pAddPost += "&cProvince_bil="+frm.cProvince_bil_alt.value;
		else pAddPost += "&cProvince_bil="+frm.cProvince_bil.options[frm.cProvince_bil.selectedIndex].text;
		if(frm.cDistrict_bil_alt.value) pAddPost += "&cDistrict_bil="+frm.cDistrict_bil_alt.value;
		else pAddPost += "&cDistrict_bil="+frm.cDistrict_bil.options[frm.cDistrict_bil.selectedIndex].text;
		if(frm.cSubDistrict_bil_alt.value) pAddPost += "&cSubDistrict_bil="+frm.cSubDistrict_bil_alt.value;
		else pAddPost += "&cSubDistrict_bil="+frm.cSubDistrict_bil.options[frm.cSubDistrict_bil.selectedIndex].text;
		pAddPost += "&cPostCode_bil="+frm.cPostCode_bil.value;
	}
	else pAddPost += "&cAddress_bil=&cProvince_bil=&cDistrict_bil=&cSubDistrict_bil=&cPostCode_bil=";
	
	pAddPost += "&cUser="+frm.cUser.value;
	var cPassword=frm.cPassword.value;
	//cPassword=$.md5(cPassword);
	pAddPost += "&cPassword="+cPassword;
	pAddPost += "&cSecQuestion="+frm.cSecQuestion.value;
	pAddPost += "&cSecAnswer="+frm.cSecAnswer.value;
		
	action="addCustomer";
	$url= "share/user.php";
	data=pAddPost;
	
	//alert(data);
	sendAction(action, $url, data);
}
function validateSecAnswer(){
    var action="validateSecAnswer";
    var $url= "share/user.php";
    var cPassword=$('#cUser').val();
    //cPassword=$.md5(cPassword);
    //alert(cPassword);
    var data="&cUser="+$('#cUser').val()+"&newPass="+cPassword+"&cQuestion="+$('#cSecQuestion').html()+"&cAnswer="+$('#cSecAnswer').val();
    
    //alert(data);
    sendAction(action, $url, data);

    return false;
}
function getSecQuestion(){
    var a = $.manageAjax({manageType: 'abortOld', maxReq: 2, blockSameRequest: true});
    var dpObj="workStat";
    
    a.add({
        data: "",
        error: function() {
            $('#'+dpObj).html('Page Not Found !');
        },
        success: function(html) {
            $('#'+dpObj).html("");
            //alert(html);
            var spamVal=$('#cSpam').val();
            spamVal=spamVal.toUpperCase();
            if(html!=spamVal) alert("Please enter correct Antispam text");
            else {
                a.add({
                    data: "&cUser="+$('#cUser').val(),
                    error: function() {
                        $('#'+dpObj).html('Page Not Found !');
                    },
                    success: function(html) {
                        $('#'+dpObj).html("");
                        //alert(html);
                        var spamVal=$('#cSpam').val();
                        spamVal=spamVal.toUpperCase();
                        if(html=="0") alert("Username not found");
                        else $('#cSecQuestion').html(html);
                    },
                    dataType: "html",
                    type:"POST",
                    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                    cache: false,
                    url: "share/user.php?action=getSecQuestion"
                });
            }
        },
        dataType: "html",
        type:"POST",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        cache: false,
        url: "share/user.php?action=getSpamImg"
    });
}
function validatePassword(frm){
    var conf=confirm("You are about to change the password?");
    //Check Old Password
    if(conf==true){

        var checked="Incorrect Input,\n";
        if(!frm.cPassword.value) checked+="\n-Password cannot be Null ";
        if(!frm.cSecQuestion.value) checked+="\n-Secret Question cannot be Null ";
        if(!frm.cSecAnswer.value) checked+="\n-Secret Answer cannot be Null ";

        //Check Equal
        if(frm.cPassword.value!=frm.cConPassword.value) checked+="\n-Incorrect Comfirm Password ";

        //Check Password length
        tempVal=frm.cPassword.value;
        if(tempVal.length<5) checked+="\n-Password need to be longer than 5 characters \n";
        //alert(frm.cOldPassword.value);

        var a = $.manageAjax({manageType: 'abortOld', maxReq: 2, blockSameRequest: true});

        var dpObj="workStat";
        var $url="share/user.php";
        $url=$url+"?action=checkOldPassword";
        //var cOldPassword=$.md5(frm.cOldPassword.value);
		var cOldPassword=$.frm.cOldPassword.value;
        var data="&key="+ss_key+"&username="+frm.cUser.value+"&oldpass="+cOldPassword+"&cID="+frm.cID.value;

        a.add({
            data: data,
            error: function() {
                $('#'+dpObj).html('Page Not Found !');
            },
            success: function(html) {
                $('#'+dpObj).html("");
                //alert(html);
                if(html!=1) {
                    checked+="\n-Old Password is not correct";
                }

                if(checked!="Incorrect Input,\n") 	alert(checked);
                else{
                    //else editCustomer(frm);
                    //cPassword=$.md5(frm.cPassword.value);
					cPassword=$.frm.cPassword.value;
                    data="&key="+ss_key+"&cUser="+frm.cUser.value+"&cPassword="+cPassword+"&cID="+frm.cID.value;
                    data=data+"&cSecQuestion="+frm.cSecQuestion.value+"&cSecAnswer="+frm.cSecAnswer.value;
                    sendAction("changePassword", "share/user.php", data);
                }
            },
            dataType: "html",
            type:"POST",
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            cache: false,
            url: $url
        });
    }
    return false;
}
function validateCustomer(frm, action){
    if(action=="add") var conf=confirm("You are about to send resgistration form?");
    else if(action=="edit") var conf=confirm("Are you going to edit your Profile?");

    if(conf==true){
        //check Null
        var checked="Incorrect Input,\n";

        if(!frm.cName.value) checked+="\n-Name cannot be Blank ";
        if(!frm.cLastname.value) checked+="\n-Lastname cannot be Blank ";

        //Check ID card
        if(!checkID(frm.cIDCard.value)) checked+="\n-Invalid ID Card Number ";

        //Check Email
        tempVal=frm.cEmail.value;
        if(tempVal.indexOf("@")==-1) checked+="\n-Invalid Email ";

        //Check Customer Level
        if(frm.cLevel.value==0) checked+="\n-Please select Customer Level ";

        //Check Delivery Address
        if(!frm.cAddress_del.value) checked+="\n-Delivery Address cannot be Null ";
        if(!frm.cProvince_del_alt.value && frm.cProvince_del.value==0) checked+="\n-Please enter Delivery Province ";
        if(!frm.cDistrict_del_alt.value && frm.cDistrict_del.value==0) checked+="\n-Please enter Delivery Amphur ";
        if(!frm.cSubDistrict_del_alt.value && frm.cSubDistrict_del.value==0) checked+="\n-Please enter Delivery Tumbon ";
        if(!frm.cPostCode_del.value) checked+="\n-Delivery Address cannot be Null \n";

        if(!frm.cSameBillDel.checked){
            if(!frm.cAddress_bil.value) checked+="\n-Billing Address cannot be Null ";
            if(!frm.cProvince_bil_alt.value && frm.cProvince_bil.value==0) checked+="\n-Please enter Billing Province ";
            if(!frm.cDistrict_bil_alt.value && frm.cDistrict_bil.value==0) checked+="\n-Please enter Billing Amphur ";
            if(!frm.cSubDistrict_bil_alt.value && frm.cSubDistrict_bil.value==0) checked+="\n-Please enter Billing Tumbon ";
            if(!frm.cPostCode_bil.value) checked+="\n-Billing Address cannot be Null \n";
        }

        //Check Password
        if(!(action=="edit")){
            if(!frm.cUser.value) checked+="\n-Username cannot be Null ";
            if(!frm.cPassword.value) checked+="\n-Password cannot be Null ";
            if(!frm.cSecQuestion.value) checked+="\n-Secret Question cannot be Null ";
            if(!frm.cSecAnswer.value) checked+="\n-Secret Answer cannot be Null ";
            //Check Equal
            if(frm.cPassword.value!=frm.cConPassword.value) checked+="\n-Incorrect Comfirm Password ";

            //Check Password length
            tempVal=frm.cPassword.value;
            if(tempVal.length<5) checked+="\n-Password need to be longer than 5 characters \n";
        }

        var a = $.manageAjax({manageType: 'abortOld', maxReq: 2, blockSameRequest: true});
        var dpObj="workStat";

        if(action=="add"){
            $('#'+dpObj).html('Checking...');
            $url="share/user.php";
            //Check Duplicate Username
            $url=$url+"?action=validateUsername";
            data="&key="+ss_key+"&username="+frm.cUser.value;

            a.add({
                    data: data,
                    error: function() {
                        $('#'+dpObj).html('Page Not Found !');
                    },
                    success: function(html) {
                        $('#'+dpObj).html("");
                        //alert(html);
                        if(html!=1) {
                                if(action=="add") checked+="\n-This Username is already existed";
                                else checked+="\n-Incorrect Old Password";
                        }

                        a.add({
                            data: data,
                            error: function() {
                                $('#'+dpObj).html('Page Not Found !');
                            },
                            success: function(html) {
                                $('#'+dpObj).html("");
                                //alert(html);
                                var spamVal=frm.cSpam.value;
                                spamVal=spamVal.toUpperCase();
                                if(html!=spamVal) {
                                        checked+="\n-Please enter correct Antispam text";
                                }

                                if(checked!="Incorrect Input,\n") 	alert(checked);
                                else{
                                        if(action=="add") addCustomer(frm);
                                        //else editCustomer(frm);
                                }
                            },
                            dataType: "html",
                            type:"POST",
                            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                            cache: false,
                            url: "share/user.php?action=getSpamImg"
                        });
                    },
                    dataType: "html",
                    type:"POST",
                    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                    cache: false,
                    url: $url
                });
        }
        else{
            if(checked!="Incorrect Input,\n") 	alert(checked);
            else editCustomer(frm);
        }
    }
    return false;
}
function newSpamImg(){
	var src="share/antispam.php?id"+Math.random() ;
	 $('#spamImg').attr("src", src);
}
function getSpamImg(){
	var a = $.manageAjax({manageType: 'abortOld', maxReq: 2, blockSameRequest: true});
	var dpObj="workStat";
	
	$('#'+dpObj).html('Loading...');
	$url="share/user.php";
	$url=$url+"?action=getSpamImg";
	
	a.add({
		data: "&key="+ss_key,
		error: function() {
			$('#'+dpObj).html('Page Not Found !');
		},
		success: function(html) {
			$('#'+dpObj).html('');
			alert(html);
		},
		dataType: "html",
		type:"POST",
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		cache: false,
		url: $url
	});
}
function checkID(id) {
	if(id.length != 13) return false;
	for(i=0, sum=0; i < 12; i++)
	sum += parseFloat(id.charAt(i))*(13-i);
	if((11-sum%11)%10!=parseFloat(id.charAt(12))) return false;
	return true;
}

function showhideBillAdd(val){
	if(val==true)	$(".billAdd").css("display","none");
	else{
		try{
	  		$(".billAdd").css("display","table-cell");
	  	}
		catch(err){
	  		$(".billAdd").css("display","block");
	  }
	}
}
function setDistrictHidden(){

    //alert($("#cDistrict_del option[@selected]").text());

    $("#cProvince_del_hid").val($("#cProvince_del option[@selected]").text());
    $("#cDistrict_del_hid").val($("#cDistrict_del option[@selected]").text());
    $("#cSubDistrict_del_hid").val($("#cSubDistrict_del option[@selected]").text());

    $("#cProvince_bil_hid").val($("#cProvince_bil option[@selected]").text());
    $("#cDistrict_bil_hid").val($("#cDistrict_bil option[@selected]").text());
    $("#cSubDistrict_bil_hid").val($("#cSubDistrict_bil option[@selected]").text());
}

function getDistrict(loccode, type, hidden){
	postText="&loccode="+loccode;
	postText=postText+"&type="+type
	
	if(type=="cDistrict_del" || type=="cSubDistrict_del")$('#cSubDistrict_del').html("<option value=\"0\">---  โปรดเลือกตำบล ---</option>"); 
	if(type=="cDistrict_bil" || type=="cSubDistrict_bil") $('#cSubDistrict_bil').html("<option value=\"0\">---  โปรดเลือกตำบล ---</option>"); 

        if(hidden==1) setDistrictHidden();
	getForm1Val("getDistrict", "share/user.php", "#"+type, postText);
}

function changeBDSelect(month){
	var text="";
        var $i;
	if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
		for($i=1;$i<=31;$i++)  text=text+"<option value=\""+$i+"\">"+$i+"</option>";
	else if(month==2) 	
		for($i=1;$i<=29;$i++)  text=text+"<option value=\""+$i+"\">"+$i+"</option>";
	else 
		for($i=1;$i<=30;$i++)  text=text+"<option value=\""+$i+"\">"+$i+"</option>";
	$('#cBirthDay').html(text);
}
