function checkTextInput(obj, originalText, action){
	if(action=="onblur"){
		if(obj.value==""){
			obj.value=originalText;
		}
	}else if(action=="onfocus"){
		if(obj.value==originalText){
			obj.value="";
		}
	}
}

function checkPasswordInput(textObj, passwordObj, originalText, action){
	if(action=="onblur"){
		if(document.getElementById(passwordObj).value==""||document.getElementById(passwordObj).value==originalText){
			document.getElementById(passwordObj).style.display="none";
			document.getElementById(textObj).style.display="block";
			document.getElementById(textObj).style.value=originalText;
		}
	}else if(action=="onfocus"){
		document.getElementById(textObj).style.display="none";
		document.getElementById(passwordObj).style.display="block";
		document.getElementById(passwordObj).focus();
	}
}


function resetAlertAdmin(obj){
	obj.style.border = "";
	obj.style.borderColor = "";
	obj.style.borderWidth = "";
}

function resetAlert(obj){
	obj.style.border = "";
	obj.style.borderColor = "";
	obj.style.borderWidth = "";
}

function setAlert(obj){
	obj.style.border = "solid";
	obj.style.borderColor = "red";
	obj.style.borderWidth = "2";
}

function doChangePage(obj, url, totalPage, parameter){
	//var pageNo = obj.value;
	if(obj.value!=""&&0<parseInt(obj.value)&&parseInt(obj.value)<=parseInt(totalPage)){
		var sURL = url+"?pageNo="+obj.value+parameter;
		location.href = sURL;
	}
}

function trimSpace(StringObject) {
//alert(StringObject.value);
	while(StringObject.value.charAt(0) == ' ' && StringObject.value.length > 0){
  			StringObject.value = StringObject.value.substr(1, StringObject.value.length -1);
  		}

    while(StringObject.value.charAt(StringObject.value.length -1) == ' ' && StringObject.value.length > 0){
      		StringObject.value = StringObject.value.substring(0, StringObject.value.length - 1);
  		}
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(strYear, strMonth, strDay){
	var daysInMonth = DaysArray(12);
	strYr=strYear;
	if(strYear=="0"||strMonth=="0"||strDay=="0"){return false;}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (month<1 || month>12){
		return false;
	}
	if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false;;
	}
	return true;
}

function isContain(strSource, strReference){
	var result = true;
	for(var x=0;x<strSource.length;x++){
		if(strReference.indexOf(strSource.charAt(x))==-1){
			result = false;
		}
	}
	return result;
}

function getRadioValue(radioObj){
  	var result="";
  	for(x=0;x<radioObj.length;x++){
  		if(radioObj[x].checked){
  			result=radioObj[x].value;
  		}
  	}
  	return result;
}

function extractNumber(obj, decimalPlaces, allowNegative)
{
	var temp = obj.value;
	
	// avoid changing things if already formatted correctly
	var reg0Str = '[0-9]*';
	if (decimalPlaces > 0) {
		reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
	} else if (decimalPlaces < 0) {
		reg0Str += '\\.?[0-9]*';
	}
	reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
	reg0Str = reg0Str + '$';
	var reg0 = new RegExp(reg0Str);
	if (reg0.test(temp)) return true;

	// first replace all non numbers
	var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
	var reg1 = new RegExp(reg1Str, 'g');
	temp = temp.replace(reg1, '');

	if (allowNegative) {
		// replace extra negative
		var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
		var reg2 = /-/g;
		temp = temp.replace(reg2, '');
		if (hasNegative) temp = '-' + temp;
	}
	
	if (decimalPlaces != 0) {
		var reg3 = /\./g;
		var reg3Array = reg3.exec(temp);
		if (reg3Array != null) {
			// keep only first occurrence of .
			//  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
			var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
			reg3Right = reg3Right.replace(reg3, '');
			reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
			temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
		}
	}
	
	obj.value = temp;
}

function blockNonNumbers(obj, e, allowDecimal, allowNegative)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	if (isNaN(key)) return true;
	
	keychar = String.fromCharCode(key);
	
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl)
	{
		return true;
	}

	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
}
  
 //**********************************************************
 var checkquantity_status = true;
 function checkQuantity(obj, max, min)
{
	var temp = obj.value;
	var id= obj.id;
	id=id.replace("quantity_", 'message_');
	
	if(obj.value>max || obj.value<min){
		 var checkquantity_status = false;
		document.getElementById(id).style.display='block';
		
	}else{
		 var checkquantity_status = true;
		document.getElementById(id).style.display='none';
	}
	
	obj.value = temp;
}

