// XMLHttp
var objXMLHttp = getAjaxObj();

function getAjaxObj()
{
	// Mozilla
	if (window.XMLHttpRequest)
		return (new XMLHttpRequest());
	// IE
	if (window.ActiveXObject)
		return (new ActiveXObject("Microsoft.XMLHTTP"));

	return false;
}

// DOM
function obj(objName)
{
	return document.getElementById(objName);
}

function trim(str)
{
	return str.replace(/^\s+|\s+$/g, "");
}

var sAlertColor = "#ffff00";
var sNormalColor = "#ffffff";

function bIsNumeric(sText)
{
	var bRet = true;

	for(var i=0; i < sText.length; i++)
	{
		if(sText.charAt(i) > "9" || sText.charAt(i) <"0")
		{
			bRet = false;
			break;
		}	
	}		

	return bRet;
}

function check_field(oField, sText)
{
	var sValue = oField.value;
	if (trim(sValue) == "")
	{
		alert(sText);
		oField.style.backgroundColor = sAlertColor;
		oField.focus();
		return false;
	}
	else
	{
		oField.style.backgroundColor = sNormalColor;
		return true;
	}
}

function check_email(oField, sText)
{
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
	var sValue = oField.value;
	if (!emailFilter.test(sValue))
	{
		oField.style.backgroundColor = sAlertColor;
		oField.focus();
		alert(sText);
		return false;
	}
	else
	{
		oField.style.backgroundColor = sNormalColor;
		return true;
	}

}

function check_phone(oField, sText)
{
	var phoneFilter = /^\d\d\d-\d\d\d-\d\d\d\d$/;
	var sValue = oField.value;
	if (!phoneFilter.test(sValue))
	{
		oField.style.backgroundColor = sAlertColor;
		oField.focus();
		alert(sText);
		return false;
	}
	else
	{
		oField.style.backgroundColor = sNormalColor;
		return true;
	}

}

function check_select(oSelect, sText)
{
	if (oSelect.selectedIndex == 0)
	{
		alert(sText);
		oSelect.style.backgroundColor = sAlertColor;
		oSelect.focus();
		return false;
	}
	else
	{
		oSelect.style.backgroundColor = sNormalColor;
		return true;
	}
}

function check_box(oCheckBox, sText)
{
	if (!oCheckBox.checked)
	{
		alert(sText);
		oCheckBox.style.backgroundColor = sAlertColor;
		oCheckBox.focus();
		return false;
	}
	else
	{
		oCheckBox.style.backgroundColor = sNormalColor;
		return true;
	}
}

function check_number(oField, sText)
{
	var sValue = oField.value;
	if (!bIsNumeric(sValue))
	{
		alert(sText);
		oField.style.backgroundColor = sAlertColor;
		oField.focus();
		return false;
	}
	else
	{
		oField.style.backgroundColor = sNormalColor;
		return true;
	}
}

function check_number_range(oField, sText, nMin, nMax)
{
	var nValue = parseInt(oField.value, 10);
	if (nValue < nMin || nValue > nMax)
	{
		var sDisplayText = sText;
		sDisplayText = sDisplayText.replace("MIN_VAL", nMin);
		sDisplayText = sDisplayText.replace("MAX_VAL", nMax);
		alert(sDisplayText);
		oField.style.backgroundColor = sAlertColor;
		oField.focus();
		return false;
	}
	else
	{
		oField.style.backgroundColor = sNormalColor;
		return true;
	}
}

function check_number_range_card_year(oField, sText)
{
	var nValue = parseInt(oField.value, 10);
	var dDate = new Date();
	var nMin = dDate.getFullYear();
	var nMax = nMin + 5;
	if (nValue < nMin || nValue > nMax)
	{
		var sDisplayText = sText;
		sDisplayText = sDisplayText.replace("MIN_VAL", nMin);
		sDisplayText = sDisplayText.replace("MAX_VAL", nMax);
		alert(sDisplayText);
		oField.style.backgroundColor = sAlertColor;
		oField.focus();
		return false;
	}
	else
	{
		oField.style.backgroundColor = sNormalColor;
		return true;
	}
}

// dd//mm/yyyy
function bIsValidDate(p_sText)
{
	var bRet = true;

	var i;
	var val = p_sText+"";
	var dateparts;
	var intYear, intMonth, intDay;

	var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var nDays;

	dateparts = val.split("/");

	if(dateparts.length != 3)
		bRet = false;
	else
	{
		intDay = dateparts[0];
		intMonth = dateparts[1];
		intYear = dateparts[2];

		if(intYear.length != 4 || intMonth.length != 2 || intDay.length != 2)
			bRet = false;
		else if(!bIsNumeric(intYear) || !bIsNumeric(intMonth) || !bIsNumeric(intDay))
			bRet = false;
		else
		{
			intDay = parseInt(intDay, 10);
			intMonth = parseInt(intMonth, 10);
			intYear = parseInt(intYear, 10);

			if(intMonth < 1 || intMonth > 12 || intDay < 1 || intDay > 31)
				bRet = false;
			else
			{
				if (intMonth==2) //february
					nDays = ((intYear%4==0) && (!(intYear%100==0) || (intYear%400==0)))?29:28;
				else
					nDays = daysInMonth[intMonth-1];

				if(intDay > nDays)
					bRet = false;
			}				
		}
	}

	return bRet;	
}

function check_date(oD, oM, oY, sText)
{
	var sDate = oD.options[oD.selectedIndex].value + "/" + oM.options[oM.selectedIndex].value + "/" + oY.options[oY.selectedIndex].value;

	if(!bIsValidDate(sDate))
	{
		alert(sText);
		return false;
	}
	return true;
}

function check_password(oField, sText)
{
	var nLength = (oField.value + "").length;
	if (nLength < 4 || nLength > 6)
	{
		alert(sText);
		oField.style.backgroundColor = sAlertColor;
		oField.focus();
		return false;
	}
	else
	{
		oField.style.backgroundColor = sNormalColor;
		return true;
	}
}


