// declare this early in your codebase
// code to execute on an error
window.onerror = WindowErrFunc;


function WindowErrFunc(message, url, lineNumber) 
{
	//alert(message + "\n" + url + "\n" + "line number: " + lineNumber);
	
	// prevents browser error messages - disable during debugging
	return true; 
};


// constants

var constEmailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

var constPhoneNumRegEx = /^((\+\d{1,3}(-| |.)?\(?\d\)?(-| |.)?\d{1,3})|(\(?\d{2,3}\)?))(-| |.)?(\d{2,4})(-| |.)?(\d{4,8})(( x| ext| ex)(-| |.)?\d{1,6}){0,1}$/;

var constFiveDigitZip = /^\d{5}/;




// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) 
{	
	return LTrim(RTrim(value));	
}


// checks for valid phone number
function ValidatePhoneNumber(phone_txt)
{
    return constPhoneNumRegEx.test(phone_txt);
}


// checks for valid email address
function ValidateEmailAddr(email_txt)
{
    return constEmailRegEx.test(email_txt);
}


function ValidateZipcode(zipcode)
{
    return constFiveDigitZip.test(zipcode);
}

function SetBodyPositionForFF()
{
    if(navigator.appName.indexOf("Microsoft") == -1)
    {
        // NOT IE
        document.getElementById("body").className = "bodyFF";
    }
}



// returns true if browser is ie, else false
function IsIe()
{
    if(navigator.appName.indexOf("Microsoft") != -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}



// changes mouse pointer to hand
function MouseOver_MakePointerHand(imgObj)
{
    imgObj.style.cursor = "pointer";
}
    

// changes mouse pointer to default pointer
function MouseOut_MakePointerDefault(imgObj)
{
    imgObj.style.cursor = "default";
}



// returns a currency formatted string for the amount (must be a real number); only operates on positive number
function GetCurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	
	
	if(isNaN(i))
	{
	    return null; 
	}
	

	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0)
	{
	    s += '.00'; 
	}
	else if(s.indexOf('.') == (s.length - 2))
	{
	    s += '0';
	}
	
	return "$" + s;
}


// scrolls to the bottom of the page
function ScrollToBottom()
{
    if(document.body.scrollHeight)
    {
        window.scroll(0, document.body.scrollHeight);
    }
    else if(screen.height)
    {
        window.scroll(0, screen.height);
    }
}



// when the input length matches the jumpLen, focus is given to the field with the jumpTo id
function Jump(jumpLen, input, jumpTo)
{
    if(input.length == jumpLen)
    {
        document.getElementById(jumpTo).focus();
    }
}



// formats into currency the value in the textbox txtBoxId - function is not very generic, must have textbox called hidCreatePrice for it to store the unformatted price in, check function before using
// made specifically for CreateNewPost.aspx and used in EditPosting.aspx also
function PriceTxtBoxBlur(txtBoxId)
{   
    var price_txt = trim(document.getElementById(txtBoxId).value);
   
    
    if(price_txt.length > 0)
    {       
        var tempPriceTxt = "";
        var iDecimalsSeen = 0;
        
        for(i=0; i<price_txt.length; i++)
        {
            if( (price_txt.charAt(i) == '0'  ||  price_txt.charAt(i) == '1')  ||  (price_txt.charAt(i) == '2'  ||  price_txt.charAt(i) == '3') )
            {
                tempPriceTxt += price_txt.charAt(i);
            }
            else if( (price_txt.charAt(i) == '4'  ||  price_txt.charAt(i) == '5')  ||  (price_txt.charAt(i) == '6'  ||  price_txt.charAt(i) == '7') )
            {
                tempPriceTxt += price_txt.charAt(i);
            }
            else if(price_txt.charAt(i) == '8'  ||  price_txt.charAt(i) == '9')
            {
                tempPriceTxt += price_txt.charAt(i);
            }
            else if(price_txt.charAt(i) == '.')
            {
                iDecimalsSeen++;
                
                if(iDecimalsSeen > 1)
                {
                    alert("Please enter a valid price");
                    
                    document.getElementById("txtPrice").value = "";
                    document.getElementById("hidCreatePrice").value = "";
                    return;
                }
                else
                {
                    tempPriceTxt += '.';
                }
           }
                
        }
        
        price_txt = tempPriceTxt;
        
        document.getElementById("hidCreatePrice").value = price_txt;
        
        price_txt = GetCurrencyFormatted(price_txt);
        
        if(price_txt == null)
        {
            price_txt = "";
            document.getElementById("txtPrice").value = price_txt;
            alert("Please enter a valid amount");
        }
        else
        {
            // put commas where applicable
            
            var seenDecimal = false;
            var tempPrice = "";
            var lastCommaSeenIntervalsAgo = 0;
            for(i=price_txt.length-1; i>=0; i--)
            {
                if(!seenDecimal)
                {
                    if(price_txt.charAt(i) == '.')
                    {
                        seenDecimal = true;
                    }
                    tempPrice = price_txt.charAt(i) + tempPrice;
                    continue;
                }
                else
                {
                    tempPrice = price_txt.charAt(i) + tempPrice;
                    lastCommaSeenIntervalsAgo++;
                    if(lastCommaSeenIntervalsAgo == 3  &&  i > 1)
                    {
                        tempPrice = ',' + tempPrice;
                        lastCommaSeenIntervalsAgo = 0;
                    }
                }
            }
            
            price_txt = tempPrice;
            document.getElementById("txtPrice").value = price_txt;
        }
    }
}



function ValidateImgExt(imgId) 
{
    var strImg = trim(document.getElementById(imgId).value);

    if (strImg.length == 0) 
    {
        return true;
    }

    var lastIndex = strImg.lastIndexOf('.');

    if (strImg.length <= lastIndex) 
    {
        return false;
    }

    var ext = strImg.substring(lastIndex + 1).toLowerCase();

    if (ext == "jpg" || ext == "jpeg") 
    {
        return true;
    }

    if (ext == "gif" || ext == "png") 
    {
        return true;
    }

    if (ext == 'bmp') 
    {
        return true;
    }

    return false;
}



// clears all values in a dropdownlist
function ClearDlist(dlist)
{
    var options_length = dlist.options.length;
    
    for(i=0; i<options_length; i++)
    {
        dlist.remove(0);
    }
}
