/**
 * Function to clear form fields
 * 
 * @param formName the form which fields should be cleared
 * 
 * @return
 */
function clearFormFields(formName) {
	var form = document.getElementById(formName);
	for (i = 0; i < form.elements.length; i++) {
		var element = form.elements[i];
		
		if (!element.disabled) {
			clearField(element);
		}
	}
}

/**
 * Function to clear a specific form field
 * 
 * @param element the form field to clear
 * 
 * @return
 */
function clearField(element) {
	var type = element.type;
	
	if (type == "text" || type == "textarea") {
		element.value = "";
	} else if (type == "select-one") {		
//		element.options.selectedIndex = 0;
		findOptionToSelect(element);
		greyDisabled(element);
	} else if (type == "checkbox") {
		element.checked = false;
	} else if (type == "radio") {
		if (element.checked == true) {
			element.checked = false;
			findRadioToSelect(element.name);
		}
	}
}

/**
 * Function to find the next possible radio button to select
 * 
 * @param element the radio button element being disabled
 * 
 * @return
 */
function findRadioToSelect(elementName) {
	var elements = document.getElementsByName(elementName);
	
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].disabled != true) {
			elements[i].checked = true;
			break;
		}
	}
}

function findOptionToSelect(element){
	var optNumber = element.options.selectedIndex;
	if (element.options[optNumber].disabled == true){
		element.options.selectedIndex = 0;
	}
}

function greyDisabled(element) {
	for (var i = 0; i < element.size; i++){
		if (element.options[i].disabled == true){
			element.options[i].style.color="#ffffff";
			document.write(element.options[i].text+"<br>");
		}
	}

}
/**
 * Function to change an images source value
 * 
 * @param obj the image whose source to change
 * @param newSource the new source value
 * 
 * @return
 */
function changeImgSource(obj, newSource) {
	if (newSource != null && newSource != "") {
		obj.src = newSource;
	}
}

/**
 * Function to open a pop-up
 * 
 * @param source the source of the pop-up
 * @param width the width of the pop-up
 * @param height the height of the pop-up
 * 
 * @return false
 */
function openPopup(source, width, height) {
	var newWindow = window.open(source, "newWindow", "toolbar=no,location=no,directories=no,status=yes, " +
								"menubar=no,scrollbars=yes,resizable=yes,width=" + width + ",height=" + height +
								",left=20,top=20");
	newWindow.focus();
	return false;
}

/**
 * Function to open a fixed pop-up
 * 
 * @param source the source of the pop-up
 * @param width the width of the pop-up
 * @param height the height of the pop-up
 * 
 * @return false
 */
function openFixedPopup(source, width, height) {
	var newWindow = window.open(source, "newWindow", "toolbar=no,location=no,directories=no,status=no, " +
								"menubar=no,scrollbars=no,resizable=no,width=" + width + ",height=" + height +
								",left=20,top=20");
	newWindow.focus();
	return false;
}

/**
 * Function to refresh the parent window and close the pop-up
 * 
 * @param msg the message to set for the parent
 * 
 * @return
 */
function closePopup(msg, refreshParent) {
	if (refreshParent) {
//		window.opener.document.back.action += "?msg=" + msg;
//		window.opener.document.back.offset.value = 0;
//		window.opener.document.back.submit();
		try {
			window.opener.document.forms[0].action += "?msg=" + msg;
			window.opener.document.forms[0].offset.value = 0;
			window.opener.document.forms[0].submit();
		} catch(error) {
			window.opener.document.forms[0].submit();
		}
	}
	
	self.window.close();
}


/**
 * Function to refresh the parent window and close the pop-up
 * 
 * @param msg the message to set for the parent
 * 
 * @return
 */
function closePopupFillParent(refreshParent) {
	if (refreshParent) {
		window.opener.document.forms[1].submit();
	}	
	self.window.close();
}

/**
 * Function to submit paging navigation
 * 
 * @return
 */
function submitpageble() {
	document.back.submit();
}

/**
 * Function to get an attributes value from an object
 * 
 * @param source the object to get the value from
 * @param name the attribute name to get value for
 * 
 * @return the found attribute value
 */
function getAttributeValue(source, name) {
	try {
		return source.attributes.getNamedItem(name).value;
	} catch (error) {
		return undefined;
	}
}

/**
 * Function to switch an elements display
 * 
 * @param name the name of the element to switch
 * 
 * @return
 */
function switchLayer(name) {
	var element = document.getElementById(name);
	
	if (element.style.display == "none" || element.style.display == "") {
		element.style.display = "block";
	} else {
		element.style.display = "none";
	}
	
	DatePickerControl.relocateButtons();
}

function switchLayerAndImg(name, btn){
	switchLayer(name);
	img = document.getElementById(btn);
	if (img.src.match("expand") != null){
		temp = img.src.replace(/expand/,"delete");
		img.src = temp;
	} else if (img.src.match("delete") != null){
		temp = img.src.replace(/delete/,"expand");
		img.src = temp;
	}
}
function parentNavigateTo(url) {
	window.opener.window.location = url;
}

function navigateTo(url) {
	window.location = url;
}

function changeVisibility(name) {
	var element = document.getElementById(name);
	if (element != undefined) {
		if (element.style.visibility == "hidden") {
			element.style.visibility = "";
		} else {
			element.style.visibility = "hidden";
		}
	}
}

function navigateWithValue(url, name) {
	var element = document.getElementById(name);
	var value;
	
	if (element.type == "select-one") {
		value = element.options[element.selectedIndex].value;
	}
	
	url += "&" + name + "=" + value;
	
	navigateTo(url);
}

function submitPagebleWithValues(names, values) {
	for (var i = 0; i < names.length; i++) {
		var el = document.getElementById(names[i]);
		el.value = values[i];
	}
	
	submitpageble();
}

function switchTabs(tab, newStyle) {
	tab.className = newStyle;
}

function search() {
	document.back.offset.value = 0;
	document.back.submit();
}

function submitConfirm(value) {
	var form = document.getElementById("frmb");
	
	var input = document.createElement("input");
	input.setAttribute("name", value);
	input.setAttribute("value", value);
	input.setAttribute("type", "hidden");
	
	form.appendChild(input);
	form.submit();
}

/*
function submitOnEnter(formName, e) {
	
	var key;
	
	if (e && e.which) {
		key = e.which;
	} else {
		key = event.keyCode;
	}
	
	if (key == 13) {
		document.getElementById(formName).submit();
	}
	
	
	if (e && e.keyCode) {
		if (e.keyCode == 13) {
			document.getElementById(formName).submit();
		}
	}
}
*/

function submitOnEnter(element, event) {
	if (event && event.keyCode) {
		if (event.keyCode == 13) {
			element.onclick();
		}
	}
}

function insertParam(key, value) {
    key = escape(key); value = escape(value);
    
    var params = document.location.search.substr(1).split('&');

    var i = params.length; 
    var x; 
    
    if (i == 1 && params[0] == "") {
    	params[0] = [key, value].join('=');
    } else {
    	while (i--) {
    		x = params[i].split('=');

    		if (x[0] == key) {
    			x[1] = value;
    			params[i] = x.join('=');
    			break;
    		}
    	}

    	if (i < 0) {
    		params[params.length] = [key, value].join('=');
    	}
    }

    //this will reload the page, it's likely better to store this until finished
    document.location.search = params.join('&'); 
}

function checkSkype(start, end) {
	var now = new Date();
	
	if (start <= now.getUTCHours() && now.getUTCHours() < end) {
		$(".skype-write").show("slow");
	} else {
		$(".skype-write").hide("slow");
	}
}

/**
 * Fix for IE documentGetElementById which returns objects with the given NAME or ID
 * which is not compliant with the W3C standard
 * Supposedly this is going to change in IE8
 * Fix from  http://msdn2.microsoft.com/en-us/library/ms536437(VS.85).aspx
 */
if (/msie/i.test (navigator.userAgent)) {
	//only override IE 
	document.nativeGetElementById = document.getElementById;   
	document.getElementById = function(id) {    
		var elem = document.nativeGetElementById(id);    
		if(elem) {      
			//make sure that it is a valid match on id      
			if(elem.attributes['id'].value == id) {      
				return elem;
			} else {       
				//otherwise find the correct element        
				for(var i=1;i<document.all[id].length;i++) {          
					if(document.all[id][i].attributes['id'].value == id) {            
						return document.all[id][i];          
					}        
				}      
			}    
		}    
		
		return null;  
	}
}
