//REQUIRED GLOBAL VARIABLES 
var g_sortfield;
var g_sortdir = 1;

/*
This functions is called when a column heading is clicked, it takes 3 params
1)type = the type of data that is getting sorted:
		cta_cta - Sorting the CTA List in the CTA Search Panel
		cta_vendor - Sorting the Vendor List in the CTA Search Panel
		vendor_cta - Sorting the Vendor list in the Vendor View Panel.
2)field = the objects property to sort by
3)dir = the direction in which to sort:
		1 = Ascending
	 	0 = Descending
*/
function fn_sort(type,field,dir){
	var targ,_tmp_field;
	g_sortdir = dir;
	_tmp_field = g_sortfield;
	if(_tmp_field!=field){
		g_sortdir=1;
	}
	switch(type){
		case 'prospects': g_sortfield=field;
						prospects.sort(fn_sort_compare);
						fn_paintAllProspects(null,g_max);						
						break;					
		case 'prospects_date': g_sortfield=field;
						prospects.sort(fn_sort_compare_date);
						fn_paintAllProspects(null,g_max);						
						break;					
		case 'companies': g_sortfield=field;
						companies.sort(fn_sort_compare);
						fn_paintAllCompanies(null,g_max);						
						break;					
		case 'contacts': g_sortfield=field;
						contacts.sort(fn_sort_compare);
						fn_paintAllContacts(null,g_max);						
						break;					
	}
	if(g_sortdir==1){
		g_sortdir=0;
	}else{
		g_sortdir=1;
	}
}


/*
This function sorts an array of javascript objects based on the field name set in g_sortdir.
To call this function use the following syntax:
	{array}.sort(fn_sort_compare);
Use this sort when data is numeric or string
*/
function fn_sort_compare(a,b){
	var a_val,b_val;
	a_val = eval('a.'+g_sortfield);
	b_val = eval('b.'+g_sortfield);
	if(a_val&&b_val){
		if(!isNaN(a_val)&&!isNaN(a_val)){
			if( (a_val*1)>(b_val*1) ){
				x = 1;
			}else{
				x = -1;
			}
		}else{
			x = a_val.localeCompare(b_val);
		}
/*
		if(a_val==b_val){
			x = a.title.localeCompare(b.name);
		}
*/
		if(g_sortdir==1){
			return x;
		}else{
			if(x==1)
				return -1;
			else if(x==-1)
				return 1;
			else
				return 0;				
		}
	}
	return 0;
}

/*
This function sorts an array of javascript objects based on the field name set in g_sortdir.
To call this function use the following syntax:
	{array}.sort(fn_sort_compare);
Use this sort when data is a date type, the date must be in the format dd/mm/yyyy
*/
function fn_sort_compare_date(a,b){
	var a_val_n,b_val_n,a_val,b_val,_date_a,_date_b;
	a_val = eval('a.'+g_sortfield);
	b_val = eval('b.'+g_sortfield);
	a_val_n = a.name;
	b_val_n = b.name;	
	if(a_val&&b_val){
		a_val = a_val.split('/');
		b_val = b_val.split('/');
		if(a_val.length==3)_date_a = new Date(a_val[2],a_val[1],a_val[0]);else _date_a = new Date(1900,01,01);
		if(b_val.length==3)_date_b = new Date(b_val[2],b_val[1],b_val[0]);else _date_b = new Date(1900,01,01);
		a_val = _date_a;
		b_val = _date_b;
		if(!isNaN(a_val)&&!isNaN(a_val)){
			if( (a_val*1)>(b_val*1) ){
				x = 1;
			}else{
				x = -1;
			}
		}else{
			x = a_val.localeCompare(b_val);
		}
//		if(a_val.getYear()==b_val.getYear()&&a_val.getMonth()==b_val.getMonth()&&a_val.getDate()==b_val.getDate()){
//			x = a.name.localeCompare(b.name);
//		}
		if(g_sortdir==1){
			return x;
		}else{
			if(x==1)
				return -1;
			else if(x==-1)
				return 1;
			else
				return 0;				
		}
	}
	return 0;
}

function fn_formatDate(_date){
var _day,_mon,_year;
	if(!_date)return '&nbsp;';
	_day = _date.getDate();
	_mon = _date.getMonth()+1;
	_year = _date.getYear();
	if(_day<10){
		_day='0'+_day;
	}
	if(_mon<10){
		_mon='0'+_mon;
	}
	return _day+'/'+_mon+'/'+_year;
}


/*
This function sorts an array of javascript objects based on the original filename set in g_sortdir.
To call this function use the following syntax:
	{array}.sort(fn_sort_compare);
Use this sort when data is numeric or string
*/
function fn_sort_compare_original(a,b){
	var a_val,b_val;
	a_val = eval('a.'+g_sortfield);
	b_val = eval('b.'+g_sortfield);
	a_val = a_val.split('.');
	b_val = b_val.split('.');
	if(a_val.length>1){
		a_val = a_val[0].split('-').join('');	
		if(isNaN(a_val)){
			a_val = 0;	
		}else{
			a_val = a_val*1;			
		}
	}
	if(b_val.length>1){
		b_val = b_val[0].split('-').join('');
		if(isNaN(b_val)){
			b_val = 0;	
		}else{
			b_val = b_val*1;	
		}
	}
//	db(a_val+'='+b_val);
	if(a_val&&b_val){
		if(!isNaN(a_val)&&!isNaN(a_val)){
			if( (a_val*1)>(b_val*1) ){
				x = 1;
			}else{
				x = -1;
			}
		}else{
			if( (a_val)>(b_val) ){
				x = 1;
			}else{
				x = -1;
			}
			//alert(a_val<b_val);
			//x = a_val.localeCompare(b_val+'');
		}
/*
		if(a_val==b_val){
			x = a.title.localeCompare(b.name);
		}
*/
		if(g_sortdir==1){
			return x;
		}else{
			if(x==1)
				return -1;
			else if(x==-1)
				return 1;
			else
				return 0;				
		}
	}
	return 0;
}