function doFormAction(formName, withoutPrepareFlag)
{
    var thisForm = eval("document." + formName);
    if (withoutPrepareFlag)
    {
        thisForm.submit();
        return true;
    }
    else if (prepareForSubmitForm())
    {
        thisForm.submit();
        return true;
    }
    else
    {
        return false;
    }
}

function doFormActionWithAction(formName, action, withoutPrepareFlag)
{
    var thisForm = eval("document." + formName);
    thisForm.action = action;
    if (withoutPrepareFlag)
    {
        thisForm.submit();
        return true;
    }
    else if (prepareForSubmitForm())
    {
        thisForm.submit();
        return true;
    }
    else
    {
        return false;
    }
}

function doGuestLoginAction(formName)
{
    var thisForm = eval("document." + formName);
    if (trim(thisForm.invitationCode.value) == "")
    {
        alert("Please Enter Your Invitation Code.");
        return false;
    }
    doFormAction(formName, true);
    return true;
}

function trim(str)
{
    var tmp = ltrim(str);
    return rtrim(tmp);
}

function rtrim(str)
{
    while(str.charAt((str.length -1))==" ")
    {
        str = str.substring(0,str.length-1);
    }
    return str;
}


function ltrim(str)
{
    while(str.charAt(0)==" ")
    {
        str = str.replace(str.charAt(0),"");
    }
    return str;
}

function emptySelectList(select)
{
	for (m=select.options.length-1;m>0;m--)
	{
		select.options[m]=null;
	}
	select.options[0].selected=true;
}

/*check date as format MM/DD/YYYY*/
function checkDate(dateString)
{
    var index = dateString.indexOf("/");
    if (index == -1)
    {
        
        return false;
    }
    
    var month = parseInt(dateString.substring(0, index));
    if (isNaN(month) || month < 1 || month > 12)
    {
        return false;
    }
    
    var tmpStr = dateString.substring(index+1);
    index = tmpStr.indexOf("/");
    if (index == -1)
    {
        return false;
    }
    
    var day = parseInt(tmpStr.substring(0, index));
    var year = parseInt(tmpStr.substring(index+1));
    if (isNaN(day) || day < 1)
    {
        return false;
    }
    if (isNaN(year) || year < 1900)
    {
        return false;
    }
    
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
    {
        if (day > 31)
        {
            return false;
        }
    }
    else if (month ==4 || month == 6 || month == 9 || month == 11)
    {
        if (day > 30)
        {
            return false;
        }
    }
    else if (year % 4 == 0)
    {
        if (day > 29)
        {
            return false;
        }
    }
    else
    {
        if (day > 28)
        {
            return false;
        }
    }

    return true;
}

/*check date from format MM/DD/YYYY to int YYYYMMDD, this method should be called after checkDate*/
function convertDate(dateString)
{
    var index = dateString.indexOf("/");
    if (index == -1)
    {
        return 0;
    }
    
    var month = parseInt(dateString.substring(0, index));
    if (isNaN(month) || month < 1 || month > 12)
    {
        return 0;
    }
    
    var tmpStr = dateString.substring(index+1);
    index = tmpStr.indexOf("/");
    if (index == -1)
    {
        return 0;
    }
    
    var day = parseInt(tmpStr.substring(0, index));
    var year = parseInt(tmpStr.substring(index+1));
    if (isNaN(day) || day < 1)
    {
        return 0;
    }
    if (isNaN(year) || year < 1900)
    {
        return 0;
    }
    
    return year*10000+month*100+day;
}

/*compare 2 date string as format MM/DD/YYYY, return -1 if dateString1 < dateString2, 0 if dateString1 = dateString2, 1 if dateString1 >dateString2*/
function compareDate(dateString1, dateString2, emptyIsLargest)
{
    var date1 = convertDate(dateString1);
    var date2 = convertDate(dateString2);
    
    if (date1 > 0 && date2 > 0)
    {
        if (date1 < date2)
        {
            return -1;
        }
        else if (date1 == date2)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
    else if (date1 == 0 && date2 > 0)
    {
        if (emptyIsLargest)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
    else if (date1 > 0 && date2 == 0)
    {
        if (emptyIsLargest)
        {
            return -1;
        }
        else
        {
            return 1;
        }
    }
    else
    {
        return 0;
    }
}

function checkQuantity(quanStr, allowZero)
{
    var quan = parseInt(quanStr);
    if (isNaN(quan))
    {
        alert("Quantity is required and is a number.");
        return false;
    }
    else if (allowZero)
    {
        if (quan < 0)
        {
            alert("Quantity should not be negative.");
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        if (quan <= 0)
        {
            alert("Quantity should be positive.");
            return false;
        }
        else
        {
            return true;
        }
    }
}
