function compareTimeStamp(startTime, endTime, currentTime, compare) {
    var start = eval("document.all." + startTime);
    var end = eval("document.all." + endTime);
    if (end && start) {
        if (currentTime != null) {
            var current = eval("document.all." + currentTime);
            if (current)
                if (eval("" + new Date(start.value).getTime() + "<" + new Date(current.value).getTime())) {
                    alert(invalidStartTime);
                    return false;
                }
        }
        if (!eval("" + new Date(start.value).getTime() + compare + new Date(end.value).getTime())) {
            alert(starWithEndTimeInvalid);
            return false;
        }
        return true;
    }
    return false;
}

function showWeekday(objName, showType, cssClass)
{
    var weekday = weekdayArray;
    if (cssClass != null && cssClass != '')
        cssClass = " class='" + cssClass + "' ";
    else cssClass = "";
    if (showType == null || showType == '') {
        document.write("<select name=" + objName + cssClass + "   pasnarequired></select>");
        var weekCombo = eval("document.all." + objName);
        weekCombo.length++;
        weekCombo.options[0].text = '--------';
        weekCombo.options[0].value = "";
        for (var i = 0; i < weekday.length; ++i) {
            var endId = weekCombo.length++;
            weekCombo.options[endId].text = weekday[i];
            weekCombo.options[endId].value = (i + 6) % 7;
        }
        weekCombo.selectedIndex = 0;
    } else
        document.write(weekday[(objName * 1 + 1) % 7]);
}

function SetTimeStamp(objName, textBoxName, dateFormat, timeFormat, yearType , len, yfrom, yto)
{
    if(len==null|| len<=0) len=10;
    this.len = len;
    if(yearType==3) {
		this.fromYear = yfrom;
		this.toYear = yto;
	}
    if (dateFormat == null && timeFormat == null) return;
    this.dateFormat = dateFormat;
    this.timeFormat = timeFormat;

    if (yearType == null || yearType == '')
        this.yearType = 0;
    else
        this.yearType = yearType;
    this.textBox = eval("document.all['" + textBoxName + "']");

    var pasnarequired = "";
    if (this.textBox.getAttribute("pasnarequired") != null) {
        pasnarequired = "pasnarequired";
        this.textBox.removeAttribute("pasnarequired")
    }

    var className = this.textBox.className;
    className = (className == null || className == "")?"":("class=\"" + className + "\"");

    var disabled = "";
    if (this.textBox.getAttribute("disabled")) {
        disabled = "disabled";
        className = "";
    }


    if (this.dateFormat != null) {
        if (this.dateFormat == '') this.dateFormat = 'dmy';
        for (k = 0; k < 3; k++) {
            if (this.dateFormat.indexOf("d") == k) {
                document.write("<select name=day" + objName + " " + disabled + " onchange=\"" + objName + ".makeTimeStamp();\" " + pasnarequired + " " + className + "></select>");
                this.dayCombo = eval("document.all.day" + objName);
                this.makeDays = SetTimeStamp_makeDays;
                this.makeDays(1, 31);
            } else if (this.dateFormat.indexOf("m") == k) {
                document.write(" <select name=month" + objName + " " + disabled + " onchange=\"" + objName + ".makeTimeStamp();\" " + pasnarequired + " " + className + "></select>");
                this.monthCombo = eval("document.all.month" + objName);
                this.Month = monthArray
                this.makeMonths = SetTimeStamp_makeMonths;
                this.makeMonths(1, 12);
            } else if (this.dateFormat.indexOf("y") == k) {
                document.write(" <select name=year" + objName + " " + disabled + " onchange=\"" + objName + ".makeTimeStamp();\" " + pasnarequired + " " + className + "></select>");
                this.yearCombo = eval("document.all.year" + objName);
                this.makeYears = SetTimeStamp_makeYears;
                this.makeYearRange = SetTimeStamp_makeYearRange;
                this.makeYearRange()
                this.makeYears(this.fromYear, this.toYear);
            }
        }
        document.write("&nbsp;&nbsp;");
    }

    if (this.timeFormat != null) {
        if (this.timeFormat == '') this.timeFormat = 'hm';
        for (j = 0; j < 3; j++) {
            if (this.timeFormat.indexOf("h") == j) {
                document.write("<select name=hour" + objName + " " + disabled + " onchange=\"" + objName + ".makeTimeStamp();\" " + pasnarequired + " " + className + "></select>");
                this.hourCombo = eval("document.all.hour" + objName);
                this.makeHour = SetTimeStamp_makeHour;
                this.makeHour(0, 23);
            } else if (this.timeFormat.indexOf("m") == j) {
                document.write(" <select name=minute" + objName + " " + disabled + " onchange=\"" + objName + ".makeTimeStamp();\" " + pasnarequired + " " + className + "></select>");
                this.minuteCombo = eval("document.all.minute" + objName);
                this.makeMinute = SetTimeStamp_makeMinute;
                this.makeMinute(0, 59);
            } else if (this.timeFormat.indexOf("s") == j) {
                document.write("<select name=second" + objName + " " + disabled + " onchange=\"" + objName + ".makeTimeStamp();\" " + pasnarequired + " " + className + "></select>");
                this.secondCombo = eval("document.all.second" + objName);
                this.makeSecond = SetTimeStamp_makeSecond;
                this.makeSecond(0, 59);
            }
        }
    }

    this.makeTimeStamp = SetTimeStamp_makeTimeStamp;
    this.setTimeStamp = SetTimeStamp_setTimeStamp;
    this.setTimeStamp();

}
function SetTimeStamp_makeYears(fromYear, toYear)
{
    this.yearCombo.length++;
    this.yearCombo.options[0].text = yearCaption;
    this.yearCombo.options[0].value = "";
    for (i = fromYear; i <= toYear; ++i) {
        endId = this.yearCombo.length++;
        this.yearCombo.options[endId].text = numberToCurrentLang("" + i);
        this.yearCombo.options[endId].value = i;
    }
    this.yearCombo.selectedIndex = 0;
}

function SetTimeStamp_makeMonths(fromMonth, toMonth)
{
    this.monthCombo.length++;
    this.monthCombo.options[0].text = monthCaption;
    this.monthCombo.options[0].value = "";
    for (i = fromMonth; i <= toMonth; ++i) {
        endId = this.monthCombo.length++;
        this.monthCombo.options[endId].text = this.Month[i - 1];
        this.monthCombo.options[endId].value = i;
        if (this.monthCombo.options[endId].value < 10)
            this.monthCombo.options[endId].value = '0' + this.monthCombo.options[endId].value;
    }
    this.monthCombo.selectedIndex = 0;
}

function SetTimeStamp_makeDays(fromDay, toDay)
{
    this.dayCombo.length = 0;
    this.dayCombo.length++;
    this.dayCombo.options[0].text = dayCaption;
    this.dayCombo.options[0].value = "";
    for (i = fromDay; i <= toDay; ++i) {
        this.dayCombo.length++;
        this.dayCombo.options[1 + i - fromDay].text = numberToCurrentLang("" + i);
        this.dayCombo.options[1 + i - fromDay].value = i;
    }
    this.dayCombo.selectedIndex = 0;
}
function SetTimeStamp_makeYearRange() {
    var pcy = getCurrentYear();
    // cy = current year
    //0 : [cy-len,cy+len]
    //1 : [cy , cy + len]
    //2 : [cy - len, cy]
    if (this.yearType == 0 || !this.yearType) {
        this.fromYear = pcy - this.len;
        this.toYear = pcy + this.len;
    } else if (this.yearType == 1) {
        this.fromYear = pcy - this.len;
        this.toYear = pcy;
    } else if (this.yearType == 2) {
        this.fromYear = pcy;
        this.toYear = pcy + this.len;
    } else if (this.yearType == 3) {
        //this.fromYear = this.yfrom;
        //this.toYear = this.yto;
    }
}

function SetTimeStamp_makeHour(from, to)
{
    this.hourCombo.length++;
    this.hourCombo.options[0].text = hourCaption;
    this.hourCombo.options[0].value = "";
    for (i = from; i <= to; ++i)
    {
        endId = this.hourCombo.length++;
        this.hourCombo.options[endId].text = numberToCurrentLang("" + i);
        this.hourCombo.options[endId].value = i;
    }
    this.hourCombo.selectedIndex = 0;
}

function SetTimeStamp_makeMinute(from, to)
{
    this.minuteCombo.length++;
    this.minuteCombo.options[0].text = minuteCaption;
    this.minuteCombo.options[0].value = "";

    for (i = from; i <= to; i += 1)
    {
        endId = this.minuteCombo.length++;
        this.minuteCombo.options[endId].text = numberToCurrentLang("" + i);
        this.minuteCombo.options[endId].value = i;
    }
    this.minuteCombo.selectedIndex = 0;
}

function SetTimeStamp_makeSecond(from, to)
{
    this.secondCombo.length++;
    this.secondCombo.options[0].text = secondCaption;
    this.secondCombo.options[0].value = "";

    for (i = from; i <= to; i += 1)
    {
        endId = this.secondCombo.length++;
        this.secondCombo.options[endId].text = numberToCurrentLang("" + i);
        this.secondCombo.options[endId].value = i;
    }
    this.secondCombo.selectedIndex = 0;
}


function SetTimeStamp_makeTimeStamp()
{
    if (this.dateFormat == null && this.timeFormat == null)return false;

    var dateStr = "01/01/1900";
    var timeStr = "00:00:00";

    var dateElements = new Array(null, null, null);
    if (this.dateFormat != null) {
        if (this.yearCombo)
            dateElements[0] = new Number(this.yearCombo.value);
        if (this.monthCombo)
            dateElements[1] = new Number(this.monthCombo.value);
        if (this.dayCombo)
            dateElements[2] = new Number(this.dayCombo.value);
        if (dateElements[0] == '' || dateElements[1] == '' || dateElements[2] == '') {
            this.textBox.value = "";
            return false;
        }
        if (dateElements[0] == null) dateElements[0] = 1900;
        if (dateElements[1] == null) dateElements[1] = 01;
        if (dateElements[2] == null) dateElements[2] = 01;
        if (this.dayCombo) {
            if (dateElements[1] < 1)//month selected?
                this.dayCombo.disabled = true;
            else {
                this.dayCombo.disabled = false;
                maxMonthLen = 31;
                for (; maxMonthLen > 28;) {
                    testMonthValidate = jd_to_persian(persian_to_jd(dateElements[0], dateElements[1], maxMonthLen));
                    if (maxMonthLen == testMonthValidate[2]) {
                        this.makeDays(1, maxMonthLen);
                        if (dateElements[2] <= maxMonthLen)
                            this.dayCombo.selectedIndex = 1 + dateElements[2] - this.monthCombo.options[1].value;
                        else
                            this.dayCombo.selectedIndex = this.dayCombo.options.length - 1;
                        break;
                    } else
                        maxMonthLen--;

                }
            }
        }
        if(getCurrentLang()==fa)
		        dateElements = jd_to_gregorian(persian_to_jd(dateElements[0], dateElements[1], dateElements[2]));
        dateStr = dateElements[1] + '/' + dateElements[2] + '/' + dateElements[0];
    }
    var timeElements = new Array(null, null, null);
    if (this.timeFormat != null) {
        if (this.hourCombo && this.hourCombo.value != '')
            timeElements [0] = new Number(this.hourCombo.value);
        if (this.minuteCombo && this.minuteCombo.value != '')
            timeElements [1] = new Number(this.minuteCombo.value);
        if (this.secondCombo && this.secondCombo.value != '')
            timeElements [2] = new Number(this.secondCombo.value);
    }
    if (timeElements[0] == null && timeElements[1] == null && timeElements[2] == null && this.dateFormat == null) {
        this.textBox.value = "";
        return false;
    } else {
        if (timeElements[0] == null) timeElements[0] = 0;
        if (timeElements[1] == null) timeElements[1] = 0;
        if (timeElements[2] == null) timeElements[2] = 0;
        timeStr = " " + timeElements[0] + ":" + timeElements[1] + ":" + timeElements[2];
    }
    this.textBox.value = dateStr + " " + timeStr;
    return true;
}

function SetTimeStamp_setTimeStamp()
{
    result = this.textBox.value;
    if (result.indexOf(" ") != -1) {
        result = result.split(" ");


        result1 = result[0].split("/");
        result2 = result[1].split(":");

        timeElements = new Array(new Number(result2[0]), new Number(result2[1]), new Number(result2[2]));

        if (this.hourCombo)
            for (i = 0; i < this.hourCombo.length; ++i)
                if (this.hourCombo.options[i].value == timeElements [0]) {
                    this.hourCombo.selectedIndex = i;
                    break;
                }

        if (this.minuteCombo)
            for (i = 0; i < this.minuteCombo.length; ++i)
                if (this.minuteCombo.options[i].value == timeElements [1]) {
                    this.minuteCombo.selectedIndex = i;
                    break;
                }

        if (this.secondCombo)
            for (i = 0; i < this.secondCombo.length; ++i)
                if (this.secondCombo.options[i].value == timeElements [2]) {
                    this.secondCombo.selectedIndex = i;
                    break;
                }

        if (timeElements[0] == 0 && this.hourCombo)
            this.hourCombo.selectedIndex = 1;
        if (timeElements[1] == 0 && this.minuteCombo)
            this.minuteCombo.selectedIndex = 1;
        if (timeElements[2] == 0 && this.secondCombo)
            this.secondCombo.selectedIndex = 1;

    } else
        result1 = result.split("/");

     if(getCurrentLang()==fa)
		    dateElements = jd_to_persian(gregorian_to_jd(new Number(result1[2]), new Number(result1[0]), new Number(result1[1])));
	else if(getCurrentLang()==en)
		    dateElements = new Array(new Number(result1[2]), new Number(result1[0]), new Number(result1[1]));
    if (this.yearCombo)
        for (i = 0; i < this.yearCombo.length; ++i)
            if (this.yearCombo.options[i].value == dateElements[0]) {
                this.yearCombo.selectedIndex = i;
                break;
            }
    if (this.monthCombo)
        for (i = 0; i < this.monthCombo.length; ++i)
            if (this.monthCombo.options[i].value == dateElements[1]) {
                this.monthCombo.selectedIndex = i;
                break;
            }
    if (this.dayCombo)
        for (i = 0; i < this.dayCombo.length; ++i)
            if (this.dayCombo.options[i].value == dateElements[2]) {
                this.dayCombo.selectedIndex = i;
                break;
            }

}

