﻿
/* user class */
function userClass(email, pass, checkUserFunc) {
    this._email = email;
    this._pass = pass;
    this._checkUserFunc = checkUserFunc;
    this.isExist = function() {
        var returnvalue;
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: this._checkUserFunc,
            dataType: "json",
            data: "{email:'" + this._email + "',pass:'" + this._pass + "'}",
            async: false,
            success: function(msg) {
                returnvalue = msg.d;
            },
            error: function(xmlHttpRequest, status, err) {
            }

        });
        return returnvalue;
    }
}

/* Email class */
function emailClass(e,conf, checkEmailDatabaseFunc) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    this._email = e;
    this._conf = conf;
    this._checkEmailDatabaseFunc = checkEmailDatabaseFunc;
    this.isEmpty = function() {
        return this._email == "";
    };
    this.isValid = function() {
        return filter.test(this._email);
    };
    this.isExist = function() {
        var returnvalue;
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: this._checkEmailDatabaseFunc,
            dataType: "json",
            data: "{email:'" + this._email + "'}",
            async: false,
            success: function(msg) {
                returnvalue = msg.d;
            },
            error: function(xmlHttpRequest, status, err) {
            }

        });
        return returnvalue;
    }
    
    this.isMatch = function() {
        return this._email== this._conf;
    }
}

/* password class */
function passClass(pass, conf, min) {
    this._pass = pass;
    this._conf = conf;
    this._min = min;
    this.isEmpty = function() {
        return this._pass == "";
    }
    this.isValid = function() {
        return this._pass.length >= min;
    }
    this.isMatch = function() {
        return pass == conf;
    }
}

/* captcha class */
function captchaClass(inputcode, code) {
    this._code = code;
    this._input = inputcode;
    this.isEmpty = function() {
        return this._input == "";
    }
    this.isMatch = function() {
        return this._input == this._code;
    }
}

/* register function to Date class */
Date.prototype.isDate = function(y, m, d) {
    Date.prototype._year = y;
    Date.prototype._month = m;
    Date.prototype._day = d;
    var date = new Date(y, m - 1, d);
    var convertedDate =
         "" + date.getFullYear() + (date.getMonth() + 1) + date.getDate();
    var givenDate = "" + y + m + d;
    return (givenDate == convertedDate);
}

/* birthday class */
function birthClass(b) {
    var reg = /^(\d{1,2})(\-|\/|\.)(\d{1,2})\2(\d{4})$/;
    this._b = b;
    this._match=reg.exec(this._b);
    this.isEmpty = function() {
        return this._b == "";
    }
    this.isRightFormat = function() {
        return reg.test(this._b);
    }
    this.isValid = function() {
        var d = new Date();
        return d.isDate(parseInt(this._match[4]), parseInt(this._match[3]), parseInt(this._match[1]));
    }

}

/*paging */
function paging(pageindex, pagesize,pageshow, totalitem) {
    this._pageindex = pageindex;
    this._pagesize = pagesize;
    this._totalitem = totalitem;
    this._pageshow = pageshow;
    this._pre = "<img src='/Res/Images/Icons/pre1.png' style='width:10px;height:10px;margin-top:10px'/>";
    this._next = "<img src='/Res/Images/Icons/next1.png' style='width:10px;height:10px;margin-top:2px'/>";
    this.Render = function() {
        var totalpage = parseInt(this._totalitem / this._pagesize) + ((this._totalitem % this._pagesize) != 0 ? 1 : 0);
        var half = pageshow / 2;
        var firstpage;
        var lastpage;
        if (totalpage <= pageshow) {
            firstpage = 1;
            lastpage = totalpage;
        }
        else {
            if (this._pageindex + half > totalpage) {
                lastpage = totalpage;
                firstpage = totalpage - pageshow < 0 ? 1 : totalpage - pageshow + 1;
            }
            else {
                if (this._pageindex - half < 0) {
                    firstpage = 1;
                    lastpage = pageshow;
                }
                else {
                    firstpage = this._pageindex - half;
                    lastpage = this._pageindex + half;
                }
            }

        }
        var str = "";
        if (this._pageindex != 1) {
            str += "<span class='first' onclick='GetPagingData(1);'>&laquo;</span>";
            str += "<span class='pre' onclick='GetPagingData(" + (this._pageindex - 1) + ");'>" + this._pre + "</span>";
        }
        for (var i = firstpage; i <= lastpage; i++) {
            if (i == this._pageindex) {
                str += "<span class='current' >" + i + "</span>";
            }
            else {
                str += "<span class='nav' onclick='GetPagingData(" + i + ");'>" + i + "</span>";
            }
        }
        if (this._pageindex < totalpage) {
            str += "<span class='next' onclick='GetPagingData(" + (this._pageindex + 1) + ");'>" + this._next + "</span>";
            str += "<span class='last' onclick='GetPagingData(" + totalpage + ");'>&raquo;</span>";
        }
        return str;
    }

}

//paging for image use for page with multi paging control
function paging1(pageindex, pagesize, pageshow, totalitem, id) {
    this._pageindex = pageindex;
    this._pagesize = pagesize;
    this._totalitem = totalitem;
    this._pageshow = pageshow;
    this.Render = function() {
        var totalpage = (this._totalitem / this._pagesize) + ((this._totalitem % this._pagesize) != 0 ? 1 : 0);
        var half = pageshow / 2;
        var firstpage;
        var lastpage;
        if (totalpage <= pageshow) {
            firstpage = 1;
            lastpage = totalpage;
        }
        else {
            if (this._pageindex + half > totalpage) {
                lastpage = totalpage;
                firstpage = totalpage - pageshow < 0 ? 1 : totalpage - pageshow + 1;
            }
            else {
                if (this._pageindex - half < 0) {
                    firstpage = 1;
                    lastpage = pageshow;
                }
                else {
                    firstpage = this._pageindex - half;
                    lastpage = this._pageindex + half;
                }
            }

        }
        var str = "";
        if (this._pageindex != 1) {
            str += "<span class='first' onclick='GetPagingData(" + id + ", 1);'>&laquo;</span>";
            str += "<span class='pre' onclick='GetPagingData(" + id + "," + (this._pageindex - 1) + ");'>pre</span>";
        }
        for (var i = firstpage; i <= lastpage; i++) {
            if (i == this._pageindex) {
                str += "<span class='current' >" + i + "</span>";
            }
            else {
                str += "<span class='nav' onclick='GetPagingData(" + id + "," + i + ");'>" + i + "</span>";
            }
        }
        if (this._pageindex < totalpage) {
            str += "<span class='next' onclick='GetPagingData(" + id + "," + (this._pageindex + 1) + ");'>next</span>";
            str += "<span class='last' onclick='GetPagingData(" + id + "," + totalpage + ");'>&raquo;</span>";
        }
        return str;
    }

}


/* format chuỗi về dạng tiền tệ */
function reFormatCurency(src, evt) {
    if ((evt.keyCode && evt.keyCode != 37 && evt.keyCode != 39) || (evt.which && evt.which != 37 && evt.which != 39)) {
        src.value = reFormat(src.value);
    }

}
function reFormat(str) {
    var newstr = "";
    var sep = ",";
    var count = 0;
    var c;
    var filter = /[0-9]/;
    for (var i = str.length; i > 0; i--) {
        c = str.substring(i - 1, i);
        if (filter.test(c)) {
            newstr = c + newstr;
            count += 1;
            if (count == 3) {
                count = 0;
                newstr = sep + newstr;
            }
        }
    }
    if (newstr.substring(0, 1) == sep) {
        newstr = newstr.substring(1, newstr.length);
    }

    return newstr;
}


function dateClass(from, current) {
    var reg = /^(\d{1,2})(\-|\/|\.)(\d{1,2})\2(\d{4})$/;
    this._from = from;
    this._current = current;
    this._matchFrom = reg.exec(this._from);
    this._matchCurr = reg.exec(this._current);
    this.isEmpty = function() {
        return this._from == "";
    }
    this.isRightFormat = function() {
        return reg.test(this._from);
    }
    this.isValid = function() {
        var d = new Date();
        return d.isDate(parseInt(this._matchFrom[4], 10), parseInt(this._matchFrom[3], 10), parseInt(this._matchFrom[1], 10));
    }
    this.isGreaterCurrent = function() {
    return Date.parse(this._matchFrom[4] + "/" + this._matchFrom[3] + "/" + this._matchFrom[1]) >= Date.parse(this._matchCurr[4] + "/" + this._matchCurr[3] + "/" + this._matchCurr[1]);
    }
}

function dateFromToClass(from, to) {
    var reg = /^(\d{1,2})(\-|\/|\.)(\d{1,2})\2(\d{4})$/;
    this._from = from;
    this._to = to;
    this._matchFrom = reg.exec(this._from);
    this._matchTo = reg.exec(this._to);
    this.isFromEmpty = function() {
        return this._from == "";
    }
    this.isEmpty = function() {
        return this._from == "" && this._to == "";
    }
    this.isFromValid = function() {
        return reg.test(this._from);
    }
    this.isRightFormat = function() {
        return reg.test(this._from) & reg.test(this._to);
    }
    this.isValid = function() {
        var d = new Date();
        return d.isDate(parseInt(this._matchFrom[4], 10), parseInt(this._matchFrom[3], 10), parseInt(this._matchFrom[1], 10)) & d.isDate(parseInt(this._matchTo[4], 10), parseInt(this._matchTo[3], 10), parseInt(this._matchTo[1], 10));
    }
    this.isOrder = function() {
        return Date.parse(this._matchFrom[4] + "/" + this._matchFrom[3] + "/" + this._matchFrom[1]) <= Date.parse(this._matchTo[4] + "/" + this._matchTo[3] + "/" + this._matchTo[1]);
    }
}

function processEnter(evt, obj) {
    if (evt.keyCode == 13) {
        obj.click();
        return false;
    }
    else return true;
}

function ShowRatingSmall(ratingNumber) {
    var strReview = "<div>";
    for (var i = 1; i <= 10; i++) {
        if (i % 2 == 1) {
            if (i <= ratingNumber) {
                strReview += "<div style='float:left;width:8px; height:16px; background:url(/Res/Images/Icons/bookbuy.png) no-repeat -524px -269px;'></div>";
            }
            else {
                strReview += "<div style='float:left;width:8px; height:16px; background:url(/Res/Images/Icons/bookbuy.png) no-repeat -524px -253px;'></div>";
            }
        }
        else {
            if (i <= ratingNumber) {
                strReview += "<div style='float:left;width:8px; height:16px; background:url(/Res/Images/Icons/bookbuy.png) no-repeat -532px -269px;'></div>";
            }
            else {
                strReview += "<div style='float:left;width:8px; height:16px; background:url(/Res/Images/Icons/bookbuy.png) no-repeat -532px -253px;'></div>";
            }
        }

    }
    strReview += "<div class='cl'/>";
    strReview += "</div>";
    
    return strReview;
}

function RegisterNewsLetter(email) {

    if (Page_ClientValidate()) {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Views/Shared/eStoreWS.asmx/RegisterNewsLetter",
            dataType: "json",
            data: "{email:'" + email + "'}",
            async: true,
            success: callbackInsertEmail,
            error: function(xmlHttpRequest, status, err) {
            }
        });
    }
}
/*check email*/

function checkEmail(strEmail)
{
	var cTemp = '';
	var i = 0;
	for(i=0;i<strEmail.length;i++)
	{
		cTemp = strEmail.charAt(i);
		if( ((cTemp <= 'z' && cTemp >= 'a') ||
			(cTemp <= 'Z' && cTemp >= 'A') ||
			(cTemp <= '9' && cTemp >= '0') ||
			(cTemp == '.') ||
			(cTemp == '@') ||
			(cTemp == '_') ||
			(cTemp == '-')) == false)
		{
			return false;
		}
	}
	
	cTemp = strEmail.charAt(0);
	if(((cTemp <= 'z' && cTemp >= 'a') || (cTemp <= 'Z' && cTemp >= 'A')) == false)
	{
		return false;
	}
	cTemp = strEmail.charAt(strEmail.length-1);
	if(cTemp == '@' || cTemp == '.')
	{
		return false;
	}
	var iTemp = strEmail.indexOf('@');
	if(iTemp == -1)
	{
		return false;
	}
	if(strEmail.indexOf("@",iTemp+1) != -1)
	{
		return false;
	}
	if(strEmail.indexOf(".") == -1)
	{
		return false;
	}
	if(strEmail.indexOf("..") != -1 || strEmail.indexOf(".@") != -1  || strEmail.indexOf("@.") != -1 )
	{
		return false;
	}
	return true;
	
}//End of checkEmail()
