// JavaScript Document
var today = new Date();

function getCurrentDate() {
	return today.getDate();	
}

function getCurrentMonth() {
	return today.getMonth();	
}

function getCurrentYear() {
	return today.getFullYear();	
}

Array.prototype.inArray = function(search_term) {
	var i = this.length;
	if ( i > 0 )
		do {
			if (this[i] === search_term) {
				return true;
			}
		} while (i--);
		
	return false;
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for single object (IE)
//other browsers can use this (no need to use changeOpacMultiObjects())
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

//change the opacity for multiple objects (IE only)
//other browsers can use changeOpac() instead of this
function changeOpacMultiObjects(opacity, id) {
    var object = document.getElementById(id).style;
	object.width = "100%";	
	object.background = "#fff";
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(tagId, millisec) {
    if ( document.getElementById('tagId').style.opacity == 0 ) {
        opacity ( tagId, 0, 100, millisec );
    } else {
        opacity ( tagId, 100, 0, millisec);
    }
}

function setBackground(elementID, imgURL) {
	document.getElementById(elementID).style.background = 'url(' + imgURL + ')';
}

function checkAll(field) {
	if ( field.length > 1 ) {
		for (i = 0; i < field.length; i++)
			field[i].checked = true ;
	}
	else field.checked = true;
}

function uncheckAll(field) {	
	if ( field.length > 1 ) {
		for (i = 0; i < field.length; i++)
			field[i].checked = false ;
	}
	else field.checked = false;
}

function setDisabledTrue(elementID) {
	document.getElementById(elementID).disabled = 'disabled';
}

function setDisabledFalse(elementID) {
	document.getElementById(elementID).disabled = false;
}

function displayNone(elementID) {
	document.getElementById(elementID).style.display = 'none';
}

function displayBlock(elementID) {
	document.getElementById(elementID).style.display = 'block';
}

function displayInline(elementID) {
	document.getElementById(elementID).style.display = 'inline';
}

function setContent(elementID, content) {
	document.getElementById(elementID).innerHTML = content;
}

function getContent(elementID, content) {
	return document.getElementById(elementID).innerHTML;
}

function getValueById(elementID) {
	return document.getElementById(elementID).value;
}

function setValueById(elementID, val) {
	document.getElementById(elementID).value = val;
}

function setFocusById(elementID) {
	document.getElementById(elementID).focus();
}

function toggleCheckbox(chboxID) {
    if ( chboxID.checked ) chboxID.checked = false;
	else chboxID.checked = true;
}

function toggleVisibility(elementID) {
	var obj = document.getElementById(elementID);
	if ( obj.style.display == "none" ) obj.style.display = "block";
	else obj.style.display = "none";
}

function hideAndShow(toHide,toShow) {
    displayNone(toHide);
	displayBlock(toShow);
}

function getCheckedRadioValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// URL : http://www.breakingpar.com/bkp/home.nsf/0/CA99375CC06FB52687256AFB0013E5E9
function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
}

// URL : http://www.breakingpar.com/bkp/home.nsf/0/CA99375CC06FB52687256AFB0013E5E9
// USAGE: var checkBoxArr = getSelectedCheckbox(document.forms[0].MyCheckBox);
function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var values = new Array();
   var selectedItems = getSelectedCheckbox(buttonGroup);
   
   if (selectedItems.length != 0) { // if there was something selected
      values.length = selectedItems.length;
	  
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            values[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            values[i] = buttonGroup.value;// return that value
         }
      }
   }
   return values;
} // Ends the "getSelectedCheckBoxValue" function

/**
 * @author Yonny Sutanto ys@ys-media.com
 * This function will remove "toRemove" from "str"
 * "toRemove" can be a char or strings
 * @return cleaned "str"
 */
function removeString(str, toRemove) {
    if ( toRemove.length == 1 ) str = RemoveThis(str, toRemove);
    else {
		for ( i = 0; i < toRemove.length; i++ ) {
			str = RemoveThis(str, toRemove[i]);
		}
	}
    
	function RemoveThis(str, remThis) {

	    var x = str.indexOf(remThis);
    	var clean = "";

	    if (x == -1) return str;
    	clean += str.substring(0,x) + removeString(str.substring(x + remThis.length), remThis);	
	
	    return clean;
   }		
   
   return str;
}

function validateString(str, validChars) {

	var validNum = "";
		
	for ( i = 0; i < str.length; i++ ){			
		if ( validChars.indexOf(str.charAt(i)) != -1 ) validNum += str.charAt(i);
	}
	return validNum;
}

function validateMoney(inputNum, decimal, decimalPoint) {

	var money = removeString(inputNum.value, ",.");
	money = validateString(money, "0123456789");
	
	if ( money.length > 3 ) {				
		var x = 0;
		var y = (money.length % 3) == 0 ? 3 : money.length % 3;
		
		var moneyArr = new Array();
		var i = 0;		

	    while ( y <= money.length ) {		    
			moneyArr[i] = money.substring(x,y);
			x = y;
			y = x + 3;
			i++;						
		}
		
		money = '';

		for ( i = 0; i < moneyArr.length; i++ ) {
			if ( i > 0 ) money += ",";
			money += moneyArr[i];			
		}		
	}
	inputNum.value = money;
}

function allowOnlyNumbers(val) {
	var validChars = "0123456789";
	var ret = validateString(val, validChars);
	return ret;
}

function addValue(add, limit, elementID) {
	var val = parseInt( getValueById(elementID) );
	
	if ( val == '' ) val = 0;
	
	var res = val + add;
	
	if ( res <= limit )	setValueById(elementID, res);
}

function subtractValue(subtract, limit, elementID) {
	var val = parseInt( getValueById(elementID) );
	
	if ( val == '' ) val = 0;
	
	var res = val - subtract;
	
    if ( res >= limit ) setValueById(elementID, res);
	else setValueById(elementID, limit);
}

function isValidEmail(email) {

	var valid = true;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	
	for (i = 0; i < email.length; i++) {
    	var letter = email.charAt(i).toLowerCase();
    	if (validchars.indexOf(letter) != -1) continue;
		else {
			valid = false;
			break;
		}
    }

    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        valid = false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        valid = false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        valid = false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
		valid = false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
		valid = false;
    }
	
    return valid;
}

function isValidDate(dd, mm, yyyy) {
	var valid = true;
	var leapYear = false;
	
	if ( yyyy == '' ) valid = false;
	else {
		if ( (yyyy % 4) == 0 ) leapYear = true;
	}
	
	if ( mm == 2 || mm == 4 || mm == 6 || mm == 9 || mm == 11 ) {
		if ( mm == 2 ) {
			if ( leapYear ) {
				if ( dd > 29 ) valid = false;	
			}
			else {
				if ( dd > 28 ) valid = false;
			}
		}
		else {
			if ( dd > 30 ) valid = false;	
		}
	}
	
	// Just in case ;)	
	if ( dd < 1 || dd > 31 ) valid = false;
	if ( mm < 1 || mm > 12 ) valid = false;

	return valid;
}

function setCookie(name, value, nSec) {
	var today = new Date();
	var expire = new Date();
	
	// Since JS cookie is in microtime, we need to change it to second (nSec*1000)
	expire.setTime( today.getTime() + nSec*1000 );

	document.cookie = name + "=" + escape(value) + ";expires=" + expire;
}

function setCookieFull(name, value, nSec, path, domain, secure) {
	var today = new Date();
	var expire = new Date();
	
	// Since JS cookie is in microtime, we need to change it to second (nSec*1000)
	expire.setTime( today.getTime() + nSec*1000 );

	document.cookie = name + "=" + escape(value) + ";expires=" + expire +
	                  ( ( path ) ? ";path=" + path : "" ) + 
 					  ( ( domain ) ? ";domain=" + domain : "" ) +
					  ( ( secure ) ? ";secure" : "" );	
}

function getCookie(name) {

	var cookieName = name + "=";
	var ca = document.cookie.split(';');

	for( var i = 0; i < ca.length; i++ ) {

		var c = ca[i];

		while (c.charAt(0)==' ') c = c.substring(1,c.length);

		if ( c.indexOf(cookieName) == 0 ) return ( c.substring(cookieName.length,c.length) );
	}
	
	return null;
}

function implode(glue, arr) {
	
	var ret = '';
	for ( var i=0; i<arr.length; i++ ) {		
		if ( i != (arr.length-1) ) ret += arr[i] + glue;
		else ret += arr[i];
	}
	return ret;
}