// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/* Kalyan's code */
/* Used in 'corners/showphotos.rhtml' and 'corners/more_photos.rhtml' */
function dbleBtn(chkName){
  var isTrue = true;
  for ( x =1; x<= 4; x++)
  {
    cb = document.getElementById(chkName+x);
    if ( cb != null && cb.checked == false)
    {
      isTrue = false;
    }
    else if( cb != null && cb.checked == true)
    {
      isTrue = true;
      break;
    }
    else
    {
      continue;
    }
  }
  
  if ( isTrue == false)
  {
      document.getElementById('Add').disabled = true;
      document.getElementById('Add').className = 'disabled';
      document.getElementById('Add').style.background = 'url(/images/add_selected_disabled.gif) no-repeat';
  }
}

function onVideoChecked(chk)
    {
        addBtn = document.getElementById('Add');
        if( chk.checked == true)
        {
          document.getElementById('Add').disabled = false;
          document.getElementById('Add').style.background = 'url(/images/add_selected.gif) no-repeat';
          document.getElementById('Add').className = 'enabled';
        }
        else if( chk.checked == false)
        {
          //alert(chk.id);
          dbleBtn('vi_');
          //addBtn.className = 'disbled';
        }
    }

function onCheck(chk){
    addBtn = document.getElementById('Add');
    if( chk.checked == true)
    {
      addBtn.disabled = false;
      addBtn.style.background = 'url(/images/add_selected.gif) no-repeat';
      
    }
    else if( chk.checked == false)
    {
      //alert(chk.id);
      dbleBtn('ph_');
      //addBtn.className = 'disbled';
    }
}

/*  End Kalyan  */

// To select entire text as soon as user clicks on textbox
// Use this function to get similar functionality for all. 
 function selectText(field)
 {
	field.focus(); 
	field.select();
 }
 
 //For photos and videos tags to display error value if nil.
 function checkBefore()
 {
	 field = document.getElementById('tags').value;
	 if( field == "" ) 
	 {
	     document.getElementById("txt_val").style.display = "block";
	     return false;
	 }
	 else
	     return true;
 }
 
 /**
  * The two functions here check whether the uploaded file is 
  * image or not - murali
  */
 function isImage(file)
 {
       //Get a file extension
       var ext = file.substr(file.lastIndexOf('.')).toLowerCase()
        
       //Check extension to image types.
       return '.gif,.jpg,.png,.jpeg,.bmp,'.indexOf(ext+',') >= 0
 }

 function imagecheck(n)
 {
      var i,imagefile,file;
      var count = 0;
      for( i = 1;i <= n;i++ )
      {
            imagefile = document.getElementById('image'+i);
            file = imagefile.value;
            
            if( file == "" || file == " ")
            {
            	count++;
            }
            else if (! isImage(file) ) 
            {
            	document.getElementById('msg'+i).innerHTML = 'Please choose some image file (.gif,.jpg,.png,.jpeg,.bmp)';
            	return false;
            }
    
  	   }
  	   
  	   if( count == 4 )
  	   {
  	   	   document.getElementById('msg1').innerHTML = 'Please choose some image file (.gif,.jpg,.png,.jpeg,.bmp)';
           return false;
  	   }
         	
      return true;
  }
  
  /**
   * Hides one 'DIV' and shows another.
   */
   
  function interchangeElements(visDiv, hideDiv) {
    visDiv.style.display = 'block';
    hideDiv.style.display = 'none';
  }
 
    // For the given 'text',we add <wbr> after every 'xx' characters 
    function getText_With_WBR(text,xx)
    {
        if (text.length > xx )
        {
            for (i=xx; i< text.length; i=i+xx)
            {
                first_half_text  = text.substring(0,i);
                second_half_text = text.substring(i,text.length);
                text             = first_half_text + "<wbr>" + second_half_text;
            }
        }
        return text;
    }
  

    // For the given 'text',we add <wbr> after every 'split_index' characters 
    function getTokens_With_WBR( text , split_index )
    {
    	
        if (text.length > split_index )
        {
            for ( i = 0; i < text.length; i = i + split_index )
            {
            	// Test the _token contains any unformatted test.
            	var _token = text.substring(i,i+split_index);
                var _count = 0;
                for(var c=0;c<=_token.length;c++)
                {
                	if ( _token.charAt(c) == '<' ) 
                		_count = _count + 1;
                    else if ( _token.charAt(c) == '>' ) 
                		_count = _count - 1;
                }
                
                // If the _token is unformmatted, try to find the closing.
                if( _count > 0 )
                {
                	_token = text.substring(i+split_index,text.length);
                	
                	for(c=0;c<=_token.length;c++)
	                {
	                	if ( _token.charAt(c) == '<' ) 
	                		_count = _count + 1;
	                    else if ( _token.charAt(c) == '>' ) 
	                		_count = _count - 1;
	                	
	                	if( _count == 0)
	                	{
	                		//extend i length
	                		i = i + c;
	                		break;
	                	}
	                }
                }
                
                first_half_text  = text.substring(0,i);
                second_half_text = text.substring(i,text.length);             
                
                text = first_half_text + "<wbr>" + second_half_text;
            }
        }
        return text;
    }
    
    // For the given 'text',
    function tokenize_text(text,index)
    {
    	  // Tokenize with " "
          var _tokens = text.split(" ");
		  text = "";
		  // Iterate through all the tokens,
		  for ( var i = 0; i < _tokens.length; i++ )
		  {
		  	// Find any of the token is too long,
		  	if( _tokens[ i ].length > index )
		  	{
		  		// Tokenize with "<br/>"
		  		var sub_tokens = _tokens[ i ].split("<br/>");
		  		var sub_text   = "";
		  		// Iterate through all the tokens,
			    for ( var j = 0; j < sub_tokens.length; j++ )
			    {	
			    	// Find any of the token is too long,if so, add <WBR> s to that token
				    if( sub_tokens[ j ].length > index )
			  		{
			  			sub_text = sub_text + "<br/>" + getTokens_With_WBR( sub_tokens[ j ] , index );
			  		} 
			  		else //keep as it is,
			  		{
			  			sub_text = sub_text + "<br/>" + sub_tokens[ j ];
			  		} 		
		  		}
		  		text = text + " " + sub_text;
		  	}
		  	else //keep as it is,
		  	{
		  		text = text + " " + _tokens[ i ];
		  	}
		  }
		  return text;
    }
       // For the given 'text', with &nbsp;
    function tokenize_text_with_nbsp(text,index)
    {
          var _tokens = text.split("&nbsp;");
		  text = "";
		
		  for ( var i = 0; i < _tokens.length; i++ )
		  {
		  	if( _tokens[ i ].length > index )
		  	{
		  		
		  		text = text + " " + getTokens_With_WBR( _tokens[ i ],index );
		  	}
		  	else
		  		text = text + " " + _tokens[ i ];
		  }
		  return text;
    } 
    
    //Returns the short text.
    function getShortChirp(text)
    {
    	var short_text = text;
    	if( short_text.length > 60 )
    	{
    		short_text = "";
	    	var _tokens = text.split("<wbr>");

			  for ( var i = 0; i < _tokens.length; i++ )
			  {
			  	if( (short_text.length + _tokens[i].length) > 60 )
			  	{	 
			  		return short_text;		
			    }
			  	else
			  	{
			  		short_text = short_text + "<wbr>" + _tokens[ i ];
			  	}
			  }
		  }
		  return short_text;
    }
    
    // TODO - Function to replace <br /> with <br/>
    function replaceBR(text)
    {
    	return text.replace(/<br /g,"<br")
    }
 
 // ****** START  ******** Code to truncate the string 
    
     // Removes leading whitespaces
    function LTrim( value ) 
    {
    	var re = /\s*((\S+\s*)*)/;
    	return value.replace(re, "$1");
    }
    
    // Removes ending whitespaces
    function RTrim( value ) 
    {
    	var re = /((\s*\S+)*)\s*/;
    	return value.replace(re, "$1");
    }
    
    // Removes leading and ending whitespaces
    function trim( value ) 
    {
    	return LTrim(RTrim(value));
    } 
  // ***** END ******** Code to truncate the string 