/*
  $Id: addas.js 2405 2008-08-06 11:52:31Z warren $ 
*/

var EMPTY_FIELDS = -1250;
var TITLE_NOT_VALID = -1251;
var TITLE_IS_LONG = -1252;
var INVALID_URL = -1253;
var EMPTY_URL = -1254;
var EMPTY_TITLE = -1255;
var ERROR_EMPTY_TITLE = 'Looks like you forgot to give a title!'; 
var ERROR_INVALID_TITLE = 'Please enter valid title';
var ERROR_LONG_TITLE  = 'Adda title cannot be more than 50 characters';
var ERROR_EMPTY_DESCRIPTION  = 'Looks like you forgot to give a description!';
var ERROR_EMPTY_URL = 'Looks like you forgot to enter link!';
var ERROR_INVALID_URL  = 'Please enter valid link';

var SUCCESS             = 0;
var error_code          = 0;


var AjaxObject = {
 handleSuccess_link_delete:function(o){
	//alert(o.responseText);
	var ele = parseInt(o.responseText);
	document.getElementById("s"+ele).style.display = "none";
  },
 handleFailure_link_delete:function(o){
  }
};

var callback_link_delete =
  {
  success:AjaxObject.handleSuccess_link_delete,
  failure:AjaxObject.handleFailure_link_delete,
  scope: AjaxObject
  };

function delete_link(adda_id,id)
{
  YAHOO.util.Connect.asyncRequest('POST',system_base_url+'ajax/adda/_delete_link/'+adda_id+'/'+id, callback_link_delete);
  if(env_value == 'Online')
	{
	    var url = system_base_url+'ajax/adda/_delete_link/'+adda_id+'/'+id;
	    pageTracker._trackPageview(url);
	}
    
}

function submit()
{
    var frm = document.getElementById('adda_links');
    frm.submit();
}

/*
function link_validations(frm)
{
   // alert("gvhgvhgvhgvh");
    
	    var title = document.getElementById('title').value;
	    var description = document.getElementById('description').value;
	    var url = document.getElementById('url').value;
	    var error_code = 0;
	    var error_php = false;
	    if(title || title == '')
		{
		    var error_title = validateTitle(title,'error_title');
		    if(error_title)
			{
			    showError(error_title,'error_title'+i);
			    error_code = error_title;
			    error_php= true;
			}
		    else
			{
			    error_php= false;
			    document.getElementById('error_title'+i).innerHTML = '';
			}
		}
	    
	    else if (description || description == '')
		{
		    var error_description = validateDescription(description,'error_description'+i);
		    if (error_description)
			{
			    showError(error_description,'error_description'+i);
			    error_code = error_description;
			    error_php= true;
			}
		    else
			{
			error_php= false;
			    document.getElementById('error_description'+i).innerHTML = '';
			}
			}
	    else if (url || url == '')
		{
		    var error_url = validateUrl(url,'error_url'+i);
		   // alert(error_url);
		    if (error_url)
			{
			    showError(error_url,'error_url'+i);
			    error_code = error_url;
			    error_php= true;
			}
		    else
			{
			    document.getElementById('error_url'+i).innerHTML = '';
			    error_php= false;
			}
		}
	}
       if(error_php == true)
	{
	    return false;
	}
       else if(error_php == false)
	{
	    return true;
	}
}
*/

function validateUrl(url,divid)
{
    //   alert(divid);
    //    var filter  = /^[a-zA-Z0-9]+$/;
    var filter = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
    if (trim(url) == '')
	{
	    //  alert("emptyurl");
	    document.getElementById(divid).innerHTML = ERROR_EMPTY_URL;
	    return EMPTY_URL;
	} 
    else if (!filter.test(url))
	{
	    //    alert("invalidur");
	      document.getElementById(divid).innerHTML = ERROR_INVALID_URL;
	    return INVALID_URL;
	}
    else
	{
	    // alert("validurl");
	     document.getElementById(divid).innerHTML = "";
	    return SUCCESS;
	}
}

function validateDescription(description,divid)
{
    if (trim(description) == '')
	{
	    //	    alert("emptydesc");
	    document.getElementById(divid).innerHTML = ERROR_EMPTY_DESCRIPTION;
	    return EMPTY_FIELDS;
	}
    else
	{
	    document.getElementById(divid).innerHTML = "";
	    return SUCCESS;
	}
}


function validateTitle(title,divid)
{
    var filter  = /^([a-zA-Z])+(([a-zA-Z0-9_\'\,\.\!\- ]))+$/;
    if(trim(title) == '')
	{
	    document.getElementById(divid).innerHTML = ERROR_EMPTY_TITLE;
	    return EMPTY_TITLE;
	}
    else if(!filter.test(title))
	{
	    document.getElementById(divid).innerHTML = ERROR_INVALID_TITLE;
	    return TITLE_NOT_VALID;
	}
    else if(title.length > 25)
	{
	    document.getElementById(divid).innerHTML = ERROR_LONG_TITLE;
	    return TITLE_IS_LONG;
	}
    else 
	{
	    document.getElementById(divid).innerHTML = "";
	    return SUCCESS;
	}
}

function showError(error_code,div_id)
{
    for (i=0;i<=4;i++)
	{
	    if(div_id == 'error_title'+i)
		{
		    if(error_code  == EMPTY_TITLE)
			{                
			    document.getElementById(div_id).innerHTML = ERROR_EMPTY_TITLE;
			}
		    else if(error_code  == TITLE_NOT_VALID)
			{                 
			    document.getElementById(div_id).innerHTML = ERROR_INVALID_TITLE;
			}
		    else if(error_code  == TITLE_IS_LONG)
			{                 
			    document.getElementById(div_id).innerHTML = ERROR_LONG_TITLE;
			}
		    else
			{
			    document.getElementById(div_id).innerHTML = '';
			}
		}
	    if (div_id == 'error_description'+i)
		{
		    if (error_code == EMPTY_FIELDS)
			{
			    document.getElementById(div_id).innerHTML = ERROR_EMPTY_DESCRIPTION;
			}
		    else
			{
			    document.getElementById(div_id).innerHTML = '';
			}
		}
	    if(div_id == 'error_url'+i)
		{
		    if(error_code  == EMPTY_URL)
			{                 
			    document.getElementById(div_id).innerHTML = ERROR_EMPTY_URL;
			}
		    else if(error_code  == INVALID_URL)
			{                 
			    document.getElementById(div_id).innerHTML = ERROR_INVALID_URL;
			}
		    else
			{
			    document.getElementById(div_id).innerHTML = '';
			}
		}
	}
}

function links_restw(frm,max,dispentered,counter_increment)
{
    var inp = frm.value.replace(/^\s+|\s+$/g,'');
    var l = parseInt(inp.length);
    if (l > max)
	{             
	    inp = inp.substr(0,max);
	    frm.value = inp;
	}
    document.getElementById(counter_increment).value = inp.length;
    document.getElementById(dispentered).innerHTML = inp.length;
    document.getElementById('show_count').innerHTML = '';
}

function addLink()
{
    var ni = document.getElementById('linkDiv');
    var numi = document.getElementById('link_counter');
    var num = (document.getElementById("link_counter").value -1)+ 2;

    var allowed_links_num = 5 - (document.getElementById("db_links_count").value);
    //    alert(num);
    if(num < allowed_links_num)
	{
	    var divIdName = "Account"+num;
	    var total_types =document.adda_links.type0.length;
	    var typestr = "<select name=\"type"+num+"\">";
	    for(var i = 0;i < total_types;i++) 
		{
		    var type_value =document.adda_links.type0[i].value;
		    //		    var type_icon_image = document.getElementById("type_icon_"+i).value;

		    typestr = typestr + "<option value=\'"+type_value+"\'>"+type_value+"</option>";
		}
	    typestr = typestr + "</select>";

	    numi.value = num;
	    var newdiv = document.createElement('div');
	    newdiv.setAttribute("id",divIdName);
	    newdiv.innerHTML = "<div class=\"col1\"><span class=\"left\">*Title:</span></div><div class=\"col2\"><input type=\"text\" name=\"title"+num+"\" id=\"title"+num+"\" class=\"input\" value=\"\" maxlength=\"25\"><br> <span class=\"col2 font11\">(Max 25 characters)</span><br><span class=\"red font11\" id=\"error_title"+num+"\"></span></div><div class=\"clear\"><br></div><div class=\"col1\"><span class=\"left\">*Description: </span></div><div class=\"col2 font11\"><textarea name=\"description"+num+"\" id=\"description"+num+"\"  onKeyUp=\"javascript:links_restw(this,'100','dispentered"+num+"','counter_increment"+num+"');\" class=\"input\"></textarea><br><br class=\"spacer3\"><input type=\"hidden\" name=\"counter_increment"+num+"\" id=\"counter_increment"+num+"\" value=\"\"><b name=\"show_count\" id=\"show_count\" class=\"red font11\"></b><b name=\"dispentered\" id=\"dispentered"+num+"\" class=\"black\">0</b>&nbsp;characters entered.&nbsp;(Max 100 characters)<br><span class=\"red font11\" id=\"error_description"+num+"\"></span></div><div class=\"clear\"><br></div><div class=\"col1\"><span class=\"left\">*Type: &nbsp; &nbsp;</span></div><div class=\"col2 font11\"><span id=\"type\">"+typestr+"</span></div> <div class=\"clear\"><br></div><div class=\"col1\"><span class=\"left\">*Link Url: &nbsp; &nbsp</span></div><div class=\"col2 font11\"> <input type=\"text\" name=\"url"+num+"\" id=\"url"+num+"\"class=\"input\" ><br><span class=\"col2 font11\">(eg. http://www.bigadda.com)</span><br><span class=\"red font11\" id=\"error_url"+num+"\"></span><br></div><br class=\"spacer10\"><div class=\"col1\"><br></div><div class=\"col2\"><a href=\"javascript:addLink();\" class=\"font10 more\">Add Another</a><br><br class=\"spacer10\"><a href=\"javascript:removelink(\'"+num+"\')\" class=\"font10 more\">Remove this</a></div><div class=\"clear\"><br></div><div class=\"gray1pxf0\"><br></div><br></div>";
	    ni.appendChild(newdiv);
	}
}


function removelink(divNum)
{
    var d = document.getElementById('linkDiv');
    var olddiv = document.getElementById('Account'+divNum);
    var num = document.getElementById("link_counter").value;
    var num1 = num-1;
    document.getElementById("link_counter").value = num1;
    d.removeChild(olddiv);
}