//GLOBALS

var altered = false;
var skinprimarybgcolor = '';
var skinprimarycolor = '';
var ajaxstat = 0;


function menusavenav(lnk){
	var mennavurl = $("#menunavurl").val();
	if (mennavurl != ""){
		var rdyval = $("#planstatus").html();
		if (rdyval == ""){
			window.location =mennavurl;
		} else {
			setTimeout("menusavenav('');",1000);
		}
	} else {
		
		if ( altered == false )var allok = 1;
		if ( altered == true )var allok = 0;
		if (allok == 1){
			setTimeout("window.location ='" + lnk + "';",1000);
		} else {
			var activetabid = $('.lefttabs:visible').attr('tabid');
			$("#menunavurl").val(lnk);
			modpop('Save Changes?', 'Would you like to save the changes?', 0, 0, activetabid,0);
		}
    }
}

function maxval(valmax, fieldid){
	var valx = $("#" + fieldid).val();
	if (valx > valmax){
		valx = valmax;
	}
	$("#" + fieldid).val(valx);
}

function checkdtext(fieldid,focustype){
	var defaulttext = $("#" + fieldid).attr("defaulttext");
	var valuetext = $("#" + fieldid).val();
	if (valuetext == ""){
		$("#" + fieldid).val(defaulttext);
	}
	
	if (focustype == 1){
		if (valuetext == defaulttext){
			$("#" + fieldid).val("");
		}
	}
}

// NOTES FUNCTIONS
function togglenote(note, imgjsdirx){
    var noteopen = $("#note" + note).css("display");
    if (noteopen == "none"){
        $("#note" + note).css("display", "block");
        $("#noteplus" + note).attr("src", imgjsdirx + "/bullet_toggle_minus.png");
    } else {
        $("#note" + note).css("display", "none");
        $("#noteplus" + note).attr("src", imgjsdirx + "/bullet_toggle_plus.png");
    }
}






function updatenote(idnum,planid,pageid,noteid){
    var newnoteopen = $("#noteseditarea" + idnum).css("display");
    if (newnoteopen == "block"){
        var subject = $("#notesubject" + idnum).val();
        var notetext = $("#notetext" + idnum).val();
        $.ajax({
        url: 'comm.html',
        type: 'GET',
        dataType: "text/html",
        data: {noteid: noteid, pageid: pageid, idnum: idnum, plannotes: 1, noteupd: 1, subject: subject, notetext: notetext},
         success: function(html){
           $("#noteseditarea" + idnum).css("display", "none");
           refreshnotes(idnum, planid, pageid);
         }
        });        
    }
}
function savenote(idnum,planid,pageid){
    var newnoteopen = $("#noteseditarea" + idnum).css("display");
    if (newnoteopen == "block"){
        var subject = $("#notesubject" + idnum).val();
        var notetext = $("#notetext" + idnum).val();
        planstatus('Saving Note', 2, 0);
        $.ajax({
        url: 'comm.html',
        type: 'GET',
        dataType: "text/html",
        data: {pageid: pageid, idnum: idnum, plannotes: 1, noteadd: 1, subject: subject, notetext: notetext},
         success: function(html){
           refreshnotes(idnum, planid, pageid);
           planstatus('Note Saved', 1, 1);
         }
        });        
    }
}
function cancelnote(idnum, planid, pageid, refreshyn){
    var newnoteopen = $("#noteseditarea" + idnum).css("display");
    if (newnoteopen == "block"){
        $("#noteseditarea" + idnum).css("display", "none");
        $("#noteseditarea" + idnum).html("");
    }
    if (refreshyn == 1){
        refreshnotes(idnum, planid, pageid);
    }
}
function editnote(idnum, planid, pageid, noteid){
    var newnoteopen = $("#noteseditarea" + idnum).css("display");
    if (newnoteopen == "none"){
        $.ajax({
        url: 'comm.html',
        type: 'GET',
        dataType: "text/html",
        data: {noteid: noteid, pageid: pageid, idnum: idnum, plannotes: 1, editnote: 1},
         success: function(html){
           $("#noteseditarea" + idnum).html(html);
           $("#noteseditarea" + idnum).css("display", "block");
           $("#notewrap" + noteid).html("");
           enableWYS();
         }
        });  
    }
}
function newnote(idnum, planid, pageid){
    var newnoteopen = $("#noteseditarea" + idnum).css("display");
    if (newnoteopen == "none"){
        $.ajax({
        url: 'comm.html',
        type: 'GET',
        dataType: "text/html",
        data: {pageid: pageid, idnum: idnum, plannotes: 1, newnote: 1},
         success: function(html){
           $("#noteseditarea" + idnum).html(html);
           $("#noteseditarea" + idnum).css("display", "block");
           enableWYS();
         }
        });  
    }
}
function deletenotes(idnum, planid, pageid, noteid){
        $.ajax({
    url: 'comm.html',
    type: 'GET',
    dataType: "text/html",
    data: {noteid: noteid, pageid: pageid, idnum: idnum, plannotes: 1, notedel: 1},
     success: function(html){
       //$("#notesarea" + idnum).html(html);
       refreshnotes(idnum, planid, pageid);
     }
    });  
}
function refreshnotes(idnum, planid, pageid){
    $.ajax({
    url: 'comm.html',
    type: 'GET',
    dataType: "text/html",
    data: {pageid: pageid, idnum: idnum, plannotes: 1, noterefresh: 1},
     success: function(html){
       $("#notesarea" + idnum).html(html);
     }
    });  
}
//NOTES FUNCTIONS END

//UPDATE PLAN STATUS BAR
function planstatus(stattxt, color123, fadeyn){
    if(color123 == 1)$("#planstatus").css("background-color", "#ccffcc").html('<b>' + stattxt + '</b>');
    if(color123 == 2)$("#planstatus").css("background-color", "#ffffcc").html('<img src=images/ajax-loading-sm.gif border=0 height=16 align=absmiddle> <b>' + stattxt + '</b>');
    if(color123 == 3)$("#planstatus").css("background-color", "#ff9999").html('<b>' + stattxt + '</b>'); 
    if(fadeyn == 1){
        $("#planstatus b").fadeOut(2000, function () {
        $("#planstatus").css("background-color", "#e6e6e6").html("");
        });
    }
}

// CONFIRM
	function sure(suremsg){
		return confirm(suremsg);
	}
	
	
//Format form field : Author: Preston Lord
function formatField(fieldx, allowdecimal, decimalplaces, allownegative, iscurrency, fieldorvalue){
	
		
	
        if(fieldorvalue == 2){
            thisdata = fieldx;
        } else {
            //get field data

    		var thisdata = $("#" + fieldx).val();
    		//met tonnes to lbs
    		 if (thisdata.indexOf('MT') > 0){
    		     var rExp = /MT/gi;
                 thisdata = thisdata.replace(rExp, "");
                 thisdata = eval(thisdata * 2204.62262);
             }
        }
		    //create invalid entry replacement
    		    var invalidreplace = "0";
    		    if (decimalplaces > 0){
    		        invalidreplace = invalidreplace + ".";
    		        for(i = 0; i < decimalplaces; i++){
    		            invalidreplace = invalidreplace + "0";
    		        }
    		    }
    		thisdata = cleannum(thisdata);
            //create testing field
                var chkdata = thisdata;
            //replace testing decimals and spaces
                var rExp = /\./gi;
                chkdata = chkdata.replace(rExp, "");
                var rExp = / /gi;
                chkdata = chkdata.replace(rExp, "");
                thisdata = thisdata.replace(rExp, "");
                var rExp = /\$/gi;
                chkdata = chkdata.replace(rExp, "");
                thisdata = thisdata.replace(rExp, "");
                var rExp = /%/gi;
                chkdata = chkdata.replace(rExp, "");
                thisdata = thisdata.replace(rExp, "");
                var rExp = /\"/gi;
                chkdata = chkdata.replace(rExp, "");
                thisdata = thisdata.replace(rExp, "");
                var rExp = /\'/gi;
                chkdata = chkdata.replace(rExp, "");
                thisdata = thisdata.replace(rExp, "");
                var rExp = /\`/gi;
                chkdata = chkdata.replace(rExp, "");
                if (chkdata.length > 1) {
                var rExp = /^0+/gi;
                chkdata = chkdata.replace(rExp, "");
                }
                thisdata = thisdata.replace(rExp, "");
            //setup to check for number
                if (allownegative == 0){
    		        if (thisdata < 0){
    		            // alert('This value must not be a negative number!');

        		        $("#" + fieldx).val(invalidreplace);
        				return;
    		        }
    		    }
                var isnum = 0 
                var checknum = parseFloat(chkdata); 
                isnum = parseFloat(chkdata); 
                if (isnum == checknum){
                //if a valid number continue
                    var hasdecimal = thisdata.indexOf('.');
                    //no decimal found
                    if (hasdecimal == -1){
                        if (allowdecimal == 1){
                        //force decimal
                            if (decimalplaces > 0){
                		        thisdata = thisdata + ".";
                		        for(i = 0; i < decimalplaces; i++){
                		            thisdata = thisdata + "0";
                		        }
                		    }
                        }
                    }else{
                    //found a decimal
                        var splitthedata = thisdata.split('.',2);
                        var datahead = splitthedata[0];
                        var datafoot = splitthedata[1];
                        if (datahead == ''){
                                datahead = '0';
                        }
                        
                        
                        for(i = datafoot.length; i < decimalplaces; i++){
                    		        datafoot = datafoot + '0';
                        }
                    		        
                        thisdata = parseFloat(eval(datahead + "." + datafoot));
                        //do this to fix floating point rounding issues
                        if (decimalplaces == 0)var xpow = 1;
                        if (decimalplaces == 1)var xpow = 10;
                        if (decimalplaces == 2)var xpow = 100;
                        if (decimalplaces == 3)var xpow = 1000;
                        if (decimalplaces == 4)var xpow = 10000;
                        if (decimalplaces == 5)var xpow = 100000;
                        if (decimalplaces == 6)var xpow = 1000000;
                        if (decimalplaces == 7)var xpow = 10000000;
                        if (decimalplaces == 8)var xpow = 100000000;
                        thisdata = (Math.round(thisdata * xpow) / xpow);
                        thisdata = '' + thisdata;
                        hasdecimal = thisdata.indexOf('.');
                        if (hasdecimal == -1){
                            if (allowdecimal == 1){
                            //force decimal
                                if (decimalplaces > 0){
                    		        thisdata = thisdata + ".";
                    		        for(i = 0; i < decimalplaces; i++){
                    		            thisdata = thisdata + "0";

                    		        }
                    		    }
                            }
                        }
                    }
                } else {
                //if not a number fix and warn
                        if (chkdata == '') {

    						$("#" + fieldx).val(invalidreplace);
    						alert("This field must not be left empty.");
    						return;
    					} else {
        					alert("This field must be a number.");

        					$("#" + fieldx).val(invalidreplace);
        					return;
    					}
                }
                //format currency layout
                if (iscurrency == 1){
                        var cursplitthedata = thisdata.split('.',2);
                        var curdatahead = cursplitthedata[0];
                        var curdatafoot = cursplitthedata[1];
                        var curheadsplit = curdatahead.split('');
                        var commaplacement = 1;
                        var curcurval = '';
                        for(i = (curheadsplit.length - 1);i >= 0;i--){
                            curcurval = curheadsplit[i] + curcurval;
                            if (commaplacement == 3){
                                if (i > 0)curcurval = ',' + curcurval;
                                commaplacement = 0;
                            }
                            commaplacement = commaplacement + 1;
                        }
                        if (curdatafoot.length == 1){
                            curdatafoot = curdatafoot + 0;
                        }
                        thisdata = "$ " + curcurval + "." + curdatafoot;
                }else{
                        var cursplitthedata = thisdata.split('.',2);
                        var curdatahead = cursplitthedata[0];
                        var curdatafoot = cursplitthedata[1];
                        var curheadsplit = curdatahead.split('');
                        var commaplacement = 1;
                        
                        
                        if (curdatafoot){
                        for(i = curdatafoot.length; i < decimalplaces; i++){
                    		        curdatafoot = curdatafoot + '0';
                        }
                        thisdata = curdatahead + "." + curdatafoot;
                        } else {
                            thisdata = curdatahead;
                        }
                        
                }
                //end format currency layout
        if(fieldorvalue == 2){
            return thisdata;
        } else {
            //get field data
            $("#" + fieldx).val(thisdata);
        }
}

function cleannum(num){
    num = '' + num;
    rExp = /[^0-9\.-]/gi;
    num = num.replace(rExp, "");
    //replace hyphens allow negative
     rExp = /\b-/gi;
    num = num.replace(rExp, "");
    return num;
}



// COMMENTS  FUNCTIONS
function togglecomment(comment, imgjsdirx){
    var commentopen = $("#comment" + comment).css("display");
    if (commentopen == "none"){
        $("#comment" + comment).css("display", "block");
        $("#commentplus" + comment).attr("src", imgjsdirx + "/bullet_toggle_minus.png");
    } else {
        $("#comment" + comment).css("display", "none");
        $("#commentplus" + comment).attr("src", imgjsdirx + "/bullet_toggle_plus.png");
    }
}






function updatecomment(idnum,planid,pageid,commentid,clientid){
	console.log("open comment");
    var newcommentopen = $("#commentseditarea" + idnum).css("display");//console.log("open comment");
    if (newcommentopen == "block"){
        var commenttext = $("#commenttext" + idnum).val();
        var subject = $("#commentsubject" + idnum).val();
        $.ajax({
        url: 'comm.html',
        type: 'GET',
        dataType: "text/html",
        data: {commentid: commentid, pageid: pageid, idnum: idnum, plancomments: 1, commentupd: 1, commenttext: commenttext, subject: subject},
         success: function(html){
           $("#commentseditarea" + idnum).css("display", "none");
           refreshcomments(idnum, planid, pageid);
         }
        });        
    }
}
function savecomment(idnum,planid,pageid,clientid){
    var newcommentopen = $("#commentseditarea" + idnum).css("display");
    if (newcommentopen == "block"){
        var commenttext = $("#commenttext" + idnum).val();
        var subject = $("#commentsubject" + idnum).val();
        planstatus('Saving Comment', 2, 0);
        $.ajax({
        url: 'comm.html',
        type: 'GET',
        dataType: "text/html",
        data: {pageid: pageid, idnum: idnum, plancomments: 1, commentadd: 1, commenttext: commenttext, subject: subject},
         success: function(html){
           refreshcomments(idnum, planid, pageid);
           planstatus('Comment Saved', 1, 1);
         }
        });        
    }
}
function cancelcomment(idnum, planid, pageid, refreshyn){
	// console.log(idnum);
    var newcommentopen = $("#commentseditarea" + idnum).css("display");
    if (newcommentopen == "block"){
        $("#commentseditarea" + idnum).css("display", "none");
        $("#commentseditarea" + idnum).html("");
    }
    if (refreshyn == 1){
        refreshcomments(idnum, planid, pageid);
    }
}
function editcomment(idnum, planid, pageid, commentid){
    var newcommentopen = $("#commentseditarea" + idnum).css("display");
    if (newcommentopen == "none"){
        $.ajax({
        url: 'comm.html',
        type: 'GET',
        dataType: "text/html",
        data: {commentid: commentid, pageid: pageid, idnum: idnum, plancomments: 1, editcomment: 1},
         success: function(html){
           $("#commentseditarea" + idnum).html(html);
           $("#commentseditarea" + idnum).css("display", "block");
           $("#commentwrap" + commentid).html("");
           enableWYS();
         }
        });  
    }
}
function newcomment(idnum, planid, pageid, clientid){
	// console.log("open comment " + idnum);
    var newcommentopen = $("#commentseditarea" + idnum).css("display"); //console.log("open comment");
    if (newcommentopen == "none"){
        $.ajax({
        url: 'comm.html',
        type: 'GET',
        dataType: "text/html",
        data: {pageid: pageid, idnum: idnum, plancomments: 1, newcomment: 1},
         success: function(html){
           $("#commentseditarea" + idnum).html(html);
           $("#commentseditarea" + idnum).css("display", "block");
           enableWYS();
         }
        });  
    }
}
function deletecomment(idnum, planid, pageid, commentid){
        $.ajax({
    url: 'comm.html',
    type: 'GET',
    dataType: "text/html",
    data: {commentid: commentid, pageid: pageid, idnum: idnum, plancomments: 1, commentdel: 1},
     success: function(html){
       //$("#commentsarea" + idnum).html(html);
       refreshcomments(idnum, planid, pageid);
     }
    });  
}
function refreshcomments(idnum, planid, pageid){
    $.ajax({
    url: 'comm.html',
    type: 'GET',
    dataType: "text/html",
    data: {pageid: pageid, idnum: idnum, plancomments: 1, commentrefresh: 1},
     success: function(html){
       $("#commentsarea" + idnum).html(html);
     }
    });  
}
//COMMENTS FUNCTIONS END





