var id = readCookie('user','userID');
var uid = readCookie('user','userID');
var un = readCookie('user','userName');
var uname = readCookie('user','userName');
var ul = unescape(readCookie('user','userPermission'));
var uperm = unescape(readCookie('user','userPermission'));

var abgColor;

function validateLoginForm() {
        e = '';
        if(document.frmUserLogin.txtemail.value == ''){e += '• The e-mail field is blank<br>'};
        if(document.frmUserLogin.txtpassword.value == ''){e += '• The password field is blank<br>'};
        if(e != ''){
            document.getElementById('loginErrors').innerHTML = 'The following mistakes were made:<br>'+e+'Please correct and try again.';
            document.frmUserLogin.btnlogin.disabled = false;
        }else{
            document.frmUserLogin.submit();
        }
}

function validateAdminUserForm() {
	var e;
	var strDate;
	document.frmUser.btnSaveUser.disabled = true;
        e = '';
        if(document.frmUser.txtaumail.value == ''){e += '• The e-mail field is blank<br>'};
        if(document.frmUser.txtauname.value == ''){e += '• The name field is blank<br>'};
        if(document.frmUser.txtaumail.value.indexOf('@') < 0){e += '• The e-mail address you provided is not a valid address<br>'};
        if(document.frmUser.txtaupwd.value == ''){e += '• The password field is blank<br>'};
        if(document.frmUser.txtaupwd.value != document.frmUser.txtpasswordconfirm.value){
			e += '• The password fields do not match<br>';
			}
					
        if(e != ''){
			document.getElementById('userEditErrors').innerHTML = 'The following mistakes were made:<br>'+e+'Please correct and try again.';
            document.frmUser.btnSaveUser.disabled = false;
        }else{
        //	document.frmUser.txtbirthday.disabled=false;
			document.frmUser.submit();
        }
}

function validateUserForm() {
	var e;
	var strDate;
	document.frmUser.btnSaveUser.disabled = true;
        e = '';
        if(document.frmUser.txtusemail.value == ''){e += '• The e-mail field is blank<br>'};
        if(document.frmUser.txtusname.value == ''){e += '• The name field is blank<br>'};
        if(document.frmUser.txtusemail.value.indexOf('@') < 0){e += '• The e-mail address you provided is not a valid address<br>'};
        if(document.frmUser.txtuspassword.value == ''){e += '• The password field is blank<br>'};
        if(document.frmUser.txtuspassword.value != document.frmUser.txtpasswordconfirm.value){
			e += '• The password fields do not match<br>';
			}
					
        if(e != ''){
			document.getElementById('userEditErrors').innerHTML = 'The following mistakes were made:<br>'+e+'Please correct and try again.';
            document.frmUser.btnSaveUser.disabled = false;
        }else{
        //	document.frmUser.txtbirthday.disabled=false;
			document.frmUser.submit();
        }
}

function readCookie(name,chip) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	var varc = "";
	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(nameEQ) == 0) {
			varc = c.substring(nameEQ.length,c.length);
		}
	}

	var lenc = varc.length;

	if (lenc>0) {
		var chipEQ = chip + "=";
		ca = varc.split('&');
		for(var i=0;i<ca.length;i++)
		{
				c = ca[i];
				while (c.charAt(0)==' ') {
					c = c.substring(1,c.length);
				}
				if (c.indexOf(chipEQ) == 0) {
					return c.substring(chipEQ.length,c.length);
				}
		}
	}

	return "";
}

function formatPaymentRow(type,id,user_id,userHash,userName,uploaded_file,file_ext,crdate,total,currency,bolEdit,bolApproved,bolPaid,adminNote){
	var strRow;
	var color;
	var link;
	
	switch(type){
		case "invoice":
			link = formatInvoiceLink(id, uploaded_file, userHash, file_ext)
			break;
		case "report":
			break;
		default:
	}
	abgColor ='alt2';
	
	strRow = "<tr id='row-" + id + "' align='center' valign='top' " + formatBackground(bolPaid) + " class='" + abgColor + "' >" +
									"<td>" + link + "</td>" +
									"<td>" + userName + "</td>" +
									"<td>" + truncateTime(crdate) + "</td>" +
									"<td>" + formatAsCurrency(total,currency) + "</td>" +
									editOptions(type,id,bolEdit,user_id) +
									approvedOptions(id,bolApproved,user_id) +
									paidOptions(id,bolPaid,user_id,adminNote) +
									deleteOptions(id,bolEdit) +
									"</tr>";
	return strRow;
}

	function editOptions(type,id,bolEdit,user_id) {
		//create option
		var strOptions;
		var strChecked;
		var varEnabled;
		if (bolEdit == 1) {
			strChecked = " checked ";
			varEnabled = true;
		} else {
			strChecked = "";
			varEnabled = false;
		}
	//checkbox to permit/prevent user from editing
		if (uperm >=3) {
			strOptions = "<td><input type='checkbox' id='chkEdit-" + id + "' name='chkEdit-" + id + "' class='chkEdit' " + strChecked + " /> " + 
						"<a href='/" + type + "Creator.asp?edit=1&" + type + "=" + id +"'>edit</a></td>";
		} else {
			if (varEnabled) {
				strOptions = "<td><a href='/" + type + "Creator.asp?edit=1&" + type + "=" + id +"'>edit</a></td>";				
			} else {
				strOptions = "<td class='muted' >edit</td>";
			}
		
		}		

		return strOptions;
	}
	
	
		function paidOptions(id,bolPaid,user_id,adminNote) {
		//create option
		var strOptions;
		
			//checkbox to permit/prevent user from editing
			if (bolPaid == 1) {
				strOptions = " checked "
			} else {
				strOptions = ""
			}
			
			if (uperm >=3) {
				strOptions = "<td width='165px'>" +
								"<input type='checkbox' id='chkPaid-" + id + "' name='chkPaid-" + id + "' class='chkPaid' " + strOptions + " />" +
								"&nbsp;&nbsp;<a href='#' id='noteLink-" + id + "' class='showAdminNote' style='padding:2px;' >admin note</a>" +
								"<div id='adminNote-" + id + "' style='display:none;'>" +
								"<textarea cols='18' rows='3'>" + adminNote + "</textarea>" + 
								"<div><a href='#' id='saveAdminNote-" + id + "' class='saveAdminNote' ><small>save</small></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='#' id='hideAdminNote-" + id + "' class='hideAdminNote' ><small>close</small></a></div>" +
								"</div>" +
								"</td>";
			} else {
				strOptions = "<td width='165px'>" +
								"<input type='checkbox' id='chkPaid-" + id + "' name='chkPaid-" + id + "' class='chkPaid' " + strOptions + " disabled /></td>"
			}
						
		return strOptions;
	}
	
	function approvedOptions(id,bolApproved,user_id) {
		//create option
		var strOptions;
		
			//checkbox to permit/prevent user from editing
			if (bolApproved == 1) {
				strOptions = " checked "
			} else {
				strOptions = ""
			}
			
			if (uperm >=5) {
				strOptions = "<td width='65px'>" +
								"<input type='checkbox' id='chkApproved-" + id + "' name='chkApproved-" + id + "' class='chkApproved' " + strOptions + " /></td>";
			} else {
				strOptions = "<td width='65px'>" +
								"<input type='checkbox' id='chkApproved-" + id + "' name='chkApproved-" + id + "' class='chkApproved' " + strOptions + " disabled /></td>"
			}
						
		return strOptions;
	}
	
	function deleteOptions(id,bolEdit){
		//create option
		var strOptions;
		
			//checkbox to permit/prevent user from editing
			if (bolEdit == 1 || uperm >=3) {
				strOptions = "<td width='35px'>" +
								"<a href='#' id='del-" + id + "' class='delObject' ><font style='color:#FF0000;'>x</font></a></td>";
			} else {
				strOptions = "<td width='35px' class='muted'>" +
								"x</td>";
			}
									
		return strOptions;
	}
	
		function formatInvoiceLink(id, uploaded_file, hash, file_ext){
		var strLink;
		
		if (uploaded_file == "1") {
			strLink = "<div style='position:relative;'><span style='position:absolute;left:3px;'><img src='/images/Paperclip.png' /></span>" +
						"<a href='" + invoice_link(id,hash,file_ext) + "?dh=" + new Date().getTime() + "' target='_blank' >" + id + "</a></div>";
		} else {
			strLink = "<a href='/invoice.asp?invoice=" + id + "&dh=" + new Date().getTime() + "' target='_blank' >" + id + "</a>";
		}
		return strLink;
	}
	
	function formatBackground(bolPaid) {
		var strBackground;

		if (bolPaid != "1") {
			strBackground = " style='background-color:#FFDBDB;'";
		} else {
			strBackground = " style='background-color:#FFFFFF;'";		
		}
		return strBackground;
	}

	
	function truncateTime(date) {
		var currDate = date.substring(0,date.indexOf(" "))
	
		return currDate;
	
	}

	function formatAsCurrency(n,curr){
		//convert to string
		var currType;
		switch(curr){
			case "1":
				currType = "$";
				break;
			case "2":
				currType = "€";
				break
			default:
				currType = "$";		
		}
		
		var varCurrency = n + '';
		if (varCurrency.indexOf(".") < 0) {
			varCurrency = varCurrency + ".00"
		} else if (varCurrency.indexOf(".") == varCurrency.length-2){
			varCurrency = varCurrency + "0"			
		}
		varCurrency = currType + varCurrency;
		return varCurrency;
	}

	
/*ALTERNATING ROW COLORS
================
       Insert the following line in any loop to get alternating colors.
       bgColor = altBgColor(bgColor)
*/
function altbgColor(bg){
		if (bg == 'alt2') {
			return 'alt1';
		} else {
			return 'alt2';
		}
}

function nullIsZero(n) {
	var varN = n;
	if (varN == "" || varN == null) {
		varN = 0;
	} 
	return varN;
}


function getFolder(i) {
	var intFR;
	var varIFR;
	var varFolder;
	
	intFR = 500;
	varIFR = i;
	varFolder = ((varIFR / intFR) * intFR) - (varIFR % intFR);
	
	return varFolder;
}

function generateOptions(name,value,selectedValue) {
	var varSelected;
	
	varSelected = '';
	if (value == selectedValue) {
		varSelected = " selected";
	}
	
	return "<option value='" + value + "'" + varSelected + ">" + name + "</option>";
}


function invoice_link(invoice_id,hash,ext) {
	return "/invoices/" + getFolder(invoice_id) + "/" + invoice_id + "_" + hash + ext;
}

function report_link(invoice_id,hash) {
	return "/invoices/" + getFolder(invoice_id) + "/" + invoice_id + "_" + hash + ".pdf";
}


function dbug(n) {
	if (id == 12) {
		alert(n);
	}

}
		


