
/* Common String Functions */

function trim(stringToTrim) {
	if(stringToTrim == '') return '';

	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	if(stringToTrim == '') return '';

	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	if(stringToTrim == '') return '';

	return stringToTrim.replace(/\s+$/,"");
}
function popupWindow(url,windowName,params){

	if(trim(params) == ''){
		params = 'width=650,height=600,scrollbars=1';
	}
	return window.open(url,windowName,params);
}

/* Common Ajax related functions */
function showLoadingInTable(elemID){
	var tbody = document.getElementById(elemID);
	var tr = tbody.insertRow(tbody.rows.length);
	var td = tr.insertCell(tr.cells.length);
	td.className = 'formText';
	td.innerHTML = '<img src="' + relativePath() + 'images/tiny_red.gif" border="0"/>&nbsp;Loading....';
}

function showLoading(elem_id){
	Element.update(elem_id,'<img src="' + relativePath() + 'images/tiny_red.gif" border="0"/>&nbsp;Loading...');
}

/* Dynamic Group, IPName and Section drop downs */
function updateSections(section_type,parent_id,dropDownName,elem_id){
	if(parent_id <= 0){
		if(section_type == 'cat'){
			Element.update('cat','Select a group');
			Element.update('ipname','Select a category');
		}else{
			Element.update('ipname','Select a category');
		}
		return;
	}

	if ('object' == typeof(document.forms['admLoginFrm'])) {
		if(parent_id == 1 || parent_id == 75){
			document.getElementById('platlabelname').style.display = 'block';
			document.getElementById('platcolon').style.display = 'block';
			document.getElementById('platdropdown').style.display = 'block';
		}
		else {
			document.getElementById('platlabelname').style.display = 'none';
			document.getElementById('platcolon').style.display = 'none';
			document.getElementById('platdropdown').style.display = 'none';
		}
	}

	var url = 'ajax.php';
	var params = 'section_type=' + section_type + '&parent_id=' + parent_id + '&name=' + dropDownName;
	var myAjax = new Ajax.Request(
						url,
						{
							method: 'post',
							parameters: params,
							onLoading: showLoading(elem_id),
							onLoaded: showLoading(elem_id),
							onInteractive: showLoading(elem_id),
							onComplete: function(originalResponse){
										  showSectionResponse(elem_id,originalResponse)
										}
						}
				);
}

function showSectionResponse(elem_id,resp){
	if(resp.responseText.indexOf('<option value=') == -1){
		var list = resp.responseText;
		if(elem_id == 'cat'){
			Element.update('ipname','Select a category');
		}
	}else{
		if(elem_id == 'cat'){
			var list = '<select name="cat_id" onChange="updateSections(\'ipname\',this.value,\'sections_id\',\'ipname\');">' + resp.responseText + '</select>';
			Element.update('ipname',"Select a category");
		}else{
			var list = '<select name="sections_id">' + resp.responseText + '</select>';
		}
	}
	Element.update(elem_id,list);
}

function updateCategories(group_id,dropDownName,elem_id){
	var url = 'ajax.php';
	var params = 'group_id=' + group_id + '&name=' + dropDownName;
	var myAjax = new Ajax.Updater(
						{success: elem_id},
						url,
						{
							method: 'post',
							parameters: params,
							onLoading: showLoading(elem_id),
							onLoaded: showLoading(elem_id),
							onInteractive: showLoading(elem_id)
						}
				);
}

/**
 * Only first 50 words of an IP summary will be displayed
 * This will toggle the view of the rest of the summary
 * it will return false so that the href when clicked will
 * just perform this function and not go anywhere.
 */
function showSummary(id){
	if($('moreText'+id).style.visibility == 'hidden'){
		$('moreText'+id).style.visibility = 'visible';
		$('moreText'+id).style.display = 'inline';
		$('moreLink'+id).innerHTML = '&nbsp;&nbsp;&nbsp;view compact';
	}else{
		$('moreText'+id).style.visibility = 'hidden';
		$('moreText'+id).style.display = 'none';
		$('moreLink'+id).innerHTML = '&nbsp;&nbsp;&nbsp;...view full';
	}

	return false;
}

function showKeys(id){

if($('moreText'+id).style.visibility == 'hidden'){
		$('moreText'+id).style.visibility = 'visible';
		$('moreText'+id).style.display = 'inline';
		$('moreLink'+id).innerHTML = '&nbsp;&nbsp;&nbsp;view compact';
	}else{
		$('moreText'+id).style.visibility = 'hidden';
		$('moreText'+id).style.display = 'none';
		$('moreLink'+id).innerHTML = '&nbsp;&nbsp;&nbsp;...view full';
	}

}

/**
 * given the URL, params and ID of the element in which the result
 * should be displayed make the AJAX call
 */
function showInline(url,params,elemID){
	var myAjax = new Ajax.Updater(
				{success: elemID},
				url,
				{
					method: 'post',
					parameters: params,
					onLoading: showLoading(elemID),
					onLoaded: showLoading(elemID),
					onInteractive: showLoading(elemID)
				}
			);

}

/* given an element id clear its contents */
function collapseElement(elemID){
	$(elemID).innerHTML = "";
}

/**
 * Given the form in which the checkboxes exist, the checkboxes name (usually an array name)
 * the optional confirmation message to be shown the error message to be shown if the required
 * number of check boxes are not checked and the minimum number of checkboxes that should be
 * checked to be considered as a success.
 */
function isAnyCheckboxSelected(frmName,checkboxName,msgConfirm,msgFalse,min){
	var frm = document.forms[frmName];
	var count = 0;
	if(isNaN(min) || min <= 0) min = 1;
	for(var i=0;i<frm.elements.length;i++){
		if(frm.elements[i].type == "checkbox" && (frm.elements[i].name == checkboxName || frm.elements[i].name == checkboxName+'[]') && frm.elements[i].checked){
			count ++;
			if(count >= min){
				if(msgConfirm != ''){
					return confirm(msgConfirm);

				}else{
					return true;

				}
			}
		}
		if(count > min){
			break;

		}

	}
	if(msgFalse != ''){
		alert(msgFalse);

	}
	return false;
}

/**
 * return the relative path to the root from the current location
 */
function relativePath(){
	var current = location.href;
	if(current.indexOf('buyers') == -1 && current.indexOf('sellers') == -1 && current.indexOf('admin') == -1){
		return '';
	}

	return '../';
}




	
