// JavaScript Document

function trim(st)
{
	if(st.length > 0)
	{
		re = / +$/g; 
		newval = st.replace(re,"");
		re = /^ +/g;
		newvala = newval.replace(re,"");
		return newvala;
	}
	return "";
}

function IsDate(mMonth, mDay, mYear)
{
	mMonth = parseInt(mMonth);
	mDay = parseInt(mDay);
	mYear = parseInt(mYear);
	var mLeap = false;
	if ( mYear%4 == 0 )
		mLeap = true;
	if ( mMonth == 1 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 2 )
	{
		if ( mLeap )
		{
			if ( mDay < 1 || mDay > 29 )
				return false;
			else
				return true;
		}
		else
		{
			if ( mDay < 1 || mDay > 28 )
				return false;
			else
				return true;
		}
	}
	else if ( mMonth == 3 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 4 )
	{
		if ( mDay < 1 || mDay > 30 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 5 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 6 )
	{
		if ( mDay < 1 || mDay > 30 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 7 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 8 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 9 )
	{
		if ( mDay < 1 || mDay > 30 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 10 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 11 )
	{
		if ( mDay < 1 || mDay > 30 )
			return false;
		else
			return true;
	}
	else if ( mMonth == 12 )
	{
		if ( mDay < 1 || mDay > 31 )
			return false;
		else
			return true;
	}
	else
	{
		return false;
	}
}

// this function opens the popoup window
function WinOpenS(winname,w,h,leftcor,topcor,pname)
{
	window.open( pname , winname, "scrollbars=1,statusbar=no,menubar=no,toolbar=no,resize=null,top=" + topcor + ",left=" + leftcor + ",width=" + w + ",height="+h ).focus();
}


function IsEmailValid(email)
{
	email = trim(email);
	valid_chars_for_email = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_@.";
	if ( email == "" )			// pending Email validation
	{
		return false;
	}
	for ( i=0; i < email.length; i++ )
	{
		ch =  "" + email.charAt(i);
		num = valid_chars_for_email.indexOf(ch);
		if ( num == -1 )
		{
			return false;
		}
	}
	if ( email.indexOf('@') == -1 )
	{
		return false;
	}
	if ( email.indexOf('.') == -1 )
	{
		return false;
	}
	return true;
}

function CompareDate(day1, month1, year1, day2, month2, year2)
{
	day1 = day1 * 1;
	month1 = month1 * 1;
	year1 = year1 * 1;
	day2 = day2 * 1;
	month2 = month2 * 1;
	year2 = year2 * 1;
	if ( (year2 == year1) && (month2 == month1) && (day2 == day1) )
	{
		return 0;			// Equal
	}
	else if ( ((year2 > year1)) || ( (year2 == year1) && (month2 > month1) ) || ( (year2 == year1) && (month2 == month1) && (day2 > day1)) )
	{
		return 2;			//  The second date is greater
	}
	else
	{
		return 1;			// The First Date is greater
	}
}

function ValidatePage( ctrl )
{
	var mVal = ctrl.value;
	mVal = trim(mVal);

	//websiteurlRegEx = /^(file|http|https):\/\/\S+\.(com|net|org|info|biz|ws|us|tv|cc|html|htm|php|php3|phtml)$/i;
	//websiteurlRegEx = /^((file|http|https):\/\/)?www\.\S+\.\S+$/i;
	websiteurlRegEx = /^((file|http|https):\/\/)?(www\.)?\S+\.\S+$/i;
	
	//alert(websiteurlRegEx + "\n\n" + mVal);

    if(mVal.search(websiteurlRegEx) == -1)
    {
		return false;
    }
	
	return true;
}
function ValidateEmailAddress( ctrl )
{
    var demovalue = ctrl.value;
	demovalue = trim(demovalue);
	
	if(demovalue == "")
    {
    	return true;
    }

	emailaddressRegEx = /^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$/;

    if(demovalue.search(emailaddressRegEx) == -1)
    {
		return false;
    }
    
    return true;
}

function ValidateFile( ctrl )
{
	var mVal = ctrl.value;
	mVal = trim(mVal);

	filepathRegEx = /^(\\\\\S+|\S+:)[^\?\*\/:'"<>|]+$/;
	//	Network File Path 	  :- \\maneet	\maneet\work\websites\idealstor\fact_Sheet.pdf
	//	Local Drive File Path :- C:       	\Documents and Settings\Administrator\Desktop\Desktop_16June05\ais176v.jpg		
	
    if(mVal.search(filepathRegEx) == -1)
    {
		return false;
    }
	
	return true;
}

function IsPhoneValid(phone)
{
    phone = trim(phone);
	
	if(phone == "")
    {
    	return false;
    }
	
	phoneRegEx = /^[0-9]+-[0-9]+-[0-9]+$/;
    
    if(phone.search(phoneRegEx) == -1)
    {
		return false;
    }
    
    return true;
}

function IsUsernameValid(username)
{
    username = trim(username);
	
	if(username == "")
    {
    	return false;
    }
    
    nameRegEx = /^[0-9a-zA-Z_]+$/;

    if(username.search(nameRegEx) == -1)
    {
    	return false;
    }
    
    return true;
}

function IsMobileValid(mobile)
{
    mobile = trim(mobile);
	
	if(mobile == "")
    {
    	return false;
    }
	
	mobileRegEx = /^[0-9]+-[0-9]+$/;
    
    if(mobile.search(mobileRegEx) == -1)
    {
		return false;
    }
    
    return true;
}

function IsPasswordValid(password)
{
    password = trim(password);
	
	if(password == "")
    {
    	return false;
    }

    if(password.length < 6 || password.length > 20)
    {
		return false;
    }
    
    return true;
}


function ValidateImage(ctrl)
{
 if (ctrl.value.length)
 {
  var iPos = ctrl.value.lastIndexOf(".")
  var sExt = ctrl.value.substring(iPos);
  
  if((sExt.toUpperCase()=='.JPEG') || (sExt.toUpperCase()=='.JPG') || 
(sExt.toUpperCase()=='.GIF') || (sExt.toUpperCase()=='.BMP') || 
(sExt.toUpperCase()=='.PNG') )
  {
   return true;
  }
  else
  {
   alert("Please enter valid image.");
   ctrl.focus();
   ctrl.select();
   return false;
  }                       
 }
 else
 {
  alert("Please select the file to be uploaded");
  ctrl.focus();
  ctrl.select();
  return false;
 }
}