var dtCh= "/";
var minYear=1900;
var maxYear=2100;
/* Function to open pop up for image */
function imagepopup(url)
{
	var newwindow='';
	newwindow=window.open(url,'View_Image','height=300,width=500, left=150,top=120');
}
function ImageDeletion()
{
	var action = confirm(MSG_IMAGE_DELETION);
	if(action)
	{
		document.frm.submit();
	}
	else
	{
		document.frm.deleteImage.value="";
		return false;
	}
}
function download(url)
{ 
	var newwindow='';
	newwindow=window.open(url,'View_Image','height=300,width=500, left=150,top=120');
	window.opener.close();
}
function vcardImageDeletion()
{ 
	var action = confirm(MSG_IMAGE_DELETION);
	if(action)
	{
		document.frm.submit();
	}
	else
	{
		document.frm.vcardImage.value="";
		return false;
	}
}
/*  Function to verify email */
function isEmail(aStr)
{
	var reEmail=/^[0-9a-zA-Z_\.-]+\@[0-9a-zA-Z_\.-]+\.[0-9a-zA-Z_\.-]+$/;
	if(!reEmail.test(aStr))
	{
		return false;
	}
	return true;
}

/*  function to validate login     */
function validateLogin()
{		
	
	if(document.frm.txt_username.value.length==0)
	{
		alert(MSG_EMPTY_USER);
		document.frm.txt_username.focus();
		return false;
	}
	if(document.frm.txt_password.value.length==0)
	{
		alert(MSG_EMPTY_PASSWORD);
		document.frm.txt_password.focus();
		return false;
	}
	return true;
}
/* Function to validate forgot password */
function chkForgotPass()
{
	if(document.frm.txt_username.value == "")
	{
		alert(MSG_EMPTY_USER);
		document.frm.txt_username.focus();
		return false;
	}
	return true;
}
/*  Function to verify email for CHANGE EMAIL */
function chkChangeEmail()
{
	
	if(document.frm.txt_email.value.length == 0)
	{
		alert(MSG_EMPTY_EMAIL);
		document.frm.txt_email.focus();
		return false;
	}
	if(!isEmail(document.frm.txt_email.value))
	{
		alert(MSG_VALID_EMAIL);
		document.frm.txt_email.focus();
		return false;
	}
}
/* Function to check the change password window */
function verifyChangePassword()
{
	if(document.frm.txt_oldpassword.value.length==0)
	{
		alert(MSG_EMPTY_OLD_PASSWORD);
		document.frm.txt_oldpassword.focus();
		return false;
	}

	if(document.frm.txt_newpassword.value.length==0)
	{
		alert(MSG_EMPTY_NEW_PASSWORD);
		document.frm.txt_newpassword.focus();
		return false;
	}

	if(document.frm.txt_confirmpassword.value.length==0)
	{
		alert(MSG_EMPTY_CONFIRM_PASSWORD);
		document.frm.txt_confirmpassword.focus();
		return false;
	}

	if(document.frm.txt_newpassword.value!=document.frm.txt_confirmpassword.value)
	{
		alert(MSG_MISMATCH_PASSWORD);
		document.frm.txt_confirmpassword.focus();
		return false;
	}

	return true;
}
/*     function to activate page     */
function activation(id)
{
	$action = confirm(MSG_PAGE_ACTIVATED);
	if($action)
	{
		document.frm.selectedAction.value="activate";
		document.frm.id.value=id;
		return true;
	}
		return false;
}
/*     function to deactivate page     */
function deactivation(id)
{
	$action = confirm(MSG_PAGE_DEACTIVATED);
	if($action)
	{
		document.frm.selectedAction.value="deactivate";
		document.frm.id.value=id;
		return true;
	}
		return false;
}
/*     function to delete page     */
function deletion(id)
{
	$action = confirm(MSG_PAGE_DELETED);
	if($action)
	{
		document.frm.selectedAction.value="delete";
		document.frm.id.value=id;
		return true;
	}
		return false;
}
/*  function to validate add / edit page form     */
function validatepage()
{		
	testRegExp = /[!@#$%^&*()~`:;,./</>]/;
	if(document.frm.txt_pagename.value.length==0)
	{
		alert(MSG_EMPTY_PAGE_NAME);
		document.frm.txt_pagename.focus();
		return false;
	}
	/*if (testRegExp.test(document.frm.txt_pagename.value))
	{
	   alert(MSG_ENTER_VALID_PAGE_NAME);
	   document.frm.txt_pagename.focus();
		return false;
	}*/
	return true;
}
/*     function to activate publication     */
function publicationActivate(publicationId)
{
	$action = confirm(MSG_PUBLICATION_ACTIVATED);
	if($action)
	{
		document.frm.selectedAction.value="activate";
		document.frm.publicationId.value=publicationId;
		return true;
	}
		return false;
}
/*     function to deactivate publication     */
function publicationDectivate(publicationId)
{ 
	$action = confirm(MSG_PUBLICATION_DEACTIVATED);
	if($action)
	{
		document.frm.selectedAction.value="deactivate";
		document.frm.publicationId.value=publicationId;
		return true;
	}
		return false;
}
/*     function to delete publication     */
function publicationDelete(publicationId)
{
	$action = confirm(MSG_PUBLICATION_DELETED);
	if($action)
	{
		document.frm.selectedAction.value="delete";
		document.frm.publicationId.value=publicationId;
		return true;
	}
		return false;
}
/*  function to validate add / edit publication form     */
function validatepublication()
{		
	if(document.frm.txt_publicationname.value.length==0)
	{
		alert(MSG_EMPTY_PUBLICATION_NAME);
		document.frm.txt_publicationname.focus();
		return false;
	}
	return true;
}

/*  Function to validate people form */
function validatepeople()
{
	testRegExp = /[!@#$%^&*()~`:;,./</>]/;
	//testRegExpImage = /[!@#$%^&*()~`;,<>]/;
	if(document.frm.txt_peoplename.value.length == 0)
	{
		alert(MSG_EMPTY_PEOPLE_NAME);
		document.frm.txt_peoplename.focus();
		return false;
	}
	/*if (testRegExp.test(document.frm.txt_peoplename.value))
	{
	   alert(MSG_ENTER_VALID_NAME);
	   document.frm.txt_peoplename.focus();
		return false;
	}*/
	if(document.frm.txt_peopleemail.value.length != 0)
	{
		if(!isEmail(document.frm.txt_peopleemail.value))
		{
			alert(MSG_VALID_EMAIL);
			document.frm.txt_peopleemail.focus();
			return false;
		}
	}
	/*if(document.frm.txt_peopleimage.value.length != 0)
	{
		if (testRegExpImage.test(document.frm.txt_peopleimage.value))
		{
		   alert(MSG_SPECIAL_CHAR_NOT_ALLOWED);
		   document.frm.txt_peopleimage.focus();
			return false;
		}
	}
	if(document.frm.txt_peoplevcard.value.length != 0)
	{
		if (testRegExpImage.test(document.frm.txt_peoplevcard.value))
		{
		   alert(MSG_SPECIAL_CHAR_NOT_ALLOWED);
		   document.frm.txt_peoplevcard.focus();
			return false;
		}
	}*/
}

/*  Function to validate job form */
function validatejob()
{
	testRegExp = /[!@#$%^&*()~`:;,./</>]/;
	//testRegExpImage = /[!@#$%^&*()~`;,<>]/;

	if(document.frm.txt_jobtitle.value.length == 0)
	{
		alert(MSG_EMPTY_JOB_TITLE);
		document.frm.txt_jobtitle.focus();
		return false;
	}
	/*if (testRegExp.test(document.frm.txt_jobtitle.value))
	{
	   alert(MSG_ENTER_VALID_JOB_TITLE);
	   document.frm.txt_jobtitle.focus();
		return false;
	}*/
	/*if(document.frm.txt_jobimage.value.length != 0)
	{
		if (testRegExpImage.test(document.frm.txt_jobimage.value))
		{
		   alert(MSG_SPECIAL_CHAR_NOT_ALLOWED);
		   document.frm.txt_jobimage.focus();
			return false;
		}
	}*/
	if((document.frm.txt_jobimage.value =='') && (document.frm.deleteImage.checked))
	{
		alert(MSG_EMPTY_JOB_DOC);
		document.frm.txt_jobimage.focus();
		return false;
	}
	if((document.frm.txt_jobimage.value =='') && (document.frm.txt_image.value.length == 0))
	{
		alert(MSG_EMPTY_JOB_DOC);
		document.frm.txt_jobimage.focus();
		return false;
	}
	if(document.frm.txt_jobshortdesc.value.length == 0)
	{
		alert(MSG_EMPTY_JOB_SHORTDESC);
		document.frm.txt_jobshortdesc.focus();
		return false;
	}
	
}

/*    FUNCTIONS TO VALIDATE NEWS    */

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

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(dtStr){
	
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	//var strMonth=dtStr.substring(0,pos1)
	//var strDay=dtStr.substring(pos1+1,pos2)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(0,pos1)
	var strYear=dtStr.substring(pos2+1)
	
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		alert("Please enter a valid 4 digit year.")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateForm(){
	var dt=document.frm.txt_newsdate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }

/*  Function to validate News form */
function validatenews()
{
	testRegExp = /[!@#$%^&*()~`:;,./</>]/;
	if(document.frm.txt_newsname.value.length == 0)
	{
		alert(MSG_EMPTY_NEWS_TITLE);
		document.frm.txt_newsname.focus();
		return false;
	}
	/*if (testRegExp.test(document.frm.txt_newsname.value))
	{
	   alert(MSG_ENTER_VALID_TITLE);
	   document.frm.txt_newsname.focus();
		return false;
	}*/
	var dt=document.frm.txt_newsdate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
	if(document.frm.txt_shortdesc.value.length == 0)
	{
		alert(MSG_EMPTY_NEWS_CONTENT);
		document.frm.txt_shortdesc.focus();
		return false;
	}
	if(document.frm.txt_shortdesc.value.length > 500)
	{
		alert(MSG_NEWS_CONTENT_LENGTH);
		document.frm.txt_shortdesc.focus();
		return false;
	}
}
/*  Function to verify email for Newsletter */
function validateEmail(val)
{
	if(document.frm_newsletter.txt_email.value == "")
	{
		alert(MSG_EMPTY_EMAIL);
		document.frm_newsletter.txt_email.focus();
		return false;
	}
	if(!isEmail(document.frm_newsletter.txt_email.value))
	{
		alert(MSG_VALID_EMAIL);
		document.frm_newsletter.txt_email.focus();
		return false;
	}
	document.frm_newsletter.txt_subscribe.value=val;
	document.frm_newsletter.submit();
}
function showDiv(url1,url2)
{
	//document.getElementById(url1).style.display='none';
	document.getElementById(url2).style.display='block';	
}