var winWebApp;

function logOff(){
	if (winWebApp){
		winWebApp.close();
	}
}//-----------------------------------------------------------

function loadApp(AppID, Loc){
	var temp;
	
	//temp = "http://localhost/WebAppQpcalc/dataEntry.aspx?secam=" + Loc + "ytr34ui76hkqcf4976dk";
	temp = "http://www.systek.us/WebAppQpcalc/dataEntry.aspx?secam=" + Loc + "ytr34ui76hkqcf4976dk";
	openWebApp(temp);
}


function warnUser(mode){
	if (mode == 0){
		alert("Because it is Not Active, \nyou cannot edit this subscription.");
		return false;
	}
	
	if (mode == 1){
		alert("Subscription is in Pending. \nPlease do not modify the subscription.");
		return false;
	}
	
	if (mode == 2){
		alert("Active subscriptions are not editable \nuntil they are Expired");
		return false;
	}
	
	if (mode == 3){
		return true;
	}
}//------------------------------------------------------------


function checkAppWin(){
	if(winWebApp){
		winWebApp.close();
	}
}

function openWebApp(url){
	var winHeight;
	var winWidth;
	var winCrown;
	
	winHeight = screen.availHeight - 50;
	winWidth = screen.availWidth - 100;
	winCrown = "menubar=0 width=" + winWidth + " height=" + winHeight + " scrollbars=1 resizable";
	
	winWebApp = window.open(url,"Qpcalc", winCrown);
	winWebApp.focus();
}//------------------------------------------------------------

//about window
function openWin(filePath){
	var winAbout;
	
	winAbout = window.open(filePath,"QpcalcDoc",'menubar=0 width=310 height=540 scrollbars=1');
	winAbout.focus();
}//------------------------------------------------------------

//disclaimer window
function openDisclaimer(filePath){
	var winDisclaimer;
	
	winDisclaimer = window.open(filePath,"Disclaimer",'menubar=0 width=560 height=540 scrollbars=1');
	winDisclaimer.focus();
}//------------------------------------------------------------

//how do i window -MAY DELETE
function openHowDoI(filePath){
	var winHowDoI;
	
	winHowDoI = window.open(filePath,"HowDoI",'menubar=0 width=310 height=540 scrollbars=1');
	winHowDoI.focus();
}//------------------------------------------------------------


//disposing individual windows
function closeMe()
{
	self.close();
}//------------------------------------------------------------


function focus_event(my_field, msg){
	if(my_field.value != null || my_field.value != ""){ 
		my_field.value = remove_coma_chars(my_field.value);
	}
	
	my_field.style.backgroundColor = "#ccffff";
	my_field.style.color = "#000000";
	my_field.select();
	
	showStatus (msg);
	
	return true;
}//--------------------------------------------------------

	
function blur_event(my_field, num_places){
	var temp;
	var str_len;
			
	temp = my_field.value;
	
	if (temp != ""){ 
		if (is_a_valid_string(temp)){
			str_len = temp.length;
			format_string(my_field, num_places);
			
			my_field.style.backgroundColor = "#ffffff";
			my_field.style.color = "#000000";
			return true;
		}
		else{
			alert("Please enter a valid number for this entry..");
			my_field.focus();
			return false;
		}
	}
	my_field.style.backgroundColor = "#ffffff";
	my_field.style.color = "#000000";
	
	window.status= "";
	return true;
}//--------------------------------------------------------


function remove_coma_chars(coma_string){
	var full_length;
	var len;
	var cur_value;
	var temp = "";
	
	cur_value = coma_string;
	full_length = cur_value.length;
	
	//it is a signed value preserve it
	if (cur_value.charAt(0) == "-" || cur_value.charAt(0) == "+"){
		temp = cur_value.substr(0, 1);
		
		//strip out the signed character
		cur_value = cur_value.substr(1, (full_length - 1));
	}
	
	//how many characters left out
	len = cur_value.length;
	
	for (var i = 0; i <= len; i++){
		if (cur_value.charAt(i) != ","){
			temp += cur_value.charAt(i);
		}	
	}
	return temp;
}//------------------------------------------------------------------------------------------------


function format_string(my_field, num_positions){
	var raw_string;
	var cur_string;
	var decimal_position;
	var integer_part;
	var decimal_part;
	var string_len;
	var num_chars;
	var temp_string;
	var my_decimal;
	var stripped_decimal;
	var stripped_integer;
	var stripped_string;
	var sign_char = "";
	
	raw_string = my_field.value;
	string_len = raw_string.length;
	//alert("String_Len at begining: " + string_len);
	
	//locate the first decimal point and split the string into two
	decimal_position = my_field.value.indexOf(".");
	//alert("Decimal Position at: " + decimal_position);
	
	if (decimal_position == -1){
		
		integer_part = my_field.value;
		//alert("Integer part is: " + integer_part);
		
		decimal_part = "";	
	}
	else{
		//here we have every characters upto the first decimal point
		integer_part = my_field.value.substr(0, decimal_position);
		//alert("Integer part is: " + integer_part);
		
		//and here we put rest of the charactes into another variable
		//alert("Decimal Position is still at: " + decimal_position);
		//alert("String_Len at: " + string_len);
		decimal_part = my_field.value.substr((decimal_position + 1),(string_len - (decimal_position)));
		//alert("Decimal Part is: " + decimal_part);
	}
	
	
	//we need to check for multiple instances of ',' or '-' or '+' or '.' and strip those away.
	//but before that, the integer_part may contain a leading '-' sign. In this case we
	//strip that character before sending it to the stripping function.
	
	if (integer_part.charAt(0) == "-"){
		sign_char = "-";
		integer_part = integer_part.substr(1, (integer_part.length - 1));
		//alert ("Integer_Part After removing First Sign Char : " + integer_part);
	}
	
	//remove any remaining comas in the string
	integer_part = remove_coma_chars(integer_part);
	//alert ("After removing all coma chars : " + integer_part);
		
	stripped_integer = strip_string(integer_part);
	//alert ("Stripped_Integer After removing ALL Signs and Dots : " + stripped_integer);

	
	
	//now for the decimal part
	if (decimal_part.length != 0){
		stripped_decimal = strip_string(decimal_part);
		//alert ("Stripped_Decimal : " + stripped_decimal);
	}
	else{
		stripped_decimal = "";	
	}
	
	//we need the decimal part tobe displayed with trailing zeros
	my_decimal = pad_decimal(stripped_decimal, num_positions);
	//alert ("After Padding : " + my_decimal);
	
	//here we add the ',' character to separate hundredth and thousandth places
	num_chars = stripped_integer.length;
	
	if (num_chars <= 3){
		if (num_positions == 0){
			my_field.value = sign_char + stripped_integer;	
		}
		else{
			my_field.value = sign_char + stripped_integer + "." + my_decimal;
		}
	}
	else{
		temp_string	= build_string(stripped_integer);
		//alert(temp_string);
		if (num_positions > 0){
			my_field.value = sign_char + temp_string + "." + my_decimal;	
		}
		else{
			my_field.value = sign_char + temp_string;
		}
	}
}//--------------------------------------------------------


function pad_decimal(decimal_part, num_positions){
	var string_len;
	var temp_string;
	var num_diff;
	
	//alert("Decimal Part: " + decimal_part + "\n" + "Num Positions Reqd: " + num_positions);
	
	string_len = decimal_part.length;
	//alert ("Length of decimal part: " + string_len);
	if (string_len != num_positions){
		if (string_len > num_positions){
			temp_string = decimal_part.substr(0, num_positions);
			//alert ("When I have more than Num Positions, will truncate: " + temp_string);
			return temp_string;	
		}
		else{
			num_diff = num_positions - string_len;
			//alert ("The difference is : " + num_diff)
			
			while (num_diff != 0){
				decimal_part += "0";
				num_diff--;
			}
			//alert("Padded Decimal Part: " + decimal_part);
			return decimal_part;	
		}
	}
	return decimal_part;
}//--------------------------------------------------------


function build_string(cur_string){
	var num_chars;
	var temp_string;
	
	num_chars = cur_string.length;
	switch (num_chars){
		case 4:
			temp_string = cur_string.charAt(0);
			temp_string += ",";
			temp_string += cur_string.substr(1,3);
			break;
		
		case 5:
			temp_string = cur_string.substr(0,2);
			temp_string += ",";
			temp_string += cur_string.substr(2,3);
			break;
		
		case 6:
			temp_string = cur_string.substr(0,3);
			temp_string += ",";
			temp_string += cur_string.substr(3,3);
			break;
			
		case 7:
			temp_string = cur_string.substr(0,1);
			temp_string += ",";
			temp_string += cur_string.substr(1,3);
			temp_string += ",";
			temp_string += cur_string.substr(4,4);
			break;
			
		case 8:
			temp_string = cur_string.substr(0,2);
			temp_string += ",";
			temp_string += cur_string.substr(2,3);
			temp_string += ",";
			temp_string += cur_string.substr(5,4);
	}
	
	return temp_string;	
}//--------------------------------------------------------


function strip_string(string_tobe_stripped){
	//this function will strip away any ',' or '-' or '.' characters from the string
	var string_len;
	var temp_string = "";
	var string_no_negatives = "";
	var string_no_dots = "";
	
	if (string_tobe_stripped != null || string_tobe_stripped != ""){
		string_len = string_tobe_stripped.length;
		
		//first we remove any Negative signs
		for(var i = 0; i <= string_len; i++){
			if (string_tobe_stripped.charAt(i) != "-" ){
				string_no_negatives += string_tobe_stripped.charAt(i);
			}
		}
		
		
		//second, we attempt to remove any Dots
		string_len = string_no_negatives.length;
		
		for(var i = 0; i <= string_len; i++){
			if (string_no_negatives.charAt(i) != "." ){
				string_no_dots += string_no_negatives.charAt(i);
			}
		}
		
		//third, we attempt to remove any ","
		string_len = string_no_dots.length;
		
		for(var i = 0; i <= string_len; i++){
			if (string_no_dots.charAt(i) != "." ){
				temp_string += string_no_dots.charAt(i);
			}
		}
		
		return temp_string;
	}
}//--------------------------------------------------------	


function is_it_empty(my_string){
	if (my_string == "" || my_string == null){
		return true;
	}
	return false;
}//--------------------------------------------------------


function is_it_integer(my_string){
	var cur_char;
						
	for (var i = 0; i < my_string.length; i++){
		cur_char = my_string.charAt(i);
			
		//check for a digit
		if (!is_it_valid_digit(cur_char)){
			return false;	
		}
	}	
	
//if we end up here, all charatcters are digits
return true;
}//--------------------------------------------------------


function is_it_valid_digit(my_char){
	var digit_chars = "0123456789";
	
	if (digit_chars.indexOf(my_char) == -1){
		return false;
	}

	return true;
}//--------------------------------------------------------


function is_a_valid_string(my_string){
	//here we are checking the supplied string contains any invalid characters
	var string_len = my_string.length;
	
	for (var i = 0; i <= string_len - 1; i++){
		if (!is_it_valid_char(my_string.charAt(i))){
			return false;
		}
	}
	return true;
}//--------------------------------------------------------


function is_it_valid_char(my_char){
	var valid_chars = "0123456789.,-+";
	
	if (valid_chars.indexOf(my_char) == -1){
		return false;
	}

	return true;
}//--------------------------------------------------------


function is_it_signed(my_string){
	var first_char;
	
	first_char = my_string.charAt(0);
	if (first_char != "+" && first_char != "-" && !its_a_digit(first_char)){
		return false;
	}
	return true;
}//--------------------------------------------------------


function fix_decimal(my_field){
	var first_char;
	var cur_value;
	
	cur_value = my_field.value;
	first_char = cur_value.charAt(0);
	if (first_char == "."){
		my_field.value = "0" + cur_value;
	}
}//--------------------------------------------------------

function b_event(my_field){
	my_field.style.backgroundColor = "#ffffff";
	my_field.style.color = "#000000";
	
	window.status = "";
return true;
}//--------------------------------------------------------

function f_event(my_field, msg){
	my_field.style.backgroundColor = "#ccffff";
	my_field.style.color = "#000000";
	
	showStatus (msg);
	return true;
}//--------------------------------------------------------

function showStatus(msg){
	if (!msg == ""){
		window.status = msg;
	}else{
		window.status = "";
	}
}
