//################### Date Functions #############

function Img_onerror(objImg, strPath)
{
	objImg.src=strPath;
}

function chkdate(mydate) {
//var dtFormat="MM/dd/yyyy";
//alert(dtFormat);
if(mydate.value=="" || mydate.value.length==0)
{
  return true;
}

var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var strSeparatorArray = new Array("-"," ","/");
var intElementNr;
var strMonthArray = new Array(12);

strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
strDate = mydate.value;

for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++)
{
	if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
	{
		strDateArray = strDate.split(strSeparatorArray[intElementNr]);
		if (strDateArray.length != 3)
		{
			alert('Two same separator should be in date');
			mydate.select();
			return false;
		}
		else
		{
		  if(dtFormat.substring(0,1)=='d' || dtFormat.substring(0,1)=='D')
		  {
			  strDay = strDateArray[0];
			  strMonth = strDateArray[1];
			  strYear = strDateArray[2];
			}
			else if(dtFormat.substring(0,1)=='m' || dtFormat.substring(0,1)=='M')
			{
			  strMonth = strDateArray[0];
			  strDay = strDateArray[1];
			  strYear = strDateArray[2];
			}
		}
		booFound = true;
	}
}

if (booFound == false)
{
	if (strDate.length==8) 
	{
	  if(dtFormat.substring(0,1)=='d' || dtFormat.substring(0,1)=='D')
		{
		  strDay = strDate.substr(0, 2);
		  strMonth = strDate.substr(2, 2);
		  strYear = strDate.substr(4);
		}
		else if(dtFormat.substring(0,1)=='m' || dtFormat.substring(0,1)=='M')
		{
		  strMonth = strDate.substr(0, 2);
		  strDay = strDate.substr(2, 2);
		  strYear = strDate.substr(4);
		}
	}
	else
	{
	  alert("Date should be 8 digit without separator");
	  mydate.select();
	  return false;
	}
}

if(strDay == "")
{
	alert("Please enter Day");
	mydate.select();
	return false;
}
else if((strDay != "") && (!validstr(strDay,4)))
{
	alert("Please enter valid Day");
	mydate.select();
	return false;
}

else if(strMonth == "")
{
	alert("Please enter Month");
	mydate.select();
	return false;
}
else if((strMonth != "") && (!validstr(strMonth,4)))
{
	alert("Please enter valid Month");
	mydate.select();
	return false;
}

else if(strYear == "")
	{
		alert("Please enter Year");
		mydate.select();
		return false;
	}
else if((strYear != "") && (!validstr(strYear,4)))
	{
		alert("Please enter valid Year");
		mydate.select();
		return false;
	}
else if (strYear.length != 4) 
{
	err=1;
	alert("Year should be 4 digit");
	mydate.select();
	return false;
}

intday = parseInt(strDay, 10);
intMonth = parseInt(strMonth, 10);
intYear = parseInt(strYear, 10);

//if (isNaN(intMonth))
//{
//	for (i = 0;i<12;i++)
//	{
//		if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase())
//		{
//			intMonth = i+1;
//			i = 12;
//		}
//	}
//  if (isNaN(intMonth))
//	{	
//		alert("Invalid Month in date");
//		return false;
//	 }
//}


if (intMonth>12 || intMonth<1)
{
		alert("Month should be between 1 and 12");
		mydate.select();
		return false;
}

if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
{
	alert("Day should be between 1 and 31");
	mydate.select();
	return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1))
{
	alert("Day should be between 1 and 30");
	mydate.select();
	return false;
}

if (intMonth == 2)
{
  var yyyyday=28;
  if (LeapYear(intYear) == true)
  {
    yyyyday = 29;
  }
  else
  {
    yyyyday = 28;
  }
  if ((intday<1) || (intday>yyyyday))
	{
		alert('Day should be between 1 and ' + yyyyday);
		mydate.select();
		return false;
	}
}
//alert(intday + " " + strMonthArray[intMonth-1] + " " + intYear);
//return intday + " " + strMonthArray[intMonth-1] + " " + intYear;

	  if(dtFormat.substring(0,1)=='d' || dtFormat.substring(0,1)=='D')
	  {
	    strDay = intday.toString();
	    strMonth = intMonth.toString();
	    if(strDay.length==1) 
	      {
	        strDay = "0" + strDay;
	      }
	    if(strMonth.length==1) 
	      {
	        strMonth = "0" + strMonth;
	      }
	    mydate.value=strDay + "/" + strMonth + "/" + intYear
	  }
	  else if(dtFormat.substring(0,1)=='m' || dtFormat.substring(0,1)=='M')
	  {
	    strDay = intday.toString();
	    strMonth = intMonth.toString();
	    if(strDay.length==1) 
	      {
	        strDay = "0" + strDay;
	      }
	    if(strMonth.length==1) 
	      {
	        strMonth = "0" + strMonth;
	      }
	    mydate.value=strMonth + "/" + strDay + "/" + intYear
	  }
	
return true;
}

function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}






//############## Common Functions ##########

function ShowLargeImage(strImagePath)
{
  if(strImagePath!="")
  {
    window.open("largeimage.aspx?imagepath=" + strImagePath,null,"height=475,width=500,top=100,left=100,status=yes,toolbar=no,menubar=no,location=no,resizable=no");
  }
}

function ConfirmAction(strMessage)
{
  if(strMessage=='' || strMessage==null) 
  {
    var a = confirm('Are you sure You wish to do this action?');
  }
  else
  {
    var a = confirm(strMessage);
  }

  if(!a)
  {
    return false;
  }
  
}
function CheckNumericValue(s)
{
    var isValid=true;
    
    if(IsNumeric(s.value)==false)
    {
      alert("Please enter numeric value");
      isValid=false;
    }
    
    else if(IsBlank(s.value))
    {
      alert('Qty should not be Empty!');
      isValid=false;
    }
    
    if(isValid==false)
    {
      window.event.keyCode=0;
      s.value="";
      s.focus();
      return false;
    }
    
}
function OpenNewWindow(strLink, opt)
{
  if(opt=="")
  {
    opt="height=500,width=800,top=80,left=100,center=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes";
  }
  window.open(strLink,null,opt);
}
function GoToLink(strLink, IsNewWindow)
{
  if(IsNewWindow==true)
  {
    window.open(strLink);
    return false;
  }
  else
  {
    window.location.href=strLink;
    return false;
  }
}

function ShowMessage(message)
{
  alert(message);
}

function validstr(str,opt)
{
	var checkOK;
	if(opt == 1)
		checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. ";
	else if(opt == 2)
		checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.&- ";
	else if(opt == 3)
		checkOK = "0123456789-+ ()";
	else if(opt == 4)
		checkOK = "0123456789";
	else if(opt == 5)
		checkOK = "Aa1234567890";
	else if(opt == 6)
		checkOK = "1234567890.";
	else if(opt == 7)
		checkOK = "1234567890.-";
		
	var checkStr = str;
	var allValid = true;
	if(str != ""){	
		for (i = 0;  i < checkStr.length;  i++){
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
				if (j == checkOK.length){
					allValid = false;
					break;
				}
		}
		if (!allValid)
			return false;
		return true;
	}
}
function LTrim(str) 
{
  for (var i=0; str.charAt(i)==" "; i++);
    return str.substring(i,str.length);
}

function RTrim(str) 
{
  for (var i=str.length-1; str.charAt(i)==" "; i--);
    return str.substring(0,i+1);
}

function Trim(str) 
{
  return LTrim(RTrim(str));
}

function IsBlank(val)
{
	if(Trim(val)=="" || val==null)
	{
		return true;
	}
	else
		return false;
}
function CheckMandatory(strObj,Message)
  {
    var obj;
    obj = document.getElementById(strObj);
    if(!IsManadatory(obj,Message))
      {
        return false;
      }
    else
      {
        return true;
      }
  }
function IsManadatory(field,str)
{
	var val=field.value;
	val=Trim(val);
	if(val=='')
	{
		alert(str + ' is Mandatory');
		field.focus();
		field.select();
		return false;
	}
	else
		return true;
}
function CheckMandatoryDrop(strObj,Message)
  {
    var obj;
    obj = document.getElementById(strObj);
    if(!IsManadatoryDrop(obj,Message))
      {
        return false;
      }
    else
      {
        return true;
      }
  }
function IsManadatoryDrop(field,str)
{
	var val=field.value;
	val=Trim(val);
	if(val=='-999999')
	{
		alert(str + ' is Mandatory');
		field.focus();
		return false;
	}
	else
		return true;
}


function IsDouble(obj,str)
{
  if((obj.value != "") && (!validstr(obj.value,6)))
  {
    alert(str + " should be Numeric");
    obj.focus();
    obj.select();
    return false;
  }
	else
		return true;
}

function IsSignDouble(obj,str)
{
  if((obj.value != "") && (!validstr(obj.value,7)))
  {
    alert(str + " should be Numeric");
    obj.focus();
    obj.select();
    return false;
  }
	else
		return true;
}


function IsGreaterThanZero(obj,str)
{
  if(obj.value<=0)
  {
    alert(str + " should be greater than zero");
    obj.select();
    obj.focus();
    return false;
  }
  else
  {
    return true;
  }
}


function IsGreaterThanEqualZero(obj,str)
{
  if(obj.value<0)
  {
    alert(str + " should be greater than or equal to zero");
    obj.select();
    obj.focus();
    return false;
  }
  else
  {
    return true;
  }
}

function IsFloat(obj)
{
	var val=obj.value;
	if(val.search(/^\d*.\d+$/)==-1)
	{
		return false;
	}
	else
		return true;
}

function IsDigit(c)
{
   return (((c >= '0') && (c <= '9')))
}

function IsNumeric(obj)
{ 
    var i;
    for (i = 0; i < obj.value.length; i++)
	{
	    var c = obj.value.charAt(i);
	    if (!(IsDigit(c)) )
	    {
	      alert("Please enter numeric value");
	      obj.focus();
		    return false;
	    }
    }
	return true;
}

function isDecimal()
{

            if ( (event.keyCode >= 48 && event.keyCode <= 58) || (event.keyCode==46) )
            {
                      
                    event.returnValue=true;

            }

            else
            {         
                    event.returnValue = false;

            }
}

function isDigitOnly()
{
  if ( (event.keyCode >= 48 && event.keyCode <= 57) )
  {
    event.returnValue=true;
  }
  else 
  {   
    event.returnValue = false;
  }
}

function IsEmailID(obj)
 {
	
		if (obj.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)+$/) == -1)
		{
		  alert("Enter valid email address");
		  obj.focus();
			return false;
		}
		else
			return true;
}

function SetFocus(field)
{
	
	if (IsControlExists('field'))
	{
		field.focus();
		return true;
	}
	else
		return false;
}

function IsControlExists(FormName,field)
{
	var Id=field.Id;
	if (Id==documents.FormName.getElementByID('field'))
	{
		return true;
	}else
		return false;
}

function selectFromCombo(FormName,Comboname,Item)
{  
	if(!IsControlExists(FormName,Comboname))
	{
		return false;
	}
	else
	{
		Comboname.options[Item].selected=true;
	}
}

function chkpassconfirm(Opass,Orepass)
{
  if(Opass.value!=Orepass.value)
	  {
		  alert('Confirm password does not match with password.');
		  Orepass.focus();
	    return false;
	  }
  else
	  {
		  return true;
	  }
}

function CheckInteger(obj)
{
  switch (window.event.keyCode)
  {
    case 48:
      break;
    case 49:
      break;
    case 49:
      break;
    case 50:
      break;
    case 51:
      break;
    case 52:
      break;
    case 53:
      break;
    case 54:
      break;
    case 55:
      break;
    case 56:
      break;
    case 57:
      break;
    default :
      event.returnValue=false;
  }
}
function CheckFloat(obj)
{
  switch (window.event.keyCode)
  {
    case 46:
      if(obj.value.indexOf(".")>=0)
        {
          event.returnValue=false;
        }
      break;
    default :
      return CheckInteger(obj);
  }
}

function CheckKey(obj)
{
  //alert(window.event.keyCode);
  switch (window.event.keyCode)
  {
    case 46:
      if(obj.value.indexOf(".")>=0)
        {
          event.returnValue=false;
        }
      break;
    case 48:
      break;
    case 49:
      break;
    case 49:
      break;
    case 50:
      break;
    case 51:
      break;
    case 52:
      break;
    case 53:
      break;
    case 54:
      break;
    case 55:
      break;
    case 56:
      break;
    case 57:
      break;
    default :
      event.returnValue=false;
  }
}
function CheckRadioButton(RadioButtonGroupName,Message)
{
  var IsChecked=false;
  for(var i=0; i<document.getElementsByName(RadioButtonGroupName).length;i++)
  {
    if(document.getElementsByName(RadioButtonGroupName).item(i).checked==true)
    {
      IsChecked=true;
      break;
    }
  }
  if(IsChecked==false) 
  {
    document.getElementsByName(RadioButtonGroupName).item(0).focus();
    alert('Please select ' + Message);
    return false;
  }
  return true;
}


