// JavaScript Document

// trim() function definition
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function validateForm( f )
{
	k = 0;
	
	if ( f.uri.value.trim().length == 0 )
	{
		alert(' URL is mandatory field...');
		f.uri.style.backgroundColor = '#FFFFAA';
		f.uri.focus();
		k = 0;
	}
	else 
	 k = 1;

	if ( k != 0 && f.rtype1.checked == true )
	{
		if ( f.desc.value.trim().length == 0 )
		{
			alert(' Description is mandatory field...');
			f.desc.style.backgroundColor = '#FFFFAA';
			f.desc.focus();
			k = 0;
		}		
		else if ( f.category !=null && f.category.selectedIndex == 0 )
		{
			alert(' Category is mandatory field...');
			f.category.style.backgroundColor = '#FFFFAA';
			f.category.focus();
			k = 0;
		}
		else if ( document.getElementById('category1') != null && document.getElementById('category1').value.trim().length == 0 )
		{
			alert(' Category is mandatory field...');
			document.getElementById('category1').style.backgroundColor = '#FFFFAA';
			document.getElementById('category1').focus();
			k = 0;
		}
		else if ( f.title.value.trim().length == 0 )
		{
			alert(' Title is mandatory field...');
			f.title.style.backgroundColor = '#FFFFAA';
			f.title.focus();
			k = 0;
		}	
		else
		 k = 1;
	}

	if ( k != 0 )
	 f.submit();
}


function changeCategory( val )
{	
	
	if ( val == 'other' )
	{
		//alert( document.getElementById('category').innerHTML );
		categoryHTML = document.getElementById('CategoryspanId').innerHTML;
		document.getElementById('revert').style.visibility = 'visible' ;
		
		document.getElementById('CategoryspanId').innerHTML = '<input type="text" name="category1" id="category1" maxlength="100" />';	
	}
}

function pauseIt( uri, val2 )
{
	pbtn = document.getElementById('pauseBtn');
	var alterTimerId = 0;
	
	if ( val2 == 'Pause' )
	{
	 pbtn.value = 'Continue';
	 alterTimerId = setTimeout("window.location='"+ uri +"'", 5000);	 
	}
	else if ( val2 == 'Continue' )
	{
	 pbtn.value = 'Pause';
	 clearTimeout ( alertTimerId );
	}
}

function redirect( uri )
{
	setTimeout("window.location='"+ uri +"'", 5000)	
}

function ClipBoard() 
{
	//alert('In');
	holdtext = document.getElementById('holdtext');
	//alert(holdtext);
	copytext = document.getElementById('tinyurl');
	//alert(holdtext + copytext);
	//alert('inside');
	holdtext.value = copytext.innerText;
	//alert(holdtext.value + copytext.innerText)	
	Copied = holdtext.createTextRange();
	Copied.execCommand("Copy");
}



// Function to validate e-mail address
function checkemail( mailId, val2 )
{
  k = 1;
  var mailto = document.getElementById(mailId);
  str = mailto.value;

  
  var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	
	if ( !filter.test( str ) )
	{
		alert('Please input a valid email address')
		mailto.style.backgroundColor = '#FFFFAA';
		mailto.focus();
		k = 0;
	}	
	//alert(k);
	if ( k )
	{
		document.getElementById('pbar').style.visibility = 'visible';	
		setTimeout("document.getElementById('pbar').style.visibility = 'hidden'", 8000);
		sendMail( val2 );
	}
}


function sendMail( val2 )
{
	isExists( 'mailto', val2 );
}


function hidePublicFields ( val )
{
	//alert (val);
	var f = document.forms[0];	
	
	if ( val == 'private' )	
	{
	document.getElementById('span2').style.visibility = 'visible';
	//document.getElementById('span2').innerHTML = 'Your url will NOT be listed in Public directory';
	document.getElementById('span2').innerHTML = '<img src="./images/hint_09.gif" alt="<--" width="359" height="23"  />';
	
	document.getElementById('span1').style.visibility = 'hidden';
	document.getElementById('span3').style.visibility = 'hidden';
	document.getElementById('span4').style.visibility = 'hidden';
	document.getElementById('span5').style.visibility = 'hidden';
	
	 f.submit2.style.visibility = 'visible';	
 	 f.submit1.style.visibility = 'hidden';		 
	 
	 f.desc.disabled = true;
	 
	 if ( f.category != null)
	  f.category.disabled = true;
 	 if ( f.category1 != null)
	  f.category1.disabled = true;
 
	 f.title.disabled = true;
	}
	else
	{
	//document.getElementById('span2').innerHTML = 'Your url will be listed in Public directory';
	document.getElementById('span2').innerHTML = '<img src="./images/hint_06.gif" alt="<--" width="359" height="23"  />';
	
	f.submit1.style.visibility = 'visible';			
	f.submit2.style.visibility = 'hidden';	
	
	f.desc.disabled = false;

	if ( f.category != null)
	  f.category.disabled = false;
 	 if ( f.category1 != null)
	  f.category1.disabled = false;

	 f.title.disabled = false;
	}
}



function SubmitForm( txtControl, e ) 
{
	//alert( document.location.href.substr( 0, document.location.href.indexOf( '&k=' ) ) );
	url = document.location.href;
	
	if ( txtControl.value.trim().length != 0 )
	{
	var isEnter = window.event == null ? e.keyCode == 13 : window.event.keyCode == 13;
	
	if( isEnter )
	 {
	   if ( url.indexOf( 'm=list' ) == -1 )	
	    document.location.href = url + '?m=list&k=' + txtControl.value.trim(); 
		
	   else if ( url.indexOf( 'm=list' ) != -1 && url.indexOf( '&k=' ) == -1 ) 
		document.location.href = url + '&k=' + txtControl.value.trim();
		
	   else
	    document.location.href = url.substr( 0, url.indexOf( '&k=' ) ) + '&k=' + txtControl.value.trim();
	 }
	}
	else
	 return false;
}

function getHint( val )
{
	var d = document;

	for ( i = 1; i <= 5; i++ )
	{
		spanid = 'span' + i;
		if ( i == val )
		 d.getElementById(spanid).style.visibility = 'visible';
		else
		 d.getElementById(spanid).style.visibility = 'hidden';
	}
	isPublicChecked();
}



function clearSeachField( fid )
{
	val = fid.value.trim();	
	
	if ( val == 'Search...' )
	 fid.value = '';
}

function retainSearchField ( fid )
{
	if ( fid.value.trim().length == 0 )
		fid.value = 'Search...';
}


function reportInvalidURL ( val, sid )
{
	if ( confirm ( 'You wish to report that a url listed here is not working. \nThe url will be removed after the verification by the Administrator. \nAre you sure you wish to report this ?' ) )
	 {
		isExists( val, 'R'); 
		spanId = 'span' + sid;
		document.getElementById(spanId).innerHTML = 'Reported';
		document.getElementById(spanId).className = 'highlight';
		//document.getElementById(val).style.visibility = 'hidden';	
	 }
}


 function firstFocus( oid )
 {
    var d = document;
	
 	if ( document.getElementById(oid) != null )
		document.getElementById(oid).focus();
 }

 function isPublicChecked()
 {
	r = document.getElementById('rtype1');
	r1 = document.getElementById('rtype2');

	if ( ( r != null && r.checked == true ) || ( r1 != null && r1.checked == true ) )
	 if ( document.getElementById('span2') != null )
	  document.getElementById('span2').style.visibility = 'visible';
  
 }	

 function goToPage( pgno , e )
 {
	 var isEnter = window.event == null ? e.keyCode == 13 : window.event.keyCode == 13;
	 loc = document.location.href;
	 
	 if ( isEnter )
	 {
	 if ( isNaN ( pgno.value.trim() ) || pgno.value.trim() <= 0 )
	  {
		alert( ' Invalid page number... Try again...' )	  
		pgno.style.backgroundColor = '#FFFFAA';
		pgno.focus();
		return;
	  }	
	  
	  curpg = pgno.value.trim() - 1;
	  
	  if ( loc.trim().indexOf('&pg=')  != -1 )
	    document.location.href = loc.substr( 0, loc.indexOf( '&pg=' ) ) + '&pg=' + curpg ;
	   else
	   document.location.href += '&pg=' + curpg;
	 }
 }

var categoryHTML;

function retain( spanId )
{
	document.getElementById(spanId).innerHTML = categoryHTML;
	document.getElementById('revert').style.visibility = 'hidden';
}


function validateContactUsForm( f )
{
	var arr = f.elements;
	k = 1;
	
	for ( i = 0; i < arr.length; i++)
	{
		if ( arr[i].value.trim().length == 0 )
		{
			alert( 'please provide the value....' );	
			arr[i].style.backgroundColor = '#FFFFAA';
			arr[i].focus();
			k = 0;
			break;
		}		
	}
	
	if ( k != 0 )
	{
		if ( verifyEmail ( document.getElementById('email') ) ) 
		  f.submit();													   	
	}
}



function verifyEmail( mailId )
{
  k = 1;
  str = mailId.value;
  
  var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	
	if ( !filter.test( str ) )
	{
		alert('Please input a valid email address')
		mailId.focus();
		k = 0;
	}	
	return k;
}
