function padLeft(value, no) {
	for (i = 0; i < no - value.length; i++) {
		value = '0' + value;
	}
	return value;
}

/**
 * Function to read a cookie from the users browser.
 **/
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

/**
 *Function to set a cookie for the user
 **/
function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

/**
 * Function to remove a users cookie.
 **/
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}


// Move options from one select list to another
function moveOption ( objSourceElement, objTargetElement ) {
	var aryTempSourceOptions = new Array();
	var x = 0;
	
	//looping through source element to find selected options
	for (var i = 0; i < objSourceElement.length; i++) {
		if (objSourceElement.options[i].selected) {
			//need to move this option to target element
			var intTargetLen = objTargetElement.length++;
			objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
			objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;
		}
		else {
			//storing options that stay to recreate select element
			var objTempValues = new Object();
			objTempValues.text = objSourceElement.options[i].text;
			objTempValues.value = objSourceElement.options[i].value;
			aryTempSourceOptions[x] = objTempValues;
			x++;
		}
	}
	
	//resetting length of source
	objSourceElement.length = aryTempSourceOptions.length;
	//looping through temp array to recreate source select element
	for (var i = 0; i < aryTempSourceOptions.length; i++) {
		objSourceElement.options[i].text = aryTempSourceOptions[i].text;
		objSourceElement.options[i].value = aryTempSourceOptions[i].value;
		objSourceElement.options[i].selected = false;
	}
}

function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}


// Usage: addFormating("U")
// Options:
// "U" - underline
// "B" - bold
// "I" - italic
function addFormating(obj,tag) {
	var selectedText = "";
	tag = "@"+tag;
	if (obj.setSelectionRange) {
		selectedText = obj.value.substring(obj.selectionStart,obj.selectionEnd)
		obj.value = obj.value.substring(0,obj.selectionStart) + tag + selectedText + tag + obj.value.substring(obj.selectionEnd,obj.value.length)
	} else {
		selectedText = obj.selection.createRange().text; 
		if (selectedText != "") {
			var newText = tag + selectedText + tag;
			obj.selection.createRange().text = newText;
		} 
	}
}

function charCounter(txtBox,counter,texto,chars) {
  var txtBoxObj = $(txtBox);
  var counterObj = $(counter);
	if ( txtBoxObj && counterObj ) {
		var len = chars - txtBoxObj.value.length;
		if(len <= 0) {
			len=0;
			texto='<span class="disable"> '+texto+' </span>';
			txtBoxObj.value=txtBoxObj.value.substr(0,chars);
		}
		counterObj.innerHTML = texto.replace("{CHAR}",len);
	}
}

// Son of suckerfish IE6 fix
sfHover = function() {
	var nav = document.getElementById("nav");
	if(nav){
		var sfEls = nav.getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover);



function toggleVisibility(eleIdToToggle, eleIdOfButton){
	var toggleEle = document.getElementById(eleIdToToggle);
	var btnEle = document.getElementById(eleIdOfButton);
	if(toggleEle){

		var isVisible;
		if (toggleEle.style.display == 'none'){
			toggleEle.style.display = '';
			isVisible = true;
		}else {
			toggleEle.style.display = 'none';
			isVisible = false;
		}

		if(!isVisible){
			btnEle.className = btnEle.className.replace("Min", "Max");
			setCookie(eleIdToToggle, 'invisible', 30, "/");
		}else{
			btnEle.className = btnEle.className.replace("Max", "Min");
			setCookie(eleIdToToggle, 'visible', 30, "/");
		}
	}
}




/**
 * Checks that the search prepare page form has some criteria specified when a user clicks on the submit button.
 */
function checkFormElements() {
	error = '';
	returnVal = false;
	returnVal2 = false;
	elements = Array('sire', 'dam', 'servingSire', 'vendor', 'sex', 'lotNo1', 'lotNo2', 'lotSet', 'bonusScheme', 'lot_type');
	for (var i=0;i<elements.length; i++) {
		if (elements[i] == 'sex') {
			var sexElements = document.getElementsByName(elements[i]);
			for (var j = 0; j < sexElements.length; j++) {
				if (sexElements[j].checked)
					returnVal = true;
			}
		}
		else if ($(elements[i]) != null) {
			//alert(i + ' ' + elements[i] + ' value = ' + $(elements[i]).value);
		    if ($(elements[i]).value != 0) {
				returnVal = true;
			}
			else {
				error = 'Please select at least one criteria.\n';
			}
		}
		else {
			error = 'Please select at least one criteria.\n';
		}
		//alert(i + ' ' + elements[i]);
	}
	var dateString1 = $('dobYear1').value + padLeft($('dobMonth1').value, 2) + padLeft($('dobDay1').value, 2);
	var dateString2 = $('dobYear2').value + padLeft($('dobMonth2').value, 2) + padLeft($('dobDay2').value, 2);
	//alert(dateString1 + ' ' + dateString2);
	//alert((dateString2 > dateString1) || ($('dateRange').checked != 'checked') + " " + $('dateRange').checked);
	if ($('dateRange').checked) {
		if (dateString2 > dateString1)
			returnVal2 = true;
		else
			error += 'Birth date from must be smaller than birth date to.\n';
	}

	if (!(returnVal || returnVal2))
		alert(error);
	//alert(returnVal && returnVal2 ? 'true' : 'false');
	return returnVal || returnVal2;
}

/**
 * Function to filter a html table, by hiding rows that don't contain the text that you filter on.
 * 
 * textArea - The id of the txtBox where you type your search filter
 * _id - the id of the html table to filter on.
 */
function filter2 (textArea, _id){
	var words = document.getElementById(textArea).value.toLowerCase().split(" ");
	var table = document.getElementById(_id);
	var ele;
	var darkLightRegex = /(dark|light)/gi;
	var numOfVisibleRows = 0;

	for (var r = 1; r < table.rows.length; r++){
		ele = table.rows[r].innerHTML.replace(/<[^>]+>/g,"");
		var displayStyle = 'none';
		for (var i = 0; i < words.length; i++) {
			if (ele.toLowerCase().indexOf(words[i])>=0){
				displayStyle = '';
			}else {
				displayStyle = 'none';
				break;
			}
		}
		table.rows[r].style.display = displayStyle;

		if(displayStyle == ''){
				//if the row is made visible, increase the counter
				numOfVisibleRows ++;

				if(numOfVisibleRows%2 == 0){	//alternate the row className between dark & light (for visible rows)
					table.rows[r].className = table.rows[r].className.replace(darkLightRegex, 'dark');
				}else{
					table.rows[r].className = table.rows[r].className.replace(darkLightRegex, 'light');
				}
		}

	}
}

// simple window popup
function simplePopup(theURL) {
  window.open(theURL,'window','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=760,height=450,left=10,top=10');
}

