/*-------------------------------------------------
 * Project Name: IHS
 * Client Name: GMU
 * Component Name: 
 * Purpose/Function: Script For Common Javascript Functions
 * Developed By: Wasim Majid
 * Start Date: 06/19/2008 
 * End Date: ongoing
 ---------------------------------------------------*/

// Validate State
function ASPStateValidation(source, arguments)
{
    arguments.IsValid = stateValidate(arguments.Value);        
}


// Validate Date
function DateValidation(source, arguments)
{
    arguments.IsValid = validateDate(arguments.Value);        
}

function hideThisControl(Id)
{
	try
	{
		var obj = document.getElementById(Id)	
		obj.style.display = "none";
	}
	catch(e)
	{ 
	}
}

// Validate Zip
function ASPZipValidation(source, arguments)
{
    arguments.IsValid = zipValidate(arguments.Value);        
}


// Validate Grade
function ASPGradeValidation(source, arguments)
{
    arguments.IsValid = gradeValidate(arguments.Value);        
}


// Validate Currency
function ASPCurrencyValidation(source, arguments)
{
    arguments.IsValid = currencyValidate(arguments.Value);        
}

// Validate Email
function ASPEmailValidation(source, arguments)
{
    arguments.IsValid = emailValidate(arguments.Value);        
}

// Validate Int
function ASPIntValidation(source, arguments)
{
    arguments.IsValid = intValidate(arguments.Value);        
}




// Validate Year
function ASPYearValidation(source, arguments)
{
    arguments.IsValid = yearValidate(arguments.Value);        
}


// Validate File
function ASPFileValidation(source, arguments)
{
    arguments.IsValid = fileValidate(arguments.Value);        
}

// Require $5.00 Donation
function FiveDollarMin(source, arguments)
{
	arguments.IsValid = donationamount(arguments.Value);
}

function donationamount(value)
{
	var reDt1 = /^([0-4])(\.[0-9]{0,2})?$/;
	var b = !reDt1.test(value)
	return b;
}

// can't start the amount with a zero or a period
function NoInitialDotOrZero(source, arguments)
{
	arguments.IsValid = nodotnozero(arguments.Value);
}

function nodotnozero(value)
{
	var reDt1 = /^[0]|^[.]/; 
	var b = !reDt1.test(value)
	return b;
}

// Check Valid State
function stateValidate(value)
{    
    var reDt1 = /^([a-zA-Z]){2}$/;     
    var b = reDt1.test(value)
    return b;
}

// Check Valid Zip
function zipValidate(value)
{     
    var reDt1 = /^\d{5}(-\d{4})?$/;     
    var b = reDt1.test(value)
    return b;
}

// Check Valid Grade
function gradeValidate(value)
{    
    var reDt1 = /^([0-9])*([.])?([0-9])+$/;     
    var b = reDt1.test(value)
    return b;
}

// Check Valid Currency
function currencyValidate(value)
{    
    var reDt1 = /^([0-9]*(,)?[0-9]*)*(.[0-9]{0,2})?$/;     
    var b = reDt1.test(value)
    return b;
}



// Check Valid Email
function emailValidate(value)
{    
    var reDt1 = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;     
    var b = reDt1.test(value)
    return b;
}

// Check Valid Email 
function validateEmailOnNonEmptyString(value)
{    
	if(trim(value) == "")
	{
		return 	true;
	}
    var reDt1 = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;     
    var b = reDt1.test(value)
    return b;
}

function emptyValue(Id)
{
	var obj = document.getElementById(Id)
	obj.value = "";
}

// Check Valid File
function fileValidate(value)
{    
	try
	{
		var len = value.length;	
		var ext = value.substring((len-4),len);
		
		if(ext.charAt(0) != '.')
		{
			ext = "." + ext;
		}
		
		for(var i=0; i<uploadedFile.length; i++)
		{
			if(uploadedFile[i]==ext)
			{
				return true;
			}
		}
	
		return false;
	}
	catch(e)
	{ }
}


var flag = 0;
function fileClicked(errorMessageDiv, e){
	var browser=navigator.appName.toLowerCase();
	if(browser =="microsoft internet explorer" || browser =="netscape" )
	{
		flag = 1;	
	}
}


// Check Valid File
function fileValidateNew(value, allowedTypes, lengthErrorMessage, errorMsgInvalidFormat, errorMsg, errorMessageDiv, isRequired ) {
	try
	{
		// Get element from ID's		
		var lengthErrorMessageElement       = document.getElementById(lengthErrorMessage); 
		var errorMsgInvalidFormatElement    = document.getElementById(errorMsgInvalidFormat); 
		var errorMsgElement                 = document.getElementById(errorMsg); 		
		var errorMessageDiv                 = document.getElementById(errorMessageDiv);
		
		//Set default error message to error message div
		errorMessageDiv.innerHTML = '&nbsp';

		if(flag > 0)
		{
			flag = 0;	
		    return true;			
		}
		else
		{
			if ( value.length < 1 && isRequired )
			{		   		    		    		    
				errorMessageDiv.innerHTML = errorMsgElement.value;		   
				return false;
			}
			
			if ( value.length < 1 && !isRequired )
			{		   		    		    		    		    
				return true;
			}
			
			if ( value.length > 220 )
			{                                    
				errorMessageDiv.innerHTML = lengthErrorMessageElement.value;		                
				return false;  
			}
							
			var alloxedExtesions = [];
			var len = value.length;			
			var ext = value.substring(value.lastIndexOf('.') + 1 );
				  
			alloxedExtesions = allowedTypes.split("|");
			for ( var i = 0; i < alloxedExtesions.length ; i ++ )
			{
				alloxedExtesions[i] = alloxedExtesions[i].toLowerCase();
			}
			ext = ext.toLowerCase();				
			
			for(var i = 0; i < alloxedExtesions.length; i++ )
			{
				if(alloxedExtesions[i] == ext)
				{						        	        
					return true;
				}
			}
			errorMessageDiv.innerHTML = errorMsgInvalidFormatElement.value;		
			
			// Set style information to error message div as these are lost attachment deletion
			errorMessageDiv.style.display     = "block";
			errorMessageDiv.style.padding     = "2px 0px 0px 0px";
			errorMessageDiv.style.color       = "Red";
			errorMessageDiv.style.marginTop   = "5px";
									
			return false;
		}
	}
	catch(e)
	{	    		
		//errorMessageDiv.style.display = errorMsgElement.value;	
		return true; 
	}
}


// this function check file name validation and show hide errorMessage div Accordingly
function fileValidateDisplayHideErrorMessage(value, allowedTypes, lengthErrorMessage, errorMsgInvalidFormat, errorMsg, errorMessageDiv, isRequired )
{    	
	try
	{
		// Get element from ID's		
		var lengthErrorMessageElement       = document.getElementById(lengthErrorMessage); 
		var errorMsgInvalidFormatElement    = document.getElementById(errorMsgInvalidFormat); 
		var errorMsgElement                 = document.getElementById(errorMsg); 		
		var errorMessageDiv                 = document.getElementById(errorMessageDiv);
		
		//Set default error message to error message div
		errorMessageDiv.innerHTML = '&nbsp';		

		if(flag > 0)
		{
			flag = 0;	
			errorMessageDiv.style.display  = "none";  
		    return true;			
		}
		else
		{
			if ( value.length < 1 && isRequired )
			{		   		    		    		    
				errorMessageDiv.innerHTML = errorMsgElement.value;		
				errorMessageDiv.style.display  = "block";   
				return false;
			}
			
			if ( value.length < 1 && !isRequired )
			{		   		    		    		    		    
				errorMessageDiv.style.display  = "none";  
				return true;
			}
			
			if ( value.length > 220 )
			{                                    
				errorMessageDiv.innerHTML = lengthErrorMessageElement.value;	
				errorMessageDiv.style.display  = "block";	                
				return false;  
			}
							
			var alloxedExtesions = [];
			var len = value.length;			
			var ext = value.substring(value.lastIndexOf('.') + 1 );
				  
			alloxedExtesions = allowedTypes.split("|");
			for ( var i = 0; i < alloxedExtesions.length ; i ++ )
			{
				alloxedExtesions[i] = alloxedExtesions[i].toLowerCase();
			}
			ext = ext.toLowerCase();				
			
			for(var i = 0; i < alloxedExtesions.length; i++ )
			{
				if(alloxedExtesions[i] == ext)
				{						        	        
					errorMessageDiv.style.display  = "none";  
					return true;
				}
			}
			errorMessageDiv.innerHTML = errorMsgInvalidFormatElement.value;		
			
			// Set style information to error message div as these are lost attachment deletion
			errorMessageDiv.style.display     = "block";
			errorMessageDiv.style.padding     = "2px 0px 0px 0px";
			errorMessageDiv.style.color       = "Red";
			errorMessageDiv.style.marginTop   = "5px";
								
			return false;
		}
	}
	catch(e)
	{	    		
		//errorMessageDiv.style.display = errorMsgElement.value;	
		return true; 
	}
}



// Check Valid Year
function yearValidate(value)
{    
    var reDt1 = /^\d{4}$/; 
	var b = reDt1.test(value)
	if(b)
	{
		if(value < 1000 || value > 3000)
		{
			return false;	
		}
	}
    return b;
}

function intValidate(value)
{    
    var reDt1 = /^(\d)+$/; 
	var b = reDt1.test(value)
    return b;
}

function deleteAttachment(id,hiddenID, hiddenAction)
{
	var hiddenID = document.getElementById(hiddenID)
	var hiddenAction = document.getElementById(hiddenAction)	
	hiddenID.value = id;
	hiddenAction.value = "delete";		
	document.forms[0].submit();  //is this needed?
}



// This function returns the days of a perticular month
// input parameter Month and Year
function getMonthDate(month,year)
{
    if(month==2)
    {
	    if((year%400)==0)
	    {
		    return 29;
	    }
	    else if((year%100)==0)
	    {
		    return 28;
	    }
	    else if((year%4)==0)
	    {
		    return 29;
	    }
	    else
	    {
		    return 28;
	    }
    }
    else if(month==4 || month==6 || month==9 || month==11)
    {
	    return 30;
    }
    else
    {
	    return 31;
    }
}


// This function returns int from 2 length String
// example - returns '1' from '01'
function checkValue(val)
{
	var value = val.toString();
	if(value.charAt(0) == "0")
	{
		return value.charAt(1);
	}
	return value;
}



// This function validate Date by regular expression
// returns true on valid date and returns false for invalid date
function validateDate(txtVal)
{
	txtVal = trim(txtVal);
    var dt1 = new Array();
    var reDt1 = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
    var b = reDt1.test(txtVal)

    if(b)
    {
        dt1 = txtVal.split("/")
		if(dt1.length !=3)
		{
			return false;	
		}
		
		for(var i=0; i<2; i++)
		{
			dt1[i] = checkValue(dt1[i])		
		}
		
	    var endDt = getMonthDate(dt1[0],dt1[2])
	    if(parseInt(dt1[0]) > 12 || parseInt(dt1[0]) <1 || parseInt(dt1[1]) >endDt || parseInt(dt1[1]) <1 || parseInt(dt1[2]) <1000 || parseInt(dt1[2]) >9999)
	    {
		    return false;
	    }

	    return 	true;
    }
    else
    {	
	    return false;
    }
}

//if d1 is bigger, return 1
function compareDate(d1,d2)
{
	dt1 = d1.value.split("/")

	for(var i=0; i<2; i++)
	{
		dt1[i] = checkValue(dt1[i])		
	}
	
	dt2 = d2.value.split("/")
	
	for(var i=0; i<2; i++)
	{
		dt2[i] = checkValue(dt2[i])		
	}		
	
	if(parseInt(dt2[2]) > parseInt(dt1[2]))
	{
		return -1;
	}
	else if(parseInt(dt2[2]) == parseInt(dt1[2]))
	{		
		if(parseInt(dt2[0]) > parseInt(dt1[0]))
		{
			return -1;
		}
		else if(parseInt(dt2[0]) == parseInt(dt1[0]))
		{			
			if(parseInt(dt2[1]) > parseInt(dt1[1]))
			{
				return -1;
			}
			else if(parseInt(dt2[1]) == parseInt(dt1[1]))
			{			
				return 0;
			}
		}		
	}	

	return 1;		
}

// Removes starting and ending whitespaces
function trim(str)
{
    return LTrim(RTrim(str));
}

// Removes starting whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}
/*
function validateDate(date)
{	
	return false;
}
*/
function handleDateField(dv,control)
{
	var dv = document.getElementById(dv);
	//var control = document.getElementByid(control);	
	//alert('1a')	
	var validDate = validateDate(control.value);
	//alert('2')	
	if(validDate)
	{
		//alert('3')		
		dv.className = "not-dvRequiredBorder";
		control.className = "not-hilight-border";		
	}
	else
	{
		//alert('4')		
		dv.className = "dvRequiredBorder";		
		control.className = "hilight-border";		
	}
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

function ValidateFckValuesNew(fckControl, dvContainer, dvAlert, message)
{
	if(trim(FCKeditorAPI.GetInstance(fckControl).GetXHTML(true)) == "")
	{
		document.getElementById(dvContainer).className = "requiredFieldMarker";
		document.getElementById(dvAlert).innerHTML = message;
		return false;
	}
	else
	{
		document.getElementById(dvContainer).className = "white-bg";
		document.getElementById(dvAlert).innerHTML = "&nbsp;";
		return true;			
	} 		
}	

function ValidateFckValues(fckControl, dvContainer, dvAlert)
{
	if(trim(FCKeditorAPI.GetInstance(fckControl).GetXHTML(true)) == "")
	{
		document.getElementById(dvContainer).className = "grey-bg";
		document.getElementById(dvAlert).style.display = "block";
		return false;
	}
	else
	{
		document.getElementById(dvContainer).className = "white-bg";
		document.getElementById(dvAlert).style.display = "none";
		return true;			
	} 		
}
	
function FckFieldIsEmpty(fckControl)
{
	if(trim(FCKeditorAPI.GetInstance(fckControl).GetXHTML(true)) == "")
	{
		return true;
	}
	else
	{
		return false;			
	} 
}

/**
Author Mostafiz
Function that is used to control subnavigation in the IHS2.0 Ebusiness site of the 
/*/	
$(function()
{           
   // Make visible the submenu of the currently active submenu
   if ( typeof(lastClickId) == "undefined") return;
   if (lastClickId != "")
   { 
        try{
        $('#' + lastClickId ).css("display","block");
        }
        catch(e){}        
   }     
    
   // Check browser type and assign appropriate handler to list   
   if ( $.browser['msie'] )
   {       
        // If the browser type is internet explorer then attach mouseleave and mouse enter event handler 
        
        // Attach mouseleave out event handler for internet explorer
       $('.TopLevelMenu').bind("mouseleave",( function(){
            try
            {  $('#' + lastClickId ).css('display','block'); }
            catch(e){}
        }));
       // Attach mouseenter event handler for internet explorer           
       $('li.TopLevelMenu').bind("mouseenter",( function(event){ 
            try
            {            
                if ( $(this).children().get(1).id  !=  lastClickId  )
                {  $('#' + lastClickId ).css('display','none');}
            }
            catch(e){}
       }));       
   }
   else
   {
        // If the browser type is not internet explorer then attach mouse over and mouse out event handler 
       // Attach Mouse out event handler to non internet explorer browser
       $('.TopLevelMenu').mouseout( function(){
            try
            {  $('#' + lastClickId ).css('display','block'); }
            catch(e){}
        });
       // Attach Mouse out event handle to non internet explorer browserr           
       $('li.TopLevelMenu').mouseover( function(event){ 
            try
            {            
                if ( $(this).children().get(1).id  !=  lastClickId  )
                {  $('#' + lastClickId ).css('display','none');}
            }
            catch(e){}
       });
    }
});
