var check= /[0-9]/;
var remail=/^([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+\.+[a-zA-Z0-9-]+|[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+\.+[a-zA-Z0-9-]+\.+[a-zA-Z0-9-]+)$/;
var checkprice= /^(0|-?[1-9][0-9]*(\.[0-9]{1,2})?)$/;
var alpha= /^([a-zA-Z])$/;
var numbers= /^([0-9])$/;
var alphanum= /^([a-zA-Z0-9])$/;

function checkIfIsNo(fieldvalue) {
	if(check.test(fieldvalue.charAt(0))){
		return true;
	} else {
		return false;		
	}
}

function checkIfIsPrice(fieldvalue) {
	if(checkprice.test(fieldvalue))
		return false; 
	else
		return true;
}

function checkEmail(fieldvalue) {
	if(remail.test(fieldvalue))
		return false;
	else
		return true;		
}

function alpha_numeric() {
	if(event.keyCode <48 || event.keyCode > 57)
		if(event.keyCode <65 || event.keyCode >90)
			if(event.keyCode <97 || event.keyCode >122)
				event.returnValue=false;
}

function only_alpha() {
	if(event.keyCode <65 || event.keyCode >90)
		if(event.keyCode <97 || event.keyCode >122)
			if(event.keyCode != 32)
				event.returnValue=false;
}

function only_numeric() {
	if(event.keyCode <48 || event.keyCode > 57) {
		event.returnValue=false;
	} else {
		event.returnValue = true;
	}
}

function only_price() {
//	if(event.keyCode <48 || event.keyCode > 57)
//		if(event.keyCode !=46)
//			event.returnValue=false;
	event.returnValue = true;
}

function alpha_numeric_dot_space() {
//		if(event.keyCode <48 || event.keyCode > 57)
//		if(event.keyCode <65 || event.keyCode >90)
//			if(event.keyCode <97 || event.keyCode >122)
//			if(event.keyCode != 32)
//			if(event.keyCode != 46)
//				event.returnValue=false;
	event.returnValue = true;
}


//             newly written Javascript validations

    function strltrim() {
      return this.replace(/^\s+/,'');
    }
        
		function strrtrim() {
			return this.replace(/\s+$/,'');
    }
        
		function strtrim() {
		 return this.replace(/^\s+/,'').replace(/\s+$/,'');
    }

		String.prototype.ltrim = strltrim;
		String.prototype.rtrim = strrtrim;
		String.prototype.trim = strtrim;

		function is_match(obj1, fieldvalue1, obj2, fieldvalue2) {

			var val_obj1 = new String(fieldvalue1);
			fieldvalue1 = val_obj1.trim();

			obj1.value = fieldvalue1;

			var val_obj2 = new String(fieldvalue2);
			fieldvalue2 = val_obj2.trim();

			obj2.value = fieldvalue2;

			if(fieldvalue1 != '' || fieldvalue2 != '') {
				if(fieldvalue1 == fieldvalue2) {
					return false;
				} else {
					return true;
				}
			}
	}

		function is_price(obj, fieldvalue) {		

			var val_obj = new String(fieldvalue);
			fieldvalue = val_obj.trim();

			obj.value = fieldvalue;

			if(fieldvalue != '') {

				var checkPrice = new RegExp("^(0|-?[1-9]+[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,2})?$","g");
				if(checkPrice.test(fieldvalue))
					return false;
				else 
					return true;
			}
		}

		function is_url(obj, fieldvalue) {						

			/*var val_obj = new String(fieldvalue);
			fieldvalue = val_obj.trim();

			obj.value = fieldvalue;

			if(fieldvalue != '') {
			var checkURL = new RegExp("^(http:)(//)((www.|in.)*)+([_a-z0-9-])", "g");
				
				fieldvalue = fieldvalue.toLowerCase();
				if(checkURL.test(fieldvalue))
					return false;
				else 
					return true;
			}*/
			return false;
		}

		function is_number(obj, fieldvalue) {									
			
			var val_obj = new String(fieldvalue);
			fieldvalue = val_obj.trim();

			obj.value = fieldvalue;			

			if(isNaN(fieldvalue)) 
				return true;
			else 				
				return false;
		}

		function is_email(obj, fieldvalue) {							

			var val_obj = new String(fieldvalue);
			fieldvalue = val_obj.trim();

			obj.value = fieldvalue;

			if(fieldvalue != '') {
			//var remail = new RegExp("^[_.0-9a-z]+@([0-9a-z][_0-9a-z\.\-]+)\.([a-z]{2,4}$)", "g");
			var remail = new RegExp("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$", "g");
			//alert(remail);
			fieldvalue = fieldvalue.toLowerCase();
			
			if(remail.test(fieldvalue))
				return false;
			else
				return true;
			}
		}

		function is_empty(obj, val) {
			var val_obj = new String(val);
			val = val_obj.trim();

			obj.value = val;
			
			if(val == '' || val.length ==0) {
				return true;
			} else {
				return false;
			}
		}


		function is_valid_image(val) {

			var file_ext = val.slice((val.length)-4);
			if(file_ext != '.bmp' && file_ext != '.gif' && file_ext != '.jpeg' && file_ext != '.jpg') 
				return true;
			else 
				return false;
		}

		function is_image(obj, val, flag) {

			var val_obj = new String(val);
			
			val = val_obj.trim();
			
			obj.value = val;

			if(flag == 1) {
				if(is_empty(obj, val)) {
					return true;
				} else if(is_valid_image(val)) {
					return true;
				}
			} else if(flag == 0) {
				if(!is_empty(obj, val)) {
					if(is_valid_image(val))
						return true;
					else 
						return false;
				} 
			}
		}

		function confirm_delete(p_url) {

			if(confirm("You want to delete. Are u sure.?")) {				
				window.location.href = p_url;
			}
		}

	function radio_selected(formNm,ElemNm) {
		var j = 0;
		var radio_buttons = new Array();
		var the_form = formNm;
		for(var i=0; i<the_form.length; i++){
			var temp = the_form.elements[i].type;
			if((temp == "radio") && (the_form.elements[i].checked)) { 
				if(ElemNm == the_form.elements[i].name)
					j++; 
				}
			}
		if(j <= 0)
			return 0;
		else
			return 1;
		}


function openImage(urlV){
	imgx = new Image();
	imgx.src = urlV;
	var width = imgx.width;
	var height = imgx.height;
	if(width == 0)
		width = 300;
	width = width+50;

	if(height == 0)
		height = 300;
	height = height+50;

	nm = window.open('','imagePopup','width='+width+',height='+height+',resizable=yes,scrollbars=yes,titlebar=no');
	nm.document.writeln('<img src='+urlV+'>');
	nm.document.writeln('<script language=javascript>');
	nm.document.writeln('self.resizeTo('+width+','+height+')</script>');
}

function hasOnlyNumeric(fieldname , fieldvalue) {
	var str = fieldvalue;
	var i = 0;
	while(i < str.length) {
		if(!((str.charAt(i) >= "0") && (str.charAt(i) <= "9")))	{
			//alert(fieldname+' can contain only numeric value');
			return false;
		} else {
			i = i + 1;
		}
	}
	return true;
}

function hasOnlyNumericwithPlusHyphenbrackets(fieldname , fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	if(str == '+'){
		alert('Please Enter Valid '+fieldname);
		return false;
	}
	while(i < str.length)
	{
		if(!(((((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))||(str.charAt(i) == '+')||(str.charAt(i) == '-')||(str.charAt(i) == '(')||(str.charAt(i) == ')')))))
		{
			alert(fieldname+' contains only alphanumeric values \n\nValid Characters :(0 to 9) and (+,-) ');
			return false;
		}
		i++;
	}
	return true;
}
function hasOnlyAlphabetsAndSpace(fieldname , fieldvalue) {
		var str = fieldvalue;
		i = 0;
		while(i < str.length) {
			if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') || (str.charAt(i) == " ") ))) {
				alert(fieldname+' can contain only alphabets\n\nValid Characters :(A to Z),(a to z),whitespace');
				return false;
			}
			i++;
		}
		return true;
}

function hasOnlyAlphaNumeric(fieldname,fieldvalue) {
	var str = fieldvalue;
	i = 0;
	while(i < str.length) {
		if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))|| ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') )||(str.charAt(i) <= ' ')))
		{
			alert(fieldname+' contains only alphanumeric values \n\nValid Characters :(A to Z),(a to z) and (0 to 9) ');
			return false;
		}
		i++;
	}
	return true;
}

function hasOnlyAlphabetsAndSpaceDot(fieldname , fieldvalue) {
		var str = fieldvalue;
		i = 0;
		while(i < str.length) {
			if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') || (str.charAt(i) == " ") || (str.charAt(i) == ".")))) {
				alert(fieldname+' can contain only alphabets\n\nValid Characters :(A to Z),(a to z),whitespace and dot');
				return false;
			}
			i++;
		}
		return true;
}
function hasOnlyNumericwithDotPlusHyphen(fieldname , fieldvalue) {
	var str = fieldvalue;
	i = 0;
	if(str == '+'){
		alert('Please Enter Valid '+fieldname);
		return false;
	}
	while(i < str.length)
	{
		if(!(((((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))||(str.charAt(i) == '.')||(str.charAt(i) == '+')||(str.charAt(i) == '-')))))
		{
			alert(fieldname+' contains only alphanumeric values \n\nValid Characters :(0 to 9) and (+,-,.) ');
			return false;
		}
		i++;
	}
	return true;
}
