/***
    @purpose    Checks if all the mandatory inputs where filled out
 ***/
function checkMandatoryInputs(element,classname,myMessage){
    //first check if the mandatory fields are filled
    var elements = $(element).getElementsByClassName(classname);    
    var myStop   = false;
    
    if(!classname){
        classname = 'mandatory';
    }
    
    for(var i = 0; i < elements.length; i++){
        if(elements[i].value.length < 1 || (elements[i].type == 'checkbox' && elements[i].checked == false)){
            elements[i].style.backgroundColor = '#ffdddd';  
            myStop = true;            
        }else{
            elements[i].style.backgroundColor = null;  
        }
    }
    
    
    if(myStop){
        if(!myMessage){
            myMessage = 'Please fill out all the mandatory fields (marked pink)!'; 
        }
        alert(myMessage);
        return false;
    }
    
    return true;
}



/***
    @purpose    works just like the php function
 ***/
function str_replace(search, replace, subject){
	return subject.split(search).join(replace);
}


/***
    @purpose    Open a Window
 ***/
function open_win(url,width,height) {
	f = open(url,"","width="+width+",height="+height+",toolbar=0,locationbar=0,status=0,menubar=0,screenX=10,screenY=10,scrollbars,resizable=yes");
}


/***
    @purpose    Clone Table Row
 ***/
function cloneRow(tid,pos){

    if(!pos){
        pos = document.getElementById(tid).getElementsByTagName('tbody')[0].getElementsByTagName('tr').length - 1;
    }

    var tab   = document.getElementById(tid);
    var root  = tab.getElementsByTagName('tr')[pos].parentNode;     //the TBODY
	var clone = tab.getElementsByTagName('tr')[pos].cloneNode(true);//the clone of the first row	 
    root.appendChild(clone);//appends the clone 
}





/***
    @purpose    Select Manipulation
 ***/
//clear what we have
function selectClear(obj){
  while(obj.lastChild) obj.removeChild(obj.lastChild);
}


//create new nodes
function selectAdd(obj, text, value){
  var item = document.createElement("option");
  if(null !== value) item.setAttribute("value", value);
  item.appendChild(document.createTextNode(text));
  obj.appendChild(item);
}
