/*
  $Id: addas.js 3325 2008-12-19 07:13:07Z poonam $ 
*/

var EMPTY_FIELDS        = -1201;
var NAME_NOT_VALID = -1202;
var NAME_IS_LONG = -1203;
var CAPTCHA_NOT_VALID = -1204;
var INVALID_TAGS = -1205;
var INVALID_URL = -1206;
var NAME_INVALID_CHARACTER = -1207;
var NAME_INVALID_STARTCHARACTER = -1208;

var ERROR_EMPTY_NAME         = 'Looks like you forgot to give a title.'; 
var ERROR_INVALID_NAME       = 'Please enter valid title (No special characters allowed).';
var ERROR_NAME_CHARACTER     = 'You may use alphabets, numbers and special characters : - ( ) & only.';
var ERROR_NAME_STARTCHARACTER = 'The first character must be an alphabet or a number.';
var ERROR_LONG_NAME          = 'Adda title cannot be more than 50 characters.';
var ERROR_EMPTY_DESCRIPTION  = 'Looks like you forgot to give a description.';
var ERROR_EMPTY_CATEGORY     = 'Please select atleast one category.';
var ERROR_EMPTY_FULLNAME     = 'Please enter your full name.';
var ERROR_EMPTY_URL          = 'Please enter adda url.';
var ERROR_EMPTY_FEATURES     = 'Please enter atleast one feature.';
var ERROR_EMPTY_CAPTCHA      = 'Please enter verification code.';
var ERROR_INVALID_TAGS       = 'Please enter valid tags. No special characters allowed.';
var ERROR_EMPTY_URL          = 'Please enter adda URL.';
var ERROR_INVALID_URL        = 'Please enter valid URL. No special characters allowed.';
var ERROR_EMPTY_FEATURES     = 'Please check atleast one feature.';

var SUCCESS             = 0;
var error_code          = 0;

var AjaxObject = {

 handleSuccess_email:function(o){
    document.getElementById("error_email").innerHTML = o.responseText;
    if(o.responseText == 'Not Available')

      {
	error_code = EMAIL_NOT_UNIQUE;
      }
    else
      {
	error_code = 0;
      }
  },
 handleFailure_email:function(o){
    // Failure handler
  },
 handleSuccess_profileid:function(o){
    document.getElementById("error_profileid").innerHTML = o.responseText;
    error_code = 0;
  },
 handleFailure_profileid:function(o){
    // Failure handler
  },
 handleSuccess_city:function(o){
    document.getElementById("error_city").innerHTML = o.responseText;
    error_code = 0;
  },
 handleFailure_city:function(o){
    // Failure handler
  },
  handleSuccess_mobileno:function(o){
    document.getElementById("error_mobileno").innerHTML = o.responseText;
    if(o.responseText == 'Not Available')
      {
	error_code = MOBILENO_NOT_UNIQUE;
      }
    else
      {
	error_code = 0;
      }
  },
 handleFailure_mobileno:function(o){
    // Failure handler
  }
};


/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback_email =
  {
  success:AjaxObject.handleSuccess_email,
  failure:AjaxObject.handleFailure_email,
  scope: AjaxObject
  };

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback_mobileno =
  {
  success:AjaxObject.handleSuccess_mobileno,
  failure:AjaxObject.handleFailure_mobileno,
  scope: AjaxObject
  };

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */

var callback_profileid =
  {
  success:AjaxObject.handleSuccess_profileid,
  failure:AjaxObject.handleFailure_profileid,
  scope: AjaxObject
  };

var callback_city=
  {
  success:AjaxObject.handleSuccess_city,
  failure:AjaxObject.handleFailure_city,
  scope: AjaxObject
  };


function adda_validations(frm)
{
    var name = document.getElementById('name').value;
    var description = document.getElementById('description').value;
    var pickList = document.getElementById("PickList");
    var psselectIndex = pickList.selectedIndex;
    var pickOptions = pickList.options;
    var pickOLength = pickOptions.length;
    var url = document.getElementById('url').value;
    var captcha = document.getElementById('captcha').value;
    var tags = document.getElementById('tags').value;
    error_code = 0;
    
    if(captcha || captcha == '')
	{
	    var error_verify_code = validateCaptcha(captcha);
	    if(error_verify_code)
		{
		    showError(error_verify_code,'error_verify_code');
		    error_code = error_verify_code;
		}
	    else
		{
		    document.getElementById('error_verify_code').innerHTML = '';
		}
	}
    if(name || name == '')
	{
	    var error_name = validateName(name);
	    if(error_name)
		{
		    showError(error_name,'error_name');
		    error_code = error_name;
		}
	    else
		{
		    document.getElementById('error_name').innerHTML = '';
		}
    }
    if (description || description == '')
	{
	    var error_description = validateDescription(description);
	    if (error_description)
		{
		    showError(error_description,'error_description');
		    error_code = error_description;
		}
	    else
		{
		    document.getElementById('error_description').innerHTML = '';
		}
	}
    if (tags || tags == '')
	{
	    var error_tags = validateTags(tags);
	    if (error_tags)
		{
		    showError(error_tags,'error_tags');
		    error_code = error_tags;
		}
	    else
		{
		    document.getElementById('error_tags').innerHTML = '';
		}
	}
    if (url || url == '')
	{
	    var error_url = validateUrl(url);
	    if (error_url)
		{
		    showError(error_url,'error_url');
		    error_code = error_url;
		}
	    else
		{
		    document.getElementById('error_url').innerHTML = '';
		}
    }
    if (pickOLength || pickOLength == '')
	{
	    var error_category = validateCategory(pickOLength);
	    if (error_category)
		{
		    showError(error_category,'error_category');
		    error_code = error_category;
		}
	    else
		{
		    document.getElementById('error_category').innerHTML = '';
		}
	}
    if(error_code < 0)
	{
	    return false;
	}
    else
	{
	    return true;
	}
}

function validateCategory(category)
{
    if (category == 0)
	{
	    return EMPTY_FIELDS;
	}
    else
	{
	    return SUCCESS;
	}
}

function validateUrl(url)
{
    var filter  = /^[a-zA-Z0-9]+$/;
    if (trim(url) == '')
	{
	    return EMPTY_FIELDS;
	} 
    else if (!filter.test(url))
	{
	    return INVALID_URL;
	}
    else
	{
	    return SUCCESS;
	}
}

function validateFeatures()
{
    var frm = document.getElementById('create_frm');
    var len = frm.elements.length;
    var i = 0;
    var str = "";
    for(i=0; i<len; i++)
	{
	    if(features.elements[i].checked)
		{ 
		    return SUCCESS;
		}
	}
    return EMPTY_FIELDS;
}

function checkCaptcha(captcha,div_id)
{
    var error_code = validateCaptcha(captcha);
    if(error_code)
	{
	    showError(error_code,div_id);
	}
    else
	{
	    document.getElementById(div_id).innerHTML = '';
	}
}

function validateTags(tags)
{
    var filter  = /^[a-zA-Z0-9\,\ ]+$/;
    if (tags != '' && !filter.test(tags))
	{
	  return INVALID_TAGS;
	}
    else
	{
	  return SUCCESS;
	}
}


function validateDescription(description)
{
    if (trim(description) == '')
	{
	    return EMPTY_FIELDS;
	}
    else
	{
	    return SUCCESS;
	}
}

function trim(str, chars)
{
    return ltrim(rtrim(str, chars), chars);
} 

function ltrim(str, chars)
{
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars)
{
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+ s", "g"), "");
}


//! method to display/request a new captcha image
/*! 
 * \param img_name => string, value of image src
 * \param base_url => string, value of base url
 * return captcha image on success
 */
function captchaRefresh(img_name, base_url)
{
  document.getElementById(img_name).src = base_url+"/plugins/captcha";    
}

//! Method to validate First/Last name
/*!
 * param name=>consists of data entered by user in FIRST/LAST name field
 * return Boolean True on success, negative error code on failure
 */
function validateName(name)
{
    //var filter = /^[a-zA-Z]+(([\'\,\.\!\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var filter  = /^(([a-zA-Z0-9\&\(\)\:\- ]))+$/;
  var filter1  = /^([\&\(\)\:\-\@\#\!\$\^\*\"\'\,\;])/;
  

  if(trim(name) == '' || name.length == 0)
	{
	    return EMPTY_FIELDS;
	}
  else if(filter1.test(trim(name)))
	{
	  return NAME_INVALID_STARTCHARACTER;
	}
  else if(!filter.test(trim(name)))
	{
	  return NAME_INVALID_CHARACTER;
	}
    else if(name.length > 50)
	{
	    return NAME_IS_LONG;
	}
    else 
	{
	    return SUCCESS;
	}
}


//! Method to validate Captcha/verification code
/*!
 * param name=>consists of data entered by user
 * return Boolean True on success, negative error code on failure
 */

function validateCaptcha(captcha)
{
  var filter  = /^[a-zA-Z0-9]+$/;
  if(captcha.length == 0 || captcha == '')
      {
	  return EMPTY_FIELDS;
      }
  else if(!filter.test(captcha))
      {
      return CAPTCHA_NOT_VALID;
      }
  else 
    {
	return SUCCESS;
    }
}

function showError(error_code,div_id)
{
    if(div_id == 'error_name')
	{
	    if(error_code  == EMPTY_FIELDS)
		{                 
		    document.getElementById(div_id).innerHTML = ERROR_EMPTY_NAME;
		}
	    else if(error_code  == NAME_NOT_VALID)
		{                 
		    document.getElementById(div_id).innerHTML = ERROR_INVALID_NAME;
		}
	    else if(error_code  == NAME_IS_LONG)
		{                 
		    document.getElementById(div_id).innerHTML = ERROR_LONG_NAME;
		}
	    else if(error_code  == NAME_INVALID_CHARACTER)
	      {                 
		document.getElementById(div_id).innerHTML = ERROR_NAME_CHARACTER;
	      }
	    else if(error_code  == NAME_INVALID_STARTCHARACTER)
	      {                 
		document.getElementById(div_id).innerHTML = ERROR_NAME_STARTCHARACTER;
	      }
	    else
		{
		    document.getElementById(div_id).innerHTML = '';
		}
	}
  
    if(div_id == 'error_verify_code')
	{
	    if(error_code  == EMPTY_FIELDS)
		{                 
		    document.getElementById(div_id).innerHTML = ERROR_EMPTY_CAPTCHA;
		}
	    else if(error_code  == CAPTCHA_NOT_VALID)
		{                 
		    document.getElementById(div_id).innerHTML = ERROR_CAPTCHA_NOT_VALID;
		}
	    else
		{
		    document.getElementById(div_id).innerHTML = '';
		}
    }
    
    if (div_id == 'error_description')
	{
	    if (error_code == EMPTY_FIELDS)
		{
		    document.getElementById(div_id).innerHTML = ERROR_EMPTY_DESCRIPTION;
		}
	    else
		{
		    document.getElementById(div_id).innerHTML = '';
		}
	}
    
    if (div_id == 'error_tags')
	{
	    if (error_code == INVALID_TAGS)
		{
		    document.getElementById(div_id).innerHTML = ERROR_INVALID_TAGS;
		}
	    else
		{
		    document.getElementById(div_id).innerHTML = '';
		}
	}
    
    if(div_id == 'error_url')
	{
	    if(error_code  == EMPTY_FIELDS)
		{                 
		    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 = '';
		}
	}
    
    if(div_id == 'error_features')
	{
	    if(error_code  == EMPTY_FIELDS)
		{                 
		    document.getElementById(div_id).innerHTML = ERROR_EMPTY_FEATURES;
		}
	    else
		{
		    document.getElementById(div_id).innerHTML = '';
		}
	}
    if(div_id == 'error_category')
	{
	    if(error_code == EMPTY_FIELDS)
		{                 
		    document.getElementById(div_id).innerHTML = ERROR_EMPTY_CATEGORY;
		}
	    else
		{
		    document.getElementById(div_id).innerHTML = '';
		}
	}
}


//Function for selection of one box to another
// It is assumed that the select list is in sort sequence initially
var singleSelect = true;  // Allows an item to be selected once only
var sortSelect = true;  // Only effective if above flag set to true
var sortPick = true;  // Will order the picklist in sort sequence

function initIt()
{
    var selectList = document.getElementById("SelectList");
    var selectOptions = selectList.options;
    var selectIndex = selectList.selectedIndex;
    var pickList = document.getElementById("PickList");
    var pickOptions = pickList.options;
    pickOptions[0] = null;  // Remove initial entry from picklist (was only used to set default width)
    if (!(selectIndex > -1))
	{
	    selectOptions[0].selected = true;  // Set first selected on load
	    selectOptions[0].defaultSelected = true;  // In case of reset/reload
	}
    selectList.focus();  // Set focus on the selectlist
}

// Adds a selected item into the picklist
function addIt() 
{
    var selectList = document.getElementById("SelectList");
    var selectIndex = selectList.selectedIndex;
    var selectOptions = selectList.options;
    var pickList = document.getElementById("PickList");
    var psselectIndex = pickList.selectedIndex;
    var pickOptions = pickList.options;
    var pickOLength = pickOptions.length;
	
    //To check whether more than 3 categories are not allowed. If already 3 are being selected then return false.
    if (pickOLength == 3)
	{
	    alert ("Not More than 3 categories are allowed");
	    return false;
	}
    
    // An item must be selected
    while (selectIndex > -1) 
	{
	    
	    pickOptions[pickOLength] = new Option(selectList[selectIndex].text);
	    pickOptions[pickOLength].value = selectList[selectIndex].value;
	    
	    // If single selection, remove the item from the select list
		if (singleSelect) 
		    {
			selectOptions[selectIndex] = null;
		    }
		if (sortPick) 
		    {
			var tempText;
			var tempValue;
			// Sort the pick list
			while (pickOLength > 0 && pickOptions[pickOLength].value < pickOptions[pickOLength-1].value) 
			    {
				tempText = pickOptions[pickOLength-1].text;
				tempValue = pickOptions[pickOLength-1].value;
				pickOptions[pickOLength-1].text = pickOptions[pickOLength].text;
				pickOptions[pickOLength-1].value = pickOptions[pickOLength].value;
				pickOptions[pickOLength].text = tempText;
				pickOptions[pickOLength].value = tempValue;
				pickOLength = pickOLength - 1;
			    }//end of while (pickOLength > 0 && pickOptions[pickOLength].....
			
		    }//end of if (sortPick) 
		
		selectIndex = selectList.selectedIndex;
		pickOLength = pickOptions.length;
	}//end of while (selectIndex > -1) 
    
    if(selectOptions.length > 0)
	{
	    selectOptions[0].selected = true;
	}
}


function addOption(selectbox,text,value )
{
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}

function removeAllOptions(selectbox)
{
    selectbox.options.length = 0;
}


//ADDING ALL THE ITEMS FROM THE LIST AT ONCE
function addAllIt(SelectList) 
{
    var i;
    var val=0;
    for(i=document.theform.SelectList.options.length - 1; i >=0; i--) 
	{
	    var SelectList=document.theform.SelectList;
	    addOption(document.theform.PickList, document.theform.SelectList[val].text, document.theform.SelectList[val].value);
	    val++;
	}
    
    removeAllOptions(document.theform.SelectList);
}


//DELETING ALL THE ITEMS FROM THE LIST AT ONCE
function removeAllIt(PickList) 
{
    var i;
    var val=0;
    for(i=document.theform.PickList.options.length - 1;i >=0; i--) 
	{
	    var PickList=document.theform.PickList;
	    addOption(document.theform.SelectList,document.theform.PickList[val].text, document.theform.PickList[val].value);
	    val++;
	}

	removeAllOptions(document.theform.PickList);
}


// Deletes an item from the picklist
function delIt() 
{
    var selectList = document.getElementById("SelectList");
    var selectOptions = selectList.options;
    var selectOLength = selectOptions.length;
    var pickList = document.getElementById("PickList");
    var pickIndex = pickList.selectedIndex;
    var pickOptions = pickList.options;
    while (pickIndex > -1) 
	{
	    // If single selection, replace the item in the select list
	    if (singleSelect) 
		{
		    selectOptions[selectOLength] = new Option(pickList[pickIndex].text);
		    selectOptions[selectOLength].value = pickList[pickIndex].value;
		}
	    
		pickOptions[pickIndex] = null;
		
		if (singleSelect && sortSelect) 
		    {
			var tempText;
			var tempValue;
			// Re-sort the select list
			while (selectOLength > 0 && selectOptions[selectOLength].value < selectOptions[selectOLength-1].value) 
			    {
				tempText = selectOptions[selectOLength-1].text;
				tempValue = selectOptions[selectOLength-1].value;
				selectOptions[selectOLength-1].text = selectOptions[selectOLength].text;
				selectOptions[selectOLength-1].value = selectOptions[selectOLength].value;
				selectOptions[selectOLength].text = tempText;
				selectOptions[selectOLength].value = tempValue;
				selectOLength = selectOLength - 1;
		  }//end of while (selectOLength > 0 && selectOptions.....
		}//end of if (singleSelect && sortSelect) 
		pickIndex = pickList.selectedIndex;
		selectOLength = selectOptions.length;
	}//end of while (pickIndex > -1) 
}

// Selection - invoked on submit
function selIt(btn) 
{
    var pickList = document.getElementById("PickList");
    var pickOptions = pickList.options;
    var pickOLength = pickOptions.length;
    if (pickOLength > 0) 
	{
	    for (var i = 0; i < pickOLength; i++) 
		{
		    pickOptions[i].selected = true;
		}
	}
    
    return true;
}

function adda_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 upload_adda_photo(frm)
{
	var frm=document.getElementById('frm_upload');
	frm.action = ""+system_base_url+"adda/_edit/save_adda_picture";
	if(document.getElementById('file').value != '')
	{
		frm.submit();
	}
	

}

