//====================================
//
// Common Functions
//
//====================================

//============================================================
//
// Force Refresh
//
function forceRefresh() {
	
	var urlAddress = new String(window.location);
	var indexOfQuestionMark = urlAddress.indexOf("?");
	
	var dateValue = new Date();
	var unixtime_ms = dateValue.getTime();
	var newURL = "";
	if(indexOfQuestionMark == -1) {
		newURL = urlAddress + '?blahblahblahblah=' + unixtime_ms;	
	} else {
		newURL = urlAddress + '&blahblahblahblah=' + unixtime_ms;	
	}
	window.location = newURL;	
}



//========================================
//
// Go to Page
//
function goToPage(url) {
	if(url != '') {
		window.location=url;
	}
}


//===================================
//
// Go to Page
//
function goNav(url) {
	window.location=url;
	waitUnloadPage();
}

//========================================
//
// Add Post To Form
//
function addPostToForm(formObj,elementName,elementValue) {
	var input = window.document.createElement('input');
	input.type = 'hidden';
	input.id = elementName;
	input.name = elementName;
	input.value = elementValue;
	
	formObj.appendChild(input);
}


//=====================================
//
// Add Input To Form
// (jquery)
//
function addInputToForm(formName,elementID,elementValue) {
	var valueString = elementValue.replace(/'/g, "\\'");
	if(valueString == "") valueString = "''";
	
	if($("#" + elementID).length == 0) {
		$("<input>").attr({
			type: 'hidden',
			id: elementID,
			name: elementID,
			value: valueString
		}).appendTo("#" + formName);
	} else {
		$("#" + elementID).val(valueString).appendTo("#" + formName);	
	}
			
}

//======================================
//
// Add Form To Window
//
function addFormToWindow(windowObj,formName) {
	// create form
	var input = windowObj.document.createElement('form');
	if(!windowObj.document.body) {
		
	}
	windowObj.document.body.appendChild(input);
	input.id = formName;
	input.name = formName;
	input.method = 'POST';
	input.target = '_top';
	input.action = windowObj.location;
	
	return input;
}


//======================================
//
// Add Form To Window
//
function createForm(windowObj,formName) {
	var input = addFormToWindow(windowObj, formName);
	return input;
}

//======================================
//
// Create Hidden Frame
//
function createFrame(windowObj,frameName) {
	var frameExist = findFrame(frameName);
	var tempIFrame;
	if(frameExist == true) {
		tempIFrame = windowObj.document.getElementsByName(frameName)[0];	
	} else {
		try {
		  tempIFrame = windowObj.document.createElement('<iframe name="'+ frameName + '">');
		} catch (ex) {
		  tempIFrame = windowObj.document.createElement('iframe');
		}
	}
	
	tempIFrame.setAttribute('id',frameName);
	tempIFrame.setAttribute('name',frameName);
	tempIFrame.style.border='0px';
	tempIFrame.style.width='0px';
	tempIFrame.style.height='0px';
	windowObj.document.body.appendChild(tempIFrame);
	return tempIFrame;
}



//=============================
//
//
//
function findFrame(what) {
    for (var i=0;i<parent.frames.length;i++) {
         if (parent.frames[i].name == what)
             return true;
    }
    return false;
}




//========================
//
// ParseFloat w/o commas
//
function parseFloatValue(value) {
	value = removeCommas(value);
	value = parseFloat(value);
	return value;

}


//======================================
//
// Add Commas (for thousands)
//
function addCommas( nValue ) {
	sValue = nValue.toString();
	var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
	while(sRegExp.test(sValue)) {
		sValue = sValue.replace(sRegExp, '$1,$2');
	}
	return sValue;
} 


//======================================
//
// Remove Commas
//
function removeCommas(sValue) {
	var tempValue = sValue.replace(/\,/g,'');
	tempValue = tempValue.replace(/$/g,'');

	return parseFloat(tempValue);
}

//=======================================
//
// Trim
//

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,'');
}




//=======================================
//
// Array Search
//

function array_search(val,arr) {
	for (var i=0; i<arr.length; i++)
		if (arr[i] == val)
			return i;
	return false;
}


//================================
//
// Set Request Data In URL
//
function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/strpos    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);    // *     returns 1: 14
    var i = (haystack+'').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}

//================================
//
// Set Request Data In URL
//
function setRequestDataInURL(urlParam,requestLabelParam,requestValueParam) {
	
	var url = new String(urlParam);
	var requestLabel = new String(requestLabelParam);
	var requestValue = new String(requestValueParam);
	
	if(url == "" || requestLabel == "" || requestValue == "") {
		return url;	
	}

	// Is there a '?' in the URL?
	var ipos = strpos(url,'?');
	
	// If not, place the new request in directly.
	if(ipos===false) {
		url = url + "?" + requestLabel + "=" + requestValue;	
	} else {
		
		// If so, parse the request data
		ipos++;
		
		var requestData = new String(url.substr(ipos));
		var urlData = new String(url.substr(0,ipos));
		
		
		// Is there any request data after the '?'?
		var requestDataLength = requestData.length;
		
		// If not, add the new request in directly
		if(requestDataLength == 0) {
			url = urlData + requestLabel + "=" + requestValue;
			
		// If so, split the request data and see if the request label given
		// already exists. If it does, replace the value with the given value,
		// if not, add it in at the end.
		} else {	
			var requestSplit = requestData.split('&');
			var numRequests = requestSplit.length;
			
			url = urlData;
			var requestFound = false;
			for(ireq=0;ireq<numRequests;ireq++) {
				
				var requestEntryString = new String(requestSplit[ireq]);
				var requestEntry = requestEntryString.split('=');
				
				if(ireq != 0) {
					url = url + "&";		
				}
				
				if(requestEntry[0] != requestLabel) {
					url = url + requestEntry[0] + "=" + requestEntry[1];	
				} else {
					url = url + requestLabel + "=" + requestValue;
					requestFound = true;
				}
			}
			if(requestFound == false) {
				if(numRequests != 0) {
					url = url + '&';	
				}
				url = url  + requestLabel + "=" + requestValue;
			}
		}
	}
	
	return url;
}


